Skip to content

Commit b3ea0fa

Browse files
committed
refactor: logic optimization
1 parent 99867a2 commit b3ea0fa

File tree

9 files changed

+1035
-135
lines changed

9 files changed

+1035
-135
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
},
3939
"license": "MIT",
4040
"peerDependencies": {
41-
"@micro-app/cli": ">=0.3.6",
42-
"@micro-app/plugin-webpack": ">=0.0.6"
41+
"@micro-app/cli": ">=0.3.7",
42+
"@micro-app/plugin-webpack": ">=0.0.8"
4343
},
4444
"devDependencies": {
4545
"@micro-app/plugin-deploy": "^0.0.6",
@@ -49,7 +49,7 @@
4949
"webpack": "^4.42.0"
5050
},
5151
"dependencies": {
52-
"@micro-app/cli": "^0.3.6",
53-
"@micro-app/plugin-webpack": "^0.0.6"
52+
"@micro-app/cli": "^0.3.7",
53+
"@micro-app/plugin-webpack": "^0.0.8"
5454
}
5555
}

src/constants.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
'use strict';
2+
3+
module.exports = {
4+
builtIn: Symbol.for('built-in'),
5+
skipTarget: 'vue-cli-plugin',
6+
};

src/index.js

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
'use strict';
22

33
const chainConfig = require('./service/chainConfig');
4+
const createService = require('./utils/createService');
5+
const CONSTANTS = require('./constants');
46

57
module.exports = function(api, vueConfig) {
6-
7-
const { createService } = require('@micro-app/cli');
88
const service = createService();
99

10-
const builtIn = Symbol.for('built-in');
11-
12-
// 注册 webapck 插件
13-
service.registerPlugin({
14-
id: '@micro-app/plugin-webpack',
15-
[builtIn]: true,
16-
});
17-
1810
// 注册插件
1911
service.registerPlugin({
20-
id: 'vue-cli:plugin-command-return-config',
21-
link: require.resolve('./plugins/return.js'),
22-
[builtIn]: true,
12+
id: 'vue-cli-plugin:plugin-command-return-config',
13+
[CONSTANTS.builtIn]: true,
14+
apply(_api) {
15+
_api.registerCommand('return-config', {
16+
description: 'return config of MicroApp.',
17+
usage: 'micro-app return-config',
18+
}, () => {
19+
return _api.config;
20+
});
21+
},
2322
});
2423

25-
const config = service.runSync('return-config');
26-
const webpackConfig = service.runSync('return-config', { _: [], key: 'config' });
24+
// 第一次加载所有配置
25+
const config = service.runSync('return-config', { _: [], target: CONSTANTS.skipTarget });
2726

28-
return chainConfig(api, vueConfig, config, webpackConfig);
27+
return chainConfig(api, vueConfig, config);
2928
};

src/plugins/return.js

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

src/service/chainConfig.js

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

3-
const { _, tryRequire } = require('@micro-app/shared-utils');
3+
const { _, tryRequire, fs } = require('@micro-app/shared-utils');
4+
const createService = require('../utils/createService');
5+
const CONSTANTS = require('../constants');
46

