Skip to content

Commit

Permalink
Merge pull request #14687 from webpack/bugfix/esm-single-quote
Browse files Browse the repository at this point in the history
fix using single quotes with esm files
  • Loading branch information
sokra committed Nov 9, 2021
2 parents 3aed1fe + 4edd043 commit 3c17f90
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/FileSystemInfo.js
Expand Up @@ -692,6 +692,11 @@ class SnapshotOptimization {
}
}

const parseString = str => {
if (str[0] === "'") str = `"${str.slice(1, -1).replace(/"/g, '\\"')}"`;
return JSON.parse(str);
};

/* istanbul ignore next */
/**
* @param {number} mtime mtime
Expand Down Expand Up @@ -1657,17 +1662,13 @@ class FileSystemInfo {
let dependency;
if (imp.d === -1) {
// import ... from "..."
dependency = JSON.parse(
dependency = parseString(
source.substring(imp.s - 1, imp.e + 1)
);
} else if (imp.d > -1) {
// import()
let expr = source.substring(imp.s, imp.e).trim();
if (expr[0] === "'")
expr = `"${expr
.slice(1, -1)
.replace(/"/g, '\\"')}"`;
dependency = JSON.parse(expr);
dependency = parseString(expr);
} else {
// e.g. import.meta
continue;
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/buildDependencies/esm.mjs
@@ -1,5 +1,9 @@
export { default } from "./esm-dep.mjs";
// prettier-ignore
import './esm-dep.mjs';

export const asyncDep = (
await import("../../js/buildDepsInput/esm-async-dependency.mjs")
).default;
// prettier-ignore
await import('../../js/buildDepsInput/esm-async-dependency.mjs')

0 comments on commit 3c17f90

Please sign in to comment.