Skip to content

Commit c76a1a1

Browse files
feat: added new url support (#753)
1 parent 4227510 commit c76a1a1

28 files changed

+146
-12
lines changed

package-lock.json

+9-8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
"npm-run-all": "^4.1.5",
7474
"prettier": "^2.2.1",
7575
"standard-version": "^9.1.0",
76-
"webpack": "^5.33.2",
76+
"webpack": "^5.36.1",
7777
"webpack-cli": "^4.5.0",
7878
"webpack-dev-server": "^3.7.2"
7979
},

src/loader.js

+13
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ export function pitch(request) {
196196
);
197197
return;
198198
}
199+
199200
this.importModule(
200201
`${this.resourcePath}.webpack[javascript/auto]!=!${request}`,
201202
{
@@ -230,6 +231,18 @@ export function pitch(request) {
230231
outputOptions
231232
);
232233

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

test/TestCases.test.js

+3-1
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

test/cases/chunkFilename-fullhash/expected/webpack-5-importModule/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ __webpack_require__.r(__webpack_exports__);
7373
/******/
7474
/******/ /* webpack/runtime/getFullHash */
7575
/******/ (() => {
76-
/******/ __webpack_require__.h = () => ("acd0bc1ae24d05fdac73")
76+
/******/ __webpack_require__.h = () => ("619e4b98882ea3a2aba3")
7777
/******/ })();
7878
/******/
7979
/******/ /* webpack/runtime/global */

test/cases/chunkFilename-fullhash/expected/webpack-5/main.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ __webpack_require__.r(__webpack_exports__);
7373
/******/
7474
/******/ /* webpack/runtime/getFullHash */
7575
/******/ (() => {
76-
/******/ __webpack_require__.h = () => ("4ecab8e2ff0fe228a728")
76+
/******/ __webpack_require__.h = () => ("962555dd7355e6c261df")
7777
/******/ })();
7878
/******/
7979
/******/ /* webpack/runtime/global */
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './style.css';
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.class {
2+
background: red;
3+
}
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const webpack = require('webpack');
2+
3+
module.exports = () =>
4+
webpack.version[0] !== '4' && !process.env.EXPERIMENTAL_USE_IMPORT_MODULE;
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import './style.css';
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.class {
2+
background: red;
3+
}
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
const webpack = require('webpack');
2+
3+
module.exports = () =>
4+
webpack.version[0] !== '4' && !process.env.EXPERIMENTAL_USE_IMPORT_MODULE;
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)