diff --git a/lib/nav.js b/lib/nav.js index 92d952d..6d373e0 100644 --- a/lib/nav.js +++ b/lib/nav.js @@ -11,7 +11,7 @@ class NavigationPage { getName() { const modules = this.stat.compilation.modules; - const target = this.compiler.options.target; + const target = utils.getTarget(this.compiler.options.target); if (target === 'node') { return 'Server'; } @@ -44,7 +44,7 @@ class NavigationPage { resolve() { const webpackConfig = this.compiler.options; const easyInfo = easyHelper.getEasyInfo(); - const target = webpackConfig.target || 'web'; + const target = utils.getTarget(webpackConfig.target) || 'web'; const port = easyInfo[target].port || this.config.port - 1; const publicPath = this.normalizePublicPath(webpackConfig.output.publicPath, port); const assets = Object.keys(this.stat.compilation.assets).sort().map(name => { diff --git a/lib/tool.js b/lib/tool.js index b05be73..31d40b7 100644 --- a/lib/tool.js +++ b/lib/tool.js @@ -66,7 +66,7 @@ class WebpackTool { } normalizeHotEntry(webpackConfig) { - const target = webpackConfig.target; + const target = utils.getTarget(webpackConfig.target); if (target === 'web') { const port = this.getPort(target); utils.normalizeHotEntry(webpackConfig, port); @@ -218,7 +218,7 @@ class WebpackTool { createWebpackServer(compiler, options = {}) { const offset = options.offset; const webpackConfig = compiler.options; - const target = webpackConfig.target; + const target = utils.getTarget(webpackConfig.target); const output = webpackConfig.output; const { devServer = {} } = this.config; const { before, after, historyApiFallback, proxy = {} } = devServer; diff --git a/lib/utils.js b/lib/utils.js index 5415a87..dbeafd8 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -113,4 +113,23 @@ utils.getWebpack = () => { return require('webpack'); }; +/** + * Why: https://webpack.js.org/configuration/target/ + * + * @param {string|string[]|false} target + */ +utils.getTarget = (target) => { + if (typeof target === 'string') return target; + if (Array.isArray(target)) { + if (target.includes('web')) { + return 'web'; + } + if (target.includes('node')) { + return 'node'; + } + } + + return target; +} + module.exports = utils; \ No newline at end of file