source-map-loader
Advanced tools
Comparing version
@@ -10,6 +10,2 @@ "use strict"; | ||
var _loaderUtils = require("loader-utils"); | ||
var _schemaUtils = require("schema-utils"); | ||
var _options = _interopRequireDefault(require("./options.json")); | ||
@@ -26,7 +22,3 @@ | ||
async function loader(input, inputMap) { | ||
const options = (0, _loaderUtils.getOptions)(this); | ||
(0, _schemaUtils.validate)(_options.default, options, { | ||
name: "Source Map Loader", | ||
baseDataPath: "options" | ||
}); | ||
const options = this.getOptions(_options.default); | ||
const { | ||
@@ -104,3 +96,3 @@ sourceMappingURL, | ||
let sourceContent; | ||
const originalSourceContent = map.sourcesContent && typeof map.sourcesContent[i] !== "undefined" ? map.sourcesContent[i] : // eslint-disable-next-line no-undefined | ||
const originalSourceContent = map.sourcesContent && typeof map.sourcesContent[i] !== "undefined" && map.sourcesContent[i] !== null ? map.sourcesContent[i] : // eslint-disable-next-line no-undefined | ||
undefined; | ||
@@ -124,3 +116,3 @@ const skipReading = typeof originalSourceContent !== "undefined"; | ||
sourceContent = originalSourceContent; | ||
} else if (!errored && sourceURL) { | ||
} else if (!errored && sourceURL && !(0, _utils.isURL)(sourceURL)) { | ||
this.addDependency(sourceURL); | ||
@@ -127,0 +119,0 @@ } // Return original value of `source` when error happens |
{ | ||
"title": "Source Map Loader options", | ||
"type": "object", | ||
@@ -3,0 +4,0 @@ "additionalProperties": false, |
@@ -8,8 +8,53 @@ "use strict"; | ||
var _whatwgMimetype = _interopRequireDefault(require("whatwg-mimetype")); | ||
var _abab = require("abab"); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
const removeLeadingAndTrailingHTTPWhitespace = string => string.replace(/^[ \t\n\r]+/, "").replace(/[ \t\n\r]+$/, ""); | ||
const removeTrailingHTTPWhitespace = string => string.replace(/[ \t\n\r]+$/, ""); | ||
const isHTTPWhitespaceChar = char => char === " " || char === "\t" || char === "\n" || char === "\r"; | ||
const solelyContainsHTTPTokenCodePoints = string => /^[-!#$%&'*+.^_`|~A-Za-z0-9]*$/.test(string); | ||
const soleyContainsHTTPQuotedStringTokenCodePoints = string => /^[\t\u0020-\u007E\u0080-\u00FF]*$/.test(string); | ||
const asciiLowercase = string => string.replace(/[A-Z]/g, l => l.toLowerCase()); | ||
const collectAnHTTPQuotedString = (input, position) => { | ||
let value = ""; // eslint-disable-next-line no-param-reassign | ||
position += 1; // eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
while (position < input.length && input[position] !== '"' && input[position] !== "\\") { | ||
value += input[position]; // eslint-disable-next-line no-param-reassign | ||
position += 1; | ||
} | ||
if (position >= input.length) { | ||
break; | ||
} | ||
const quoteOrBackslash = input[position]; // eslint-disable-next-line no-param-reassign | ||
position += 1; | ||
if (quoteOrBackslash === "\\") { | ||
if (position >= input.length) { | ||
value += "\\"; | ||
break; | ||
} | ||
value += input[position]; // eslint-disable-next-line no-param-reassign | ||
position += 1; | ||
} else { | ||
break; | ||
} | ||
} | ||
return [value, position]; | ||
}; | ||
function isASCIIHex(c) { | ||
@@ -58,10 +103,10 @@ return c >= 0x30 && c <= 0x39 || c >= 0x41 && c <= 0x46 || c >= 0x61 && c <= 0x66; | ||
let position = 0; | ||
let mimeType = ""; | ||
let mediaType = ""; | ||
while (position < input.length && input[position] !== ",") { | ||
mimeType += input[position]; | ||
mediaType += input[position]; | ||
position += 1; | ||
} | ||
mimeType = mimeType.replace(/^[ \t\n\f\r]+/, "").replace(/[ \t\n\f\r]+$/, ""); | ||
mediaType = mediaType.replace(/^[ \t\n\f\r]+/, "").replace(/[ \t\n\f\r]+$/, ""); | ||
@@ -76,3 +121,3 @@ if (position === input.length) { | ||
const mimeTypeBase64MatchResult = /(.*); *[Bb][Aa][Ss][Ee]64$/.exec(mimeType); | ||
const mimeTypeBase64MatchResult = /(.*); *[Bb][Aa][Ss][Ee]64$/.exec(mediaType); | ||
@@ -88,21 +133,113 @@ if (mimeTypeBase64MatchResult) { | ||
body = Buffer.from(asString, "binary"); | ||
[, mimeType] = mimeTypeBase64MatchResult; | ||
[, mediaType] = mimeTypeBase64MatchResult; | ||
} | ||
if (mimeType.startsWith(";")) { | ||
mimeType = `text/plain ${mimeType}`; | ||
if (mediaType.startsWith(";")) { | ||
mediaType = `text/plain ${mediaType}`; | ||
} | ||
let mimeTypeRecord; | ||
const result = { | ||
// eslint-disable-next-line no-undefined | ||
type: undefined, | ||
// eslint-disable-next-line no-undefined | ||
subtype: undefined, | ||
parameters: new Map(), | ||
isBase64: Boolean(mimeTypeBase64MatchResult), | ||
body | ||
}; | ||
try { | ||
mimeTypeRecord = new _whatwgMimetype.default(mimeType); | ||
} catch (e) { | ||
mimeTypeRecord = new _whatwgMimetype.default("text/plain;charset=US-ASCII"); | ||
if (!mediaType) { | ||
return result; | ||
} | ||
return { | ||
mimeType: mimeTypeRecord, | ||
body | ||
}; | ||
const inputMediaType = removeLeadingAndTrailingHTTPWhitespace(mediaType); | ||
let positionMediaType = 0; | ||
let type = ""; | ||
while (positionMediaType < inputMediaType.length && inputMediaType[positionMediaType] !== "/") { | ||
type += inputMediaType[positionMediaType]; | ||
positionMediaType += 1; | ||
} | ||
if (type.length === 0 || !solelyContainsHTTPTokenCodePoints(type)) { | ||
return result; | ||
} | ||
if (positionMediaType >= inputMediaType.length) { | ||
return result; | ||
} // Skips past "/" | ||
positionMediaType += 1; | ||
let subtype = ""; | ||
while (positionMediaType < inputMediaType.length && inputMediaType[positionMediaType] !== ";") { | ||
subtype += inputMediaType[positionMediaType]; | ||
positionMediaType += 1; | ||
} | ||
subtype = removeTrailingHTTPWhitespace(subtype); | ||
if (subtype.length === 0 || !solelyContainsHTTPTokenCodePoints(subtype)) { | ||
return result; | ||
} | ||
result.type = asciiLowercase(type); | ||
result.subtype = asciiLowercase(subtype); | ||
while (positionMediaType < inputMediaType.length) { | ||
// Skip past ";" | ||
positionMediaType += 1; | ||
while (isHTTPWhitespaceChar(inputMediaType[positionMediaType])) { | ||
positionMediaType += 1; | ||
} | ||
let parameterName = ""; | ||
while (positionMediaType < inputMediaType.length && inputMediaType[positionMediaType] !== ";" && inputMediaType[positionMediaType] !== "=") { | ||
parameterName += inputMediaType[positionMediaType]; | ||
positionMediaType += 1; | ||
} | ||
parameterName = asciiLowercase(parameterName); | ||
if (positionMediaType < inputMediaType.length) { | ||
if (inputMediaType[positionMediaType] === ";") { | ||
// eslint-disable-next-line no-continue | ||
continue; | ||
} // Skip past "=" | ||
positionMediaType += 1; | ||
} | ||
let parameterValue = ""; | ||
if (inputMediaType[positionMediaType] === '"') { | ||
[parameterValue, positionMediaType] = collectAnHTTPQuotedString(inputMediaType, positionMediaType); | ||
while (positionMediaType < inputMediaType.length && inputMediaType[positionMediaType] !== ";") { | ||
positionMediaType += 1; | ||
} | ||
} else { | ||
while (positionMediaType < inputMediaType.length && inputMediaType[positionMediaType] !== ";") { | ||
parameterValue += inputMediaType[positionMediaType]; | ||
positionMediaType += 1; | ||
} | ||
parameterValue = removeTrailingHTTPWhitespace(parameterValue); | ||
if (parameterValue === "") { | ||
// eslint-disable-next-line no-continue | ||
continue; | ||
} | ||
} | ||
if (parameterName.length > 0 && solelyContainsHTTPTokenCodePoints(parameterName) && soleyContainsHTTPQuotedStringTokenCodePoints(parameterValue) && !result.parameters.has(parameterName)) { | ||
result.parameters.set(parameterName, parameterValue); | ||
} | ||
} | ||
return result; | ||
} |
@@ -6,5 +6,6 @@ "use strict"; | ||
}); | ||
exports.getSourceMappingURL = getSourceMappingURL; | ||
exports.fetchFromURL = fetchFromURL; | ||
exports.flattenSourceMap = flattenSourceMap; | ||
exports.getSourceMappingURL = getSourceMappingURL; | ||
exports.isURL = isURL; | ||
@@ -15,8 +16,6 @@ var _path = _interopRequireDefault(require("path")); | ||
var _sourceMap = _interopRequireDefault(require("source-map")); | ||
var _sourceMapJs = _interopRequireDefault(require("source-map-js")); | ||
var _iconvLite = require("iconv-lite"); | ||
var _loaderUtils = require("loader-utils"); | ||
var _parseDataUrl = _interopRequireDefault(require("./parse-data-url")); | ||
@@ -41,6 +40,6 @@ | ||
async function flattenSourceMap(map) { | ||
const consumer = await new _sourceMap.default.SourceMapConsumer(map); | ||
const generatedMap = map.file ? new _sourceMap.default.SourceMapGenerator({ | ||
const consumer = await new _sourceMapJs.default.SourceMapConsumer(map); | ||
const generatedMap = map.file ? new _sourceMapJs.default.SourceMapGenerator({ | ||
file: map.file | ||
}) : new _sourceMap.default.SourceMapGenerator(); | ||
}) : new _sourceMapJs.default.SourceMapGenerator(); | ||
consumer.sources.forEach(sourceFile => { | ||
@@ -95,4 +94,6 @@ const sourceContent = consumer.sourceContentFor(sourceFile, true); | ||
function getAbsolutePath(context, url, sourceRoot) { | ||
const request = (0, _loaderUtils.urlToRequest)(url, true); | ||
function getAbsolutePath(context, request, sourceRoot) { | ||
if (isURL(sourceRoot)) { | ||
return new URL(request, sourceRoot).toString(); | ||
} | ||
@@ -104,3 +105,3 @@ if (sourceRoot) { | ||
return _path.default.join(context, (0, _loaderUtils.urlToRequest)(sourceRoot, true), request); | ||
return _path.default.join(context, sourceRoot, request); | ||
} | ||
@@ -115,4 +116,6 @@ | ||
if (dataURL) { | ||
dataURL.encodingName = labelToName(dataURL.mimeType.parameters.get("charset")) || "UTF-8"; | ||
return (0, _iconvLite.decode)(dataURL.body, dataURL.encodingName); | ||
// https://tools.ietf.org/html/rfc4627 | ||
// JSON text SHALL be encoded in Unicode. The default encoding is UTF-8. | ||
const encodingName = labelToName(dataURL.parameters.get("charset")) || "UTF-8"; | ||
return (0, _iconvLite.decode)(dataURL.body, encodingName); | ||
} | ||
@@ -126,2 +129,8 @@ | ||
if (isURL(sourceURL)) { | ||
return { | ||
path: sourceURL | ||
}; | ||
} | ||
try { | ||
@@ -141,3 +150,6 @@ buffer = await new Promise((resolve, reject) => { | ||
return buffer.toString(); | ||
return { | ||
path: sourceURL, | ||
data: buffer.toString() | ||
}; | ||
} | ||
@@ -166,5 +178,9 @@ | ||
function isURL(value) { | ||
return /^[a-z][a-z0-9+.-]*:/i.test(value) && !_path.default.win32.isAbsolute(value); | ||
} | ||
async function fetchFromURL(loaderContext, context, url, sourceRoot, skipReading = false) { | ||
// 1. It's an absolute url and it is not `windows` path like `C:\dir\file` | ||
if (/^[a-z][a-z0-9+.-]*:/i.test(url) && !_path.default.win32.isAbsolute(url)) { | ||
if (isURL(url)) { | ||
const { | ||
@@ -199,3 +215,5 @@ protocol | ||
const sourceContent = await fetchFromFilesystem(loaderContext, sourceURL); | ||
const { | ||
data: sourceContent | ||
} = await fetchFromFilesystem(loaderContext, sourceURL); | ||
return { | ||
@@ -217,3 +235,3 @@ sourceURL, | ||
if (_path.default.isAbsolute(url)) { | ||
const sourceURL = _path.default.normalize(url); | ||
let sourceURL = _path.default.normalize(url); | ||
@@ -229,3 +247,5 @@ let sourceContent; | ||
sourceContent = await fetchPathsFromFilesystem(loaderContext, possibleRequests); | ||
const result = await fetchPathsFromFilesystem(loaderContext, possibleRequests); | ||
sourceURL = result.path; | ||
sourceContent = result.data; | ||
} | ||
@@ -244,3 +264,6 @@ | ||
if (!skipReading) { | ||
sourceContent = await fetchFromFilesystem(loaderContext, sourceURL); | ||
const { | ||
data | ||
} = await fetchFromFilesystem(loaderContext, sourceURL); | ||
sourceContent = data; | ||
} | ||
@@ -247,0 +270,0 @@ |
{ | ||
"name": "source-map-loader", | ||
"version": "1.1.3", | ||
"version": "4.0.1", | ||
"description": "extracts inlined source map and offers it to webpack", | ||
@@ -16,3 +16,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">= 10.13.0" | ||
"node": ">= 14.15.0" | ||
}, | ||
@@ -34,5 +34,4 @@ "scripts": { | ||
"test": "npm run test:coverage", | ||
"prepare": "npm run build", | ||
"release": "standard-version", | ||
"defaults": "webpack-defaults" | ||
"prepare": "husky install && npm run build", | ||
"release": "standard-version" | ||
}, | ||
@@ -43,35 +42,31 @@ "files": [ | ||
"peerDependencies": { | ||
"webpack": "^4.0.0 || ^5.0.0" | ||
"webpack": "^5.72.1" | ||
}, | ||
"dependencies": { | ||
"abab": "^2.0.5", | ||
"iconv-lite": "^0.6.2", | ||
"loader-utils": "^2.0.0", | ||
"schema-utils": "^3.0.0", | ||
"source-map": "^0.6.1", | ||
"whatwg-mimetype": "^2.3.0" | ||
"abab": "^2.0.6", | ||
"iconv-lite": "^0.6.3", | ||
"source-map-js": "^1.0.2" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.12.8", | ||
"@babel/core": "^7.12.9", | ||
"@babel/preset-env": "^7.12.7", | ||
"@commitlint/cli": "^11.0.0", | ||
"@commitlint/config-conventional": "^11.0.0", | ||
"@webpack-contrib/defaults": "^6.3.0", | ||
"@babel/cli": "^7.19.3", | ||
"@babel/core": "^7.19.3", | ||
"@babel/preset-env": "^7.19.3", | ||
"@commitlint/cli": "^16.3.0", | ||
"@commitlint/config-conventional": "^16.2.4", | ||
"@webpack-contrib/eslint-config-webpack": "^3.0.0", | ||
"babel-jest": "^26.6.3", | ||
"babel-jest": "^29.1.2", | ||
"cross-env": "^7.0.3", | ||
"del": "^6.0.0", | ||
"del-cli": "^3.0.1", | ||
"eslint": "^7.14.0", | ||
"eslint-config-prettier": "^6.15.0", | ||
"eslint-plugin-import": "^2.22.1", | ||
"husky": "^4.3.0", | ||
"jest": "^26.6.3", | ||
"lint-staged": "^10.5.2", | ||
"memfs": "^3.2.0", | ||
"del-cli": "^4.0.1", | ||
"eslint": "^8.24.0", | ||
"eslint-config-prettier": "^8.5.0", | ||
"eslint-plugin-import": "^2.26.0", | ||
"husky": "^8.0.1", | ||
"jest": "^29.1.2", | ||
"lint-staged": "^12.4.1", | ||
"memfs": "^3.4.7", | ||
"npm-run-all": "^4.1.5", | ||
"prettier": "^2.2.1", | ||
"standard-version": "^9.0.0", | ||
"webpack": "^5.9.0" | ||
"prettier": "^2.7.1", | ||
"standard-version": "^9.5.0", | ||
"webpack": "^5.74.0" | ||
}, | ||
@@ -78,0 +73,0 @@ "keywords": [ |
@@ -9,3 +9,2 @@ <div align="center"> | ||
[![node][node]][node-url] | ||
[![deps][deps]][deps-url] | ||
[![tests][tests]][tests-url] | ||
@@ -24,6 +23,18 @@ [![coverage][cover]][cover-url] | ||
```bash | ||
```console | ||
npm i -D source-map-loader | ||
``` | ||
or | ||
```console | ||
yarn add -D source-map-loader | ||
``` | ||
or | ||
```console | ||
pnpm add -D source-map-loader | ||
``` | ||
Then add the plugin to your `webpack` config. For example: | ||
@@ -156,4 +167,2 @@ | ||
[node-url]: https://nodejs.org | ||
[deps]: https://david-dm.org/webpack-contrib/source-map-loader.svg | ||
[deps-url]: https://david-dm.org/webpack-contrib/source-map-loader | ||
[tests]: https://github.com/webpack-contrib/source-map-loader/workflows/source-map-loader/badge.svg | ||
@@ -160,0 +169,0 @@ [tests-url]: https://github.com/webpack-contrib/source-map-loader/actions |
4
-42.86%21
-4.55%721
18.98%173
5.49%32008
-4.29%9
-10%+ Added
+ Added
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
- Removed
Updated
Updated