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 1 commit
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
13 changes: 8 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,12 @@ 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)
.groups.url;
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 +160,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=(?<url>[^\s'"`]+?)[ \t]*$/;
Copy link
Contributor Author

@JLHwung JLHwung Dec 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dogfooding named-capturing-regex here.

The file size is bloated by helpers (6678 B -> 11276 B) but I think it is okay since named-capturing-regex is supported on Node.js 10+, which means we can get rid of the helpers in v8.

I have changed my mind.


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