Skip to content

Commit dad0ee9

Browse files
committed
refactor: merge config
1 parent b40cea9 commit dad0ee9

File tree

9 files changed

+422
-416
lines changed

9 files changed

+422
-416
lines changed

bin/micro-app-build

Lines changed: 0 additions & 10 deletions
This file was deleted.

package.json

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
"version": "0.0.9",
44
"description": "[Plugin] webpack adapter plugin.",
55
"main": "src/index.js",
6-
"bin": {
7-
"micro-app-build": "./bin/micro-app-build"
8-
},
96
"scripts": {
107
"prepublishOnly": "npm run test",
118
"lint": "eslint .",
@@ -21,7 +18,6 @@
2118
"release:patch": "npm run release patch"
2219
},
2320
"files": [
24-
"bin",
2521
"src"
2622
],
2723
"homepage": "https://github.com/MicroAppJS/plugin-webpack",
@@ -52,12 +48,12 @@
5248
}
5349
},
5450
"peerDependencies": {
55-
"@micro-app/cli": ">=0.3.7"
51+
"@micro-app/core": "^0.3.16"
5652
},
5753
"devDependencies": {
58-
"@babel/core": "^7.8.7",
59-
"@babel/preset-env": "^7.8.7",
60-
"@micro-app/cli": "^0.3.7",
54+
"@babel/core": "^7.9.0",
55+
"@babel/preset-env": "^7.9.0",
56+
"@micro-app/cli": "^0.3.8",
6157
"@micro-app/plugin-compatible": "0.0.3",
6258
"@micro-app/plugin-deploy": "^0.0.6",
6359
"@types/jest": "^24.9.1",
@@ -75,23 +71,23 @@
7571
"@soda/friendly-errors-webpack-plugin": "^1.7.1",
7672
"address": "^1.1.2",
7773
"autoprefixer": "^9.7.4",
78-
"babel-loader": "^8.0.6",
74+
"babel-loader": "^8.1.0",
7975
"cache-loader": "^4.1.0",
8076
"case-sensitive-paths-webpack-plugin": "^2.3.0",
8177
"cli-highlight": "^2.1.4",
82-
"clipboardy": "^2.2.0",
78+
"clipboardy": "^2.3.0",
8379
"css-loader": "^3.4.2",
8480
"cssnano": "^4.1.10",
85-
"default-gateway": "^5.0.5",
81+
"default-gateway": "^6.0.0",
8682
"file-loader": "^5.1.0",
8783
"launch-editor-middleware": "^2.2.1",
8884
"mini-css-extract-plugin": "^0.9.0",
8985
"node-loader": "^0.6.0",
9086
"portfinder": "^1.0.25",
9187
"postcss-loader": "^3.0.0",
92-
"ts-loader": "^6.2.1",
93-
"url-loader": "^3.0.0",
94-
"vue-loader": "^15.9.0",
88+
"ts-loader": "^6.2.2",
89+
"url-loader": "^4.0.0",
90+
"vue-loader": "^15.9.1",
9591
"vue-style-loader": "^4.1.2",
9692
"webpack": "^4",
9793
"webpack-chain": "^6.4.0",

src/extends/enhance/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module.exports = function WebpackAdapter(api, opts) {
44

5-
api.assertVersion('>=0.2.0');
5+
api.assertVersion('>=0.3.0');
66

77
const registerMethods = require('./methods');
88
registerMethods(api);

src/extends/unified/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ module.exports = function unifiedExtend(api, opts) {
5555
const isProd = api.mode === 'production';
5656

5757
// load html
58-
const multiPageConfig = options.pages;
58+
const multiPageConfig = options.pages || {};
5959
const pages = Object.keys(multiPageConfig);
6060
if (pages.length > 0) {
6161
const HTMLPlugin = tryRequire('html-webpack-plugin');

src/extends/webpack/configCombine.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ module.exports = function configCombine(obj) {
3030
}
3131

3232
function nodeModulesPaths() {
33-
const nodeModules = obj.nodeModules;
34-
if (nodeModules && !Array.isArray(nodeModules)) {
35-
return [ nodeModules ];
36-
} else if (Array.isArray(nodeModules)) {
37-
return nodeModules;
33+
const nodeModulesPath = obj.nodeModulesPath;
34+
if (nodeModulesPath && !Array.isArray(nodeModulesPath)) {
35+
return [ nodeModulesPath ];
36+
} else if (Array.isArray(nodeModulesPath)) {
37+
return nodeModulesPath;
3838
}
3939
return [];
4040
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
module.exports = {
4+
additionalProperties: true,
5+
properties: {
6+
webpack: {
7+
description: 'webpack 配置, 只针对自己有效. ( object )',
8+
type: 'object',
9+
},
10+
entry: {
11+
description: '入口配置. ( object )',
12+
type: 'object',
13+
},
14+
htmls: {
15+
description: '模版配置. ( array<object> )',
16+
items: {
17+
required: [ 'template' ],
18+
type: 'object',
19+
},
20+
minItems: 1,
21+
type: 'array',
22+
},
23+
staticPath: {
24+
description: 'static resource path. ( stirng | array<string> )',
25+
anyOf: [{
26+
items: {
27+
type: 'string',
28+
},
29+
minItems: 1,
30+
type: 'array',
31+
},
32+
{
33+
type: 'string',
34+
}],
35+
},
36+
css: {
37+
description: 'css配置. ( object )',
38+
type: 'object',
39+
},
40+
devServer: {
41+
description: '开发服务器配置. ( object )',
42+
type: 'object',
43+
},
44+
publicPath: {
45+
description: 'public resource path. ( stirng )',
46+
type: 'string',
47+
},
48+
},
49+
type: 'object',
50+
};

src/extends/webpack/index.js

Lines changed: 48 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,68 @@
22

33
module.exports = function extendWebpack(api, opts) {
44

5-
api.assertVersion('>=0.3.0');
5+
api.assertVersion('>=0.3.14');
66

77
const registerMethods = require('./methods');
88
registerMethods(api);
99

1010
const { _, smartMerge } = require('@micro-app/shared-utils');
1111

12-
// 扩增 microsConfig 配置
13-
api.modifyMicrosConfig(_config => {
14-
const configParser = require('./configParser');
15-
const microsExtraConfig = api.microsExtraConfig;
16-
return Object.keys(_config).reduce((obj, key) => {
17-
const _configParser = configParser(obj, key, microsExtraConfig[key]);
18-
Object.assign(obj[key], {
19-
entry: _configParser.entry(),
20-
htmls: _configParser.htmls(),
21-
staticPaths: _configParser.staticPaths(),
22-
});
23-
return obj;
24-
}, _config);
25-
});
26-
27-
// 扩增 config 配置
28-
api.modifyDefaultConfig(_config => {
29-
const configCombine = require('./configCombine');
30-
const microsConfig = api.microsConfig;
31-
const selfConfig = api.selfConfig;
32-
const _selfConfigCombine = configCombine(selfConfig);
33-
34-
function pickOptions(obj) {
35-
return _.pick(obj, [
36-
'alias',
37-
'resolveAlias',
38-
'shared',
39-
'resolveShared',
40-
'entry',
41-
'htmls',
42-
'staticPaths',
43-
]);
44-
}
45-
46-
const micros = api.micros;
47-
const finalMicrosConfigs = micros.map(key => {
48-
const obj = microsConfig[key];
49-
if (!obj) return {};
50-
const _configCombine = configCombine(obj);
51-
return Object.assign({
52-
pages: _configCombine.pages(),
53-
nodeModulesPaths: _configCombine.nodeModulesPaths(),
54-
}, pickOptions(obj));
12+
// 直接在 microsConfig 上设置, 扩增 microsConfig 配置
13+
const configParser = require('./configParser');
14+
const configCombine = require('./configCombine');
15+
const microsExtraConfig = api.microsExtraConfig;
16+
const microsConfig = api.microsConfig;
17+
Object.keys(microsConfig).forEach(key => {
18+
const item = microsConfig[key];
19+
const _configParser = configParser(microsConfig, key, microsExtraConfig[key]);
20+
Object.assign(item, {
21+
entry: _configParser.entry(),
22+
htmls: _configParser.htmls(),
23+
staticPaths: _configParser.staticPaths(),
24+
});
25+
const _configCombine = configCombine(item);
26+
Object.assign(item, {
27+
pages: _configCombine.pages(),
28+
nodeModulesPaths: _configCombine.nodeModulesPaths(),
5529
});
30+
});
5631

57-
const finalConfig = smartMerge({}, ...finalMicrosConfigs, Object.assign({
58-
pages: _selfConfigCombine.pages(),
59-
nodeModulesPaths: _selfConfigCombine.nodeModulesPaths(),
60-
}, pickOptions(selfConfig), selfConfig));
32+
// 直接在 config 上设置, 扩增 config 配置
33+
const config = api.config;
6134

62-
const originalConfig = selfConfig.originalConfig || {};
63-
const defaultConfig = {
64-
outputDir: 'dist',
65-
publicPath: '/',
66-
assetsDir: '',
67-
devServer: {},
68-
css: {},
69-
};
70-
const otherConfig = Object.assign({ // default config
71-
...defaultConfig,
72-
}, _.pick(originalConfig, [
35+
// 只使用所有配置中的一个
36+
const originalConfig = smartMerge({}, ...Object.values(microsConfig).map(item => {
37+
const originalConfig = item.originalConfig || {};
38+
return _.pick(originalConfig, [
7339
'outputDir',
7440
'publicPath',
7541
'assetsDir',
7642
'devServer',
7743
'css',
78-
]));
79-
// 校验
80-
return Object.assign({}, _config, _.cloneDeep(finalConfig), otherConfig);
81-
});
44+
]);
45+
}));
46+
47+
// 增加校验配置
48+
api.validateSchema(require('./configSchema'), originalConfig);
49+
50+
const defaultConfig = { // default config
51+
outputDir: 'dist',
52+
publicPath: '/',
53+
assetsDir: '',
54+
devServer: {},
55+
css: {},
56+
};
57+
smartMerge(config, ...Object.values(microsConfig).map(item => {
58+
return _.pick(item || {}, [
59+
'entry',
60+
'htmls',
61+
'staticPaths',
62+
'pages',
63+
'nodeModulesPaths',
64+
]);
65+
}), defaultConfig, originalConfig);
66+
8267
};
8368

8469
module.exports.configuration = {

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ const commands = [
1919
'inspect',
2020
];
2121

22+
const SKIP_TARGET = [ 'pure' ];
23+
2224
// 只能通过集中初始化去实现, 不可进行插件注册(registerPlugins). 否则顺序不可控.
2325
module.exports = [
2426
...extendConfigs.map(name => {
@@ -34,8 +36,8 @@ module.exports = [
3436
if (!item.configuration.skipTarget) {
3537
item.configuration.skipTarget = [];
3638
}
37-
item.configuration.skipTarget.push( // 统一适配 vue-cli
38-
'vue-cli-plugin'
39+
item.configuration.skipTarget.push( // 统一适配当 target = pure 时,不使用所有内置配置
40+
...SKIP_TARGET
3941
);
4042
}
4143
return item;

0 commit comments

Comments
 (0)