Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for babel.config.mjs and .babelrc.mjs
- Loading branch information
1 parent
c762cdd
commit fb4d8b7
Showing
10 changed files
with
222 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// We keep this in a seprate file so that in older node versions, where | ||
// import() isn't supported, we can try/catch around the require() call | ||
// when loading this file. | ||
|
||
export default function import_(filepath: string) { | ||
return import(filepath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { isAsync, waitFor } from "../../gensync-utils/async"; | ||
import type { Handler } from "gensync"; | ||
|
||
let import_; | ||
try { | ||
// Node < 13.3 doesn't support import() syntax. | ||
import_ = require("./import").default; | ||
} catch {} | ||
|
||
export default function* loadCjsOrMjsDefault( | ||
filepath: string, | ||
asyncError: string, | ||
): Handler<mixed> { | ||
switch (guessJSModuleType(filepath)) { | ||
case "cjs": | ||
return loadCjsDefault(filepath); | ||
case "unknown": | ||
try { | ||
return loadCjsDefault(filepath); | ||
} catch (e) { | ||
if (e.code !== "ERR_REQUIRE_ESM") throw e; | ||
} | ||
case "mjs": | ||
if (yield* isAsync()) { | ||
return yield* waitFor(loadMjsDefault(filepath)); | ||
} | ||
throw new Error(asyncError); | ||
} | ||
} | ||
|
||
function guessJSModuleType(path: string): "cjs" | "mjs" | "unknown" { | ||
switch (path.slice(-4)) { | ||
case ".cjs": | ||
return "cjs"; | ||
case ".mjs": | ||
return "mjs"; | ||
default: | ||
return "unknown"; | ||
} | ||
} | ||
|
||
function loadCjsDefault(filepath: string) { | ||
const module = (require(filepath): mixed); | ||
return module?.__esModule ? module.default ?? undefined : module; | ||
} | ||
|
||
async function loadMjsDefault(filepath: string) { | ||
if (!import_) { | ||
throw new Error( | ||
"Internal error: Native ECMAScript modules aren't supported" + | ||
" by this platform.\n", | ||
); | ||
} | ||
|
||
const module = await import_(filepath); | ||
return module.default; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/babel-core/test/fixtures/config/config-files-templates/.babelrc.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// Until Jest supports native mjs, we must simulate it 馃し | ||
|
||
module.exports = new Promise(resolve => resolve({ | ||
default: { | ||
comments: true | ||
} | ||
})); | ||
module.exports.__esModule = true; |
Oops, something went wrong.