Skip to content

Commit 91d5384

Browse files
committed
chore: refactor code & update deps
1 parent c04e5fd commit 91d5384

File tree

9 files changed

+150
-309
lines changed

9 files changed

+150
-309
lines changed

microapp/config.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,34 @@ const config = {
44
type: '', // types 类型
55
};
66

7-
config.plugins = [
8-
'@micro-app/plugin-deploy', // test
9-
];
7+
if (process.env.NODE_ENV === 'test') {
8+
config.plugins = [
9+
'@micro-app/plugin-deploy', // test
10+
];
11+
12+
Object.assign(config, {
13+
entry: {
14+
main: './client/main.js',
15+
},
16+
17+
// staticPath: '',
18+
19+
htmls: [
20+
{
21+
filename: 'index.html',
22+
hash: true,
23+
chunks: [ 'common', 'main' ],
24+
template: './client/index.html',
25+
},
26+
],
27+
alias: { // 前端共享
28+
api: './client/api.js',
29+
config: {
30+
type: 'server', // 后端共享
31+
link: './server/config.js',
32+
},
33+
},
34+
});
35+
}
1036

1137
module.exports = config;

package.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@
3737
"email": "[email protected]"
3838
},
3939
"license": "MIT",
40-
"peerDependencies": {
41-
"@micro-app/cli": ">=0.3.7",
42-
"@micro-app/plugin-webpack": ">=0.0.8"
43-
},
4440
"devDependencies": {
4541
"@micro-app/plugin-deploy": "^0.0.6",
4642
"@types/jest": "^24.9.1",
@@ -49,7 +45,7 @@
4945
"webpack": "^4.42.0"
5046
},
5147
"dependencies": {
52-
"@micro-app/cli": "^0.3.7",
53-
"@micro-app/plugin-webpack": "^0.0.8"
48+
"@micro-app/core": "^0.3.18",
49+
"@micro-app/plugin-webpack": "^0.0.11"
5450
}
5551
}

src/constants.js

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

33
module.exports = {
4-
builtIn: Symbol.for('built-in'),
5-
skipTarget: 'vue-cli-plugin',
4+
BUILT_IN: Symbol.for('built-in'),
5+
SKIP_TARGET: 'pure',
66
};

src/index.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,13 @@
22

33
const chainConfig = require('./service/chainConfig');
44
const createService = require('./utils/createService');
5-
const CONSTANTS = require('./constants');
65

76
module.exports = function(api, vueConfig) {
87
const service = createService();
98

10-
// 注册插件
11-
service.registerPlugin({
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-
},
22-
});
23-
24-
// 第一次加载所有配置
25-
const config = service.runSync('return-config', { _: [], target: CONSTANTS.skipTarget });
9+
// 加载获取所有配置
10+
service.initSync();
11+
const config = service.config;
2612

2713
return chainConfig(api, vueConfig, config);
2814
};

src/service/chainConfig.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const { _, tryRequire, fs } = require('@micro-app/shared-utils');
44
const createService = require('../utils/createService');
5-
const CONSTANTS = require('../constants');
5+
const { BUILT_IN } = require('../constants');
66

77
// 以 vue-cli 配置为主
88
module.exports = function chainDefault(api, vueConfig, options) {
@@ -14,7 +14,9 @@ module.exports = function chainDefault(api, vueConfig, options) {
1414
]
1515
.forEach(key => {
1616
if (!_.isUndefined(options[key])) {
17-
vueConfig[key] = options[key];
17+
if (_.isUndefined(vueConfig[key])) { // vueConfig 中是否已经存在配置了
18+
vueConfig[key] = options[key];
19+
}
1820
}
1921
});
2022

@@ -80,8 +82,11 @@ module.exports = function chainDefault(api, vueConfig, options) {
8082
// 注册插件
8183
service.registerPlugin({
8284
id: 'vue-cli-plugin:plugin-command-return-webpack-chain',
83-
[CONSTANTS.builtIn]: true,
85+
[BUILT_IN]: true,
8486
apply(_api) {
87+
// 修改默认配置
88+
89+
8590
_api.registerCommand('return-webpack-chain', {
8691
description: 'return config of webpack-chain.',
8792
usage: 'micro-app return-webpack-chain',
@@ -93,6 +98,6 @@ module.exports = function chainDefault(api, vueConfig, options) {
9398
});
9499

95100
// 同步扩充 webpack-chain config
96-
return service.runSync('return-webpack-chain', { _: [], target: CONSTANTS.skipTarget });
101+
return service.runSync('return-webpack-chain');
97102
});
98103
};

src/utils/createService.js

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

3-
const { createService } = require('@micro-app/cli');
4-
const CONSTANTS = require('../constants');
3+
const Service = require('@micro-app/core');
4+
const { SKIP_TARGET, BUILT_IN } = require('../constants');
55

66
module.exports = function() {
7-
const service = createService();
7+
const service = new Service({ target: SKIP_TARGET });
88

9-
const webpackPluginId = '@micro-app/plugin-webpack';
10-
if (!service.hasPlugin(webpackPluginId)) {
9+
const WEBPACK_PLUGIN_ID = '@micro-app/plugin-webpack';
10+
if (!service.hasPlugin(WEBPACK_PLUGIN_ID)) {
1111
// 注册 webapck 插件
1212
service.registerPlugin({
13-
id: webpackPluginId,
14-
[CONSTANTS.builtIn]: true,
13+
id: WEBPACK_PLUGIN_ID,
14+
[BUILT_IN]: true,
1515
});
1616
}
1717

test/createService.spec.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,21 @@ describe('Vue CLI Plugin', () => {
99
it('createService', async () => {
1010
const service = createService();
1111

12-
expect(typeof service === 'object').not.toBeUndefined();
12+
expect(typeof service === 'object').toBeTruthy();
1313

1414
await service.run('help');
1515
});
1616

17+
it('getConfig', () => {
18+
const service = createService();
19+
20+
// 加载获取所有配置
21+
service.initSync();
22+
const config = service.config;
23+
24+
expect(typeof config === 'object').toBeTruthy();
25+
26+
console.warn('config:', config);
27+
});
28+
1729
});

test/index.spec.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,4 @@ describe('Vue CLI Plugin', () => {
1616
plugin(api, {});
1717
});
1818

19-
it('plugin', async () => {
20-
21-
const api = {
22-
chainWebpack() {
23-
24-
},
25-
};
26-
27-
plugin(api, {});
28-
});
29-
3019
});

0 commit comments

Comments
 (0)