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: skip merging large input sourcemaps #10890

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion lib/third-party-libs.js.flow
Expand Up @@ -178,7 +178,7 @@ declare module "convert-source-map" {
SourceMap: SourceMap,
Converter: Converter,
fromObject(obj: SourceMap | SourceMapGenerator): Converter,
fromJSON(str: string): Converter,
fromJSON(str: string | Buffer): Converter,
fromBase64(str: string): Converter,
fromComment(str: string): Converter,
fromMapFileComment(str: string, dir: string): Converter,
Expand Down
19 changes: 12 additions & 7 deletions packages/babel-core/src/transformation/normalize-file.js
Expand Up @@ -13,6 +13,7 @@ import File from "./file/file";
import generateMissingPluginMessage from "./util/missing-plugin-helper";

const debug = buildDebug("babel:transform:file");
const LARGE_INPUT_SOURCEMAP_THRESHOLD = 1_000_000;

export type NormalizedFile = {
code: string,
Expand Down Expand Up @@ -65,14 +66,18 @@ export default function normalizeFile(
const lastComment = extractComments(EXTERNAL_SOURCEMAP_REGEX, ast);
if (typeof options.filename === "string" && lastComment) {
try {
const match = EXTERNAL_SOURCEMAP_REGEX.exec(lastComment);
if (!match) throw new Error("Invalid source map comment format.");
inputMap = convertSourceMap.fromJSON(
fs.readFileSync(
path.resolve(path.dirname(options.filename), match[1]),
"utf8",
),
// when `lastComment` is non-null, EXTERNAL_SOURCEMAP_REGEX must have matches
const match: [string, string] = (EXTERNAL_SOURCEMAP_REGEX.exec(
lastComment,
): any);
Copy link
Member

Choose a reason for hiding this comment

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

Is this guaranteed to never return null?

Copy link
Contributor Author

@JLHwung JLHwung Dec 18, 2019

Choose a reason for hiding this comment

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

Yes because the capturing group

([^\s'"`]+)

has a + quantifier, if the whole regex passed, the capturing group must not be null.

const inputMapContent: Buffer = fs.readFileSync(
path.resolve(path.dirname(options.filename), match[1]),
);
if (inputMapContent.length > LARGE_INPUT_SOURCEMAP_THRESHOLD) {
debug("skip merging input map > 1 MB");
} else {
inputMap = convertSourceMap.fromJSON(inputMapContent);
}
} catch (err) {
debug("discarding unknown file input sourcemap", err);
}
Expand Down