Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
feat: added new url support
  • Loading branch information
cap-Bernardito committed Apr 29, 2021
commit 9df2cd5b2a4860b7e9416b98031d22f8d2a53c6e
12 changes: 12 additions & 0 deletions src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ export function pitch(request) {
outputOptions
);

// The templates are compiled and executed by NodeJS - similar to server side rendering
// Unfortunately this causes issues as some loaders require an absolute URL to support ES Modules
// The following config enables relative URL support for the child compiler
childCompiler.options.module = { ...childCompiler.options.module };
childCompiler.options.module.parser = {
...childCompiler.options.module.parser,
};
childCompiler.options.module.parser.javascript = {
...childCompiler.options.module.parser.javascript,
url: 'relative',
};

const { NodeTemplatePlugin } = webpack.node;
const NodeTargetPlugin = webpack.node.NodeTargetPlugin
? webpack.node.NodeTargetPlugin
Expand Down
4 changes: 3 additions & 1 deletion test/TestCases.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ describe('TestCases', () => {
directoryForCase,
'webpack.config.js'
));
const { context } = webpackConfig;

for (const config of [].concat(webpackConfig)) {
Object.assign(
Expand All @@ -132,7 +133,8 @@ describe('TestCases', () => {
}
return p;
}),
}
},
context ? { context } : {}
);
}

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/cases/new-url-with-public-path-auto/app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './style.css';
12 changes: 12 additions & 0 deletions test/cases/new-url-with-public-path-auto/app/mockLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function loader() {
const callback = this.async();

callback(
null,
`export default [
[0, ".foo {background: url(" + new URL("./img.png", import.meta.url) + ")}", ""],
[1, ".bar {background: url(" + new URL("../outer-img.png", import.meta.url) + ")}", ""],
[2, ".baz {background: url(" + new URL("./nested/nested-img.png", import.meta.url) + ")}", ""]
]`
);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/cases/new-url-with-public-path-auto/app/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.class {
background: red;
}
3 changes: 3 additions & 0 deletions test/cases/new-url-with-public-path-auto/expected/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.foo {background: url(img.png)}
.bar {background: url(../outer-img.png)}
.baz {background: url(nested/nested-img.png)}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/cases/new-url-with-public-path-auto/test.filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const webpack = require('webpack');

module.exports = () => webpack.version[0] !== '4';
36 changes: 36 additions & 0 deletions test/cases/new-url-with-public-path-auto/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import path from 'path';

import Self from '../../../src';

module.exports = {
entry: './index.js',
context: path.resolve(__dirname, 'app'),
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: Self.loader,
options: {
publicPath: 'auto',
},
},
'./mockLoader',
],
},
{
test: /\.png$/,
type: 'asset/resource',
generator: {
filename: '[path][name][ext]',
},
},
],
},
plugins: [
new Self({
filename: '[name].css',
}),
],
};
Binary file added test/cases/new-url-with-public-path/app/img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/cases/new-url-with-public-path/app/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import './style.css';
12 changes: 12 additions & 0 deletions test/cases/new-url-with-public-path/app/mockLoader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default function loader() {
const callback = this.async();

callback(
null,
`export default [
[0, ".foo {background: url(" + new URL("./img.png", import.meta.url) + ")}", ""],
[1, ".bar {background: url(" + new URL("../outer-img.png", import.meta.url) + ")}", ""],
[2, ".baz {background: url(" + new URL("./nested/nested-img.png", import.meta.url) + ")}", ""]
]`
);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/cases/new-url-with-public-path/app/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.class {
background: red;
}
3 changes: 3 additions & 0 deletions test/cases/new-url-with-public-path/expected/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.foo {background: url(public/img.png)}
.bar {background: url(public/../outer-img.png)}
.baz {background: url(public/nested/nested-img.png)}
Binary file added test/cases/new-url-with-public-path/outer-img.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/cases/new-url-with-public-path/test.filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const webpack = require('webpack');

module.exports = () => webpack.version[0] !== '4';
36 changes: 36 additions & 0 deletions test/cases/new-url-with-public-path/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import path from 'path';

import Self from '../../../src';

module.exports = {
entry: './index.js',
context: path.resolve(__dirname, 'app'),
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: Self.loader,
options: {
publicPath: 'public',
},
},
'./mockLoader',
],
},
{
test: /\.png$/,
type: 'asset/resource',
generator: {
filename: '[path][name][ext]',
},
},
],
},
plugins: [
new Self({
filename: '[name].css',
}),
],
};