Skip to content

fix: error when using function in webpack output.publicPath #874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ export function pitch(request) {

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

if (
Expand Down
19 changes: 19 additions & 0 deletions test/__snapshots__/public-path.test.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`public path should work with function output.publicPath: DOM 1`] = `
"<!DOCTYPE html><html><head>
<title>style-loader test</title>
<style id=\\"existing-style\\">.existing { color: yellow }</style>
</head>
<body>
<h1>Body</h1>
<div class=\\"target\\"></div>
<iframe class=\\"iframeTarget\\"></iframe>


</body></html>"
`;

exports[`public path should work with function output.publicPath: errors 1`] = `Array []`;

exports[`public path should work with function output.publicPath: warnings 1`] = `Array []`;
43 changes: 43 additions & 0 deletions test/public-path.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/* eslint-env browser */
import path from "path";

import MiniCssExtractPlugin from "../src/cjs";

import {
compile,
getCompiler,
getErrors,
getWarnings,
runInJsDom,
} from "./helpers/index";

describe("public path", () => {
it("should work with function output.publicPath", async () => {
const compiler = getCompiler(
"simple.js",
{},
{
output: {
publicPath() {
return "";
},
path: path.resolve(__dirname, "../outputs"),
filename: "[name].bundle.js",
},
plugins: [
new MiniCssExtractPlugin({
filename: "[name].css",
}),
],
}
);
const stats = await compile(compiler);

runInJsDom("main.bundle.js", compiler, stats, (dom) => {
expect(dom.serialize()).toMatchSnapshot("DOM");
});

expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});
});