Skip to content

Commit 665aae9

Browse files
authored
fix(plugin-html): fix template with process (web-infra-dev#2226)
* fix(plugin-html): fix template with process * test: add test
1 parent 791b30b commit 665aae9

File tree

4 files changed

+80
-16
lines changed

4 files changed

+80
-16
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@rspack/plugin-html": patch
3+
---
4+
5+
fix(plugin-html): fix template with process

packages/rspack-plugin-html/src/template.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export async function evaluate(
5050
}
5151
const vmContext = vm.createContext({
5252
...global,
53+
process,
5354
HTML_WEBPACK_PLUGIN: true,
5455
require: require,
5556
htmlWebpackPluginPublicPath: publicPath,

packages/rspack-plugin-html/tests/basic.spec.js

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@ function testHtmlPlugin(
5353
expectWarnings
5454
) {
5555
outputFile = outputFile || "index.html";
56+
const envSetup = () => {
57+
let prevEnv = process.env.NODE_ENV;
58+
process.env.NODE_ENV = webpackConfig.mode;
59+
return () => {
60+
process.env.NODE_ENV = prevEnv;
61+
};
62+
};
63+
64+
let restoreEnv = envSetup();
65+
let prevDone = done;
66+
done = (...args) => {
67+
restoreEnv();
68+
prevDone(...args);
69+
};
70+
5671
rspack({ ...webpackConfig, builtins: { minify: false } }, (err, stats) => {
5772
expect(err).toBeFalsy();
5873
const statsJson = stats.toJson({ all: true });
@@ -92,26 +107,31 @@ function testHtmlPlugin(
92107
let chunksInfo;
93108
for (let i = 0; i < expectedResults.length; i++) {
94109
const expectedResult = expectedResults[i];
95-
if (expectedResult instanceof RegExp) {
96-
expect(htmlContent).toMatch(expectedResult);
97-
} else if (typeof expectedResult === "object") {
98-
if (expectedResult.type === "chunkhash") {
99-
if (!chunksInfo) {
100-
chunksInfo = getChunksInfoFromStats(stats);
110+
try {
111+
if (expectedResult instanceof RegExp) {
112+
expect(htmlContent).toMatch(expectedResult);
113+
} else if (typeof expectedResult === "object") {
114+
if (expectedResult.type === "chunkhash") {
115+
if (!chunksInfo) {
116+
chunksInfo = getChunksInfoFromStats(stats);
117+
}
118+
// TODO: stats.hash
119+
// const chunkhash = chunksInfo[expectedResult.chunkName].hash;
120+
expect(htmlContent).toContain(
121+
// expectedResult.containStr.replace("%chunkhash%", chunkhash)
122+
expectedResult
123+
);
101124
}
102-
// TODO: stats.hash
103-
// const chunkhash = chunksInfo[expectedResult.chunkName].hash;
125+
} else {
104126
expect(htmlContent).toContain(
105-
// expectedResult.containStr.replace("%chunkhash%", chunkhash)
127+
// TODO: stats.hash
128+
// expectedResult.replace("%hash%", stats.hash)
106129
expectedResult
107130
);
108131
}
109-
} else {
110-
expect(htmlContent).toContain(
111-
// TODO: stats.hash
112-
// expectedResult.replace("%hash%", stats.hash)
113-
expectedResult
114-
);
132+
} catch (e) {
133+
done(e);
134+
return;
115135
}
116136
}
117137
done();
@@ -132,7 +152,7 @@ function getChunksInfoFromStats(stats) {
132152
}
133153

134154
describe("HtmlWebpackPlugin", () => {
135-
jest.setTimeout(process.env.CI ? 120000 : 30000);
155+
jest.setTimeout(process.env.CI ? 120000 : 10000);
136156

137157
beforeEach(done => {
138158
rimraf(OUTPUT_DIR, done);
@@ -624,6 +644,28 @@ describe("HtmlWebpackPlugin", () => {
624644
);
625645
});
626646

647+
it("works with process (issue#2179)", done => {
648+
testHtmlPlugin(
649+
{
650+
mode: "development",
651+
devtool: "source-map",
652+
entry: path.join(__dirname, "fixtures/index.js"),
653+
output: {
654+
path: OUTPUT_DIR,
655+
filename: "[name]_bundle.js"
656+
},
657+
plugins: [
658+
new HtmlWebpackPlugin({
659+
template: path.join(__dirname, "fixtures/issue2179.html")
660+
})
661+
]
662+
},
663+
["console.log(1)"],
664+
null,
665+
done
666+
);
667+
});
668+
627669
it.skip("handles hashes in bundle filenames", done => {
628670
testHtmlPlugin(
629671
{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Rspack + React</title>
7+
<% if (process.env.NODE_ENV === 'development') { %>
8+
<script>
9+
console.log(1);
10+
</script>
11+
<% } %>
12+
</head>
13+
<body>
14+
<div id="root"></div>
15+
</body>
16+
</html>

0 commit comments

Comments
 (0)