Skip to content

Commit

Permalink
fix: allow to use auto value with the publicPath option (#709)
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Feb 25, 2021
1 parent 470ba0e commit 1be21d2
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/loader.js
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/cases/publicpath-compiler-auto/expected/main.css
@@ -0,0 +1,5 @@
body {
background: red;
background-image: url(c9e192c015437a21dea1faa1d30f4941.svg);
}

1 change: 1 addition & 0 deletions test/cases/publicpath-compiler-auto/index.js
@@ -0,0 +1 @@
import './style.css';
1 change: 1 addition & 0 deletions test/cases/publicpath-compiler-auto/react.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/cases/publicpath-compiler-auto/style.css
@@ -0,0 +1,4 @@
body {
background: red;
background-image: url(./react.svg);
}
38 changes: 38 additions & 0 deletions 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',
}),
],
};
5 changes: 5 additions & 0 deletions test/cases/publicpath-loader-auto/expected/main.css
@@ -0,0 +1,5 @@
body {
background: red;
background-image: url(c9e192c015437a21dea1faa1d30f4941.svg);
}

1 change: 1 addition & 0 deletions test/cases/publicpath-loader-auto/index.js
@@ -0,0 +1 @@
import './style.css';
1 change: 1 addition & 0 deletions test/cases/publicpath-loader-auto/react.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/cases/publicpath-loader-auto/style.css
@@ -0,0 +1,4 @@
body {
background: red;
background-image: url(./react.svg);
}
37 changes: 37 additions & 0 deletions 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',
}),
],
};

0 comments on commit 1be21d2

Please sign in to comment.