From c3713834d0bfb03f6069d37df0b2f654789f8f9d Mon Sep 17 00:00:00 2001 From: wolfy1339 Date: Wed, 8 May 2024 09:36:34 -0400 Subject: [PATCH] fix(build): actually output ESM Fixes #2676 --- package.json | 4 ++-- scripts/build.mjs | 49 +++++++++++++++++++---------------------------- 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/package.json b/package.json index 6fc9bbffe5..21e86ffb1c 100644 --- a/package.json +++ b/package.json @@ -14,8 +14,8 @@ "license": "MIT", "scripts": { "build": "node scripts/build.mjs && tsc -p tsconfig.json", - "lint": "prettier --check \"{src,test}/**/*.{js,json,ts}\" \"*.{md,json}\"", - "lint:fix": "prettier --write \"{src,test}/**/*.{js,json,ts}\" \"*.{md,json}\"", + "lint": "prettier --check \"{src,test,scripts}/**/*.{js,json,ts}\" \"*.{md,json}\"", + "lint:fix": "prettier --write \"{src,test,scripts}/**/*.{js,json,ts,mjs}\" \"*.{md,json}\"", "pretest": "npm run -s lint", "test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest --coverage", "test:typescript": "npx tsc --noEmit --declaration --noUnusedLocals --esModuleInterop --module node16 --strict --allowImportingTsExtensions test/typescript-validate.ts" diff --git a/scripts/build.mjs b/scripts/build.mjs index 60a7f18bb4..2d31efac2a 100644 --- a/scripts/build.mjs +++ b/scripts/build.mjs @@ -12,6 +12,9 @@ const sharedOptions = { minify: false, allowOverwrite: true, packages: "external", + platform: "neutral", + format: "esm", + target: "es2022", }; async function main() { @@ -22,8 +25,6 @@ async function main() { entryPoints: await glob(["./src/*.ts", "./src/**/*.ts"]), outdir: "pkg/dist-src", bundle: false, - platform: "neutral", - format: "esm", ...sharedOptions, sourcemap: false, }); @@ -39,27 +40,13 @@ async function main() { const entryPoints = ["./pkg/dist-src/index.js"]; - await Promise.all([ - // Build the a CJS Node.js bundle - esbuild.build({ - entryPoints, - outdir: "pkg/dist-node", - bundle: true, - platform: "node", - target: "node18", - format: "cjs", - ...sharedOptions, - }), - // Build an ESM browser bundle - esbuild.build({ - entryPoints, - outdir: "pkg/dist-web", - bundle: true, - platform: "browser", - format: "esm", - ...sharedOptions, - }), - ]); + // Build an ESM bundle + await esbuild.build({ + entryPoints, + outdir: "pkg/dist-bundle", + bundle: true, + ...sharedOptions, + }); // Copy the README, LICENSE to the pkg folder await copyFile("LICENSE", "pkg/LICENSE"); @@ -78,15 +65,19 @@ async function main() { { ...pkg, files: ["dist-*/**", "bin/**"], - main: "dist-node/index.js", - module: "dist-web/index.js", - types: "dist-types/index.d.ts", - source: "dist-src/index.js", + types: "./dist-types/index.d.ts", + exports: { + ".": { + types: "./dist-types/index.d.ts", + import: "./dist-bundle/index.js", + default: "./dist-bundle/index.js", + }, + }, sideEffects: false, }, null, - 2 - ) + 2, + ), ); } main();