Skip to content
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

fix: publicPath auto #709

Merged
merged 1 commit into from
Feb 25, 2021
Merged
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
8 changes: 7 additions & 1 deletion src/loader.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './style.css';
1 change: 1 addition & 0 deletions test/cases/publicpath-compiler-auto/react.svg
Loading
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './style.css';
1 change: 1 addition & 0 deletions test/cases/publicpath-loader-auto/react.svg
Loading
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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',
}),
],
};