Skip to content

Commit

Permalink
fix: avoid string copy when processing input source-map (#10885)
Browse files Browse the repository at this point in the history
Co-Authored-By: Justin Ridgewell <justin@ridgewell.name>
  • Loading branch information
2 people authored and existentialism committed Dec 18, 2019
1 parent ff8a295 commit b3c7df9
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/babel-core/src/transformation/normalize-file.js
@@ -1,5 +1,6 @@
// @flow

import fs from "fs";
import path from "path";
import buildDebug from "debug";
import cloneDeep from "lodash/cloneDeep";
Expand Down Expand Up @@ -64,10 +65,13 @@ export default function normalizeFile(
const lastComment = extractComments(EXTERNAL_SOURCEMAP_REGEX, ast);
if (typeof options.filename === "string" && lastComment) {
try {
inputMap = convertSourceMap.fromMapFileComment(
// fromMapFileComment requires the whole comment block
`//${lastComment}`,
path.dirname(options.filename),
const inputMapFilename = EXTERNAL_SOURCEMAP_REGEX.exec(
lastComment,
)[1];
inputMap = convertSourceMap.fromJSON(
fs.readFileSync(
path.resolve(path.dirname(options.filename), inputMapFilename),
),
);
} catch (err) {
debug("discarding unknown file input sourcemap", err);
Expand Down Expand Up @@ -157,7 +161,7 @@ function parser(

// eslint-disable-next-line max-len
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) {
if (comments) {
Expand Down

0 comments on commit b3c7df9

Please sign in to comment.