Skip to content

Commit

Permalink
:enhance: add --mode and add inspect command.
Browse files Browse the repository at this point in the history
  • Loading branch information
zyao89 committed Sep 28, 2019
1 parent 6e2f0e8 commit 455ffc9
Show file tree
Hide file tree
Showing 13 changed files with 303 additions and 2,930 deletions.
8 changes: 3 additions & 5 deletions bin/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ const yParser = require('yargs-parser');
const cmd = process.argv[2];
const argv = yParser(process.argv.slice(3));

// 全局环境
if ([ 'start', 'build' ].includes(cmd)) {
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
} else if ([ 'serve' ].includes(cmd)) {
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
// 全局环境模式
if (argv.mode) { // production, development
process.env.NODE_ENV = argv.mode || process.env.NODE_ENV || 'development';
}

const { Service, logger } = require('@micro-app/core');
Expand Down
2 changes: 1 addition & 1 deletion bin/micro-app-build.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
'use strict';

process.env.NODE_ENV = 'production';
process.env.NODE_ENV = process.env.NODE_ENV || 'production';

const yParser = require('yargs-parser');
const argv = yParser(process.argv.slice(2));
Expand Down
3 changes: 1 addition & 2 deletions bin/micro-app-dev.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!/usr/bin/env node
'use strict';

process.env.NODE_ENV = 'development';
process.env.NODE_ENV = process.env.NODE_ENV || 'development';

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

const { service } = require('./base');

service.run('serve', argv);
2 changes: 1 addition & 1 deletion bin/micro-app-start.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node
'use strict';

process.env.NODE_ENV = 'production';
process.env.NODE_ENV = process.env.NODE_ENV || 'production';

const yParser = require('yargs-parser');
const argv = yParser(process.argv.slice(2));
Expand Down
21 changes: 10 additions & 11 deletions micro-app.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = {

strict: true,

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

plugins: [
[{
id: 'test',
description: '这是test',
link: __dirname + '/test/testPlugin',
}, {
a: 1,
}],
],

// plugins: [
// [{
// id: 'test',
// description: '这是test',
// link: __dirname + '/test/testPlugin',
// }, {
// a: 1,
// }],
// ],
};
16 changes: 7 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@micro-app/cli",
"version": "0.1.4-beta.3",
"version": "0.1.5",
"description": "[CLI] Pluggable micro application framework.",
"bin": {
"micro-app": "./bin/micro-app.js",
Expand Down Expand Up @@ -36,27 +36,25 @@
},
"license": "MIT",
"devDependencies": {
"@micro-app/core": "^0.1.4-beta.2",
"@micro-app/plugin-webpack-adapter": "^0.0.4",
"@micro-app/core": "^0.1.7",
"@micro-app/plugin-koa-static-server": "^0.0.1",
"@micro-app/plugin-koa-webpack-middleware": "^0.0.2",
"@micro-app/plugin-vue-cli": "^0.0.3-beta.3",
"@micro-app/plugin-vusion-cli": "^0.0.4",
"@micro-app/plugin-webpack-adapter": "^0.0.5",
"@types/jest": "^24.0.18",
"babel-eslint": "^10.0.2",
"babel-eslint": "^10.0.3",
"coveralls": "^3.0.6",
"eslint": "^5.16.0",
"eslint-config-2o3t": "^1.1.17",
"jest": "^24.9.0",
"webpack": "^4.39.2"
},
"peerDependencies": {
"@micro-app/core": ">=0.1.5",
"@micro-app/plugin-webpack-adapter": "^0.0.3"
"@micro-app/core": ">=0.1.7",
"@micro-app/plugin-webpack-adapter": "^0.0.5"
},
"dependencies": {
"cli-highlight": "^2.1.1",
"koa": "^2.8.1",
"opn": "^5.5.0",
"shelljs": "^0.8.3",
"update-notifier": "^3.0.1",
"yargs-parser": "^13.1.1"
Expand Down
6 changes: 3 additions & 3 deletions plugins/commands/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ module.exports = function buildCommand(api, opts) {
description: 'build for production',
usage: 'micro-app build [options]',
options: {
'-': 'default webpack.',
'-t <type>': 'adapter type, eg. [ webpack, vusion ].',
'--mode': 'specify env mode (default: development)',
'--type <type>': 'adapter type, eg. [ webpack, vusion ].',
'--progress': 'show how progress is reported during a compilation.',
},
details: `
Examples:
${chalk.gray('# vusion')}
micro-app build -t vusion
micro-app build --type vusion
`.trim(),
}, args => {
process.env.NODE_ENV = process.env.NODE_ENV || 'production';
Expand Down
107 changes: 107 additions & 0 deletions plugins/commands/inspect/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
'use strict';
module.exports = function inspectCommand(api, opts) {

const chalk = require('chalk');
const _ = require('lodash');

const logger = api.logger;

api.registerCommand('inspect', {
description: 'inspect internal webpack config',
usage: 'micro-app inspect [options] [...paths]',
options: {
'--mode': 'specify env mode (default: development)',
'--rule <ruleName>': 'inspect a specific module rule',
'--plugin <pluginName>': 'inspect a specific plugin',
'--rules': 'list all module rule names',
'--plugins': 'list all plugin names',
'--verbose': 'show full function definitions in output',
'--type <type>': 'adapter type, eg. [ webpack, vusion, etc. ].',
'--open-soft-link': '启用开发软链接',
'--open-disabled-entry': '支持可配置禁用部分模块入口.',
},
details: `
Examples:
${chalk.gray('# vusion')}
micro-app inspect --type vusion
${chalk.gray('# open soft link')}
micro-app inspect --type vusion --open-soft-link
`.trim(),
},
args => {
const { toString } = require('webpack-chain');
const { highlight } = require('cli-highlight');

const { _: paths, verbose } = args;

let config = api.resolveWebpackConfig();

const { webpackConfig } = api.applyPluginHooks('modifyWebpackConfig', {
args,
webpackConfig: config,
});

if (
!_.isPlainObject(webpackConfig) || !webpackConfig
) {
logger.error('[Plugin] modifyWebpackConfig must return { args, webpackConfig }');
return process.exit(1);
}

// 更新一次
api.setState('webpackConfig', webpackConfig);
config = _.cloneDeep(webpackConfig);

let res;
let hasUnnamedRule;
if (args.rule) {
res = config.module.rules.find(r => r.__ruleNames[0] === args.rule);
} else if (args.plugin) {
res = Array.isArray(config.plugins)
? config.plugins.find(p => p.__pluginName === args.plugin)
: {};
} else if (args.rules) {
res = config.module && Array.isArray(config.module.rules)
? config.module.rules.map(r => {
const name = r.__ruleNames ? r.__ruleNames[0] : 'Nameless Rule (*)';

hasUnnamedRule = hasUnnamedRule || !r.__ruleNames;

return name;
})
: [];
} else if (args.plugins) {
res = Array.isArray(config.plugins)
? config.plugins.map(p => p.__pluginName || p.constructor.name)
: [];
} else if (paths.length > 1) {
res = {};
paths.forEach(path => {
res[path] = _.get(config, path);
});
} else if (paths.length === 1) {
res = _.get(config, paths[0]);
} else {
res = config;
}

const output = toString(res, { verbose });
logger.logo(highlight(output, { language: 'js' }));

// Log explanation for Nameless Rules
if (hasUnnamedRule) {
logger.logo(`--- ${chalk.green('Footnotes')} ---`);
logger.logo(`*: ${chalk.green(
'Nameless Rules'
)} were added through the ${chalk.green(
'configureWebpack()'
)} API (possibly by a plugin) instead of ${chalk.green(
'chainWebpack()'
)} (recommended).
You can run ${chalk.green(
'micro-app inspect'
)} without any arguments to inspect the full config and read these rules' config.`);
}
}
);
};
74 changes: 74 additions & 0 deletions plugins/commands/inspect/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict';

/* global expect */

const path = require('path');

describe('Plugin micro-app:inspect', () => {

it('inspect', () => {
const { service } = require('../../../bin/base');
service.registerPlugin({
id: '@micro-app/plugin-webpack-adapter',
});
service.registerPlugin({
id: 'test:inspect',
link: path.join(__dirname, './index.js'),
});

service.run('inspect', { _: [] });
});

it('inspect-plugins', () => {
const { service } = require('../../../bin/base');
service.registerPlugin({
id: '@micro-app/plugin-webpack-adapter',
});
service.registerPlugin({
id: 'test:inspect',
link: path.join(__dirname, './index.js'),
});

service.run('inspect', { _: [], plugins: true });
});

it('inspect-rules', () => {
const { service } = require('../../../bin/base');
service.registerPlugin({
id: '@micro-app/plugin-webpack-adapter',
});
service.registerPlugin({
id: 'test:inspect',
link: path.join(__dirname, './index.js'),
});

service.run('inspect', { _: [], rules: true });
});

it('inspect-verbose', () => {
const { service } = require('../../../bin/base');
service.registerPlugin({
id: '@micro-app/plugin-webpack-adapter',
});
service.registerPlugin({
id: 'test:inspect',
link: path.join(__dirname, './index.js'),
});

service.run('inspect', { _: [], verbose: true });
});

it('inspect-path', () => {
const { service } = require('../../../bin/base');
service.registerPlugin({
id: '@micro-app/plugin-webpack-adapter',
});
service.registerPlugin({
id: 'test:inspect',
link: path.join(__dirname, './index.js'),
});

service.run('inspect', { _: [ 'entry.main', 'resolve.alias' ], verbose: true });
});

});
2 changes: 1 addition & 1 deletion plugins/commands/serve/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ module.exports = function devCommand(api, opts) {
description: 'runs server for development',
usage: 'micro-app serve [options]',
options: {
'-': 'default.',
'--mode': 'specify env mode (default: development)',
'--type <type>': 'adapter type, eg. [ webpack, vusion, etc. ].',
'--host <host>': 'node server host.',
'--port <port>': 'node server port.',
Expand Down
2 changes: 1 addition & 1 deletion plugins/commands/start/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module.exports = function startCommand(api, opts) {
description: 'runs server for production',
usage: 'micro-app start [options]',
options: {
'-': 'default.',
'--mode': 'specify env mode (default: development)',
'--type <type>': 'adapter type, eg. [ webpack, vusion, etc. ].',
'--host <host>': 'node server host.',
'--port <port>': 'node server port.',
Expand Down
14 changes: 8 additions & 6 deletions src/server/createServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module.exports = function(api, args = {}, devCb = false) {
const serverConfig = api.serverConfig;

const app = new Koa();
app._name = 'KOA2'; // 设置名称, 给后面插件判断使用

// 兼容koa1的中间件
const _use = app.use;
app.use = x => _use.call(app, koaConvert(x));
Expand All @@ -35,22 +37,22 @@ module.exports = function(api, args = {}, devCb = false) {
logger.error('koa server error: ', error);
});

api.applyPluginHooks('onServerInit', { app, config: serverConfig, args });
api.applyPluginHooks('onServerInit', { app, args });
applyHooks(_HookEvent, 'init');

api.applyPluginHooks('beforeServerEntry', { app, config: serverConfig, args });
api.applyPluginHooks('beforeServerEntry', { app, args });
applyHooks(_HookEvent, 'before');

initEntrys(app, serverConfig, logger);

api.applyPluginHooks('afterServerEntry', { app, config: serverConfig, args });
api.applyPluginHooks('afterServerEntry', { app, args });
applyHooks(_HookEvent, 'after');

if (isDev && devCb && _.isFunction(devCb)) {
devCb(app, serverConfig, args);
}

api.applyPluginHooks('onServerInitDone', { app, config: serverConfig, args });
api.applyPluginHooks('onServerInitDone', { app, args });
applyHooks(_HookEvent, 'done');

const port = args.port || serverConfig.port || 8888;
Expand All @@ -59,14 +61,14 @@ module.exports = function(api, args = {}, devCb = false) {
app.listen(port, host === 'localhost' ? '0.0.0.0' : host, err => {
if (err) {
logger.error(err);
api.applyPluginHooks('onServerRunFail', { host, port, config: serverConfig, err, args });
api.applyPluginHooks('onServerRunFail', { host, port, err, args });
reject(err);
return;
}
console.log('\n');
logger.success(`Server running... listen on ${port}, host: ${host}`);

api.applyPluginHooks('onServerRunSuccess', { host, port, config: serverConfig, args });
api.applyPluginHooks('onServerRunSuccess', { host, port, args });

const url = `http://${host}:${port}`;
resolve({ host, port, url });
Expand Down

0 comments on commit 455ffc9

Please sign in to comment.