Skip to content

Commit b13892b

Browse files
feat: new URL('./source.ext', import.meta.url) syntax is using for importing sources
BREAKING CHANGE: `new URL('./source.ext', import.meta.url)` syntax is using for importing `sources`
1 parent 497a2a5 commit b13892b

File tree

10 files changed

+2701
-2727
lines changed

10 files changed

+2701
-2727
lines changed

src/runtime/getUrl.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ module.exports = (url, options) => {
44
options = {};
55
}
66

7-
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
8-
url = url && url.__esModule ? url.default : url;
9-
10-
if (typeof url !== 'string') {
7+
if (!url) {
118
return url;
129
}
1310

11+
// eslint-disable-next-line no-underscore-dangle, no-param-reassign
12+
url = String(url.__esModule ? url.default : url);
13+
1414
if (options.hash) {
1515
// eslint-disable-next-line no-param-reassign
1616
url += options.hash;

src/utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ export function getImportCode(html, loaderContext, imports, options) {
908908
const { importName, source } = item;
909909

910910
code += options.esModule
911-
? `import ${importName} from ${source};\n`
911+
? `var ${importName} = new URL(${source}, import.meta.url);\n`
912912
: `var ${importName} = require(${source});\n`;
913913
}
914914

test/__snapshots__/esModule-option.test.js.snap

Lines changed: 350 additions & 350 deletions
Large diffs are not rendered by default.

test/__snapshots__/loader.test.js.snap

Lines changed: 186 additions & 186 deletions
Large diffs are not rendered by default.

test/__snapshots__/minimize-option.test.js.snap

Lines changed: 598 additions & 598 deletions
Large diffs are not rendered by default.

test/__snapshots__/preprocessor-option.test.js.snap

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ exports[`'process' option should work with Async "preprocessor" Function option:
55
exports[`'process' option should work with Async "preprocessor" Function option: module 1`] = `
66
"// Imports
77
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
8-
import ___HTML_LOADER_IMPORT_0___ from \\"./image.png\\";
8+
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
99
// Module
1010
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
1111
var code = \\"<div>\\\\n <p>Alexander Krasnoyarov</p>\\\\n <img src=\\\\\\"\\" + ___HTML_LOADER_REPLACEMENT_0___ + \\"\\\\\\" alt=\\\\\\"alt\\\\\\" />\\\\n<div>\\\\n\\";
@@ -16,7 +16,7 @@ export default code;"
1616
exports[`'process' option should work with Async "preprocessor" Function option: result 1`] = `
1717
"<div>
1818
<p>Alexander Krasnoyarov</p>
19-
<img src=\\"/webpack/public/path/image.png\\" alt=\\"alt\\" />
19+
<img src=\\"replaced_file_protocol_/webpack/public/path/image.png\\" alt=\\"alt\\" />
2020
<div>
2121
"
2222
`;
@@ -28,8 +28,8 @@ exports[`'process' option should work with the "preprocessor" option #2: errors
2828
exports[`'process' option should work with the "preprocessor" option #2: module 1`] = `
2929
"// Imports
3030
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
31-
import ___HTML_LOADER_IMPORT_0___ from \\"./image.png.webp\\";
32-
import ___HTML_LOADER_IMPORT_1___ from \\"./image.png\\";
31+
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png.webp\\", import.meta.url);
32+
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"./image.png\\", import.meta.url);
3333
// Module
3434
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
3535
var ___HTML_LOADER_REPLACEMENT_1___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_1___);
@@ -39,7 +39,7 @@ export default code;"
3939
`;
4040
4141
exports[`'process' option should work with the "preprocessor" option #2: result 1`] = `
42-
"<picture><source type=\\"image/webp\\" srcset=\\"/webpack/public/path/image.png.webp\\"><img src=\\"/webpack/public/path/image.png\\"></picture>
42+
"<picture><source type=\\"image/webp\\" srcset=\\"replaced_file_protocol_/webpack/public/path/image.png.webp\\"><img src=\\"replaced_file_protocol_/webpack/public/path/image.png\\"></picture>
4343
"
4444
`;
4545
@@ -50,7 +50,7 @@ exports[`'process' option should work with the "preprocessor" option: errors 1`]
5050
exports[`'process' option should work with the "preprocessor" option: module 1`] = `
5151
"// Imports
5252
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
53-
import ___HTML_LOADER_IMPORT_0___ from \\"./image.png\\";
53+
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
5454
// Module
5555
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
5656
var code = \\"<div>\\\\n <p>Alexander Krasnoyarov</p>\\\\n <img src=\\\\\\"\\" + ___HTML_LOADER_REPLACEMENT_0___ + \\"\\\\\\" alt=\\\\\\"alt\\\\\\" />\\\\n<div>\\\\n\\";
@@ -61,7 +61,7 @@ export default code;"
6161
exports[`'process' option should work with the "preprocessor" option: result 1`] = `
6262
"<div>
6363
<p>Alexander Krasnoyarov</p>
64-
<img src=\\"/webpack/public/path/image.png\\" alt=\\"alt\\" />
64+
<img src=\\"replaced_file_protocol_/webpack/public/path/image.png\\" alt=\\"alt\\" />
6565
<div>
6666
"
6767
`;
@@ -73,8 +73,8 @@ exports[`'process' option should work with the Async "preprocessor" Function opt
7373
exports[`'process' option should work with the Async "preprocessor" Function option #2: module 1`] = `
7474
"// Imports
7575
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
76-
import ___HTML_LOADER_IMPORT_0___ from \\"./image.png.webp\\";
77-
import ___HTML_LOADER_IMPORT_1___ from \\"./image.png\\";
76+
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png.webp\\", import.meta.url);
77+
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"./image.png\\", import.meta.url);
7878
// Module
7979
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
8080
var ___HTML_LOADER_REPLACEMENT_1___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_1___);
@@ -84,7 +84,7 @@ export default code;"
8484
`;
8585
8686
exports[`'process' option should work with the Async "preprocessor" Function option #2: result 1`] = `
87-
"<picture><source type=\\"image/webp\\" srcset=\\"/webpack/public/path/image.png.webp\\"><img src=\\"/webpack/public/path/image.png\\"></picture>
87+
"<picture><source type=\\"image/webp\\" srcset=\\"replaced_file_protocol_/webpack/public/path/image.png.webp\\"><img src=\\"replaced_file_protocol_/webpack/public/path/image.png\\"></picture>
8888
"
8989
`;
9090

test/__snapshots__/sources-option.test.js.snap

Lines changed: 1543 additions & 1576 deletions
Large diffs are not rendered by default.

test/helpers/execute.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import path from 'path';
33

44
const parentModule = module;
55

6+
function replaceAbsolutePath(data) {
7+
return typeof data === 'string'
8+
? data.replace(/file:\/\/\/(\D:\/)?/gi, 'replaced_file_protocol_/')
9+
: data;
10+
}
11+
612
export default (code) => {
713
const resource = 'test.js';
814
const module = new Module(resource, parentModule);
@@ -18,5 +24,5 @@ export default (code) => {
1824
resource
1925
);
2026

21-
return module.exports;
27+
return replaceAbsolutePath(module.exports);
2228
};

test/helpers/getCompiler.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default (fixture, loaderOptions = {}, config = {}) => {
1313
path: path.resolve(__dirname, '../outputs'),
1414
filename: '[name].bundle.js',
1515
chunkFilename: '[name].chunk.js',
16+
chunkLoading: 'require',
1617
publicPath: '/webpack/public/path/',
1718
library: '___TEST___',
1819
assetModuleFilename: '[name][ext]',

test/runtime/__snapshots__/getUrl.test.js.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`getUrl should work 1`] = `true`;
3+
exports[`getUrl should work 1`] = `"true"`;
44

55
exports[`getUrl should work 2`] = `null`;
66

0 commit comments

Comments
 (0)