Skip to content

Commit 49347dc

Browse files
authored
Migrate a few workspaces to "module" format (#424)
Per https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c Note that trying to get `ts-node` to work was quite painful, but `tsx` came to the rescue. So I think I'm going to favor `tsx` going forward.
1 parent 791dc3e commit 49347dc

File tree

15 files changed

+65
-42
lines changed

15 files changed

+65
-42
lines changed

workspaces/download-leetcode-submissions/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,25 @@
1212
"name": "Miorel-Lucian Palii",
1313
"url": "https://github.com/miorel"
1414
},
15+
"exports": "./src/main.ts",
1516
"scripts": {
1617
"format": "prettier --color --write .",
1718
"lint": "eslint --color --max-warnings=0 .",
18-
"start": "ts-node src/main.ts",
19+
"start": "tsx src/main.ts",
1920
"typecheck": "tsc --pretty --project ."
2021
},
2122
"dependencies": {
2223
"@code-chronicles/leetcode-api": "workspace:*",
2324
"@code-chronicles/util": "workspace:*",
2425
"nullthrows": "1.1.1",
25-
"ts-node": "10.9.2",
2626
"zod": "3.23.8"
2727
},
2828
"devDependencies": {
2929
"@code-chronicles/eslint-config": "workspace:*",
3030
"@types/node": "22.5.5",
3131
"eslint": "9.10.0",
3232
"prettier": "3.3.3",
33+
"tsx": "4.19.1",
3334
"typescript": "5.6.2"
3435
}
3536
}

workspaces/fetch-leetcode-problem-list/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ yarn
3636
yarn build
3737

3838
# Run it with Node!
39-
node dist/fetch-leetcode-problem-list.js
39+
node dist/fetch-leetcode-problem-list.cjs
4040

4141
# Or if your system can handle executable files, try running it directly:
42-
./dist/fetch-leetcode-problem-list.js
42+
./dist/fetch-leetcode-problem-list.cjs
4343

4444
# Examine the output:
4545
cat problems.jsonl

