-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
:enhance: add --mode and add inspect command.
- Loading branch information
Showing
13 changed files
with
303 additions
and
2,930 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.`); | ||
} | ||
} | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
}); | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.