Skip to content

Commit c568970

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

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-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

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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() { return "" },
22+
path: path.resolve(__dirname, "../outputs"),
23+
filename: "[name].bundle.js",
24+
},
25+
plugins: [
26+
new MiniCssExtractPlugin({
27+
filename: "[name].css",
28+
}),
29+
],
30+
}
31+
);
32+
const stats = await compile(compiler);
33+
34+
runInJsDom("main.bundle.js", compiler, stats, (dom, bundle) => {
35+
expect(dom.serialize()).toMatchSnapshot("DOM");
36+
});
37+
38+
expect(getWarnings(stats)).toMatchSnapshot("warnings");
39+
expect(getErrors(stats)).toMatchSnapshot("errors");
40+
});
41+
});

0 commit comments

Comments
 (0)