From 10d6189a72e745ade9710c506c1ab3e2ec6aeaad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 17 Dec 2019 16:22:41 -0500 Subject: [PATCH 1/3] fix: avoid string copy when processing input source-map --- .../babel-core/src/transformation/normalize-file.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/babel-core/src/transformation/normalize-file.js b/packages/babel-core/src/transformation/normalize-file.js index cc7db60a5478..deacee21238b 100644 --- a/packages/babel-core/src/transformation/normalize-file.js +++ b/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"; @@ -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); @@ -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=(?[^\s'"`]+?)[ \t]*$/; function extractCommentsFromList(regex, comments, lastComment) { if (comments) { From d4047f295399ddccf3cc1a5e7a2e4745f820595d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 17 Dec 2019 17:03:21 -0500 Subject: [PATCH 2/3] The helpers are so big, I have changed my mind --- packages/babel-core/src/transformation/normalize-file.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/babel-core/src/transformation/normalize-file.js b/packages/babel-core/src/transformation/normalize-file.js index deacee21238b..85308d1d327d 100644 --- a/packages/babel-core/src/transformation/normalize-file.js +++ b/packages/babel-core/src/transformation/normalize-file.js @@ -65,8 +65,9 @@ export default function normalizeFile( const lastComment = extractComments(EXTERNAL_SOURCEMAP_REGEX, ast); if (typeof options.filename === "string" && lastComment) { try { - const inputMapFilename = EXTERNAL_SOURCEMAP_REGEX.exec(lastComment) - .groups.url; + const inputMapFilename = EXTERNAL_SOURCEMAP_REGEX.exec( + lastComment, + )[1]; inputMap = convertSourceMap.fromJSON( fs.readFileSync( path.resolve(path.dirname(options.filename), inputMapFilename), @@ -160,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) { From d975bb3cc57a8644f9acc4228c920110d010a9db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Tue, 17 Dec 2019 20:25:44 -0500 Subject: [PATCH 3/3] Update packages/babel-core/src/transformation/normalize-file.js Co-Authored-By: Justin Ridgewell --- packages/babel-core/src/transformation/normalize-file.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/babel-core/src/transformation/normalize-file.js b/packages/babel-core/src/transformation/normalize-file.js index 85308d1d327d..4bae84306843 100644 --- a/packages/babel-core/src/transformation/normalize-file.js +++ b/packages/babel-core/src/transformation/normalize-file.js @@ -161,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) {