From 7551aaf72c45953ced5178a6a70c0b4b767c555c Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Thu, 6 Dec 2018 23:16:58 +0800 Subject: [PATCH] feat: support pages option --- core/poi/lib/index.js | 2 +- core/poi/lib/plugins/config-html.js | 50 ++++++++++++++------------ core/poi/lib/utils/WebpackChain.js | 2 +- core/poi/lib/utils/validateConfig.js | 6 ++-- core/poi/lib/webpack/webpack.config.js | 30 +++++++++------- 5 files changed, 51 insertions(+), 39 deletions(-) diff --git a/core/poi/lib/index.js b/core/poi/lib/index.js index 6ebc42b8..011a3162 100644 --- a/core/poi/lib/index.js +++ b/core/poi/lib/index.js @@ -229,7 +229,7 @@ module.exports = class PoiCore { const cliConfig = this.createConfigFromCLIOptions() logger.debug(`Config from CLI options`, cliConfig) - this.config = validateConfig(this, merge(this.config, cliConfig)) + this.config = validateConfig(this, merge({}, this.config, cliConfig)) await this.cli.runMatchedCommand() } diff --git a/core/poi/lib/plugins/config-html.js b/core/poi/lib/plugins/config-html.js index 77bab29a..250316b7 100644 --- a/core/poi/lib/plugins/config-html.js +++ b/core/poi/lib/plugins/config-html.js @@ -82,31 +82,37 @@ exports.apply = api => { : undefined } - const { pages, html } = api.config.output - if (pages) { - for (const entryName of Object.keys(pages)) { - const page = merge( - defaultHtmlOpts, - { - filename: `${entryName}.html`, - chunks: ['chunk-vendors', 'chunk-common', entryName] - }, - pages[entryName] - ) + const { pages } = api.config + const { html } = api.config.output + + if (html !== false) { + if (pages) { + for (const entryName of Object.keys(pages)) { + const page = merge( + {}, + defaultHtmlOpts, + { + filename: `${entryName}.html`, + chunks: ['chunk-vendors', 'chunk-common', entryName] + }, + typeof pages[entryName] === 'string' ? {} : pages[entryName] + ) + page.template = api.resolveCwd(page.template) + config.plugin(`html-page-${entryName}`).use(HtmlPlugin, [page]) + } + } else { + const page = merge({}, defaultHtmlOpts, html) page.template = api.resolveCwd(page.template) - config.plugin(`html-page-${entryName}`).use(HtmlPlugin, [page]) + config.plugin('html').use(HtmlPlugin, [page]) } - } else if (html !== false) { - const page = merge(defaultHtmlOpts, html) - page.template = api.resolveCwd(page.template) - config.plugin('html').use(HtmlPlugin, [page]) + + config + .plugin('inline-runtime-chunk') + .use(require('@poi/dev-utils/InlineChunkHtmlPlugin'), [ + require('html-webpack-plugin'), + [/runtime~.+[.]js/] + ]) } - config - .plugin('inline-runtime-chunk') - .use(require('@poi/dev-utils/InlineChunkHtmlPlugin'), [ - require('html-webpack-plugin'), - [/runtime~.+[.]js/] - ]) }) } diff --git a/core/poi/lib/utils/WebpackChain.js b/core/poi/lib/utils/WebpackChain.js index a55f427c..53d077f0 100644 --- a/core/poi/lib/utils/WebpackChain.js +++ b/core/poi/lib/utils/WebpackChain.js @@ -12,7 +12,7 @@ module.exports = class WebpackChain extends Chain { if (typeof this.configureWebpack === 'function') { config = this.configureWebpack(config) || config } else if (typeof this.configureWebpack === 'object') { - config = merge(config, this.configureWebpack) + config = merge({}, config, this.configureWebpack) } return config } diff --git a/core/poi/lib/utils/validateConfig.js b/core/poi/lib/utils/validateConfig.js index 91bb95e9..3f7843ad 100644 --- a/core/poi/lib/utils/validateConfig.js +++ b/core/poi/lib/utils/validateConfig.js @@ -28,8 +28,7 @@ module.exports = (api, config) => { image: struct.optional('string') }) ), - html: struct.optional('object'), - page: struct.optional('object') + html: struct.optional(struct.union(['boolean', 'object'])) }, { dir: 'dist', @@ -130,7 +129,8 @@ module.exports = (api, config) => { chainWebpack: struct.optional('function'), configureWebpack: struct.optional(struct.union(['object', 'function'])), assets, - publicFolder: struct.union(['string', 'boolean']) + publicFolder: struct.union(['string', 'boolean']), + pages: struct.optional('object') }, { cache: true, diff --git a/core/poi/lib/webpack/webpack.config.js b/core/poi/lib/webpack/webpack.config.js index ffa7570c..945958e1 100644 --- a/core/poi/lib/webpack/webpack.config.js +++ b/core/poi/lib/webpack/webpack.config.js @@ -17,24 +17,30 @@ const normalizeEntry = v => { module.exports = (config, api) => { /** Set entry */ - let { entry } = api.config - if (typeof entry === 'string') { - entry = { - index: [entry] + + const webpackEntry = {} + const { entry, pages } = api.config + if (pages) { + for (const entryName of Object.keys(pages)) { + const value = pages[entryName] + webpackEntry[entryName] = [].concat( + typeof value === 'string' ? value : value.entry + ) } + api.logger.debug('Using `pages` option thus `entry` is ignored') + } else if (typeof entry === 'string') { + webpackEntry.index = [entry] } else if (Array.isArray(entry)) { - entry = { - index: entry - } - } else { - entry = Object.assign({}, entry) + webpackEntry.index = entry + } else if (typeof entry === 'object') { + Object.assign(webpackEntry, entry) } - for (const name of Object.keys(entry)) { - entry[name] = entry[name].map(v => normalizeEntry(v)) + for (const name of Object.keys(webpackEntry)) { + webpackEntry[name] = webpackEntry[name].map(v => normalizeEntry(v)) } - config.merge({ entry }) + config.merge({ entry: webpackEntry }) /** Set extensions */ config.resolve.extensions.merge(['.js', '.json', '.jsx', '.ts', '.tsx'])