Skip to content

Commit

Permalink
fix: crash when publicPath is function (#881)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Dec 15, 2021
1 parent 0651fa5 commit 41bd828
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,22 @@ export function pitch(request) {
return;
}

const isAbsolutePublicPath = /^[a-zA-Z][a-zA-Z\d+\-.]*?:/.test(publicPath);
const publicPathForExtract = isAbsolutePublicPath
? publicPath
: `${ABSOLUTE_PUBLIC_PATH}${publicPath.replace(
/\./g,
SINGLE_DOT_PATH_SEGMENT
)}`;
let publicPathForExtract;

if (typeof publicPath === "string") {
const isAbsolutePublicPath = /^[a-zA-Z][a-zA-Z\d+\-.]*?:/.test(
publicPath
);

publicPathForExtract = isAbsolutePublicPath
? publicPath
: `${ABSOLUTE_PUBLIC_PATH}${publicPath.replace(
/\./g,
SINGLE_DOT_PATH_SEGMENT
)}`;
} else {
publicPathForExtract = publicPath;
}

this.importModule(
`${this.resourcePath}.webpack[javascript/auto]!=!!!${request}`,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
body {
background: green;
background-image: url(http://example.com/XXXX/c9e192c015437a21dea1.svg);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
body {
background: red;
background-image: url(http://example.com/XXXX/c9e192c015437a21dea1.svg);
}

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
body {
background: green;
background-image: url(http://example.com/4d869bb95ecdf3f870fb/c9e192c015437a21dea1.svg);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
body {
background: red;
background-image: url(http://example.com/ca6e489a6807af3e778c/c9e192c015437a21dea1.svg);
}

4 changes: 4 additions & 0 deletions test/cases/publicpath-function-2/nested/again/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
background: green;
background-image: url(../../react.svg);
}
4 changes: 4 additions & 0 deletions test/cases/publicpath-function-2/nested/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
body {
background: red;
background-image: url(../react.svg);
}
1 change: 1 addition & 0 deletions test/cases/publicpath-function-2/react.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions test/cases/publicpath-function-2/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Self from "../../../src";

module.exports = {
entry: {
// Specific CSS entry point, with output to a nested folder
"nested/style": "./nested/style.css",
// Note that relative nesting of output is the same as that of the input
"nested/again/style": "./nested/again/style.css",
},
output: {
// Compute publicPath relative to the CSS output
publicPath: (pathData) => `http://example.com/${pathData.hash}/`,
},
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: Self.loader,
},
"css-loader",
],
},
],
},
plugins: [
new Self({
filename: "[name].css",
}),
],
};

0 comments on commit 41bd828

Please sign in to comment.