Skip to content

Commit

Permalink
fix: avoid using file protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Mar 9, 2021
1 parent de93b86 commit a309fe0
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 41 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

70 changes: 68 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pathToFileURL } from 'url';
import path from 'path';

import HtmlSourceError from './HtmlSourceError';

Expand Down Expand Up @@ -1050,14 +1050,80 @@ export function getFilter(filter) {
};
}

const WINDOWS_ABS_PATH_REGEXP = /^[a-zA-Z]:[\\/]|^\\\\/;
const WINDOWS_PATH_SEPARATOR_REGEXP = /\\/g;

const absoluteToRequest = (context, maybeAbsolutePath) => {
if (maybeAbsolutePath[0] === '/') {
if (
maybeAbsolutePath.length > 1 &&
maybeAbsolutePath[maybeAbsolutePath.length - 1] === '/'
) {
// this 'path' is actually a regexp generated by dynamic requires.
// Don't treat it as an absolute path.
return maybeAbsolutePath;
}

const querySplitPos = maybeAbsolutePath.indexOf('?');

let resource =
querySplitPos === -1
? maybeAbsolutePath
: maybeAbsolutePath.slice(0, querySplitPos);
resource = path.posix.relative(context, resource);

if (!resource.startsWith('../')) {
resource = `./${resource}`;
}

return querySplitPos === -1
? resource
: resource + maybeAbsolutePath.slice(querySplitPos);
}

if (WINDOWS_ABS_PATH_REGEXP.test(maybeAbsolutePath)) {
const querySplitPos = maybeAbsolutePath.indexOf('?');
let resource =
querySplitPos === -1
? maybeAbsolutePath
: maybeAbsolutePath.slice(0, querySplitPos);

resource = path.win32.relative(context, resource);

if (!WINDOWS_ABS_PATH_REGEXP.test(resource)) {
resource = resource.replace(WINDOWS_PATH_SEPARATOR_REGEXP, '/');

if (!resource.startsWith('../')) {
resource = `./${resource}`;
}
}

return querySplitPos === -1
? resource
: resource + maybeAbsolutePath.slice(querySplitPos);
}

// not an absolute path
return maybeAbsolutePath;
};

const contextify = (context, request) =>
request
.split('!')
.map((r) => absoluteToRequest(context, r))
.join('!');

const GET_SOURCE_FROM_IMPORT_NAME = '___HTML_LOADER_GET_SOURCE_FROM_IMPORT___';

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

const fileURLToHelper = pathToFileURL(require.resolve('./runtime/getUrl.js'));
const fileURLToHelper = contextify(
loaderContext.context,
require.resolve('./runtime/getUrl.js')
);

let code = options.esModule
? `import ${GET_SOURCE_FROM_IMPORT_NAME} from "${fileURLToHelper}";\n`
Expand Down
6 changes: 3 additions & 3 deletions test/__snapshots__/esModule-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`'esModule' option should use a CommonJS export by default: errors 1`] =

