Skip to content

Commit c1ccbbc

Browse files
authored
Upgrade html-webpack-plugin to fix tests (#5031)
* Upgrade html webpack plugin * Fix build
1 parent d555256 commit c1ccbbc

File tree

5 files changed

+43
-45
lines changed

5 files changed

+43
-45
lines changed

packages/react-dev-utils/InterpolateHtmlPlugin.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
// This Webpack plugin lets us interpolate custom variables into `index.html`.
9-
// Usage: `new InterpolateHtmlPlugin({ 'MY_VARIABLE': 42 })`
9+
// Usage: `new InterpolateHtmlPlugin(HtmlWebpackPlugin, { 'MY_VARIABLE': 42 })`
1010
// Then, you can use %MY_VARIABLE% in your `index.html`.
1111

1212
// It works in tandem with HtmlWebpackPlugin.
@@ -17,15 +17,16 @@
1717
const escapeStringRegexp = require('escape-string-regexp');
1818

1919
class InterpolateHtmlPlugin {
20-
constructor(replacements) {
20+
constructor(htmlWebpackPlugin, replacements) {
21+
this.htmlWebpackPlugin = htmlWebpackPlugin;
2122
this.replacements = replacements;
2223
}
2324

2425
apply(compiler) {
2526
compiler.hooks.compilation.tap('InterpolateHtmlPlugin', compilation => {
26-
compilation.hooks.htmlWebpackPluginBeforeHtmlProcessing.tap(
27-
'InterpolateHtmlPlugin',
28-
data => {
27+
this.htmlWebpackPlugin
28+
.getHooks(compilation)
29+
.beforeEmit.tap('InterpolateHtmlPlugin', data => {
2930
// Run HTML through a series of user-specified string replacements.
3031
Object.keys(this.replacements).forEach(key => {
3132
const value = this.replacements[key];
@@ -34,8 +35,7 @@ class InterpolateHtmlPlugin {
3435
value
3536
);
3637
});
37-
}
38-
);
38+
});
3939
});
4040
}
4141
}

packages/react-dev-utils/README.md

+33-35
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var publicUrl = '/my-custom-url';
3434
module.exports = {
3535
output: {
3636
// ...
37-
publicPath: publicUrl + '/'
37+
publicPath: publicUrl + '/',
3838
},
3939
// ...
4040
plugins: [
@@ -45,18 +45,17 @@ module.exports = {
4545
}),
4646
// Makes the public URL available as %PUBLIC_URL% in index.html, e.g.:
4747
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
48-
new InterpolateHtmlPlugin({
49-
PUBLIC_URL: publicUrl
48+
new InterpolateHtmlPlugin(HtmlWebpackPlugin, {
49+
PUBLIC_URL: publicUrl,
5050
// You can pass any key-value pairs, this was just an example.
5151
// WHATEVER: 42 will replace %WHATEVER% with 42 in index.html.
5252
}),
5353
// ...
5454
],
5555
// ...
56-
}
56+
};
5757
```
5858

59-
6059
#### `new ModuleScopePlugin(appSrc: string | string[], allowedFiles?: string[])`
6160

6261
This Webpack plugin ensures that relative imports from app's source directories don't reach outside of it.
@@ -65,7 +64,6 @@ This Webpack plugin ensures that relative imports from app's source directories
6564
var path = require('path');
6665
var ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
6766

68-
6967
module.exports = {
7068
// ...
7169
resolve: {
@@ -77,7 +75,7 @@ module.exports = {
7775
// ...
7876
},
7977
// ...
80-
}
78+
};
8179
```
8280

8381
#### `new WatchMissingNodeModulesPlugin(nodeModulesPath: string)`
@@ -99,10 +97,10 @@ module.exports = {
9997
// to restart the development server for Webpack to discover it. This plugin
10098
// makes the discovery automatic so you don't have to restart.
10199
// See https://github.com/facebook/create-react-app/issues/186
102-
new WatchMissingNodeModulesPlugin(path.resolve('node_modules'))
100+
new WatchMissingNodeModulesPlugin(path.resolve('node_modules')),
103101
],
104102
// ...
105-
}
103+
};
106104
```
107105

108106
#### `checkRequiredFiles(files: Array<string>): boolean`
@@ -115,10 +113,12 @@ If a file is not found, prints a warning message and returns `false`.
115113
var path = require('path');
116114
var checkRequiredFiles = require('react-dev-utils/checkRequiredFiles');
117115

118-
if (!checkRequiredFiles([
119-
path.resolve('public/index.html'),
120-
path.resolve('src/index.js')
121-
])) {
116+
if (
117+
!checkRequiredFiles([
118+
path.resolve('public/index.html'),
119+
path.resolve('src/index.js'),
120+
])
121+
) {
122122
process.exit(1);
123123
}
124124
```
@@ -145,22 +145,22 @@ const eslintFormatter = require('react-dev-utils/eslintFormatter');
145145
// In your webpack config:
146146
// ...
147147
module: {
148-
rules: [
149-
{
150-
test: /\.(js|jsx)$/,
151-
include: paths.appSrc,
152-
enforce: 'pre',
153-
use: [
154-
{
155-
loader: 'eslint-loader',
156-
options: {
157-
// Pass the formatter:
158-
formatter: eslintFormatter,
159-
},
148+
rules: [
149+
{
150+
test: /\.(js|jsx)$/,
151+
include: paths.appSrc,
152+
enforce: 'pre',
153+
use: [
154+
{
155+
loader: 'eslint-loader',
156+
options: {
157+
// Pass the formatter:
158+
formatter: eslintFormatter,
160159
},
161-
],
162-
}
163-
]
160+
},
161+
],
162+
},
163+
];
164164
}
165165
```
166166

@@ -264,7 +264,6 @@ Attempts to open the browser with a given URL.<br>
264264
On Mac OS X, attempts to reuse an existing Chrome tab via AppleScript.<br>
265265
Otherwise, falls back to [opn](https://github.com/sindresorhus/opn) behavior.
266266

267-
268267
```js
269268
var path = require('path');
270269
var openBrowser = require('react-dev-utils/openBrowser');
@@ -321,10 +320,10 @@ module.exports = {
321320
// require.resolve('webpack-dev-server/client') + '?/',
322321
// require.resolve('webpack/hot/dev-server'),
323322
'react-dev-utils/webpackHotDevClient',
324-
'src/index'
323+
'src/index',
325324
],
326325
// ...
327-
}
326+
};
328327
```
329328

330329
#### `getCSSModuleLocalIdent(context: Object, localIdentName: String, localName: String, options: Object): string`
@@ -340,7 +339,7 @@ const getCSSModuleLocalIdent = require('react-dev-utils/getCSSModuleLocalIdent')
340339
// In your webpack config:
341340
// ...
342341
module: {
343-
rules: [
342+
rules: [
344343
{
345344
test: /\.module\.css$/,
346345
use: [
@@ -358,8 +357,7 @@ module: {
358357
options: postCSSLoaderOptions,
359358
},
360359
],
361-
}
362-
]
360+
},
361+
];
363362
}
364363
```
365-

