Skip to content

Commit 4463b59

Browse files
committed
fix: refactoring code
1 parent 6143bf1 commit 4463b59

File tree

4 files changed

+51
-75
lines changed

4 files changed

+51
-75
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@
4646
},
4747
"dependencies": {
4848
"@micro-app/cli": "^0.3.15",
49-
"@micro-app/plugin-webpack": "^0.0.11"
49+
"@micro-app/plugin-webpack": "^0.0.12"
5050
}
5151
}

src/service/chainConfig.js

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,34 @@ module.exports = function chainDefault(api, vueConfig, options) {
7676
});
7777

7878
// webpack 所有配置合入
79-
api.chainWebpack(webpackChain => {
80-
return silentService(service => {
81-
// 注册插件
82-
service.registerPlugin({
83-
id: 'vue-cli-plugin:plugin-command-return-webpack-chain',
84-
[BUILT_IN]: true,
85-
apply(_api) {
86-
_api.registerCommand('return-webpack-chain', {
87-
description: 'return config of webpack-chain.',
88-
usage: 'micro-app return-webpack-chain',
89-
}, () => {
90-
_api.createChainWebpackConfigInstance(webpackChain);
91-
return _api.resolveChainableWebpackConfig();
92-
});
93-
},
94-
});
95-
96-
// 同步扩充 webpack-chain config
97-
return service.runSync('return-webpack-chain');
79+
return silentService(service => {
80+
// 注册插件
81+
service.registerPlugin({
82+
id: 'vue-cli-plugin:plugin-command-return-webpack-chain',
83+
[BUILT_IN]: true,
84+
apply(_api) {
85+
_api.registerCommand('return-webpack-chain', {
86+
description: 'return config of webpack-chain.',
87+
usage: 'micro-app return-webpack-chain',
88+
}, () => {
89+
// global save vueConfig
90+
_api.setState('vueConfig', vueConfig);
91+
// 覆盖逻辑
92+
if (api.resolveWebpackConfig) {
93+
const originaFn = api.resolveWebpackConfig;
94+
api.resolveWebpackConfig = function(chainableConfig) {
95+
const finalWebpackChainConfig = api.resolveChainableWebpackConfig(chainableConfig);
96+
const webpackConfig = originaFn(finalWebpackChainConfig);
97+
const finalWebpackConfig = api.applyPluginHooks('modifyWebpackConfig', webpackConfig);
98+
api.setState('webpackConfig', finalWebpackConfig);
99+
return finalWebpackConfig;
100+
};
101+
}
102+
});
103+
},
98104
});
105+
106+
// 同步扩充 webpack-chain config
107+
return service.runSync('return-webpack-chain');
99108
});
100109
};

src/utils/silentService.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,36 @@
11
'use strict';
22

33
const { _, logger } = require('@micro-app/shared-utils');
4-
const { createService } = require('@micro-app/cli');
4+
const { createService, service: $s } = require('@micro-app/cli');
55
const { SKIP_TARGET, BUILT_IN } = require('../constants');
66

7+
// 提前注册
8+
registerWebpackPlugin($s);
9+
710
// 无日志服务
811
module.exports = function silentService(cb) {
912
// record
1013
const ORIGINAL_LEVEL = logger.level;
1114
logger.level = 'error';
1215

1316
const service = createService({ target: SKIP_TARGET });
14-
15-
const WEBPACK_PLUGIN_ID = '@micro-app/plugin-webpack';
16-
if (!service.hasPlugin(WEBPACK_PLUGIN_ID)) {
17-
// 注册 webapck 插件
18-
service.registerPlugin({
19-
id: WEBPACK_PLUGIN_ID,
20-
[BUILT_IN]: true,
21-
});
22-
}
17+
registerWebpackPlugin(service);
2318

2419
const result = _.isFunction(cb) && cb(service);
2520

2621
// recovery
2722
logger.level = ORIGINAL_LEVEL;
2823
return result;
2924
};
25+
26+
27+
function registerWebpackPlugin(s) {
28+
const WEBPACK_PLUGIN_ID = '@micro-app/plugin-webpack';
29+
if (!s.hasPlugin(WEBPACK_PLUGIN_ID)) {
30+
// 注册 webapck 插件
31+
s.registerPlugin({
32+
id: WEBPACK_PLUGIN_ID,
33+
[BUILT_IN]: true,
34+
});
35+
}
36+
}

yarn.lock

Lines changed: 5 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -350,14 +350,14 @@
350350
"@release-it/conventional-changelog" "^1.1.0"
351351
release-it "^13.1.1"
352352

353-
"@micro-app/plugin-webpack@^0.0.11":
354-
version "0.0.11"
355-
resolved "https://registry.yarnpkg.com/@micro-app/plugin-webpack/-/plugin-webpack-0.0.11.tgz#998664f039472ed0daa47e9451d5a12478405b4b"
356-
integrity sha512-+Ku8V2UJUpAbAfwXgEse1dhcKxH+ybByFOeLL9KivNxKelxEmYI01XFY7V6wXKiSlDfgxl/IUoZXH35VrBxcsg==
353+
"@micro-app/plugin-webpack@^0.0.12":
354+
version "0.0.12"
355+
resolved "https://registry.yarnpkg.com/@micro-app/plugin-webpack/-/plugin-webpack-0.0.12.tgz#bbc646350badc54afe13b878322c1c4063416efe"
356+
integrity sha512-V7UD8p4ETxrEBzzx2LjypgTqdiu1UQs74ZDaTmn7hF5S/7qAfd15GOTptgwqXBkC6GVOOf3nSGSdpw04rkCOWQ==
357357
dependencies:
358358
"@intervolga/optimize-cssnano-plugin" "^1.0.6"
359359
"@marshallofsound/webpack-asset-relocator-loader" "^0.5.0"
360-
"@micro-app/shared-utils" "^0.1.16"
360+
"@micro-app/shared-utils" "^0.1.17"
361361
"@soda/friendly-errors-webpack-plugin" "^1.7.1"
362362
address "^1.1.2"
363363
autoprefixer "^9.7.4"
@@ -383,46 +383,6 @@
383383
webpack-chain "^6.4.0"
384384
webpack-dev-server "^3"
385385

386-
"@micro-app/shared-utils@^0.1.16":
387-
version "0.1.16"
388-
resolved "https://registry.yarnpkg.com/@micro-app/shared-utils/-/shared-utils-0.1.16.tgz#4952238c8caf17156548e33a43990ef42368e363"
389-
integrity sha512-n4/E5gRFAijnMa33slUBF+REKVoPZ3SczY3xim3cRT0xI0vqG1uhyLZjEAwUq9mStGORgcllYN4ygUuJx0ki0Q==
390-
dependencies:
391-
ajv "^6.10.2"
392-
ajv-keywords "^3.4.1"
393-
assert "^2.0.0"
394-
chalk "^3.0.0"
395-
cheerio "^1.0.0-rc.3"
396-
debug "^4.1.1"
397-
dedent "^0.7.0"
398-
execa "^3.4.0"
399-
fs-extra "^8.1.0"
400-
git-url-parse "^11.1.2"
401-
glob-parent "^5.1.0"
402-
globby "^10.0.2"
403-
hash-sum "^2.0.0"
404-
import-fresh "^3.2.1"
405-
inquirer "^7.1.0"
406-
is-glob "^4.0.1"
407-
lodash "^4.17.15"
408-
lru-cache "^5.1.1"
409-
multimatch "^4.0.0"
410-
npm-package-arg "^6.1.1"
411-
npmlog "^4.1.2"
412-
open "^7.0.3"
413-
ora "^3.4.0"
414-
parse-json "^5.0.0"
415-
semver "^6.3.0"
416-
semver-regex "^3.1.1"
417-
shelljs "^0.8.3"
418-
signal-exit "^3.0.2"
419-
stream-to-string "^1.2.0"
420-
stringify-object "^3.3.0"
421-
try-require "^1.2.1"
422-
yaml "^1.8.2"
423-
yargs-parser "^18.1.1"
424-
yargs-unparser "^1.5.0"
425-
426386
"@micro-app/shared-utils@^0.1.17":
427387
version "0.1.17"
428388
resolved "https://registry.yarnpkg.com/@micro-app/shared-utils/-/shared-utils-0.1.17.tgz#e99b9aa4385cf4eefe5f96f0f553162ab9342743"

0 commit comments

Comments
 (0)