Skip to content

Commit

Permalink
Add build script to configure esmodules
Browse files Browse the repository at this point in the history
  • Loading branch information
mattphillips committed Jan 25, 2022
1 parent e61baa2 commit b97a1f4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
19 changes: 9 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
"name": "deep-object-diff",
"version": "1.1.3",
"description": "Deep diffs two objects, including nested structures of arrays and objects, and return the difference.",
"main": "dist/index.js",
"module": "dist-es/index.js",
"main": "cjs/index.js",
"module": "mjs/index.js",
"exports": {
".": {
"import": "./cjs/index.js",
"require": "./mjs/index.js"
}
},
"types": "./index.d.ts",
"files": [
"dist",
"dist-es",
"index.d.ts",
"README.md"
],
"scripts": {
"build": "babel src -d dist && yarn build:esmodule",
"build:esmodule": "rm -rf dist-es && mkdir dist-es && cp -r src/* dist-es && rm -rf dist-es/**/*.test.js",
"build": "rm -rf dist && babel src -d dist/cjs && node scripts/build.mjs",
"prepublish": "yarn build",
"lint": "eslint src",
"test": "jest",
Expand Down
26 changes: 26 additions & 0 deletions scripts/build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as path from "path";
import * as fs from "fs";

const DIST = "dist";
const SRC = "src";
const MJS = "mjs";
const CJS = "cjs";
const PKG = "package.json";
const FILES = ["index.d.ts", "README.md", "LICENSE"];

fs.mkdirSync(path.join(DIST, MJS));
fs.readdirSync("./src").forEach((file) => fs.copyFileSync(path.join(SRC, file), path.join(DIST, MJS, file)));

fs.writeFileSync(path.join(DIST, CJS, PKG), JSON.stringify({ type: "commonjs" }, null, 2));
fs.writeFileSync(path.join(DIST, MJS, PKG), JSON.stringify({ type: "module" }, null, 2));

const pkg = fs.readFileSync(PKG, "utf-8");
const json = JSON.parse(pkg);

delete json.scripts;
delete json.devDependencies;
delete json.babel;

fs.writeFileSync(path.join(DIST, PKG), JSON.stringify(json, null, 2));

FILES.forEach((file) => fs.copyFileSync(file, path.join(DIST, file)));

0 comments on commit b97a1f4

Please sign in to comment.