diff --git a/src/loader.js b/src/loader.js index 450790c9..a72366ac 100644 --- a/src/loader.js +++ b/src/loader.js @@ -44,16 +44,22 @@ export function pitch(request) { const childFilename = '*'; const publicPath = typeof options.publicPath === 'string' - ? options.publicPath === '' || options.publicPath.endsWith('/') + ? options.publicPath === 'auto' + ? '' + : options.publicPath === '' || options.publicPath.endsWith('/') ? options.publicPath : `${options.publicPath}/` : typeof options.publicPath === 'function' ? options.publicPath(this.resourcePath, this.rootContext) + : this._compilation.outputOptions.publicPath === 'auto' + ? '' : this._compilation.outputOptions.publicPath; + const outputOptions = { filename: childFilename, publicPath, }; + const childCompiler = this._compilation.createChildCompiler( `${pluginName} ${request}`, outputOptions diff --git a/test/cases/publicpath-compiler-auto/expected/main.css b/test/cases/publicpath-compiler-auto/expected/main.css new file mode 100644 index 00000000..339ed982 --- /dev/null +++ b/test/cases/publicpath-compiler-auto/expected/main.css @@ -0,0 +1,5 @@ +body { + background: red; + background-image: url(c9e192c015437a21dea1faa1d30f4941.svg); +} + diff --git a/test/cases/publicpath-compiler-auto/index.js b/test/cases/publicpath-compiler-auto/index.js new file mode 100644 index 00000000..aa3357bf --- /dev/null +++ b/test/cases/publicpath-compiler-auto/index.js @@ -0,0 +1 @@ +import './style.css'; diff --git a/test/cases/publicpath-compiler-auto/react.svg b/test/cases/publicpath-compiler-auto/react.svg new file mode 100644 index 00000000..5b3b22a4 --- /dev/null +++ b/test/cases/publicpath-compiler-auto/react.svg @@ -0,0 +1 @@ +logo-on-dark-bg \ No newline at end of file diff --git a/test/cases/publicpath-compiler-auto/style.css b/test/cases/publicpath-compiler-auto/style.css new file mode 100644 index 00000000..52c2b3e4 --- /dev/null +++ b/test/cases/publicpath-compiler-auto/style.css @@ -0,0 +1,4 @@ +body { + background: red; + background-image: url(./react.svg); +} diff --git a/test/cases/publicpath-compiler-auto/webpack.config.js b/test/cases/publicpath-compiler-auto/webpack.config.js new file mode 100644 index 00000000..9b1ec19a --- /dev/null +++ b/test/cases/publicpath-compiler-auto/webpack.config.js @@ -0,0 +1,38 @@ +import Self from '../../../src'; + +module.exports = { + entry: './index.js', + output: { + publicPath: 'auto', + }, + module: { + rules: [ + { + test: /\.css$/, + use: [ + { + loader: Self.loader, + options: {}, + }, + 'css-loader', + ], + }, + { + test: /\.(svg|png)$/, + use: [ + { + loader: 'file-loader', + options: { + filename: '[name].[ext]', + }, + }, + ], + }, + ], + }, + plugins: [ + new Self({ + filename: '[name].css', + }), + ], +}; diff --git a/test/cases/publicpath-loader-auto/expected/main.css b/test/cases/publicpath-loader-auto/expected/main.css new file mode 100644 index 00000000..339ed982 --- /dev/null +++ b/test/cases/publicpath-loader-auto/expected/main.css @@ -0,0 +1,5 @@ +body { + background: red; + background-image: url(c9e192c015437a21dea1faa1d30f4941.svg); +} + diff --git a/test/cases/publicpath-loader-auto/index.js b/test/cases/publicpath-loader-auto/index.js new file mode 100644 index 00000000..aa3357bf --- /dev/null +++ b/test/cases/publicpath-loader-auto/index.js @@ -0,0 +1 @@ +import './style.css'; diff --git a/test/cases/publicpath-loader-auto/react.svg b/test/cases/publicpath-loader-auto/react.svg new file mode 100644 index 00000000..5b3b22a4 --- /dev/null +++ b/test/cases/publicpath-loader-auto/react.svg @@ -0,0 +1 @@ +logo-on-dark-bg \ No newline at end of file diff --git a/test/cases/publicpath-loader-auto/style.css b/test/cases/publicpath-loader-auto/style.css new file mode 100644 index 00000000..52c2b3e4 --- /dev/null +++ b/test/cases/publicpath-loader-auto/style.css @@ -0,0 +1,4 @@ +body { + background: red; + background-image: url(./react.svg); +} diff --git a/test/cases/publicpath-loader-auto/webpack.config.js b/test/cases/publicpath-loader-auto/webpack.config.js new file mode 100644 index 00000000..9ea565d3 --- /dev/null +++ b/test/cases/publicpath-loader-auto/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: 'auto', + }, + }, + 'css-loader', + ], + }, + { + test: /\.(svg|png)$/, + use: [ + { + loader: 'file-loader', + options: { + filename: '[name].[ext]', + }, + }, + ], + }, + ], + }, + plugins: [ + new Self({ + filename: '[name].css', + }), + ], +};