@alcalzone/esm2cjs
Advanced tools
Comparing version 1.2.3 to 1.3.0
@@ -13,3 +13,4 @@ import { BuildOptions } from "esbuild"; | ||
writePackageJson?: boolean; | ||
packageJsonSideEffects?: boolean | "inherit" | string[]; | ||
} | ||
export declare function esm2cjs({ inDir, outDir, globs, sourcemap, logLevel, platform, target, cleanOutDir, writePackageJson, }: ESM2CJSOptions): Promise<void>; | ||
export declare function esm2cjs({ inDir, outDir, globs, sourcemap, logLevel, platform, target, cleanOutDir, writePackageJson, packageJsonSideEffects, }: ESM2CJSOptions): Promise<void>; |
@@ -43,3 +43,3 @@ "use strict"; | ||
const shimsDir = import_node_path.default.join(_dirname, "../../shims"); | ||
async function esm2cjs({ inDir, outDir, globs = ["**/*.js"], sourcemap = true, logLevel = "warning", platform = "node", target = "node18", cleanOutDir = false, writePackageJson = true }) { | ||
async function esm2cjs({ inDir, outDir, globs = ["**/*.js"], sourcemap = true, logLevel = "warning", platform = "node", target = "node18", cleanOutDir = false, writePackageJson = true, packageJsonSideEffects = "inherit" }) { | ||
if (cleanOutDir) | ||
@@ -78,8 +78,23 @@ await import_fs_extra.default.emptyDir(outDir); | ||
if (writePackageJson) { | ||
await import_fs_extra.default.writeJSON(import_node_path.default.join(inDir, "package.json"), { type: "module" }, { | ||
spaces: 4 | ||
}); | ||
await import_fs_extra.default.writeJSON(import_node_path.default.join(outDir, "package.json"), { type: "commonjs" }, { | ||
spaces: 4 | ||
}); | ||
let inheritedSideEffects; | ||
if (packageJsonSideEffects === "inherit") { | ||
const parentPackageJson = await import_fs_extra.default.readJSON(import_node_path.default.join(process.cwd(), "package.json")).catch(() => void 0); | ||
if (parentPackageJson?.sideEffects != void 0) { | ||
inheritedSideEffects = { | ||
sideEffects: parentPackageJson.sideEffects | ||
}; | ||
} | ||
} | ||
const sideEffects = ( | ||
// Assume the package has side effects, unless explicitly stated otherwise | ||
packageJsonSideEffects === true || packageJsonSideEffects === void 0 ? {} : packageJsonSideEffects === "inherit" ? inheritedSideEffects ?? {} : { sideEffects: packageJsonSideEffects } | ||
); | ||
await import_fs_extra.default.writeJSON(import_node_path.default.join(inDir, "package.json"), { | ||
type: "module", | ||
...sideEffects | ||
}, { spaces: 4 }); | ||
await import_fs_extra.default.writeJSON(import_node_path.default.join(outDir, "package.json"), { | ||
type: "commonjs", | ||
...sideEffects | ||
}, { spaces: 4 }); | ||
} | ||
@@ -86,0 +101,0 @@ } |
@@ -13,3 +13,4 @@ import { BuildOptions } from "esbuild"; | ||
writePackageJson?: boolean; | ||
packageJsonSideEffects?: boolean | "inherit" | string[]; | ||
} | ||
export declare function esm2cjs({ inDir, outDir, globs, sourcemap, logLevel, platform, target, cleanOutDir, writePackageJson, }: ESM2CJSOptions): Promise<void>; | ||
export declare function esm2cjs({ inDir, outDir, globs, sourcemap, logLevel, platform, target, cleanOutDir, writePackageJson, packageJsonSideEffects, }: ESM2CJSOptions): Promise<void>; |
@@ -8,3 +8,3 @@ import path from "node:path"; | ||
export const shimsDir = path.join(_dirname, "../../shims"); | ||
export async function esm2cjs({ inDir, outDir, globs = ["**/*.js"], sourcemap = true, logLevel = "warning", platform = "node", target = "node18", cleanOutDir = false, writePackageJson = true, }) { | ||
export async function esm2cjs({ inDir, outDir, globs = ["**/*.js"], sourcemap = true, logLevel = "warning", platform = "node", target = "node18", cleanOutDir = false, writePackageJson = true, packageJsonSideEffects = "inherit", }) { | ||
// Clean the output dir if necessary | ||
@@ -51,10 +51,31 @@ if (cleanOutDir) | ||
if (writePackageJson) { | ||
await fs.writeJSON(path.join(inDir, "package.json"), { type: "module" }, { | ||
spaces: 4, | ||
}); | ||
await fs.writeJSON(path.join(outDir, "package.json"), { type: "commonjs" }, { | ||
spaces: 4, | ||
}); | ||
let inheritedSideEffects; | ||
if (packageJsonSideEffects === "inherit") { | ||
const parentPackageJson = await fs | ||
.readJSON(path.join(process.cwd(), "package.json")) | ||
.catch(() => undefined); | ||
if (parentPackageJson?.sideEffects != undefined) { | ||
inheritedSideEffects = { | ||
sideEffects: parentPackageJson.sideEffects, | ||
}; | ||
} | ||
} | ||
const sideEffects = | ||
// Assume the package has side effects, unless explicitly stated otherwise | ||
packageJsonSideEffects === true || | ||
packageJsonSideEffects === undefined | ||
? {} | ||
: packageJsonSideEffects === "inherit" | ||
? inheritedSideEffects ?? {} | ||
: { sideEffects: packageJsonSideEffects }; | ||
await fs.writeJSON(path.join(inDir, "package.json"), { | ||
type: "module", | ||
...sideEffects, | ||
}, { spaces: 4 }); | ||
await fs.writeJSON(path.join(outDir, "package.json"), { | ||
type: "commonjs", | ||
...sideEffects, | ||
}, { spaces: 4 }); | ||
} | ||
} | ||
//# sourceMappingURL=esm2cjs.js.map |
@@ -7,3 +7,3 @@ { | ||
"description": "Command line utility to compile a JS project from ES modules to CommonJS", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"bin": "bin/esm2cjs.cjs", | ||
@@ -10,0 +10,0 @@ "type": "module", |
@@ -153,2 +153,5 @@ # esm2cjs | ||
--> | ||
### 1.3.0 (2024-11-06) | ||
* Support specifying `sideEffects` in the generated `package.json` as a hint for bundlers | ||
### 1.2.3 (2024-11-04) | ||
@@ -155,0 +158,0 @@ * Update default target to node18 |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
29589
343
173
0