Rename .mjs files to .js (#14605)
This commit is contained in:
1 parent
47f4e59fc0
commit
186a177017
41 files changed
+39
-44
No files matched your search
@@ -0,0 +1,38 @@
|
||||
import createEsmUtils from "esm-utils";
|
||||
import serialize from "serialize-javascript";
|
||||
import { isValidIdentifier } from "@babel/types";
|
||||
|
||||
const { importModule } = createEsmUtils(import.meta);
|
||||
|
||||
export default function esbuildPluginEvaluate() {
|
||||
return {
|
||||
name: "evaluate",
|
||||
setup(build) {
|
||||
const { format } = build.initialOptions;
|
||||
|
||||
build.onLoad({ filter: /\.evaluate\.c?js$/ }, async ({ path }) => {
|
||||
const module = await importModule(path);
|
||||
const text = Object.entries(module)
|
||||
.map(([specifier, value]) => {
|
||||
value = serialize(value, { space: 2 });
|
||||
|
||||
if (specifier === "default") {
|
||||
return format === "cjs"
|
||||
? `module.exports = ${value};`
|
||||
: `export default ${value};`;
|
||||
}
|
||||
|
||||
if (!isValidIdentifier(specifier)) {
|
||||
throw new Error(`${specifier} is not a valid specifier`);
|
||||
}
|
||||
|
||||
return format === "cjs"
|
||||
? `exports.${specifier} = ${value};`
|
||||
: `export const ${specifier} = ${value};`;
|
||||
})
|
||||
.join("\n");
|
||||
return { contents: text };
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user