Skip to content

Commit 4d98c18

Browse files
committed
Fix support for esm
1 parent f2e3ea5 commit 4d98c18

File tree

5 files changed

+46
-35
lines changed

5 files changed

+46
-35
lines changed

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,23 @@
22
"name": "deep-object-diff",
33
"version": "1.2.0",
44
"description": "Deep diffs two objects, including nested structures of arrays and objects, and return the difference.",
5-
"main": "./dist/cjs/index.js",
6-
"module": "./dist/esm/index.js",
5+
"main": "./dist/cjs/index.cjs",
6+
"module": "./dist/mjs/index.mjs",
7+
"type": "module",
78
"exports": {
89
".": {
910
"import": {
10-
"types": "./dist/esm/index.d.ts",
11-
"default": "./dist/esm/index.js"
11+
"types": "./dist/mjs/index.d.mts",
12+
"default": "./dist/mjs/index.mjs"
1213
},
1314
"require": {
14-
"types": "./dist/cjs/index.d.ts",
15-
"default": "./dist/cjs/index.js"
15+
"types": "./dist/cjs/index.d.cts",
16+
"default": "./dist/cjs/index.cjs"
1617
}
1718
}
1819
},
1920
"scripts": {
20-
"build": "rm -rf dist && tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json",
21+
"build": "rm -rf dist && tsc -p tsconfig.cjs.json && tsc -p tsconfig.mjs.json && node ./scripts/build.js",
2122
"prepare": "npm run build",
2223
"lint": "eslint src",
2324
"test": "jest",

scripts/build.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import * as path from "path";
2+
import * as fs from "fs";
3+
4+
const DIST = "dist";
5+
const MJS = "mjs";
6+
const CJS = "cjs";
7+
const PKG = "package.json";
8+
const FILES = ["README.md", "LICENSE"];
9+
10+
fs.writeFileSync(
11+
path.join(DIST, CJS, PKG),
12+
JSON.stringify({ type: "commonjs" }, null, 2)
13+
);
14+
fs.writeFileSync(
15+
path.join(DIST, MJS, PKG),
16+
JSON.stringify({ type: "module" }, null, 2)
17+
);
18+
19+
// Inside the MJS folder we need to replace all import x from "y" with import x
20+
// from "y.js"
21+
fs.readdirSync(path.join(DIST, MJS)).forEach((file) => {
22+
const content = fs
23+
.readFileSync(path.join(DIST, MJS, file), "utf-8")
24+
.replace(/from "([^"]+)"/g, 'from "$1.js"');
25+
fs.writeFileSync(path.join(DIST, MJS, file), content);
26+
});
27+
28+
const pkg = fs.readFileSync(PKG, "utf-8");
29+
const json = JSON.parse(pkg);
30+
31+
delete json.scripts;
32+
delete json.devDependencies;
33+
34+
fs.writeFileSync(path.join(DIST, PKG), JSON.stringify(json, null, 2));
35+
36+
FILES.forEach((file) => fs.copyFileSync(file, path.join(DIST, file)));

scripts/build.mjs

Lines changed: 0 additions & 26 deletions
This file was deleted.

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
7878
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
7979
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
80-
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
80+
// "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
8181
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
8282
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
8383

tsconfig.esm.json renamed to tsconfig.mjs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"extends": "./tsconfig",
33
"compilerOptions": {
44
"module": "esnext",
5-
"outDir": "./dist/esm"
5+
"outDir": "./dist/mjs"
66
},
77
"exclude": ["test/**/*.ts"]
88
}

0 commit comments

Comments
 (0)