57
// 以 vue-cli 配置为主
6-
module.exports = function chainDefault(api, vueConfig, options, webpackConfig) {
7-
const webpackConfigAlias = webpackConfig.resolve.alias || {};
8+
module.exports = function chainDefault(api, vueConfig, options) {
89

910
[ // string
1011
'publicPath',
@@ -28,9 +29,8 @@ module.exports = function chainDefault(api, vueConfig, options, webpackConfig) {
2829
}
2930
});
3031

32+
// 补充
3133
api.chainWebpack(webpackChain => {
32-
const outputDir = api.resolve(options.outputDir);
33-
3434
const nodeModulesPaths = options.nodeModulesPaths || [];
3535
webpackChain.resolve
3636
.set('symlinks', true)
@@ -44,30 +44,55 @@ module.exports = function chainDefault(api, vueConfig, options, webpackConfig) {
4444
.merge(nodeModulesPaths)
4545
.end();
4646

47-
const alias = Object.assign({}, options.resolveAlias || {}, webpackConfigAlias);
47+
const alias = Object.assign({}, options.resolveAlias || {});
4848
// alias
4949
webpackChain.resolve
5050
.alias
5151
.merge(alias)
5252
.end();
5353

54-
// const entry = options.entry || {};
55-
// // entry
56-
// Object.keys(entry).forEach(key => {
57-
// webpackChain.entry(key).merge(entry[key]);
58-
// });
59-
60-
const CopyWebpackPlugin = tryRequire('copy-webpack-plugin');
61-
if (CopyWebpackPlugin) {
62-
const staticPaths = options.staticPaths || [];
63-
// 有的时候找不到原来的 CopyWebpackPlugin,不知道为什么
64-
webpackChain.plugin('copyEx').use(CopyWebpackPlugin, [
65-
staticPaths.map(staticPath => {
66-
return { from: staticPath, to: outputDir, ignore: [ '.*' ] };
67-
}),
68-
]);
54+
// copy static
55+
const staticPaths = (options.staticPaths || []).filter(item => fs.existsSync(item));
56+
if (staticPaths.length) {
57+
const CopyWebpackPlugin = tryRequire('copy-webpack-plugin');
58+
if (CopyWebpackPlugin) {
59+
webpackChain
60+
.plugin('copy')
61+
.use(CopyWebpackPlugin, [ staticPaths.map(publicDir => {
62+
return {
63+
from: publicDir,
64+
toType: 'dir',
65+
ignore: [ '.*' ],
66+
};
67+
}) ]);
68+
} else {
69+
api.logger.warn('[webpack]', 'Not Found "copy-webpack-plugin"');
70+
}
6971
}
7072

7173
return webpackChain;
7274
});
75+
76+
// webpack 所有配置合入
77+
api.chainWebpack(webpackChain => {
78+
const service = createService();
79+
80+
// 注册插件
81+
service.registerPlugin({
82+
id: 'vue-cli-plugin:plugin-command-return-webpack-chain',
83+
[CONSTANTS.builtIn]: 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+
_api.createChainWebpackConfigInstance(webpackChain);
90+
return _api.resolveChainableWebpackConfig();
91+
});
92+
},
93+
});
94+
95+
// 同步扩充 webpack-chain config
96+
return service.runSync('return-webpack-chain', { _: [], target: CONSTANTS.skipTarget });
97+
});
7398
};

src/utils/createService.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
const { createService } = require('@micro-app/cli');
4+
const CONSTANTS = require('../constants');
5+
6+
module.exports = function() {
7+
const service = createService();
8+
9+
const webpackPluginId = '@micro-app/plugin-webpack';
10+
if (!service.hasPlugin(webpackPluginId)) {
11+
// 注册 webapck 插件
12+
service.registerPlugin({
13+
id: webpackPluginId,
14+
[CONSTANTS.builtIn]: true,
15+
});
16+
}
17+
18+
return service;
19+
};

test/createService.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
/* global expect */
4+
5+
describe('Vue CLI Plugin', () => {
6+
7+
const createService = require('../src/utils/createService');
8+
9+
it('createService', async () => {
10+
const service = createService();
11+
12+
expect(typeof service === 'object').not.toBeUndefined();
13+
14+
await service.run('help');
15+
});
16+
17+
});

test/index.spec.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,19 @@
44

55
describe('Vue CLI Plugin', () => {
66

7+
const plugin = require('../src');
8+
9+
it('plugin', async () => {
10+
const api = {
11+
chainWebpack() {
12+
13+
},
14+
};
15+
16+
plugin(api, {});
17+
});
18+
719
it('plugin', async () => {
8-
const plugin = require('../src');
920

1021
const api = {
1122
chainWebpack() {

0 commit comments

Comments
 (0)