Skip to content
This repository has been archived by the owner on Dec 6, 2021. It is now read-only.

Commit

Permalink
feat: support pages option
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Dec 6, 2018
1 parent 2a47b8a commit 7551aaf
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 39 deletions.
2 changes: 1 addition & 1 deletion core/poi/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
50 changes: 28 additions & 22 deletions core/poi/lib/plugins/config-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/]
])
})
}

Expand Down
2 changes: 1 addition & 1 deletion core/poi/lib/utils/WebpackChain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions core/poi/lib/utils/validateConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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,
Expand Down
30 changes: 18 additions & 12 deletions core/poi/lib/webpack/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down

0 comments on commit 7551aaf

Please sign in to comment.