workspaces/fetch-leetcode-problem-list/package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
"name": "Miorel-Lucian Palii",
1313
"url": "https://github.com/miorel"
1414
},
15-
"main": "src/main.ts",
15+
"type": "module",
16+
"exports": "./src/main.ts",
1617
"scripts": {
17-
"build": "webpack && chmod +x dist/fetch-leetcode-problem-list.js",
18+
"build": "cross-env NODE_OPTIONS=\"--import tsx\" webpack && chmod +x dist/fetch-leetcode-problem-list.cjs",
1819
"format": "prettier --color --write .",
1920
"lint": "eslint --color --max-warnings=0 .",
20-
"start": "ts-node src/main.ts",
21+
"start": "tsx src/main.ts",
2122
"typecheck": "tsc --pretty --project ."
2223
},
2324
"dependencies": {
@@ -27,10 +28,11 @@
2728
"devDependencies": {
2829
"@code-chronicles/eslint-config": "workspace:*",
2930
"@types/node": "22.5.5",
31+
"cross-env": "7.0.3",
3032
"eslint": "9.10.0",
3133
"prettier": "3.3.3",
3234
"ts-loader": "9.5.1",
33-
"ts-node": "10.9.2",
35+
"tsx": "4.19.1",
3436
"typescript": "5.6.2",
3537
"webpack": "5.94.0",
3638
"webpack-cli": "5.1.4"

workspaces/fetch-leetcode-problem-list/webpack.config.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import webpack, {
99
import { stripPrefix } from "@code-chronicles/util/stripPrefix";
1010
import { stripPrefixOrThrow } from "@code-chronicles/util/stripPrefixOrThrow";
1111

12-
import packageJson from "./package.json";
12+
import packageJson from "./package.json" with { type: "module" };
1313

1414
const config: Configuration = {
1515
target: "node",
16-
entry: path.resolve(__dirname, packageJson.main),
16+
entry: path.resolve(__dirname, packageJson.exports),
1717
output: {
18-
filename: stripPrefixOrThrow(packageJson.name, "@code-chronicles/") + ".js",
18+
filename:
19+
stripPrefixOrThrow(packageJson.name, "@code-chronicles/") + ".cjs",
1920
path: path.resolve(__dirname, "dist"),
2021
},
2122

@@ -40,6 +41,9 @@ const config: Configuration = {
4041

4142
resolve: {
4243
extensions: [".tsx", ".ts", "..."],
44+
extensionAlias: {
45+
".js": [".ts", ".tsx", ".js"],
46+
},
4347
},
4448

4549
externalsType: "commonjs",

workspaces/fetch-recent-accepted-leetcode-submissions/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ yarn
3131
yarn build
3232

3333
# Run it with Node!
34-
node dist/fetch-recent-accepted-leetcode-submissions.js elimanzo VehicleOfPuzzle
34+
node dist/fetch-recent-accepted-leetcode-submissions.cjs elimanzo VehicleOfPuzzle
3535

3636
# Or if your system can handle executable files, try running it directly:
37-
./dist/fetch-recent-accepted-leetcode-submissions.js elimanzo VehicleOfPuzzle
37+
./dist/fetch-recent-accepted-leetcode-submissions.cjs elimanzo VehicleOfPuzzle
3838
```
3939

4040
The output is JSON and will look something like:

workspaces/fetch-recent-accepted-leetcode-submissions/package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
"name": "Miorel-Lucian Palii",
1313
"url": "https://github.com/miorel"
1414
},
15-
"main": "src/main.ts",
15+
"type": "module",
16+
"exports": "./src/main.ts",
1617
"scripts": {
17-
"build": "webpack && chmod +x dist/fetch-recent-accepted-leetcode-submissions.js",
18+
"build": "cross-env NODE_OPTIONS=\"--import tsx\" webpack && chmod +x dist/fetch-recent-accepted-leetcode-submissions.cjs",
1819
"format": "prettier --color --write .",
1920
"lint": "eslint --color --max-warnings=0 .",
20-
"start": "ts-node src/main.ts",
21+
"start": "tsx src/main.ts",
2122
"typecheck": "tsc --pretty --project ."
2223
},
2324
"dependencies": {
@@ -27,10 +28,11 @@
2728
"devDependencies": {
2829
"@code-chronicles/eslint-config": "workspace:*",
2930
"@types/node": "22.5.5",
31+
"cross-env": "7.0.3",
3032
"eslint": "9.10.0",
3133
"prettier": "3.3.3",
3234
"ts-loader": "9.5.1",
33-
"ts-node": "10.9.2",
35+
"tsx": "4.19.1",
3436
"typescript": "5.6.2",
3537
"webpack": "5.94.0",
3638
"webpack-cli": "5.1.4"

workspaces/fetch-recent-accepted-leetcode-submissions/webpack.config.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import webpack, {
99
import { stripPrefix } from "@code-chronicles/util/stripPrefix";
1010
import { stripPrefixOrThrow } from "@code-chronicles/util/stripPrefixOrThrow";
1111

12-
import packageJson from "./package.json";
12+
import packageJson from "./package.json" with { type: "module" };
1313

1414
const config: Configuration = {
1515
target: "node",
16-
entry: path.resolve(__dirname, packageJson.main),
16+
entry: path.resolve(__dirname, packageJson.exports),
1717
output: {
18-
filename: stripPrefixOrThrow(packageJson.name, "@code-chronicles/") + ".js",
18+
filename:
19+
stripPrefixOrThrow(packageJson.name, "@code-chronicles/") + ".cjs",
1920
path: path.resolve(__dirname, "dist"),
2021
},
2122

@@ -40,6 +41,9 @@ const config: Configuration = {
4041

4142
resolve: {
4243
extensions: [".tsx", ".ts", "..."],
44+
extensionAlias: {
45+
".js": [".ts", ".tsx", ".js"],
46+
},
4347
},
4448

4549
externalsType: "commonjs",

workspaces/post-leetcode-potd-to-discord/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ To use:
4848
yarn build
4949

5050
# Run it with Node!
51-
node dist/post-leetcode-potd-to-discord.js
51+
node dist/post-leetcode-potd-to-discord.cjs
5252

5353
# Or if your system can handle executable files, try running it directly:
54-
./dist/post-leetcode-potd-to-discord.js
54+
./dist/post-leetcode-potd-to-discord.cjs
5555
```
5656

5757
Here's an example message:

workspaces/post-leetcode-potd-to-discord/package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
"name": "Miorel-Lucian Palii",
1313
"url": "https://github.com/miorel"
1414
},
15-
"main": "src/main.ts",
15+
"type": "module",
16+
"exports": "./src/main.ts",
1617
"scripts": {
17-
"build": "webpack && chmod +x dist/post-leetcode-potd-to-discord.js",
18+
"build": "cross-env NODE_OPTIONS=\"--import tsx\" webpack && chmod +x dist/post-leetcode-potd-to-discord.cjs",
1819
"format": "prettier --color --write .",
1920
"lint": "eslint --color --max-warnings=0 .",
20-
"start": "ts-node src/main.ts",
21+
"start": "tsx src/main.ts",
2122
"typecheck": "tsc --pretty --project ."
2223
},
2324
"dependencies": {
@@ -34,9 +35,10 @@
3435
"@code-chronicles/eslint-config": "workspace:*",
3536
"@types/invariant": "2.2.37",
3637
"@types/node": "22.5.5",
38+
"cross-env": "7.0.3",
3739
"eslint": "9.10.0",
3840
"prettier": "3.3.3",
39-
"ts-node": "10.9.2",
41+
"tsx": "4.19.1",
4042
"typescript": "5.6.2",
4143
"webpack": "5.94.0",
4244
"webpack-cli": "5.1.4"

workspaces/post-leetcode-potd-to-discord/src/getPotdMessage.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { ActiveDailyCodingChallengeQuestion } from "@code-chronicles/leetcode-api";
2-
import { formatTimestampForDiscord } from "./formatTimestampForDiscord";
3-
import { yearMonthDayToTimestampInSeconds } from "@code-chronicles/util/yearMonthDayToTimestampInSeconds";
42
import { SEC_IN_DAY } from "@code-chronicles/util/timeConstants";
3+
import { yearMonthDayToTimestampInSeconds } from "@code-chronicles/util/yearMonthDayToTimestampInSeconds";
4+
5+
import { formatTimestampForDiscord } from "./formatTimestampForDiscord.js";
56

67
export function getPotdMessage({
78
date,

0 commit comments

Comments
 (0)