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

Commit

Permalink
Tweak html files
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Nov 30, 2018
1 parent 7607b5b commit 81b9e0b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 17 deletions.
6 changes: 4 additions & 2 deletions core/poi/lib/index.js
Expand Up @@ -234,7 +234,8 @@ module.exports = class PoiCore {
port,
open,
proxy,
fileNames
fileNames,
html
} = this.cli.options
return {
entry: this.cli.args.length > 0 ? this.cli.args : undefined,
Expand All @@ -247,7 +248,8 @@ module.exports = class PoiCore {
publicUrl,
target,
clean,
fileNames
fileNames,
html
},
parallel,
cache,
Expand Down
1 change: 1 addition & 0 deletions core/poi/lib/plugins/command-options.js
Expand Up @@ -28,6 +28,7 @@ exports.apply = api => {
command.option('--module-name <name>', 'Module name for "umd" format')
command.option('--file-names <filenames>', 'Customize output filenames')
command.option('--no-clean', `Don't clean output directory before bundling`)
command.option('--html <options>', 'Configure generated HTML file')

if (api.isProd) {
command.option('--no-minimize', 'Disable minimization')
Expand Down
48 changes: 36 additions & 12 deletions core/poi/lib/plugins/config-html.js
Expand Up @@ -53,13 +53,12 @@ exports.apply = api => {

const defaultHtmlOpts = {
template: getDefaultTemplate(),
templateParameters: {
title: api.pkg.data.name || 'Poi App',
templateParameters: templateParametersGenerator({
pkg: api.pkg.data,
envs: api.webpackUtils.envs,
constants: api.webpackUtils.constants
},
pkg: api.pkg.data,
}),
filename: 'index.html',
minify: api.isProd
? {
removeComments: true,
Expand Down Expand Up @@ -89,14 +88,8 @@ exports.apply = api => {
page.template = api.resolveCwd(page.template)
config.plugin(`html-page-${entryName}`).use(HtmlPlugin, [page])
}
} else {
const page = merge(
defaultHtmlOpts,
{
filename: 'index.html'
},
api.config.html
)
} else if (api.config.html !== false) {
const page = merge(defaultHtmlOpts, api.config.html)
page.template = api.resolveCwd(page.template)
config.plugin('html').use(HtmlPlugin, [page])
}
Expand All @@ -108,3 +101,34 @@ exports.apply = api => {
])
})
}

function templateParametersGenerator(data) {
const { htmlTagObjectToString } = require('html-webpack-plugin/lib/html-tags')

return (compilation, assets, assetTags, options) => {
const { xhtml } = options
assetTags.headTags.toString = function() {
return this.map(assetTagObject =>
htmlTagObjectToString(assetTagObject, xhtml)
).join('')
}
assetTags.bodyTags.toString = function() {
return this.map(assetTagObject =>
htmlTagObjectToString(assetTagObject, xhtml)
).join('')
}
return Object.assign(
{
compilation,
webpackConfig: compilation.options,
htmlWebpackPlugin: {
tags: assetTags,
files: assets,
options
},
html: options
},
data
)
}
}
3 changes: 2 additions & 1 deletion core/poi/lib/utils/validateConfig.js
Expand Up @@ -28,7 +28,8 @@ module.exports = (api, config) => {
font: struct.optional('string'),
image: struct.optional('string')
})
)
),
html: struct.optional('object')
},
{
dir: 'dist',
Expand Down
4 changes: 2 additions & 2 deletions core/poi/lib/webpack/default-template.html
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title><%= title %></title>
<title><%= html.title %></title>

<% if (pkg.description) { %>
<meta name="description" content="<%= pkg.description %>">
Expand All @@ -13,7 +13,7 @@
</head>
<body>
<noscript>
We're sorry but <%= title %> doesn't work properly without JavaScript enabled. Please enable it to continue.
We're sorry but <%= html.title %> doesn't work properly without JavaScript enabled. Please enable it to continue.
</noscript>
<div id="app"></div>
</body>
Expand Down

0 comments on commit 81b9e0b

Please sign in to comment.