Skip to content

Commit 2c0b240

Browse files
authored
BREAKING CHANGE: chore(node): migrate to full esm (#338)
1 parent 54303f0 commit 2c0b240

File tree

11 files changed

+53
-42
lines changed

11 files changed

+53
-42
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
"parser": "@typescript-eslint/parser",
88
"parserOptions": {
9-
"project": "./tsconfig.lint.json"
9+
"project": "./tsconfig.json"
1010
},
1111
"plugins": ["import", "@typescript-eslint"],
1212
"ignorePatterns": ["scripts/*"],

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ Typescript Library Starter relies on [volta](https://volta.sh/) to ensure node v
3434
### Typescript
3535

3636
Leverages [esbuild](https://github.com/evanw/esbuild) for blazing fast builds, but keeps `tsc` to generate `.d.ts` files.
37-
Generates two builds to support both ESM and CJS.
37+
Generates a single ESM build.
3838

3939
Commands:
4040

41-
- `build`: runs typechecking then generates CJS, ESM and `d.ts` files in the `build/` directory
41+
- `build`: runs typechecking then ESM and `d.ts` files in the `build/` directory
4242
- `clean`: removes the `build/` directory
4343
- `type:dts`: only generates `d.ts`
4444
- `type:check`: only run typechecking
45-
- `type:build`: only generates CJS and ESM
45+
- `type:build`: only generates ESM
4646

4747
### Tests
4848

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
"bugs": "https://github.com/gjuchault/typescript-library-starter/issues",
1313
"author": "Gabriel Juchault <[email protected]>",
1414
"repository": "gjuchault/typescript-library-starter",
15-
"main": "./build/cjs/index.js",
16-
"module": "./build/esm/index.js",
15+
"type": "module",
16+
"exports": "./build/index.js",
1717
"types": "./build/src/index.d.ts",
1818
"license": "MIT",
1919
"engines": {
@@ -32,7 +32,7 @@
3232
"build": "npm run clean && npm run type:dts && npm run build:main",
3333
"build:main": "tsx ./scripts/build",
3434
"clean": "tsx ./scripts/clean",
35-
"type:dts": "tsc --emitDeclarationOnly",
35+
"type:dts": "tsc --emitDeclarationOnly --project tsconfig.build.json",
3636
"type:check": "tsc --noEmit",
3737
"format": "prettier \"src/**/*.ts\" --write",
3838
"format:check": "prettier \"src/**/*.ts\" --check",

scripts/build.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
import path from "path";
1+
import path from "node:path";
2+
import url from "node:url";
23
import { build as esbuild, BuildOptions } from "esbuild";
34

5+
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
6+
47
const baseConfig: BuildOptions = {
58
platform: "node",
6-
target: "esnext",
7-
format: "cjs",
9+
target: "node18",
10+
format: "esm",
811
nodePaths: [path.join(__dirname, "../src")],
912
sourcemap: true,
1013
external: [],
@@ -14,18 +17,13 @@ const baseConfig: BuildOptions = {
1417
async function main() {
1518
await esbuild({
1619
...baseConfig,
17-
outdir: path.join(__dirname, "../build/cjs"),
18-
entryPoints: [path.join(__dirname, "../src/index.ts")],
19-
});
20-
21-
await esbuild({
22-
...baseConfig,
23-
format: "esm",
24-
outdir: path.join(__dirname, "../build/esm"),
20+
outdir: path.join(__dirname, "../build"),
2521
entryPoints: [path.join(__dirname, "../src/index.ts")],
2622
});
2723
}
2824

29-
if (require.main === module) {
30-
main();
25+
if (import.meta.url.startsWith("file:")) {
26+
if (process.argv[1] === url.fileURLToPath(import.meta.url)) {
27+
await main();
28+
}
3129
}

scripts/clean.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import fs from "fs/promises";
2-
import path from "path";
1+
import fs from "node:fs/promises";
2+
import url from "node:url";
3+
import path from "node:path";
4+
5+
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
36

47
async function main() {
58
await Promise.all([rmrf("build"), rmrf("coverage"), rmrf(".nyc_output")]);
@@ -12,6 +15,8 @@ async function rmrf(pathFromRoot: string): Promise<void> {
1215
});
1316
}
1417

15-
if (require.main === module) {
16-
main();
18+
if (import.meta.url.startsWith("file:")) {
19+
if (process.argv[1] === url.fileURLToPath(import.meta.url)) {
20+
await main();
21+
}
1722
}

scripts/setup.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
import path from "path";
1+
import path from "node:path";
2+
import fs from "node:fs/promises";
3+
import childProcess from "node:child_process";
4+
import { promisify } from "node:util";
5+
import url from "node:url";
26
import slugify from "slugify";
3-
import fs from "fs/promises";
4-
import childProcess from "child_process";
5-
import { promisify } from "util";
67
import prompts from "prompts";
78

89
const exec = promisify(childProcess.exec);
10+
const __filename = url.fileURLToPath(import.meta.url);
11+
const __dirname = url.fileURLToPath(new URL(".", import.meta.url));
912

1013
const rootPath = path.join(__dirname, "..");
1114
const releaseRcPath = path.join(rootPath, ".releaserc.json");
@@ -231,6 +234,8 @@ async function logAsyncTask<TResolve>(
231234
return output;
232235
}
233236

234-
if (require.main === module) {
235-
main();
237+
if (import.meta.url.startsWith("file:")) {
238+
if (process.argv[1] === url.fileURLToPath(import.meta.url)) {
239+
await main();
240+
}
236241
}

scripts/testSetup.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import childProcess from "child_process";
2-
import { promisify } from "util";
1+
import childProcess from "node:child_process";
2+
import { promisify } from "node:util";
3+
import url from "node:url";
34
import { run } from "./setup";
45

56
const exec = promisify(childProcess.exec);
@@ -60,6 +61,8 @@ async function testNoGrep(pattern: string) {
6061
}
6162
}
6263

63-
if (require.main === module) {
64-
main();
64+
if (import.meta.url.startsWith("file:")) {
65+
if (process.argv[1] === url.fileURLToPath(import.meta.url)) {
66+
await main();
67+
}
6568
}

src/__tests__/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from "vitest";
2-
import { foobar } from "../index";
2+
import { foobar } from "../index.js";
33

44
describe("foobar()", () => {
55
describe("given two positive integers", () => {

src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { bar } from "./bar";
2-
import { foo } from "./foo";
1+
import { bar } from "./bar.js";
2+
import { foo } from "./foo.js";
33

44
export function foobar(a: number, b: number) {
55
return foo().repeat(a).length + bar().repeat(b).length;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{
22
"extends": "./tsconfig.json",
3-
"exclude": []
3+
"exclude": ["./src/**/__tests__"]
44
}

0 commit comments

Comments
 (0)