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

Set "type": "commonjs" to internally track CJS vs ESM #15665

Merged
merged 1 commit into from
May 29, 2023
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 Makefile.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Makefile.source.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ target["clean-runtime-helpers"] = function () {
*/

target["use-cjs"] = function () {
node(["scripts/set-module-type.js", "script"]);
node(["scripts/set-module-type.js", "commonjs"]);

target["bootstrap"]();
};
Expand Down
8 changes: 4 additions & 4 deletions babel.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ module.exports = function (api) {

const outputType = api.cache.invalidate(() => {
try {
return fs.readFileSync(__dirname + "/.module-type", "utf-8").trim();
} catch (_) {
return "script";
}
const type = fs.readFileSync(__dirname + "/.module-type", "utf-8").trim();
if (type === "module") return type;
} catch (_) {}
return "script";
});

const sources = ["packages/*/src", "codemods/*/src", "eslint/*/src"];
Expand Down
15 changes: 7 additions & 8 deletions packages/babel-helpers/scripts/generate-regenerator-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import { createRequire } from "module";
const [parse, generate] = await Promise.all([
import("@babel/parser").then(ns => ns.parse),
import("@babel/generator").then(ns => ns.default.default || ns.default),
]).catch(error =>
Promise.reject(
new Error(
"Before running generate-helpers.js you must compile @babel/parser and @babel/generator.",
{ cause: error }
)
)
);
]).catch(error => {
console.error(error);
throw new Error(
"Before running generate-helpers.js you must compile @babel/parser and @babel/generator.",
{ cause: error }
);
});

const REGENERATOR_RUNTIME_IN_FILE = fs.readFileSync(
createRequire(import.meta.url).resolve("regenerator-runtime"),
Expand Down
4 changes: 2 additions & 2 deletions scripts/set-module-type.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ if (process.argv.length >= 3) {
} else if (fs.existsSync(root(".module-type"))) {
moduleType = fs.readFileSync(root(".module-type"), "utf-8").trim();
} else {
moduleType = "script";
moduleType = "commonjs";
}

if (moduleType === "clean") {
try {
fs.unlinkSync(root(".module-type"));
} catch {}
} else if (moduleType === "module" || moduleType === "script") {
} else if (moduleType === "module" || moduleType === "commonjs") {
fs.writeFileSync(root(".module-type"), moduleType);
} else {
throw new Error(`Unknown module type: ${moduleType}`);
Expand Down