From 0fc53b26d81e7f1c241e8e2f1c25c0c246373e53 Mon Sep 17 00:00:00 2001 From: JayNecessary Date: Mon, 9 Mar 2026 11:28:57 +0100 Subject: [PATCH] Module-Starter ESM Migration Summary: Ref T2858 Reviewers: #testers, kuba-orlik Reviewed By: #testers, kuba-orlik Maniphest Tasks: T2858 Differential Revision: https://hub.sealcode.org/D1662 --- esbuild.cjs => esbuild.js | 29 +++++++++++++++++++++-------- package.json | 8 +++++++- test.cjs | 1 + tsconfig.json | 4 ++-- 4 files changed, 31 insertions(+), 11 deletions(-) rename esbuild.cjs => esbuild.js (68%) diff --git a/esbuild.cjs b/esbuild.js similarity index 68% rename from esbuild.cjs rename to esbuild.js index b8641e2..0a087f1 100644 --- a/esbuild.cjs +++ b/esbuild.js @@ -1,7 +1,11 @@ -const { build } = require("esbuild"); -const glob = require("tiny-glob"); -const { promises: fs } = require("node:fs"); -const { extname, resolve, relative } = require("node:path"); +import * as esbuild from "esbuild"; +import { default as glob } from "tiny-glob"; +import { promises as fs } from "node:fs"; +import { extname, resolve, relative, dirname } from "node:path"; +import { fileURLToPath } from "node:url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); const watch = process.argv.at(-1) === "--watch"; @@ -40,15 +44,24 @@ const handle_absolute_paths = (project_dir) => ({ let entryPoints = Object.fromEntries( (await glob("./src/**/*.ts")).map((e) => [e.replace(/\.ts$/, ""), e]) ); - build({ + + const buildOptions = { entryPoints, sourcemap: true, + bundle: false, outdir: "./lib", logLevel: "info", platform: "node", - watch, - target: "node16", + target: "es2022", format: "esm", plugins: [handle_absolute_paths(__dirname)], - }); + }; + + if (watch) { + const context = await esbuild.context(buildOptions); + await context.watch(); + } else { + await esbuild.build(buildOptions); + } })(); + diff --git a/package.json b/package.json index 786eed7..6430d66 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,14 @@ "version": "0.0.1", "description": "module template", "main": "lib/src/index.js", + "exports": { + ".": { + "types": "./@types/index.d.ts", + "default": "./lib/src/index.js" + } + }, "scripts": { - "build": "node ./esbuild.cjs", + "build": "node ./esbuild.js", "prepare": "rm -rf @types && npm run typecheck && npm run build-declarations && npm run build", "pretest": "npm run build", "test": "node test.cjs", diff --git a/test.cjs b/test.cjs index 768cf6d..831b080 100644 --- a/test.cjs +++ b/test.cjs @@ -13,6 +13,7 @@ let mocha_options = [ "--timeout=10000", "--require", "source-map-support/register", + "--exit", ]; if (args["test-report"]) { diff --git a/tsconfig.json b/tsconfig.json index 1aa072e..6fe7e6e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { - "module": "commonjs", - "moduleResolution": "node", + "module": "nodenext", + "moduleResolution": "nodenext", "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true,