Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from Dec 18, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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]*$/;
JLHwung marked this conversation as resolved.
Show resolved Hide resolved

function extractCommentsFromList(regex, comments, lastComment) {
if (comments) {
Expand Down