Skip to content

Commit a89ae76

Browse files
committed
fix: error when using function in webpack output.publicPath
1 parent e8c08a1 commit a89ae76

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

Diff for: src/loader.js

+5
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ export function pitch(request) {
188188

189189
if (publicPath === "auto") {
190190
publicPath = AUTO_PUBLIC_PATH;
191+
} else if (typeof publicPath === "function") {
192+
// `hash` property is not available in this context, Webpack itself just set `hash` with
193+
// an arbitrary value.
194+
// See: https://github.com/webpack/webpack/blob/main/lib/runtime/PublicPathRuntimeModule.js#L19-L27
195+
publicPath = publicPath({ hash: 'XXXX' });
191196
}
192197

193198
if (

Diff for: test/__snapshots__/public-path.test.js.snap

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`public path should work with function output.publicPath: DOM 1`] = `
4+
"<!DOCTYPE html><html><head>
5+
<title>style-loader test</title>
6+
<style id=\\"existing-style\\">.existing { color: yellow }</style>
7+
</head>
8+
<body>
9+
<h1>Body</h1>
10+
<div class=\\"target\\"></div>
11+
<iframe class=\\"iframeTarget\\"></iframe>
12+
13+
14+
</body></html>"
15+
`;
16+
17+
exports[`public path should work with function output.publicPath: errors 1`] = `Array []`;
18+
19+
exports[`public path should work with function output.publicPath: warnings 1`] = `Array []`;

Diff for: test/public-path.test.js

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint-env browser */
2+
import path from "path";
3+
4+
import MiniCssExtractPlugin from "../src/cjs";
5+
6+
import {
7+
compile,
8+
getCompiler,
9+
getErrors,
10+
getWarnings,
11+
runInJsDom,
12+
} from "./helpers/index";
13+
14+
describe("public path", () => {
15+
it("should work with function output.publicPath", async () => {
16+
const compiler = getCompiler(
17+
"simple.js",
18+
{},
19+
{
20+
output: {
21+
publicPath() {
22+
return "";
23+
},
24+
path: path.resolve(__dirname, "../outputs"),
25+
filename: "[name].bundle.js",
26+
},
27+
plugins: [
28+
new MiniCssExtractPlugin({
29+
filename: "[name].css",
30+
}),
31+
],
32+
}
33+
);
34+
const stats = await compile(compiler);
35+
36+
runInJsDom("main.bundle.js", compiler, stats, (dom) => {
37+
expect(dom.serialize()).toMatchSnapshot("DOM");
38+
});
39+
40+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
41+
expect(getErrors(stats)).toMatchSnapshot("errors");
42+
});
43+
});

0 commit comments

Comments
 (0)