packages/react-scripts/config/webpack.config.dev.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ module.exports = {
363363
// The public URL is available as %PUBLIC_URL% in index.html, e.g.:
364364
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
365365
// In development, this will be an empty string.
366-
new InterpolateHtmlPlugin(env.raw),
366+
new InterpolateHtmlPlugin(HtmlWebpackPlugin, env.raw),
367367
// Makes some environment variables available to the JS code, for example:
368368
// if (process.env.NODE_ENV === 'development') { ... }. See `./env.js`.
369369
new webpack.DefinePlugin(env.stringified),

packages/react-scripts/config/webpack.config.prod.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ module.exports = {
409409
// <link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico">
410410
// In production, it will be an empty string unless you specify "homepage"
411411
// in `package.json`, in which case it will be the pathname of that URL.
412-
new InterpolateHtmlPlugin(env.raw),
412+
new InterpolateHtmlPlugin(HtmlWebpackPlugin, env.raw),
413413
// Makes some environment variables available to the JS code, for example:
414414
// if (process.env.NODE_ENV === 'production') { ... }. See `./env.js`.
415415
// It is absolutely essential that NODE_ENV was set to production here.

packages/react-scripts/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
"fs-extra": "5.0.0",
4848
"graphql": "0.13.2",
4949
"graphql-tag": "2.9.2",
50-
"html-webpack-plugin": "3.2.0",
50+
"html-webpack-plugin": "4.0.0-alpha.2",
5151
"identity-obj-proxy": "3.0.0",
5252
"jest": "23.5.0",
5353
"loader-utils": "1.1.0",

0 commit comments

Comments
 (0)