fix: avoid string copy when processing input source-map (#10885)

Co-Authored-By: Justin Ridgewell <justin@ridgewell.name>
This commit is contained in:
Huáng Jùnliàng 2019-12-17 22:12:55 -05:00 committed by Brian Ng
parent ff8a295ea7
commit b3c7df9314

View File

@ -1,5 +1,6 @@
// @flow // @flow
import fs from "fs";
import path from "path"; import path from "path";
import buildDebug from "debug"; import buildDebug from "debug";
import cloneDeep from "lodash/cloneDeep"; import cloneDeep from "lodash/cloneDeep";
@ -64,10 +65,13 @@ export default function normalizeFile(
const lastComment = extractComments(EXTERNAL_SOURCEMAP_REGEX, ast); const lastComment = extractComments(EXTERNAL_SOURCEMAP_REGEX, ast);
if (typeof options.filename === "string" && lastComment) { if (typeof options.filename === "string" && lastComment) {
try { try {
inputMap = convertSourceMap.fromMapFileComment( const inputMapFilename = EXTERNAL_SOURCEMAP_REGEX.exec(
// fromMapFileComment requires the whole comment block lastComment,
`//${lastComment}`, )[1];
path.dirname(options.filename), inputMap = convertSourceMap.fromJSON(
fs.readFileSync(
path.resolve(path.dirname(options.filename), inputMapFilename),
),
); );
} catch (err) { } catch (err) {
debug("discarding unknown file input sourcemap", err); debug("discarding unknown file input sourcemap", err);
@ -157,7 +161,7 @@ function parser(
// eslint-disable-next-line max-len // eslint-disable-next-line max-len
const INLINE_SOURCEMAP_REGEX = /^[@#]\s+sourceMappingURL=data:(?:application|text)\/json;(?:charset[:=]\S+?;)?base64,(?:.*)$/; const INLINE_SOURCEMAP_REGEX = /^[@#]\s+sourceMappingURL=data:(?:application|text)\/json;(?:charset[:=]\S+?;)?base64,(?:.*)$/;
const EXTERNAL_SOURCEMAP_REGEX = /^[@#][ \t]+sourceMappingURL=(?:[^\s'"`]+?)[ \t]*$/; const EXTERNAL_SOURCEMAP_REGEX = /^[@#][ \t]+sourceMappingURL=([^\s'"`]+)[ \t]*$/;
function extractCommentsFromList(regex, comments, lastComment) { function extractCommentsFromList(regex, comments, lastComment) {
if (comments) { if (comments) {