Skip to content

Commit a309fe0

Browse files
fix: avoid using file protocol
1 parent de93b86 commit a309fe0

8 files changed

+114
-41
lines changed

package-lock.json

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

src/utils.js

+68-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { pathToFileURL } from 'url';
1+
import path from 'path';
22

33
import HtmlSourceError from './HtmlSourceError';
44

@@ -1050,14 +1050,80 @@ export function getFilter(filter) {
10501050
};
10511051
}
10521052

1053+
const WINDOWS_ABS_PATH_REGEXP = /^[a-zA-Z]:[\\/]|^\\\\/;
1054+
const WINDOWS_PATH_SEPARATOR_REGEXP = /\\/g;
1055+
1056+
const absoluteToRequest = (context, maybeAbsolutePath) => {
1057+
if (maybeAbsolutePath[0] === '/') {
1058+
if (
1059+
maybeAbsolutePath.length > 1 &&
1060+
maybeAbsolutePath[maybeAbsolutePath.length - 1] === '/'
1061+
) {
1062+
// this 'path' is actually a regexp generated by dynamic requires.
1063+
// Don't treat it as an absolute path.
1064+
return maybeAbsolutePath;
1065+
}
1066+
1067+
const querySplitPos = maybeAbsolutePath.indexOf('?');
1068+
1069+
let resource =
1070+
querySplitPos === -1
1071+
? maybeAbsolutePath
1072+
: maybeAbsolutePath.slice(0, querySplitPos);
1073+
resource = path.posix.relative(context, resource);
1074+
1075+
if (!resource.startsWith('../')) {
1076+
resource = `./${resource}`;
1077+
}
1078+
1079+
return querySplitPos === -1
1080+
? resource
1081+
: resource + maybeAbsolutePath.slice(querySplitPos);
1082+
}
1083+
1084+
if (WINDOWS_ABS_PATH_REGEXP.test(maybeAbsolutePath)) {
1085+
const querySplitPos = maybeAbsolutePath.indexOf('?');
1086+
let resource =
1087+
querySplitPos === -1
1088+
? maybeAbsolutePath
1089+
: maybeAbsolutePath.slice(0, querySplitPos);
1090+
1091+
resource = path.win32.relative(context, resource);
1092+
1093+
if (!WINDOWS_ABS_PATH_REGEXP.test(resource)) {
1094+
resource = resource.replace(WINDOWS_PATH_SEPARATOR_REGEXP, '/');
1095+
1096+
if (!resource.startsWith('../')) {
1097+
resource = `./${resource}`;
1098+
}
1099+
}
1100+
1101+
return querySplitPos === -1
1102+
? resource
1103+
: resource + maybeAbsolutePath.slice(querySplitPos);
1104+
}
1105+
1106+
// not an absolute path
1107+
return maybeAbsolutePath;
1108+
};
1109+
1110+
const contextify = (context, request) =>
1111+
request
1112+
.split('!')
1113+
.map((r) => absoluteToRequest(context, r))
1114+
.join('!');
1115+
10531116
const GET_SOURCE_FROM_IMPORT_NAME = '___HTML_LOADER_GET_SOURCE_FROM_IMPORT___';
10541117

10551118
export function getImportCode(html, loaderContext, imports, options) {
10561119
if (imports.length === 0) {
10571120
return '';
10581121
}
10591122

1060-
const fileURLToHelper = pathToFileURL(require.resolve('./runtime/getUrl.js'));
1123+
const fileURLToHelper = contextify(
1124+
loaderContext.context,
1125+
require.resolve('./runtime/getUrl.js')
1126+
);
10611127

10621128
let code = options.esModule
10631129
? `import ${GET_SOURCE_FROM_IMPORT_NAME} from "${fileURLToHelper}";\n`

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`'esModule' option should use a CommonJS export by default: errors 1`] =
44

55
exports[`'esModule' option should use a CommonJS export by default: module 1`] = `
66
"// Imports
7-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
7+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
88
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
99
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
1010
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);
@@ -523,7 +523,7 @@ exports[`'esModule' option should use a CommonJS export when the value is "false
523523
524524
exports[`'esModule' option should use a CommonJS export when the value is "false": module 1`] = `
525525
"// Imports
526-
var ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ = require(\\"file:///<cwd>//src/runtime/getUrl.js\\");
526+
var ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ = require(\\"../../src/runtime/getUrl.js\\");
527527
var ___HTML_LOADER_IMPORT_0___ = require(\\"./image.png\\");
528528
var ___HTML_LOADER_IMPORT_1___ = require(\\"/image.png\\");
529529
var ___HTML_LOADER_IMPORT_2___ = require(\\"aliasImg\\");
@@ -1042,7 +1042,7 @@ exports[`'esModule' option should use an ES module export when the value is "tru
10421042
10431043
exports[`'esModule' option should use an ES module export when the value is "true": module 1`] = `
10441044
"// Imports
1045-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
1045+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
10461046
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
10471047
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
10481048
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);

test/__snapshots__/loader.test.js.snap

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`loader should emit an error on broken HTML syntax: errors 1`] = `Array
44

