Skip to content

Commit a824a8d

Browse files
committed
refactor: migrate to ES module syntax
1 parent a7ab30f commit a824a8d

19 files changed

Lines changed: 864 additions & 921 deletions

babel.config.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"use strict";
2-
3-
module.exports = (api) => {
1+
export default (api) => {
42
api.cache(true);
53

64
return {
@@ -24,7 +22,7 @@ module.exports = (api) => {
2422
"@babel/preset-env",
2523
{
2624
targets: {
27-
node: "20.9.0",
25+
node: "22.12.0",
2826
},
2927
},
3028
],

bin/webpack-dev-server.js

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22
/* Based on webpack/bin/webpack.js */
33
/* eslint-disable no-console */
44

5-
"use strict";
5+
import cp from "node:child_process";
6+
import nodeModule from "node:module";
7+
import path from "node:path";
8+
import readLine from "node:readline";
9+
import { fileURLToPath, pathToFileURL } from "node:url";
10+
import fs from "graceful-fs";
611

712
/**
813
* @param {string} command process to run
914
* @param {string[]} args command line arguments
1015
* @returns {Promise<void>} promise
1116
*/
12-
const runCommand = (command, args) => {
13-
const cp = require("node:child_process");
14-
15-
return new Promise((resolve, reject) => {
17+
const runCommand = (command, args) =>
18+
new Promise((resolve, reject) => {
1619
const executedCommand = cp.spawn(command, args, {
1720
stdio: "inherit",
1821
shell: true,
@@ -30,7 +33,6 @@ const runCommand = (command, args) => {
3033
}
3134
});
3235
});
33-
};
3436

3537
/**
3638
* @param {string} packageName name of the package
@@ -41,10 +43,7 @@ const isInstalled = (packageName) => {
4143
return true;
4244
}
4345

44-
const path = require("node:path");
45-
const fs = require("graceful-fs");
46-
47-
let dir = __dirname;
46+
let dir = path.dirname(fileURLToPath(import.meta.url));
4847

4948
do {
5049
try {
@@ -58,9 +57,9 @@ const isInstalled = (packageName) => {
5857
}
5958
} while (dir !== (dir = path.dirname(dir)));
6059

61-
// https://github.com/nodejs/node/blob/v18.9.1/lib/internal/modules/cjs/loader.js#L1274
60+
// https://github.com/nodejs/node/blob/v22.12.0/lib/internal/modules/cjs/loader.js#L1818
6261
// @ts-expect-error
63-
for (const internalPath of require("node:module").globalPaths) {
62+
for (const internalPath of nodeModule.globalPaths) {
6463
try {
6564
if (fs.statSync(path.join(internalPath, packageName)).isDirectory()) {
6665
return true;
@@ -75,29 +74,20 @@ const isInstalled = (packageName) => {
7574

7675
/**
7776
* @param {CliOption} cli options
78-
* @returns {void}
77+
* @returns {Promise<void>}
7978
*/
80-
const runCli = (cli) => {
79+
const runCli = async (cli) => {
8180
if (cli.preprocess) {
8281
cli.preprocess();
8382
}
8483

85-
const path = require("node:path");
84+
const pkgUrl = import.meta.resolve(`${cli.package}/package.json`);
85+
const pkgPath = fileURLToPath(pkgUrl);
86+
const pkg = (await import(pkgUrl, { with: { type: "json" } })).default;
8687

87-
const pkgPath = require.resolve(`${cli.package}/package.json`);
88+
const binPath = path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName]);
8889

89-
const pkg = require(pkgPath);
90-
91-
if (pkg.type === "module" || /\.mjs/i.test(pkg.bin[cli.binName])) {
92-
import(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName])).catch(
93-
(error) => {
94-
console.error(error);
95-
process.exitCode = 1;
96-
},
97-
);
98-
} else {
99-
require(path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName]));
100-
}
90+
await import(pathToFileURL(binPath).href);
10191
};
10292

10393
/**
@@ -123,10 +113,6 @@ const cli = {
123113
};
124114

125115
if (!cli.installed) {
126-
const path = require("node:path");
127-
const fs = require("graceful-fs");
128-
const readLine = require("node:readline");
129-
130116
const notify = `CLI for webpack must be installed.\n ${cli.name} (${cli.url})\n`;
131117

132118
console.error(notify);
@@ -187,14 +173,17 @@ if (!cli.installed) {
187173
);
188174

189175
runCommand(packageManager, [...installOptions, cli.package])
190-
.then(() => {
191-
runCli(cli);
192-
})
176+
.then(() => runCli(cli))
193177
.catch((error) => {
194178
console.error(error);
195179
process.exitCode = 1;
196180
});
197181
});
198182
} else {
199-
runCli(cli);
183+
try {
184+
await runCli(cli);
185+
} catch (error) {
186+
console.error(error);
187+
process.exitCode = 1;
188+
}
200189
}

client-src/webpack.config.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
"use strict";
1+
import path from "node:path";
2+
import { fileURLToPath } from "node:url";
3+
import webpack from "webpack";
4+
import { merge } from "webpack-merge";
25

3-
const path = require("node:path");
4-
const webpack = require("webpack");
5-
const { merge } = require("webpack-merge");
6+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
67

78
const library = {
89
library: {
@@ -37,7 +38,7 @@ const baseForModules = {
3738
},
3839
};
3940

40-
module.exports = [
41+
export default [
4142
merge(baseForModules, {
4243
entry: path.join(__dirname, "modules/logger/index.js"),
4344
output: {

commitlint.config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"use strict";
2-
3-
module.exports = {
1+
export default {
42
extends: ["@commitlint/config-conventional"],
53
rules: {
64
"header-max-length": [0],

eslint.config.mjs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ export default defineConfig([
77
{
88
extends: [config],
99
ignores: ["client-src/**/*", "!client-src/webpack.config.js"],
10+
languageOptions: {
11+
// ES2025 needed for import attributes (`import(x, { with: ... })`).
12+
// eslint-config-webpack pins ecmaVersion to 2024 for Node 22.
13+
ecmaVersion: "latest",
14+
},
1015
rules: {
1116
// TODO fix me
1217
"prefer-destructuring": "off",

jest.config.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
"use strict";
2-
3-
module.exports = {
1+
export default {
42
testEnvironmentOptions: {
53
url: "http://localhost/",
64
},

0 commit comments

Comments
 (0)