From 582ebfecb975c5e72d1644f16bf1109877a6d5e1 Mon Sep 17 00:00:00 2001 From: Rasmus Melchior Jacobsen Date: Tue, 14 May 2019 12:01:37 +0200 Subject: [PATCH] fix(loader): fix `publicPath` regression (#384) --- src/loader.js | 2 +- .../publicpath-emptystring/expected/main.css | 2 + test/cases/publicpath-emptystring/index.js | 1 + test/cases/publicpath-emptystring/react.svg | 1 + test/cases/publicpath-emptystring/style.css | 1 + .../publicpath-emptystring/webpack.config.js | 37 +++++++++++++++++++ 6 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 test/cases/publicpath-emptystring/expected/main.css create mode 100644 test/cases/publicpath-emptystring/index.js create mode 100644 test/cases/publicpath-emptystring/react.svg create mode 100644 test/cases/publicpath-emptystring/style.css create mode 100644 test/cases/publicpath-emptystring/webpack.config.js diff --git a/src/loader.js b/src/loader.js index 04181c42..a91dd787 100644 --- a/src/loader.js +++ b/src/loader.js @@ -68,7 +68,7 @@ export function pitch(request) { const childFilename = '*'; // eslint-disable-line no-path-concat const publicPath = typeof options.publicPath === 'string' - ? options.publicPath.endsWith('/') + ? options.publicPath === '' || options.publicPath.endsWith('/') ? options.publicPath : `${options.publicPath}/` : typeof options.publicPath === 'function' diff --git a/test/cases/publicpath-emptystring/expected/main.css b/test/cases/publicpath-emptystring/expected/main.css new file mode 100644 index 00000000..f6674281 --- /dev/null +++ b/test/cases/publicpath-emptystring/expected/main.css @@ -0,0 +1,2 @@ +body { background: red; background-image: url(cd0bb358c45b584743d8ce4991777c42.svg); } + diff --git a/test/cases/publicpath-emptystring/index.js b/test/cases/publicpath-emptystring/index.js new file mode 100644 index 00000000..aa3357bf --- /dev/null +++ b/test/cases/publicpath-emptystring/index.js @@ -0,0 +1 @@ +import './style.css'; diff --git a/test/cases/publicpath-emptystring/react.svg b/test/cases/publicpath-emptystring/react.svg new file mode 100644 index 00000000..5b3b22a4 --- /dev/null +++ b/test/cases/publicpath-emptystring/react.svg @@ -0,0 +1 @@ +logo-on-dark-bg \ No newline at end of file diff --git a/test/cases/publicpath-emptystring/style.css b/test/cases/publicpath-emptystring/style.css new file mode 100644 index 00000000..edcbc24a --- /dev/null +++ b/test/cases/publicpath-emptystring/style.css @@ -0,0 +1 @@ +body { background: red; background-image: url(./react.svg); } diff --git a/test/cases/publicpath-emptystring/webpack.config.js b/test/cases/publicpath-emptystring/webpack.config.js new file mode 100644 index 00000000..eb6125c8 --- /dev/null +++ b/test/cases/publicpath-emptystring/webpack.config.js @@ -0,0 +1,37 @@ +import Self from '../../../src'; + +module.exports = { + entry: './index.js', + module: { + rules: [ + { + test: /\.css$/, + use: [ + { + loader: Self.loader, + options: { + publicPath: '', + }, + }, + 'css-loader', + ], + }, + { + test: /\.(svg|png)$/, + use: [ + { + loader: 'file-loader', + options: { + filename: '[name].[ext]', + }, + }, + ], + }, + ], + }, + plugins: [ + new Self({ + filename: '[name].css', + }), + ], +};