exports[`'esModule' option should use a CommonJS export by default: module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);
Expand Down Expand Up @@ -523,7 +523,7 @@ exports[`'esModule' option should use a CommonJS export when the value is "false
exports[`'esModule' option should use a CommonJS export when the value is "false": module 1`] = `
"// Imports
var ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ = require(\\"file:///<cwd>//src/runtime/getUrl.js\\");
var ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ = require(\\"../../src/runtime/getUrl.js\\");
var ___HTML_LOADER_IMPORT_0___ = require(\\"./image.png\\");
var ___HTML_LOADER_IMPORT_1___ = require(\\"/image.png\\");
var ___HTML_LOADER_IMPORT_2___ = require(\\"aliasImg\\");
Expand Down Expand Up @@ -1042,7 +1042,7 @@ exports[`'esModule' option should use an ES module export when the value is "tru
exports[`'esModule' option should use an ES module export when the value is "true": module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);
Expand Down
12 changes: 6 additions & 6 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`loader should emit an error on broken HTML syntax: errors 1`] = `Array

exports[`loader should emit an error on broken HTML syntax: module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
// Module
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
Expand Down Expand Up @@ -66,7 +66,7 @@ exports[`loader should pass queries to other loader: errors 1`] = `Array []`;
exports[`loader should pass queries to other loader: module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./icons.svg?color=%23BAAFDB%3F\\", import.meta.url);
// Module
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___, { hash: \\"#foo\\" });
Expand Down Expand Up @@ -104,7 +104,7 @@ exports[`loader should work with "resolve.roots": errors 1`] = `Array []`;
exports[`loader should work with "resolve.roots": module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"/image2.png\\", import.meta.url);
// Module
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
Expand Down Expand Up @@ -149,7 +149,7 @@ exports[`loader should work with server-relative url: errors 1`] = `Array []`;
exports[`loader should work with server-relative url: module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"/nested/image3.png\\", import.meta.url);
// Module
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
Expand All @@ -171,7 +171,7 @@ exports[`loader should work with webpackIgnore comment: errors 1`] = `Array []`;
exports[`loader should work with webpackIgnore comment: module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
// Module
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
Expand Down Expand Up @@ -305,7 +305,7 @@ exports[`loader should work: errors 1`] = `Array []`;
exports[`loader should work: module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);
Expand Down
14 changes: 7 additions & 7 deletions test/__snapshots__/minimize-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`"minimize" option should be turned off by default: errors 1`] = `Array

exports[`"minimize" option should be turned off by default: module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);
Expand Down Expand Up @@ -523,7 +523,7 @@ exports[`"minimize" option should be turned off in "development" mode: errors 1`
exports[`"minimize" option should be turned off in "development" mode: module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);
Expand Down Expand Up @@ -1042,7 +1042,7 @@ exports[`"minimize" option should be turned on in "production" mode: errors 1`]
exports[`"minimize" option should be turned on in "production" mode: module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);
Expand Down Expand Up @@ -1126,7 +1126,7 @@ Parse Error: < img src=\\"image.png\\" >",
exports[`"minimize" option should emit an error on broken HTML syntax: module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
// Module
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
Expand All @@ -1153,7 +1153,7 @@ exports[`"minimize" option should not work with a value equal to "false": errors
exports[`"minimize" option should not work with a value equal to "false": module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);
Expand Down Expand Up @@ -1672,7 +1672,7 @@ exports[`"minimize" option should support options for minimizer: errors 1`] = `A
exports[`"minimize" option should support options for minimizer: module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);
Expand Down Expand Up @@ -1766,7 +1766,7 @@ exports[`"minimize" option should work with a value equal to "true": errors 1`]
exports[`"minimize" option should work with a value equal to "true": module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"/image.png\\", import.meta.url);
var ___HTML_LOADER_IMPORT_2___ = new URL(\\"aliasImg\\", import.meta.url);
Expand Down
8 changes: 4 additions & 4 deletions test/__snapshots__/preprocessor-option.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`'process' option should work with Async "preprocessor" Function option:

exports[`'process' option should work with Async "preprocessor" Function option: module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
// Module
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
Expand All @@ -27,7 +27,7 @@ exports[`'process' option should work with the "preprocessor" option #2: errors
exports[`'process' option should work with the "preprocessor" option #2: module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png.webp\\", import.meta.url);
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"./image.png\\", import.meta.url);
// Module
Expand All @@ -49,7 +49,7 @@ exports[`'process' option should work with the "preprocessor" option: errors 1`]
exports[`'process' option should work with the "preprocessor" option: module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png\\", import.meta.url);
// Module
var ___HTML_LOADER_REPLACEMENT_0___ = ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___(___HTML_LOADER_IMPORT_0___);
Expand All @@ -72,7 +72,7 @@ exports[`'process' option should work with the Async "preprocessor" Function opt
exports[`'process' option should work with the Async "preprocessor" Function option #2: module 1`] = `
"// Imports
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"file:///<cwd>//src/runtime/getUrl.js\\";
import ___HTML_LOADER_GET_SOURCE_FROM_IMPORT___ from \\"../../src/runtime/getUrl.js\\";
var ___HTML_LOADER_IMPORT_0___ = new URL(\\"./image.png.webp\\", import.meta.url);
var ___HTML_LOADER_IMPORT_1___ = new URL(\\"./image.png\\", import.meta.url);
// Module
Expand Down
38 changes: 21 additions & 17 deletions test/__snapshots__/sources-option.test.js.snap

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion test/fixtures/sources.html
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,7 @@

<meta content="name=Check Order Status;
action-uri=./orderStatus.aspx?src=IE9;
icon-uri=" name="msapplication-task">
icon-uri=" name="msapplication-task">

<a href="#" class="gmail-link">[email protected]</a>
<a href="mailto:[email protected]" class="gmail-link">[email protected]</a>

0 comments on commit a309fe0

Please sign in to comment.