From 0b808f6428d53f4d4baa65b824aafc763d4160e2 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Fri, 8 Jul 2022 12:25:30 +0100 Subject: [PATCH] parse `import` expressions correctly (#5551) fixes #5550 --- lib/parse.js | 10 ++++------ test/compress/imports.js | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/lib/parse.js b/lib/parse.js index c832758f..3a71245d 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -858,13 +858,11 @@ function parse($TEXT, options) { next(); return export_(); case "import": - if (!toplevel && options.module !== "") unexpected(); var token = peek(); - if (!(token.type == "punc" && /^[(.]$/.test(token.value))) { - next(); - return import_(); - } - break; + if (token.type == "punc" && /^[(.]$/.test(token.value)) break; + if (!toplevel && options.module !== "") unexpected(); + next(); + return import_(); case "let": if (is_vardefs()) { next(); diff --git a/test/compress/imports.js b/test/compress/imports.js index c9b59e93..2f671633 100644 --- a/test/compress/imports.js +++ b/test/compress/imports.js @@ -227,3 +227,33 @@ issue_4708_2: { import a from "foo"; } } + +pr_5550_1: { + input: { + if (console) + import("foo"); + else + import.meta.url.replace(/bar/g, console.log); + } + expect: { + if (console) + import("foo"); + else + import.meta.url.replace(/bar/g, console.log); + } +} + +pr_5550_2: { + input: { + L: { + import("foo"); + import.meta.url.replace(/bar/g, console.log); + } + } + expect: { + L: { + import("foo"); + import.meta.url.replace(/bar/g, console.log); + } + } +}