55
exports[`loader should emit an error on broken HTML syntax: module 1`] = `
66
"// Imports
7-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
7+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
88
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___);
@@ -66,7 +66,7 @@ exports[`loader should pass queries to other loader: errors 1`] = `Array []`;
6666
6767
exports[`loader should pass queries to other loader: module 1`] = `
6868
"// Imports
69-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
69+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
7070
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./icons.svg?color=%23BAAFDB%3F\\", import.meta.url);
7171
// Module
7272
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___, { hash: \\"#foo\\" });
@@ -104,7 +104,7 @@ exports[`loader should work with "resolve.roots": errors 1`] = `Array []`;
104104
105105
exports[`loader should work with "resolve.roots": module 1`] = `
106106
"// Imports
107-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
107+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
108108
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"/image2.png\\", import.meta.url);
109109
// Module
110110
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
@@ -149,7 +149,7 @@ exports[`loader should work with server-relative url: errors 1`] = `Array []`;
149149
150150
exports[`loader should work with server-relative url: module 1`] = `
151151
"// Imports
152-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
152+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
153153
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"/nested/image3.png\\", import.meta.url);
154154
// Module
155155
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
@@ -171,7 +171,7 @@ exports[`loader should work with webpackIgnore comment: errors 1`] = `Array []`;
171171
172172
exports[`loader should work with webpackIgnore comment: module 1`] = `
173173
"// Imports
174-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
174+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
175175
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
176176
// Module
177177
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
@@ -305,7 +305,7 @@ exports[`loader should work: errors 1`] = `Array []`;
305305
306306
exports[`loader should work: module 1`] = `
307307
"// Imports
308-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
308+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
309309
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
310310
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
311311
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`"minimize" option should be turned off by default: errors 1`] = `Array
44

55
exports[`"minimize" option should be turned off by default: module 1`] = `
66
"// Imports
7-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
7+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
88
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
99
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
1010
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);
@@ -523,7 +523,7 @@ exports[`"minimize" option should be turned off in "development" mode: errors 1`
523523
524524
exports[`"minimize" option should be turned off in "development" mode: module 1`] = `
525525
"// Imports
526-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
526+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
527527
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
528528
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
529529
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);
@@ -1042,7 +1042,7 @@ exports[`"minimize" option should be turned on in "production" mode: errors 1`]
10421042
10431043
exports[`"minimize" option should be turned on in "production" mode: module 1`] = `
10441044
"// Imports
1045-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
1045+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
10461046
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
10471047
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
10481048
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);
@@ -1126,7 +1126,7 @@ Parse Error: < img src=\\"image.png\\" >",
11261126
11271127
exports[`"minimize" option should emit an error on broken HTML syntax: module 1`] = `
11281128
"// Imports
1129-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
1129+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
11301130
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
11311131
// Module
11321132
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
@@ -1153,7 +1153,7 @@ exports[`"minimize" option should not work with a value equal to "false": errors
11531153
11541154
exports[`"minimize" option should not work with a value equal to "false": module 1`] = `
11551155
"// Imports
1156-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
1156+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
11571157
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
11581158
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
11591159
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);
@@ -1672,7 +1672,7 @@ exports[`"minimize" option should support options for minimizer: errors 1`] = `A
16721672
16731673
exports[`"minimize" option should support options for minimizer: module 1`] = `
16741674
"// Imports
1675-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
1675+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
16761676
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
16771677
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
16781678
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);
@@ -1766,7 +1766,7 @@ exports[`"minimize" option should work with a value equal to "true": errors 1`]
17661766
17671767
exports[`"minimize" option should work with a value equal to "true": module 1`] = `
17681768
"// Imports
1769-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
1769+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
17701770
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
17711771
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
17721772
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ exports[`'process' option should work with Async "preprocessor" Function option:
44

55
exports[`'process' option should work with Async "preprocessor" Function option: module 1`] = `
66
"// Imports
7-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
7+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
88
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___);
@@ -27,7 +27,7 @@ exports[`'process' option should work with the "preprocessor" option #2: errors
2727
2828
exports[`'process' option should work with the "preprocessor" option #2: module 1`] = `
2929
"// Imports
30-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
30+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
3131
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png.webp\\", import.meta.url);
3232
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"./image.png\\", import.meta.url);
3333
// Module
@@ -49,7 +49,7 @@ exports[`'process' option should work with the "preprocessor" option: errors 1`]
4949
5050
exports[`'process' option should work with the "preprocessor" option: module 1`] = `
5151
"// Imports
52-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
52+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
5353
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___);
@@ -72,7 +72,7 @@ exports[`'process' option should work with the Async "preprocessor" Function opt
7272
7373
exports[`'process' option should work with the Async "preprocessor" Function option #2: module 1`] = `
7474
"// Imports
75-
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
75+
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
7676
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png.webp\\", import.meta.url);
7777
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"./image.png\\", import.meta.url);
7878
// Module

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

+21-17
Large diffs are not rendered by default.

test/fixtures/sources.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -287,4 +287,7 @@
287287

288288
<meta content="name=Check Order Status;
289289
action-uri=./orderStatus.aspx?src=IE9;
290-
icon-uri=" name="msapplication-task">
290+
icon-uri=" name="msapplication-task">
291+
292+
<a href="#" class="gmail-link">[email protected]</a>
293+
<a href="mailto:[email protected]" class="gmail-link">[email protected]</a>

0 commit comments

Comments
 (0)