Skip to content

Commit 9df2cd5

Browse files
feat: added new url support
1 parent 4227510 commit 9df2cd5

File tree

20 files changed

+131
-1
lines changed

20 files changed

+131
-1
lines changed

src/loader.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,18 @@ export function pitch(request) {
230230
outputOptions
231231
);
232232

233+
// The templates are compiled and executed by NodeJS - similar to server side rendering
234+
// Unfortunately this causes issues as some loaders require an absolute URL to support ES Modules
235+
// The following config enables relative URL support for the child compiler
236+
childCompiler.options.module = { ...childCompiler.options.module };
237+
childCompiler.options.module.parser = {
238+
...childCompiler.options.module.parser,
239+
};
240+
childCompiler.options.module.parser.javascript = {
241+
...childCompiler.options.module.parser.javascript,
242+
url: 'relative',
243+
};
244+
233245
const { NodeTemplatePlugin } = webpack.node;
234246
const NodeTargetPlugin = webpack.node.NodeTargetPlugin
235247
? webpack.node.NodeTargetPlugin

test/TestCases.test.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ describe('TestCases', () => {
106106
directoryForCase,
107107
'webpack.config.js'
108108
));
109+
const { context } = webpackConfig;
109110

110111
for (const config of [].concat(webpackConfig)) {
111112
Object.assign(
@@ -132,7 +133,8 @@ describe('TestCases', () => {
132133
}
133134
return p;
134135
}),
135-
}
136+
},
137+
context ? { context } : {}
136138
);
137139
}
138140

Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './style.css';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default function loader() {
2+
const callback = this.async();
3+
4+
callback(
5+
null,
6+
`export default [
7+
[0, ".foo {background: url(" + new URL("./img.png", import.meta.url) + ")}", ""],
8+
[1, ".bar {background: url(" + new URL("../outer-img.png", import.meta.url) + ")}", ""],
9+
[2, ".baz {background: url(" + new URL("./nested/nested-img.png", import.meta.url) + ")}", ""]
10+
]`
11+
);
12+
}
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.class {
2+
background: red;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.foo {background: url(img.png)}
2+
.bar {background: url(../outer-img.png)}
3+
.baz {background: url(nested/nested-img.png)}
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const webpack = require('webpack');
2+
3+
module.exports = () => webpack.version[0] !== '4';
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import path from 'path';
2+
3+
import Self from '../../../src';
4+
5+
module.exports = {
6+
entry: './index.js',
7+
context: path.resolve(__dirname, 'app'),
8+
module: {
9+
rules: [
10+
{
11+
test: /\.css$/,
12+
use: [
13+
{
14+
loader: Self.loader,
15+
options: {
16+
publicPath: 'auto',
17+
},
18+
},
19+
'./mockLoader',
20+
],
21+
},
22+
{
23+
test: /\.png$/,
24+
type: 'asset/resource',
25+
generator: {
26+
filename: '[path][name][ext]',
27+
},
28+
},
29+
],
30+
},
31+
plugins: [
32+
new Self({
33+
filename: '[name].css',
34+
}),
35+
],
36+
};
76.3 KB
Loading
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './style.css';
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export default function loader() {
2+
const callback = this.async();
3+
4+
callback(
5+
null,
6+
`export default [
7+
[0, ".foo {background: url(" + new URL("./img.png", import.meta.url) + ")}", ""],
8+
[1, ".bar {background: url(" + new URL("../outer-img.png", import.meta.url) + ")}", ""],
9+
[2, ".baz {background: url(" + new URL("./nested/nested-img.png", import.meta.url) + ")}", ""]
10+
]`
11+
);
12+
}
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.class {
2+
background: red;
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.foo {background: url(public/img.png)}
2+
.bar {background: url(public/../outer-img.png)}
3+
.baz {background: url(public/nested/nested-img.png)}
Loading
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const webpack = require('webpack');
2+
3+
module.exports = () => webpack.version[0] !== '4';
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import path from 'path';
2+
3+
import Self from '../../../src';
4+
5+
module.exports = {
6+
entry: './index.js',
7+
context: path.resolve(__dirname, 'app'),
8+
module: {
9+
rules: [
10+
{
11+
test: /\.css$/,
12+
use: [
13+
{
14+
loader: Self.loader,
15+
options: {
16+
publicPath: 'public',
17+
},
18+
},
19+
'./mockLoader',
20+
],
21+
},
22+
{
23+
test: /\.png$/,
24+
type: 'asset/resource',
25+
generator: {
26+
filename: '[path][name][ext]',
27+
},
28+
},
29+
],
30+
},
31+
plugins: [
32+
new Self({
33+
filename: '[name].css',
34+
}),
35+
],
36+
};

0 commit comments

Comments
 (0)