Skip to content

Commit 4987907

Browse files
committed
Chore: Updating html-webpack-plugin to v4 (#1608)
1 parent 6385ec1 commit 4987907

File tree

11 files changed

+302
-885
lines changed

11 files changed

+302
-885
lines changed

.changeset/rude-walls-dress.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'preact-cli': minor
3+
'@preact/prerender-data-provider': patch
4+
---
5+
6+
Updates to use html-webpack-plugin v4

packages/cli/lib/lib/webpack/render-html-plugin.js

Lines changed: 62 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const { resolve, join } = require('path');
22
const os = require('os');
33
const { existsSync, readFileSync, writeFileSync, mkdirSync } = require('fs');
4-
const HtmlWebpackExcludeAssetsPlugin = require('html-webpack-exclude-assets-plugin');
4+
const {
5+
HtmlWebpackSkipAssetsPlugin,
6+
} = require('html-webpack-skip-assets-plugin');
57
const HtmlWebpackPlugin = require('html-webpack-plugin');
68
const prerender = require('./prerender');
79
const createLoadManifest = require('./create-load-manifest');
@@ -15,8 +17,8 @@ function read(path) {
1517
return readFileSync(resolve(__dirname, path), 'utf-8');
1618
}
1719

18-
module.exports = async function (config) {
19-
const { cwd, dest, isProd, src } = config;
20+
module.exports = async function renderHTMLPlugin(config) {
21+
const { cwd, dest, src } = config;
2022
const inProjectTemplatePath = resolve(src, 'template.html');
2123
let template = defaultTemplate;
2224
if (existsSync(inProjectTemplatePath)) {
@@ -32,14 +34,11 @@ module.exports = async function (config) {
3234
}
3335

3436
let content = read(template);
35-
if (/preact\.headEnd|preact\.bodyEnd/.test(content)) {
37+
if (/preact\.(title|headEnd|bodyEnd)/.test(content)) {
3638
const headEnd = read('../../resources/head-end.ejs');
3739
const bodyEnd = read('../../resources/body-end.ejs');
3840
content = content
39-
.replace(
40-
/<%[=]?\s+preact\.title\s+%>/,
41-
'<%= htmlWebpackPlugin.options.title %>'
42-
)
41+
.replace(/<%[=]?\s+preact\.title\s+%>/, '<%= cli.title %>')
4342
.replace(/<%\s+preact\.headEnd\s+%>/, headEnd)
4443
.replace(/<%\s+preact\.bodyEnd\s+%>/, bodyEnd);
4544

@@ -54,53 +53,69 @@ module.exports = async function (config) {
5453
}
5554

5655
const htmlWebpackConfig = values => {
57-
const { url, title, ...routeData } = values;
56+
let { url, title, ...routeData } = values;
57+
58+
title =
59+
title ||
60+
config.title ||
61+
config.manifest.name ||
62+
config.manifest.short_name ||
63+
(config.pkg.name || '').replace(/^@[a-z]\//, '') ||
64+
'Preact App';
65+
5866
// Do not create a folder if the url is for a specific file.
5967
const filename = url.endsWith('.html')
6068
? resolve(dest, url.substring(1))
6169
: resolve(dest, url.substring(1), 'index.html');
62-
return Object.assign(values, {
70+
71+
return {
72+
title,
6373
filename,
6474
template: `!!${require.resolve('ejs-loader')}?esModule=false!${template}`,
65-
minify: isProd && {
66-
collapseWhitespace: true,
67-
removeScriptTypeAttributes: true,
68-
removeRedundantAttributes: true,
69-
removeStyleLinkTypeAttributes: true,
70-
removeComments: true,
75+
templateParameters: (compilation, assets, assetTags, options) => {
76+
let entrypoints = {};
77+
compilation.entrypoints.forEach((entrypoint, name) => {
78+
let entryFiles = entrypoint.getFiles();
79+
entrypoints[name] =
80+
assets.publicPath +
81+
entryFiles.find(file => /\.(m?js)(\?|$)/.test(file));
82+
});
83+
84+
let loadManifest = compilation.assets['push-manifest.json']
85+
? JSON.parse(compilation.assets['push-manifest.json'].source())
86+
: createLoadManifest(
87+
compilation.assets,
88+
compilation.namedChunkGroups
89+
);
90+
91+
return {
92+
cli: {
93+
title,
94+
url,
95+
manifest: config.manifest,
96+
inlineCss: config['inline-css'],
97+
preload: config.preload,
98+
config,
99+
preRenderData: values,
100+
CLI_DATA: { preRenderData: { url, ...routeData } },
101+
ssr: config.prerender ? prerender({ cwd, dest, src }, values) : '',
102+
loadManifest,
103+
entrypoints,
104+
},
105+
htmlWebpackPlugin: {
106+
tags: assetTags,
107+
files: assets,
108+
options,
109+
},
110+
};
71111
},
112+
inject: true,
113+
scriptLoading: 'defer',
72114
favicon: existsSync(resolve(src, 'assets/favicon.ico'))
73115
? 'assets/favicon.ico'
74116
: '',
75-
inject: true,
76-
compile: true,
77-
inlineCss: config['inline-css'],
78-
preload: config.preload,
79-
manifest: config.manifest,
80-
title:
81-
title ||
82-
config.title ||
83-
config.manifest.name ||
84-
config.manifest.short_name ||
85-
(config.pkg.name || '').replace(/^@[a-z]\//, '') ||
86-
'Preact App',
87117
excludeAssets: [/(bundle|polyfills)(\..*)?\.js$/],
88-
createLoadManifest: (assets, namedChunkGroups) => {
89-
if (assets['push-manifest.json']) {
90-
return JSON.parse(assets['push-manifest.json'].source());
91-
}
92-
return createLoadManifest(assets, namedChunkGroups);
93-
},
94-
config,
95-
url,
96-
ssr() {
97-
return config.prerender && url !== PREACT_FALLBACK_URL
98-
? prerender({ cwd, dest, src }, values)
99-
: '';
100-
},
101-
scriptLoading: 'defer',
102-
CLI_DATA: { preRenderData: { url, ...routeData } },
103-
});
118+
};
104119
};
105120

106121
let pages = [{ url: '/' }];
@@ -151,7 +166,7 @@ module.exports = async function (config) {
151166
const resultPages = pages
152167
.map(htmlWebpackConfig)
153168
.map(conf => new HtmlWebpackPlugin(conf))
154-
.concat([new HtmlWebpackExcludeAssetsPlugin()]);
169+
.concat([new HtmlWebpackSkipAssetsPlugin()]);
155170

156171
return config.prerender
157172
? resultPages.concat([
@@ -163,10 +178,9 @@ module.exports = async function (config) {
163178
// Adds a preact_prerender_data in every folder so that the data could be fetched separately.
164179
class PrerenderDataExtractPlugin {
165180
constructor(page) {
166-
const cliData = page.CLI_DATA || {};
167-
const { url } = cliData.preRenderData || {};
181+
const url = page.url;
168182
this.location_ = url.endsWith('/') ? url : url + '/';
169-
this.data_ = JSON.stringify(cliData.preRenderData || {});
183+
this.data_ = JSON.stringify(page || {});
170184
}
171185
apply(compiler) {
172186
compiler.hooks.emit.tap('PrerenderDataExtractPlugin', compilation => {

packages/cli/lib/lib/webpack/webpack-client-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ function isProd(env) {
256256
preload: 'media',
257257
pruneSource: false,
258258
logLevel: 'silent',
259-
additionalStylesheets: ['*.css'],
259+
additionalStylesheets: ['route-*.css'],
260260
})
261261
);
262262
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
<%= htmlWebpackPlugin.options.ssr() %>
2-
<% if (htmlWebpackPlugin.options.config.prerender === true) { %>
1+
<%= cli.ssr %>
2+
<% if (cli.config.prerender === true) { %>
33
<script type="__PREACT_CLI_DATA__">
4-
<%= encodeURI(JSON.stringify(htmlWebpackPlugin.options.CLI_DATA)) %>
4+
<%= encodeURI(JSON.stringify(cli.CLI_DATA)) %>
55
</script>
66
<% } %>
7-
<% if (webpack.assets.filter(entry => entry.name.match(/bundle(\.\w{5})?.esm.js$/)).length > 0) { %>
8-
<script crossorigin="anonymous" src="<%= htmlWebpackPlugin.files.publicPath %><%= webpack.assets.filter(entry => entry.name.match(/bundle(\.\w{5})?.esm.js$/))[0].name %>" type="module"></script>
7+
<% if (htmlWebpackPlugin.files.js.filter(entry => entry.match(/bundle(\.\w{5})?.esm.js$/)).length > 0) { %>
8+
<script crossorigin="anonymous" src="<%= htmlWebpackPlugin.files.js.filter(entry => entry.match(/bundle(\.\w{5})?.esm.js$/))[0] %>" type="module"></script>
99
<%
1010
/*Fetch and Promise polyfills are not needed for browsers that support type=module
1111
Please re-evaluate below line if adding more polyfills.*/
1212
%>
13-
<script nomodule src="<%= htmlWebpackPlugin.files.chunks["polyfills"].entry %>"></script>
14-
<script nomodule defer src="<%= htmlWebpackPlugin.files.chunks['bundle'].entry %>"></script>
13+
<script nomodule src="<%= cli.entrypoints['polyfills'] %>"></script>
14+
<script nomodule defer src="<%= cli.entrypoints['bundle'] %>"></script>
1515
<% } else { %>
16-
<script <%= htmlWebpackPlugin.options.scriptLoading %> src="<%= htmlWebpackPlugin.files.chunks['bundle'].entry %>"></script>
17-
<script nomodule src="<%= htmlWebpackPlugin.files.chunks["polyfills"].entry %>"></script>
16+
<script <%= htmlWebpackPlugin.options.scriptLoading %> src="<%= cli.entrypoints['bundle'] %>"></script>
17+
<script nomodule src="<%= cli.entrypoints['polyfills'] %>"></script>
1818
<% } %>

packages/cli/lib/resources/head-end.ejs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
<link rel="manifest" href="<%= htmlWebpackPlugin.files.publicPath %>manifest.json">
2-
<% if (htmlWebpackPlugin.options.manifest.theme_color) { %>
3-
<meta name="theme-color" content="<%= htmlWebpackPlugin.options.manifest.theme_color %>">
2+
<% if (cli.manifest.theme_color) { %>
3+
<meta name="theme-color" content="<%= cli.manifest.theme_color %>">
44
<% } %>
5-
<% const loadManifest = htmlWebpackPlugin.options.createLoadManifest(compilation.assets, webpack.namedChunkGroups);%>
6-
<% const filesRegexp = htmlWebpackPlugin.options.inlineCss ? /\.(chunk\.\w{5}\.css|js)$/ : /\.(css|js)$/;%>
7-
<% for (const file in loadManifest[htmlWebpackPlugin.options.url]) { %>
8-
<% if (htmlWebpackPlugin.options.preload && file && file.match(filesRegexp)) { %>
5+
<% const filesRegexp = cli.inlineCss ? /\.(chunk\.\w{5}\.css|js)$/ : /\.(css|js)$/;%>
6+
<% for (const file in cli.loadManifest[cli.url]) { %>
7+
<% if (cli.preload && file && file.match(filesRegexp)) { %>
98
<% /* crossorigin for main bundle as that is loaded from `<script type=module` tag, other lazy loaded bundles are from webpack so its not needed */ %>
109
<link rel="preload" href="<%= htmlWebpackPlugin.files.publicPath + file %>" as="<%= file.match(/\.css$/)?'style':'script' %>" <%= file.match(/bundle\.\w{5}\.esm\.js$/)?'crossorigin="anonymous"':'' %>>
1110
<% } %>

packages/cli/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
"compression-webpack-plugin": "^6.1.1",
9595
"console-clear": "^1.0.0",
9696
"copy-webpack-plugin": "^6.4.0",
97-
"critters-webpack-plugin": "^2.5.0",
97+
"critters-webpack-plugin": "^3.0.2",
9898
"cross-spawn-promise": "^0.10.1",
9999
"css-loader": "^5.2.7",
100100
"dotenv": "^16.0.0",
@@ -106,8 +106,8 @@
106106
"get-port": "^5.0.0",
107107
"gittar": "^0.1.0",
108108
"glob": "^8.0.3",
109-
"html-webpack-exclude-assets-plugin": "0.0.7",
110-
"html-webpack-plugin": "^3.2.0",
109+
"html-webpack-plugin": "^4.5.2",
110+
"html-webpack-skip-assets-plugin": "1.0.3",
111111
"ip": "^1.1.8",
112112
"isomorphic-unfetch": "^3.1.0",
113113
"kleur": "^4.1.4",

packages/cli/tests/images/build.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ exports.prerender.heads.home = `
6969
<link rel="apple-touch-icon" href=\\"\\/assets\\/icons\\/apple-touch-icon\\.png\\">
7070
<link rel="manifest" href="\\/manifest\\.json">
7171
<style>html{padding:0}<\\/style>
72-
<link href=\\"/bundle.\\w{5}.css\\" rel=\\"stylesheet\\" media=\\"only x\\" onload=\\"this.media='all'\\">
72+
<link href=\\"/bundle.\\w{5}.css\\" rel=\\"stylesheet\\" media=\\"print\\" onload=\\"this.media='all'\\">
7373
<noscript>
7474
<link rel=\\"stylesheet\\" href=\\"\\/bundle.\\w{5}.css\\">
7575
</noscript>
@@ -86,7 +86,7 @@ exports.prerender.heads.route66 = `
8686
<link rel="apple-touch-icon" href=\\"\\/assets\\/icons\\/apple-touch-icon\\.png\\">
8787
<link rel="manifest" href="\\/manifest\\.json">
8888
<style>html{padding:0}<\\/style>
89-
<link href=\\"/bundle.\\w{5}.css\\" rel=\\"stylesheet\\" media=\\"only x\\" onload=\\"this.media='all'\\">
89+
<link href=\\"/bundle.\\w{5}.css\\" rel=\\"stylesheet\\" media=\\"print\\" onload=\\"this.media='all'\\">
9090
<noscript>
9191
<link rel=\\"stylesheet\\" href=\\"\\/bundle.\\w{5}.css\\">
9292
</noscript>
@@ -103,7 +103,7 @@ exports.prerender.heads.custom = `
103103
<link rel="apple-touch-icon" href=\\"\\/assets\\/icons\\/apple-touch-icon\\.png\\">
104104
<link rel="manifest" href="\\/manifest\\.json">
105105
<style>html{padding:0}<\\/style>
106-
<link href=\\"/bundle.\\w{5}.css\\" rel=\\"stylesheet\\" media=\\"only x\\" onload=\\"this.media='all'\\">
106+
<link href=\\"/bundle.\\w{5}.css\\" rel=\\"stylesheet\\" media=\\"print\\" onload=\\"this.media='all'\\">
107107
<noscript>
108108
<link rel=\\"stylesheet\\" href=\\"\\/bundle.\\w{5}.css\\">
109109
</noscript>
@@ -125,7 +125,7 @@ exports.preload.true = `
125125
<link rel=\\"preload\\" href=\\"\\/route-home\\.chunk\\.\\w{5}\\.js\\" as=\\"script\\">
126126
<link rel=\\"preload\\" href=\\"\\/route-home\\.chunk\\.\\w{5}\\.css\\" as=\\"style\\">
127127
<style>html{padding:0}<\\/style>
128-
<link href=\\"\\/bundle\\.\\w{5}\\.css\\" rel=\\"stylesheet\\" media=\\"only x\\" onload=\\"this.media='all'\\">
128+
<link href=\\"\\/bundle\\.\\w{5}\\.css\\" rel=\\"stylesheet\\" media=\\"print\\" onload=\\"this.media='all'\\">
129129
<noscript>
130130
<link rel=\\"stylesheet\\" href=\\"\\/bundle.\\w{5}.css\\">
131131
</noscript>
@@ -142,7 +142,7 @@ exports.preload.false = `
142142
<link rel=\\"apple-touch-icon\\" href=\\"\\/assets\\/icons\\/apple-touch-icon\\.png\\">
143143
<link rel=\\"manifest\\" href=\\"\\/manifest\\.json\\">
144144
<style>html{padding:0}<\\/style>
145-
<link href=\\"\\/bundle\\.\\w{5}\\.css\\" rel=\\"stylesheet\\" media=\\"only x\\" onload=\\"this.media='all'\\">
145+
<link href=\\"\\/bundle\\.\\w{5}\\.css\\" rel=\\"stylesheet\\" media=\\"print\\" onload=\\"this.media='all'\\">
146146
<noscript>
147147
<link rel=\\"stylesheet\\" href=\\"\\/bundle.\\w{5}.css\\">
148148
</noscript>
@@ -320,7 +320,6 @@ exports.publicPath = `
320320
<link rel="apple-touch-icon" href="/example-path/assets/icons/apple-touch-icon.png">
321321
<link rel="manifest" href="/example-path/manifest.json">
322322
<link href="/example-path/bundle.\\w{5}.css" rel="stylesheet">
323-
<style></style>
324323
</head>
325324
<body>
326325
<h1>Public path test</h1>

packages/cli/tests/subjects/custom-template/template.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,15 @@
33
<head>
44
<meta charset="utf-8">
55
<title><% preact.title %></title>
6-
<% if (htmlWebpackPlugin.options.config.isProd) { %>
6+
<% if (cli.config.isProd) { %>
77
<meta name="example-meta" content="Hello World">
88
<% } %>
99
<% preact.headEnd %>
1010
</head>
1111
<body>
1212
<h1>Guess what</h1>
13-
<%= htmlWebpackPlugin.options.ssr({
14-
url: '/'
15-
}) %>
16-
<script defer src="<%= htmlWebpackPlugin.files.chunks['bundle'].entry %>"></script>
13+
<%= cli.ssr %>
14+
<script defer src="<%= cli.entrypoints['bundle'] %>"></script>
1715
<% preact.bodyEnd %>
1816
</body>
1917
</html>

packages/prerender-data-provider/src/hook.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ function usePrerenderData(props, doAutomaticFetch = true) {
3838
}
3939
}
4040

41-
if (contextValue.CLI_DATA && contextValue.CLI_DATA.preRenderData) {
42-
value = contextValue.CLI_DATA.preRenderData;
41+
if (contextValue) {
42+
value = contextValue;
4343
}
4444

4545
const data = getPrerenderdata(state.value || value, props);

packages/prerender-data-provider/src/render-prop.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ class PreRenderDataSource extends Component {
4949
{contextValue => {
5050
let obtainedContextValue;
5151
// If the data is in script tag, it will be accesible from the following chaining
52-
if (contextValue.CLI_DATA && contextValue.CLI_DATA.preRenderData) {
53-
obtainedContextValue = contextValue.CLI_DATA.preRenderData;
52+
if (contextValue) {
53+
obtainedContextValue = contextValue;
5454
}
5555
const preRenderDataToBePassed = getPrerenderdata(
5656
value || obtainedContextValue,

0 commit comments

Comments
 (0)