Skip to content

Commit 455ffc9

Browse files
committed
:enhance: add --mode and add inspect command.
1 parent 6e2f0e8 commit 455ffc9

File tree

13 files changed

+303
-2930
lines changed

13 files changed

+303
-2930
lines changed

bin/base.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ const yParser = require('yargs-parser');
1010
const cmd = process.argv[2];
1111
const argv = yParser(process.argv.slice(3));
1212

13-
// 全局环境
14-
if ([ 'start', 'build' ].includes(cmd)) {
15-
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
16-
} else if ([ 'serve' ].includes(cmd)) {
17-
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
13+
// 全局环境模式
14+
if (argv.mode) { // production, development
15+
process.env.NODE_ENV = argv.mode || process.env.NODE_ENV || 'development';
1816
}
1917

2018
const { Service, logger } = require('@micro-app/core');

bin/micro-app-build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22
'use strict';
33

4-
process.env.NODE_ENV = 'production';
4+
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
55

66
const yParser = require('yargs-parser');
77
const argv = yParser(process.argv.slice(2));

bin/micro-app-dev.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
#!/usr/bin/env node
22
'use strict';
33

4-
process.env.NODE_ENV = 'development';
4+
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
55

66
const yParser = require('yargs-parser');
77
const argv = yParser(process.argv.slice(2));
8-
98
const { service } = require('./base');
109

1110
service.run('serve', argv);

bin/micro-app-start.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22
'use strict';
33

4-
process.env.NODE_ENV = 'production';
4+
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
55

66
const yParser = require('yargs-parser');
77
const argv = yParser(process.argv.slice(2));

micro-app.config.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ module.exports = {
4545

4646
strict: true,
4747

48-
micros: [ 'test' ], // 被注册的容器
48+
// micros: [ 'test' ], // 被注册的容器
4949
// micros$$test: { // 单独配置
5050
// disabled: true, // 禁用入口
5151
// link: '', // 本地路径, 进行本地开发使用的软链接.
@@ -60,14 +60,13 @@ module.exports = {
6060
},
6161
},
6262

63-
plugins: [
64-
[{
65-
id: 'test',
66-
description: '这是test',
67-
link: __dirname + '/test/testPlugin',
68-
}, {
69-
a: 1,
70-
}],
71-
],
72-
63+
// plugins: [
64+
// [{
65+
// id: 'test',
66+
// description: '这是test',
67+
// link: __dirname + '/test/testPlugin',
68+
// }, {
69+
// a: 1,
70+
// }],
71+
// ],
7372
};

