Skip to content

Commit ec485fa

Browse files
committed
:feat: add target: plugin
1 parent 7968bd7 commit ec485fa

File tree

13 files changed

+261
-111
lines changed

13 files changed

+261
-111
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,7 @@ dist
8686
*.njsproj
8787
*.sln
8888
*.sw*
89+
90+
91+
# custom
92+
microapp/.temp

micro-app.config.js

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

3-
module.exports = {
3+
const config = {
44
name: '@micro-app/demo',
55
description: '',
66
version: '0.0.1',
@@ -43,14 +43,6 @@ module.exports = {
4343
},
4444
},
4545

46-
strict: true,
47-
48-
// micros: [ 'test' ], // 被注册的容器
49-
// micros$$test: { // 单独配置
50-
// disabled: true, // 禁用入口
51-
// link: '', // 本地路径, 进行本地开发使用的软链接.
52-
// },
53-
5446
// 服务配置
5547
server: {
5648
entry: '', // 服务端入口
@@ -59,11 +51,11 @@ module.exports = {
5951
// 服务端回调参数
6052
},
6153
},
62-
63-
plugins: [
64-
{
65-
id: 'test',
66-
link: __dirname + '/src/index.js',
67-
},
68-
],
6954
};
55+
56+
57+
config.plugins = [
58+
__dirname,
59+
];
60+
61+
module.exports = config;

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@
4747
"@micro-app/cli": ">=0.3.0"
4848
},
4949
"devDependencies": {
50-
"@micro-app/cli": "file:../MicroApp-CLI",
50+
"@micro-app/cli": "^0.3.0-alpha.4",
5151
"@types/jest": "^24.0.19",
5252
"babel-eslint": "^10.0.3",
5353
"coveralls": "^3.0.7",
5454
"eslint": "^5.16.0",
5555
"eslint-config-2o3t": "^1.1.17",
56+
"hash-sum": "^2.0.0",
5657
"husky": "^3.0.9",
5758
"jest": "^24.9.0",
5859
"lint-staged": "^9.4.2",

src/commands/build/index.js

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@ const defaults = {
55
target: 'app',
66
};
77

8-
const modifyConfig = (config, fn) => {
9-
if (Array.isArray(config)) {
10-
config.forEach(c => fn(c));
11-
} else {
12-
fn(config);
13-
}
14-
};
15-
168
module.exports = function buildCommand(api, opts) {
179

1810
const { tryRequire, chalk, fs } = require('@micro-app/shared-utils');
@@ -28,15 +20,14 @@ module.exports = function buildCommand(api, opts) {
2820
'--mode': 'specify env mode (default: development)',
2921
'--type <type>': 'adapter type, eg. [ webpack, etc. ].',
3022
'--dest': 'specify output directory',
31-
'--watch': 'watch for changes',
3223
'--clean': 'remove the dist directory before building the project',
33-
// '--target': `app | lib (default: ${defaults.target})`,
24+
'--target': `app | lib | plugin (default: ${defaults.target})`,
3425
},
3526
details: `
3627
Examples:
3728
${chalk.gray('# watch')}
3829
micro-app build --watch
39-
`.trim(),
30+
`.trim(),
4031
}, async args => {
4132
const logger = api.logger;
4233

@@ -63,13 +54,9 @@ Examples:
6354

6455
api.applyPluginHooks('beforeBuild', { args });
6556

66-
const webpackConfig = api.resolveWebpackConfig();
67-
68-
if (args.watch) {
69-
modifyConfig(webpackConfig, config => {
70-
config.watch = true;
71-
});
72-
}
57+
const webpackConfig = api.resolveWebpackConfig({
58+
target: args.target,
59+
});
7360

7461
if (args.dest) {
7562
// Override outputDir before resolving webpack config as config relies on it (#2327)
@@ -102,6 +89,7 @@ Examples:
10289
if (stats.hasErrors()) {
10390
// 在这里处理错误
10491
api.applyPluginHooks('onBuildFail', { stats, args });
92+
// console.warn(stats);
10593
return reject('Build failed with errors.');
10694
}
10795

@@ -114,7 +102,6 @@ Examples:
114102
if (args.target === 'app') {
115103
if (!args.watch) {
116104
logger.success(`Build complete. The ${chalk.cyan(targetDirShort)} directory is ready to be deployed.`);
117-
logger.info(`Check out deployment instructions at ${chalk.cyan('https://cli.vuejs.org/guide/deployment.html')}\n`);
118105
} else {
119106
logger.success('Build complete. Watching for changes...');
120107
}
@@ -127,7 +114,7 @@ Examples:
127114
resolve();
128115
});
129116
}).then(() => {
130-
api.logger.success('>>> Build Success >>>');
117+
api.logger.success('>>> Build Success !!!');
131118
}).catch(e => {
132119
api.logger.error('>>> Build Error >>>', e);
133120
});

src/commands/inspect/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ Examples:
3232

3333
const { _: paths, verbose } = args;
3434

35-
const webpackConfig = api.resolveWebpackConfig();
35+
const webpackConfig = api.resolveWebpackConfig({
36+
target: args.target,
37+
});
3638

3739
const config = _.cloneDeep(webpackConfig);
3840

src/commands/serve/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ module.exports = function serveCommand(api, opts) {
105105
return webpackChain;
106106
});
107107

108-
const webpackConfig = api.resolveWebpackConfig();
108+
const webpackConfig = api.resolveWebpackConfig({
109+
target: args.target,
110+
});
109111

110112
// check for common config errors
111113
validateWebpackConfig(webpackConfig, api, options);
@@ -194,6 +196,8 @@ module.exports = function serveCommand(api, opts) {
194196

195197
const isWebpackDevServer3 = semver.satisfies(webpackDevServerVersion, '>=3');
196198

199+
const contentBase = Array.isArray(options.staticPaths) ? (options.staticPaths.length ? options.staticPaths : false) : options.staticPaths || false;
200+
197201
// create server
198202
const server = new WebpackDevServer(compiler, Object.assign(isWebpackDevServer3 ? {
199203
logLevel: 'silent',
@@ -203,7 +207,7 @@ module.exports = function serveCommand(api, opts) {
203207
disableDotRule: true,
204208
rewrites: genHistoryApiFallbackRewrites(options.publicPath, options.pages),
205209
},
206-
contentBase: options.staticPaths || [],
210+
contentBase,
207211
watchContentBase: !isProduction,
208212
hot: !isProduction,
209213
compress: isProduction,
@@ -390,5 +394,5 @@ function genHistoryApiFallbackRewrites(baseUrl, pages = {}) {
390394

391395
module.exports.configuration = {
392396
description: 'webpack hot serve for dev',
393-
mode: 'development',
397+
// mode: 'development',
394398
};

src/extends/enhance/index.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,47 @@ module.exports = function WebpackAdapter(api, opts) {
1212

1313
let initialized = false;
1414

15+
const logger = api.logger;
16+
1517
api.extendMethod('resolveChainableWebpackConfig', {
1618
description: 'resolve webpack-chain config.',
17-
}, () => {
19+
}, ({ target = 'app' } = {}) => {
1820
if (!initialized) {
19-
api.logger.error('please call after "onInitWillDone" !');
20-
process.exit(1);
21+
logger.throw('please call after "onInitWillDone" !');
2122
}
22-
const selfConfig = api.selfConfig || {};
23-
const originalConfig = selfConfig.originalConfig || {};
24-
const _originalWebpackConfig = _.cloneDeep(originalConfig.webpack || {});
25-
delete _originalWebpackConfig.entry; // 不接受 entry, 内部已经做了兼容
26-
delete _originalWebpackConfig.plugins; // 不接受 plugins
2723

2824
const webpackChainConfig = new Config();
29-
webpackChainConfig.merge(_originalWebpackConfig);
25+
let finalWebpackChainConfig = webpackChainConfig;
26+
if (target === 'plugin') {
27+
// TODO 针对所有 plugin 的配置进行处理
28+
finalWebpackChainConfig = api.applyPluginHooks('modifyChainWebpackPluginConfig', webpackChainConfig);
29+
api.applyPluginHooks('onChainWebpcakPluginConfig', finalWebpackChainConfig);
30+
} else {
31+
const selfConfig = api.selfConfig || {};
32+
const originalConfig = selfConfig.originalConfig || {};
33+
const _originalWebpackConfig = _.cloneDeep(originalConfig.webpack || {});
34+
delete _originalWebpackConfig.entry; // 不接受 entry, 内部已经做了兼容
35+
delete _originalWebpackConfig.plugins; // 不接受 plugins
36+
webpackChainConfig.merge(_originalWebpackConfig);
3037

31-
const finalWebpackChainConfig = api.applyPluginHooks('modifyChainWebpackConfig', webpackChainConfig);
32-
api.applyPluginHooks('onChainWebpcakConfig', finalWebpackChainConfig);
38+
finalWebpackChainConfig = api.applyPluginHooks('modifyChainWebpackConfig', webpackChainConfig);
39+
api.applyPluginHooks('onChainWebpcakConfig', finalWebpackChainConfig);
40+
}
3341

3442
api.setState('webpackChainConfig', finalWebpackChainConfig);
3543
return finalWebpackChainConfig;
3644
});
3745

3846
api.extendMethod('resolveWebpackConfig', {
3947
description: 'resolve webpack config.',
40-
}, () => {
41-
const finalWebpackChainConfig = api.resolveChainableWebpackConfig();
42-
const webpackConfig = api.applyPluginHooks('modifyWebpackConfig', finalWebpackChainConfig.toConfig());
48+
}, ({ target = 'app' } = {}) => {
49+
const finalWebpackChainConfig = api.resolveChainableWebpackConfig({ target });
50+
const webpackConfig = finalWebpackChainConfig.toConfig();
51+
if (target === 'plugin') {
52+
api.applyPluginHooks('modifyWebpackPluginConfig', webpackConfig);
53+
} else {
54+
api.applyPluginHooks('modifyWebpackConfig', webpackConfig);
55+
}
4356

4457
api.setState('webpackConfig', webpackConfig);
4558
return webpackConfig;

src/extends/enhance/methods.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,17 @@ module.exports = (api, opts) => {
1515
description: '合并之后提供 webpack config 进行再次修改事件',
1616
});
1717

18+
// plugin webpack
19+
api.registerMethod('modifyChainWebpackPluginConfig', {
20+
type: api.API_TYPE.MODIFY,
21+
description: '合并所有 plugin 之后提供 webpack-chain 进行再次修改事件',
22+
});
23+
api.registerMethod('onChainWebpcakPluginConfig', {
24+
type: api.API_TYPE.EVENT,
25+
description: '修改所有 plugin 之后提供 webpack-chain 进行查看事件',
26+
});
27+
api.registerMethod('modifyWebpackPluginConfig', {
28+
type: api.API_TYPE.MODIFY,
29+
description: '合并所有 plugin 之后提供 webpack config 进行再次修改事件',
30+
});
1831
};

0 commit comments

Comments
 (0)