Skip to content

Commit 0067a89

Browse files
committed
Convert to ESM, with refactoring codebase
1 parent f5312e0 commit 0067a89

File tree

129 files changed

+1181
-1584
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+1181
-1584
lines changed

.github/workflows/get_artifact_dir_name.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const fs = require("node:fs");
2-
const os = require("node:os");
1+
import * as fs from "node:fs";
2+
import * as os from "node:os";
33

4-
const { platformName } = require("#cli/bin_path");
4+
import { platformName } from "#cli/paths";
55

66
// Pass artifactDirName to subsequent GitHub actions
77
fs.appendFileSync(

.github/workflows/prepare_package_upload.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
const fs = require("node:fs");
2-
const os = require("node:os");
1+
import * as fs from "node:fs";
2+
import * as os from "node:os";
33

4-
const packageSpec = require("rescript/package.json");
5-
const { version } = packageSpec;
4+
import { version } from "rescript/package.json" with { type: "json" };
65

76
const commitHash = process.argv[2] || process.env.GITHUB_SHA;
87
const commitHashShort = commitHash.substring(0, 7);

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ clean-rewatch:
8989
cargo clean --manifest-path rewatch/Cargo.toml && rm -f rewatch/rewatch
9090

9191
clean:
92-
(cd runtime && ../cli/rescript clean)
92+
(cd runtime && ../cli/rescript.js clean)
9393
dune clean
9494

9595
clean-all: clean clean-gentype clean-rewatch

biome.json

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
"enabled": true,
1111
"rules": {
1212
"recommended": true,
13+
"nursery": {
14+
"noCommonJs": "error"
15+
},
1316
"correctness": {
1417
"useImportExtensions": "error",
1518
"noUndeclaredDependencies": "error",
@@ -48,6 +51,7 @@
4851
"tests/analysis_tests/**/src/**",
4952
"tests/build_tests/**/src/**",
5053
"tests/docstring_tests/**",
54+
"tests/gentype_tests/**",
5155
"tests/tests/**/src/**",
5256
"tests/tools_tests/**/src/**",
5357
"analysis/examples/**/src/**",

cli/rescript_arg.js renamed to cli/_args.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ class StringBuilder {
1515
}
1616
}
1717

18-
class ArgError extends Error {}
18+
export class ArgError extends Error {}
1919

2020
/**
2121
* @param {string} s
2222
*/
23-
function bad_arg(s) {
23+
export function bad_arg(s) {
2424
throw new ArgError(s);
2525
}
2626

@@ -118,7 +118,7 @@ function stop_raise(usage, error, specs) {
118118
* @param {number} start
119119
* @param {number} finish
120120
*/
121-
function parse_exn(
121+
export function parse_exn(
122122
usage,
123123
argv,
124124
specs,
@@ -174,6 +174,3 @@ function parse_exn(
174174
}
175175
annofun(list);
176176
}
177-
exports.bad_arg = bad_arg;
178-
exports.parse_exn = parse_exn;
179-
exports.ArgError = ArgError;

cli/rescript_bsb.js renamed to cli/_bsb.js

+12-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
// @ts-check
22

3-
const fs = require("node:fs");
4-
const path = require("node:path");
5-
const os = require("node:os");
6-
const child_process = require("node:child_process");
7-
const { createServer } = require("node:http");
8-
const { MiniWebSocket: WebSocket } = require("#lib/minisocket");
9-
const { rescript_exe } = require("#cli/bin_path");
3+
import * as child_process from "node:child_process";
4+
import * as fs from "node:fs";
5+
import { createServer } from "node:http";
6+
import * as os from "node:os";
7+
import * as path from "node:path";
8+
import { WebSocket } from "#lib/minisocket";
9+
10+
import { rescript_exe } from "./_paths.js";
1011

1112
const cwd = process.cwd();
1213
const lockFileName = path.join(cwd, ".bsb.lock");
@@ -27,7 +28,7 @@ const lockFileName = path.join(cwd, ".bsb.lock");
2728
* @type {child_process.ChildProcess | null}
2829
*/
2930
let ownerProcess = null;
30-
function releaseBuild() {
31+
export function releaseBuild() {
3132
if (ownerProcess) {
3233
ownerProcess.kill("SIGHUP");
3334
try {
@@ -100,14 +101,14 @@ function delegate(args, maybeOnClose) {
100101
/**
101102
* @param {Array<string>} args
102103
*/
103-
function info(args) {
104+
export function info(args) {
104105
delegate(["info", ...args]);
105106
}
106107

107108
/**
108109
* @param {Array<string>} args
109110
*/
110-
function clean(args) {
111+
export function clean(args) {
111112
delegate(["clean", ...args]);
112113
}
113114

@@ -475,7 +476,7 @@ Please pick a different one using the \`-ws [host:]port\` flag from bsb.`);
475476
/**
476477
* @param {Array<string>} args
477478
*/
478-
function build(args) {
479+
export function build(args) {
479480
// We want to show the compile time for build
480481
// But bsb might show a help message when --help or invalid arguments are passed
481482
// We don't want to show the compile time in that case
@@ -499,8 +500,3 @@ function build(args) {
499500
}
500501
delegate(["build", ...args]);
501502
}
502-
503-
exports.releaseBuild = releaseBuild;
504-
exports.info = info;
505-
exports.clean = clean;
506-
exports.build = build;

cli/_paths.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// @ts-check
2+
3+
import * as path from "node:path";
4+
import { fileURLToPath } from "node:url";
5+
6+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
7+
8+
export const cliPath = path.join(__dirname, "rescript.js");
9+
10+
/**
11+
* For compatibility reasons, if the architecture is x64, omit it from the bin directory name.
12+
* So we'll have "darwin", "linux" and "win32" for x64 arch,
13+
* but "darwinarm64" and "linuxarm64" for arm64 arch.
14+
* Also, we do not have Windows ARM binaries yet. But the x64 binaries do work on Windows 11 ARM.
15+
* So omit the architecture for Windows, too.
16+
*/
17+
export const platformName =
18+
process.arch === "x64" || process.platform === "win32"
19+
? process.platform
20+
: process.platform + process.arch;
21+
22+
export const platformDir = path.resolve(__dirname, "..", platformName);
23+
24+
export const bsc_exe = path.join(platformDir, "bsc.exe");
25+
26+
export const ninja_exe = path.join(platformDir, "ninja.exe");
27+
28+
export const rescript_exe = path.join(platformDir, "rescript.exe");
29+
30+
export const rescript_tools_exe = path.join(platformDir, "rescript-tools.exe");
31+
32+
export const rescript_editor_analysis_exe = path.join(
33+
platformDir,
34+
"rescript-editor-analysis.exe",
35+
);
36+
37+
export const rewatch_exe = path.join(platformDir, "rewatch.exe");

cli/bin_path.js

-62
This file was deleted.

cli/bsc renamed to cli/bsc.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
// @ts-check
44

5-
"use strict";
5+
import { execFileSync } from "node:child_process";
66

7-
const child_process = require("child_process");
8-
const { bsc_exe } = require("./bin_path.js");
7+
import { bsc_exe } from "./_paths.js";
98

109
const delegate_args = process.argv.slice(2);
1110

1211
try {
13-
child_process.execFileSync(bsc_exe, delegate_args, { stdio: "inherit" });
12+
execFileSync(bsc_exe, delegate_args, { stdio: "inherit" });
1413
} catch (e) {
1514
if (e.code === "ENOENT") {
1615
console.error(String(e));

cli/rescript-tools renamed to cli/rescript-tools.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
// @ts-check
44

5-
"use strict";
5+
import * as child_process from "node:child_process";
66

7-
const child_process = require("node:child_process");
8-
const { rescript_tools_exe } = require("./bin_path.js");
7+
import { rescript_tools_exe } from "./_paths.js";
98

109
const args = process.argv.slice(2);
1110

cli/rescript renamed to cli/rescript.js

+21-25
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
//@ts-check
44

5-
"use strict";
6-
75
// This script is supposed to be running in project root directory
86
// It matters since we need read .sourcedirs(location)
97
// and its content are file/directories with regard to project root
108

11-
const tty = require("node:tty");
12-
const { bsc_exe, rescript_exe } = require("./bin_path.js");
13-
const bsb = require("./rescript_bsb.js");
9+
import * as tty from "node:tty";
10+
import packageJson from "rescript/package.json" with { type: "json" };
11+
12+
import * as bsb from "./_bsb.js";
13+
import { bsc_exe, rescript_exe } from "./_paths.js";
1414

1515
const cwd = process.cwd();
1616
process.env.BSB_PROJECT_ROOT = cwd;
@@ -21,7 +21,10 @@ if (process.env.FORCE_COLOR === undefined) {
2121
process.env.NINJA_ANSI_FORCED = "1";
2222
}
2323
} else {
24-
if (process.env.FORCE_COLOR === "1" && process.env.NINJA_ANSI_FORCED === undefined) {
24+
if (
25+
process.env.FORCE_COLOR === "1" &&
26+
process.env.NINJA_ANSI_FORCED === undefined
27+
) {
2528
process.env.NINJA_ANSI_FORCED = "1";
2629
}
2730
if (process.argv.includes("-verbose")) {
@@ -77,19 +80,20 @@ try {
7780

7881
const args = process.argv.slice(2);
7982
const argPatterns = {
80-
help: ['help', '-h', '-help', '--help'],
81-
version: ['version', '-v', '-version', '--version'],
83+
help: ["help", "-h", "-help", "--help"],
84+
version: ["version", "-v", "-version", "--version"],
8285
};
8386

8487
const helpArgIndex = args.findIndex(arg => argPatterns.help.includes(arg));
8588
const firstPositionalArgIndex = args.findIndex(arg => !arg.startsWith("-"));
8689

87-
if (helpArgIndex !== -1 && (firstPositionalArgIndex === -1 || helpArgIndex <= firstPositionalArgIndex)) {
90+
if (
91+
helpArgIndex !== -1 &&
92+
(firstPositionalArgIndex === -1 || helpArgIndex <= firstPositionalArgIndex)
93+
) {
8894
console.log(helpMessage);
89-
9095
} else if (argPatterns.version.includes(args[0])) {
91-
console.log(require("../package.json").version);
92-
96+
console.log(packageJson.version);
9397
} else if (firstPositionalArgIndex !== -1) {
9498
const subcmd = args[firstPositionalArgIndex];
9599
const subcmdArgs = args.slice(firstPositionalArgIndex + 1);
@@ -108,25 +112,17 @@ if (helpArgIndex !== -1 && (firstPositionalArgIndex === -1 || helpArgIndex <= fi
108112
break;
109113
}
110114
case "format": {
111-
require("./rescript_format.js").main(
112-
subcmdArgs,
113-
rescript_exe,
114-
bsc_exe
115-
);
115+
const mod = await import("./rescript/rescript_format.js");
116+
await mod.main(subcmdArgs, rescript_exe, bsc_exe);
116117
break;
117118
}
118119
case "dump": {
119-
require("./rescript_dump.js").main(
120-
subcmdArgs,
121-
rescript_exe,
122-
bsc_exe
123-
);
120+
const mod = await import("./rescript/rescript_dump.js");
121+
mod.main(subcmdArgs, rescript_exe, bsc_exe);
124122
break;
125123
}
126124
default: {
127-
console.error(
128-
`Error: Unknown command "${subcmd}".\n${helpMessage}`
129-
);
125+
console.error(`Error: Unknown command "${subcmd}".\n${helpMessage}`);
130126
process.exit(2);
131127
}
132128
}

0 commit comments

Comments
 (0)