package.json

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@micro-app/cli",
3-
"version": "0.1.4-beta.3",
3+
"version": "0.1.5",
44
"description": "[CLI] Pluggable micro application framework.",
55
"bin": {
66
"micro-app": "./bin/micro-app.js",
@@ -36,27 +36,25 @@
3636
},
3737
"license": "MIT",
3838
"devDependencies": {
39-
"@micro-app/core": "^0.1.4-beta.2",
40-
"@micro-app/plugin-webpack-adapter": "^0.0.4",
39+
"@micro-app/core": "^0.1.7",
4140
"@micro-app/plugin-koa-static-server": "^0.0.1",
4241
"@micro-app/plugin-koa-webpack-middleware": "^0.0.2",
43-
"@micro-app/plugin-vue-cli": "^0.0.3-beta.3",
44-
"@micro-app/plugin-vusion-cli": "^0.0.4",
42+
"@micro-app/plugin-webpack-adapter": "^0.0.5",
4543
"@types/jest": "^24.0.18",
46-
"babel-eslint": "^10.0.2",
44+
"babel-eslint": "^10.0.3",
4745
"coveralls": "^3.0.6",
4846
"eslint": "^5.16.0",
4947
"eslint-config-2o3t": "^1.1.17",
5048
"jest": "^24.9.0",
5149
"webpack": "^4.39.2"
5250
},
5351
"peerDependencies": {
54-
"@micro-app/core": ">=0.1.5",
55-
"@micro-app/plugin-webpack-adapter": "^0.0.3"
52+
"@micro-app/core": ">=0.1.7",
53+
"@micro-app/plugin-webpack-adapter": "^0.0.5"
5654
},
5755
"dependencies": {
56+
"cli-highlight": "^2.1.1",
5857
"koa": "^2.8.1",
59-
"opn": "^5.5.0",
6058
"shelljs": "^0.8.3",
6159
"update-notifier": "^3.0.1",
6260
"yargs-parser": "^13.1.1"

plugins/commands/build/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ module.exports = function buildCommand(api, opts) {
1414
description: 'build for production',
1515
usage: 'micro-app build [options]',
1616
options: {
17-
'-': 'default webpack.',
18-
'-t <type>': 'adapter type, eg. [ webpack, vusion ].',
17+
'--mode': 'specify env mode (default: development)',
18+
'--type <type>': 'adapter type, eg. [ webpack, vusion ].',
1919
'--progress': 'show how progress is reported during a compilation.',
2020
},
2121
details: `
2222
Examples:
2323
${chalk.gray('# vusion')}
24-
micro-app build -t vusion
24+
micro-app build --type vusion
2525
`.trim(),
2626
}, args => {
2727
process.env.NODE_ENV = process.env.NODE_ENV || 'production';

plugins/commands/inspect/index.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
'use strict';
2+
module.exports = function inspectCommand(api, opts) {
3+
4+
const chalk = require('chalk');
5+
const _ = require('lodash');
6+
7+
const logger = api.logger;
8+
9+
api.registerCommand('inspect', {
10+
description: 'inspect internal webpack config',
11+
usage: 'micro-app inspect [options] [...paths]',
12+
options: {
13+
'--mode': 'specify env mode (default: development)',
14+
'--rule <ruleName>': 'inspect a specific module rule',
15+
'--plugin <pluginName>': 'inspect a specific plugin',
16+
'--rules': 'list all module rule names',
17+
'--plugins': 'list all plugin names',
18+
'--verbose': 'show full function definitions in output',
19+
'--type <type>': 'adapter type, eg. [ webpack, vusion, etc. ].',
20+
'--open-soft-link': '启用开发软链接',
21+
'--open-disabled-entry': '支持可配置禁用部分模块入口.',
22+
},
23+
details: `
24+
Examples:
25+
${chalk.gray('# vusion')}
26+
micro-app inspect --type vusion
27+
${chalk.gray('# open soft link')}
28+
micro-app inspect --type vusion --open-soft-link
29+
`.trim(),
30+
},
31+
args => {
32+
const { toString } = require('webpack-chain');
33+
const { highlight } = require('cli-highlight');
34+
35+
const { _: paths, verbose } = args;
36+
37+
let config = api.resolveWebpackConfig();
38+
39+
const { webpackConfig } = api.applyPluginHooks('modifyWebpackConfig', {
40+
args,
41+
webpackConfig: config,
42+
});
43+
44+
if (
45+
!_.isPlainObject(webpackConfig) || !webpackConfig
46+
) {
47+
logger.error('[Plugin] modifyWebpackConfig must return { args, webpackConfig }');
48+
return process.exit(1);
49+
}
50+
51+
// 更新一次
52+
api.setState('webpackConfig', webpackConfig);
53+
config = _.cloneDeep(webpackConfig);
54+
55+
let res;
56+
let hasUnnamedRule;
57+
if (args.rule) {
58+
res = config.module.rules.find(r => r.__ruleNames[0] === args.rule);
59+
} else if (args.plugin) {
60+
res = Array.isArray(config.plugins)
61+
? config.plugins.find(p => p.__pluginName === args.plugin)
62+
: {};
63+
} else if (args.rules) {
64+
res = config.module && Array.isArray(config.module.rules)
65+
? config.module.rules.map(r => {
66+
const name = r.__ruleNames ? r.__ruleNames[0] : 'Nameless Rule (*)';
67+
68+
hasUnnamedRule = hasUnnamedRule || !r.__ruleNames;
69+
70+
return name;
71+
})
72+
: [];
73+
} else if (args.plugins) {
74+
res = Array.isArray(config.plugins)
75+
? config.plugins.map(p => p.__pluginName || p.constructor.name)
76+
: [];
77+
} else if (paths.length > 1) {
78+
res = {};
79+
paths.forEach(path => {
80+
res[path] = _.get(config, path);
81+
});
82+
} else if (paths.length === 1) {
83+
res = _.get(config, paths[0]);
84+
} else {
85+
res = config;
86+
}
87+
88+
const output = toString(res, { verbose });
89+
logger.logo(highlight(output, { language: 'js' }));
90+
91+
// Log explanation for Nameless Rules
92+
if (hasUnnamedRule) {
93+
logger.logo(`--- ${chalk.green('Footnotes')} ---`);
94+
logger.logo(`*: ${chalk.green(
95+
'Nameless Rules'
96+
)} were added through the ${chalk.green(
97+
'configureWebpack()'
98+
)} API (possibly by a plugin) instead of ${chalk.green(
99+
'chainWebpack()'
100+
)} (recommended).
101+
You can run ${chalk.green(
102+
'micro-app inspect'
103+
)} without any arguments to inspect the full config and read these rules' config.`);
104+
}
105+
}
106+
);
107+
};
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict';
2+
3+
/* global expect */
4+
5+
const path = require('path');
6+
7+
describe('Plugin micro-app:inspect', () => {
8+
9+
it('inspect', () => {
10+
const { service } = require('../../../bin/base');
11+
service.registerPlugin({
12+
id: '@micro-app/plugin-webpack-adapter',
13+
});
14+
service.registerPlugin({
15+
id: 'test:inspect',
16+
link: path.join(__dirname, './index.js'),
17+
});
18+
19+
service.run('inspect', { _: [] });
20+
});
21+
22+
it('inspect-plugins', () => {
23+
const { service } = require('../../../bin/base');
24+
service.registerPlugin({
25+
id: '@micro-app/plugin-webpack-adapter',
26+
});
27+
service.registerPlugin({
28+
id: 'test:inspect',
29+
link: path.join(__dirname, './index.js'),
30+
});
31+
32+
service.run('inspect', { _: [], plugins: true });
33+
});
34+
35+
it('inspect-rules', () => {
36+
const { service } = require('../../../bin/base');
37+
service.registerPlugin({
38+
id: '@micro-app/plugin-webpack-adapter',
39+
});
40+
service.registerPlugin({
41+
id: 'test:inspect',
42+
link: path.join(__dirname, './index.js'),
43+
});
44+
45+
service.run('inspect', { _: [], rules: true });
46+
});
47+
48+
it('inspect-verbose', () => {
49+
const { service } = require('../../../bin/base');
50+
service.registerPlugin({
51+
id: '@micro-app/plugin-webpack-adapter',
52+
});
53+
service.registerPlugin({
54+
id: 'test:inspect',
55+
link: path.join(__dirname, './index.js'),
56+
});
57+
58+
service.run('inspect', { _: [], verbose: true });
59+
});
60+
61+
it('inspect-path', () => {
62+
const { service } = require('../../../bin/base');
63+
service.registerPlugin({
64+
id: '@micro-app/plugin-webpack-adapter',
65+
});
66+
service.registerPlugin({
67+
id: 'test:inspect',
68+
link: path.join(__dirname, './index.js'),
69+
});
70+
71+
service.run('inspect', { _: [ 'entry.main', 'resolve.alias' ], verbose: true });
72+
});
73+
74+
});

plugins/commands/serve/dev.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module.exports = function devCommand(api, opts) {
1111
description: 'runs server for development',
1212
usage: 'micro-app serve [options]',
1313
options: {
14-
'-': 'default.',
14+
'--mode': 'specify env mode (default: development)',
1515
'--type <type>': 'adapter type, eg. [ webpack, vusion, etc. ].',
1616
'--host <host>': 'node server host.',
1717
'--port <port>': 'node server port.',

0 commit comments

Comments
 (0)