Skip to content

Commit ebddec0

Browse files
committed
refactor: migrate to ES module syntax (#5665)
* refactor: migrate to ES module syntax * refactor: migrate ports-map.js to ES module syntax * refactor: migrate helper files to ES module syntax * refactor: migrate fixtures configs to ES module syntax * refactor: migrate test files to ES module syntax * refactor: migrate test files to ES module syntax * refactor: migrate test files to use fileURLToPath for __dirname * refactor: remove duplicate import of http in cross-origin request tests * refactor: update library type to module and enable outputModule experiment in webpack config * refactor: update client transport implementation checks and adjust module federation test assertions * refactor: enhance client transport handling with pathToFileURL for absolute paths * fixup! * fixup! * refactor: improve lazy loading of webpack peer dependency and update addAdditionalEntries to async * fixup! * refactor: update lazy initialization of webpack dev middleware to async * refactor: update TypeScript configuration to target ES2024 and improve type definitions * fixup! * refactor: simplify package installation check using require.resolve * fix: update Node.js target version in Babel configuration to 22.15.0 * fixup! * fixup! * refactor: dynamically import 'node:net' in Server class for improved module loading * refactor: update type definition for onLoadQueue to ensure correct function signature * fixup! * refactor: migrate examples to ES module syntax and remove "use strict" directives - Updated all example files to use ES module imports instead of CommonJS require. - Removed unnecessary "use strict" directives from JavaScript files. - Adjusted webpack configuration files to utilize import.meta for context and URL handling. - Ensured consistent use of import statements across all examples. * refactor: update webpack configuration and asset handling for improved module support * refactor: update webSocketServer configuration to use object syntax for improved clarity * refactor: update test coverage script to include additional directories for improved coverage reporting
1 parent 2396c57 commit ebddec0

282 files changed

Lines changed: 4082 additions & 3393 deletions

File tree

Some content is hidden

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

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ npm-debug.log
77
client
88
!/examples/client
99
!/test/client
10+
11+
examples/**/dist
12+
1013
coverage
1114
node_modules
1215
.vscode

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.15.0",
2826
},
2927
},
3028
],

bin/webpack-dev-server.js

Lines changed: 29 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
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 { createRequire } 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";
11+
12+
const require = createRequire(import.meta.url);
613

714
/**
815
* @param {string} command process to run
916
* @param {string[]} args command line arguments
1017
* @returns {Promise<void>} promise
1118
*/
12-
const runCommand = (command, args) => {
13-
const cp = require("node:child_process");
14-
15-
return new Promise((resolve, reject) => {
19+
const runCommand = (command, args) =>
20+
new Promise((resolve, reject) => {
1621
const executedCommand = cp.spawn(command, args, {
1722
stdio: "inherit",
1823
shell: true,
@@ -30,7 +35,6 @@ const runCommand = (command, args) => {
3035
}
3136
});
3237
});
33-
};
3438

3539
/**
3640
* @param {string} packageName name of the package
@@ -41,63 +45,30 @@ const isInstalled = (packageName) => {
4145
return true;
4246
}
4347

44-
const path = require("node:path");
45-
const fs = require("graceful-fs");
46-
47-
let dir = __dirname;
48-
49-
do {
50-
try {
51-
if (
52-
fs.statSync(path.join(dir, "node_modules", packageName)).isDirectory()
53-
) {
54-
return true;
55-
}
56-
} catch {
57-
// Nothing
58-
}
59-
} while (dir !== (dir = path.dirname(dir)));
60-
61-
// https://github.com/nodejs/node/blob/v18.9.1/lib/internal/modules/cjs/loader.js#L1274
62-
// @ts-expect-error
63-
for (const internalPath of require("node:module").globalPaths) {
64-
try {
65-
if (fs.statSync(path.join(internalPath, packageName)).isDirectory()) {
66-
return true;
67-
}
68-
} catch {
69-
// Nothing
70-
}
48+
try {
49+
require.resolve(packageName);
50+
return true;
51+
} catch {
52+
return false;
7153
}
72-
73-
return false;
7454
};
7555

7656
/**
7757
* @param {CliOption} cli options
78-
* @returns {void}
58+
* @returns {Promise<void>}
7959
*/
80-
const runCli = (cli) => {
60+
const runCli = async (cli) => {
8161
if (cli.preprocess) {
8262
cli.preprocess();
8363
}
8464

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

89-
const pkg = require(pkgPath);
69+
const binPath = path.resolve(path.dirname(pkgPath), pkg.bin[cli.binName]);
9070

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-
}
71+
await import(pathToFileURL(binPath).href);
10172
};
10273

10374
/**
@@ -123,10 +94,6 @@ const cli = {
12394
};
12495

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

13299
console.error(notify);
@@ -187,14 +154,17 @@ if (!cli.installed) {
187154
);
188155

189156
runCommand(packageManager, [...installOptions, cli.package])
190-
.then(() => {
191-
runCli(cli);
192-
})
157+
.then(() => runCli(cli))
193158
.catch((error) => {
194159
console.error(error);
195160
process.exitCode = 1;
196161
});
197162
});
198163
} else {
199-
runCli(cli);
164+
try {
165+
await runCli(cli);
166+
} catch (error) {
167+
console.error(error);
168+
process.exitCode = 1;
169+
}
200170
}

client-src/clients/WebSocketClient.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { log } from "../utils/log.js";
22

3-
/** @typedef {import("../index").EXPECTED_ANY} EXPECTED_ANY */
3+
/** @typedef {import("../index.js").EXPECTED_ANY} EXPECTED_ANY */
44

55
/**
66
* @implements {CommunicationClient}

client-src/globals.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ declare module "ansi-html-community" {
2020
function setColors(colors: Record<string, string | string[]>): void;
2121
}
2222

23-
export = ansiHtmlCommunity;
23+
export default ansiHtmlCommunity;
2424
}

client-src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import socket from "./socket.js";
99
import { log, setLogLevel } from "./utils/log.js";
1010
import sendMessage from "./utils/sendMessage.js";
1111

12-
// eslint-disable-next-line jsdoc/no-restricted-syntax
12+
// eslint-disable-next-line jsdoc/reject-any-type
1313
/** @typedef {any} EXPECTED_ANY */
1414

1515
/**

client-src/overlay.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import ansiHTML from "ansi-html-community";
55

6-
/** @typedef {import("./index").EXPECTED_ANY} EXPECTED_ANY */
6+
/** @typedef {import("./index.js").EXPECTED_ANY} EXPECTED_ANY */
77

88
/**
99
* @type {(input: string, position: number) => number | undefined}
@@ -79,16 +79,16 @@ function encode(text) {
7979

8080
/**
8181
* @typedef {object} Context
82-
* @property {'warning' | 'error'} level level
83-
* @property {(string | Message)[]} messages messages
84-
* @property {'build' | 'runtime'} messageSource message source
82+
* @property {"warning" | "error"} level level
83+
* @property {(string | Message)[]} messages messages
84+
* @property {"build" | "runtime"} messageSource message source
8585
*/
8686

8787
/** @typedef {{ type: string } & Record<string, EXPECTED_ANY>} Event */
8888

8989
/**
9090
* @typedef {object} Options
91-
* @property {{ [state: string]: { on: Record<string, { target: string; actions?: Array<string> }> } }} states states
91+
* @property {{ [state: string]: { on: Record<string, { target: string, actions?: string[] }> } }} states states
9292
* @property {Context} context context
9393
* @property {string} initial initial
9494
*/
@@ -149,9 +149,9 @@ function createMachine({ states, context, initial }, { actions }) {
149149

150150
/**
151151
* @typedef {object} ShowOverlayData
152-
* @property {'warning' | 'error'} level level
153-
* @property {(string | Message)[]} messages messages
154-
* @property {'build' | 'runtime'} messageSource message source
152+
* @property {"warning" | "error"} level level
153+
* @property {(string | Message)[]} messages messages
154+
* @property {"build" | "runtime"} messageSource message source
155155
*/
156156

157157
/**
@@ -390,7 +390,7 @@ const colors = {
390390

391391
ansiHTML.setColors(colors);
392392

393-
/** @typedef {Error & { file?: string, moduleName?: string, moduleIdentifier?: string, loc?: string, message?: string; stack?: string | string[] }} Message */
393+
/** @typedef {Error & { file?: string, moduleName?: string, moduleIdentifier?: string, loc?: string, message?: string, stack?: string | string[] }} Message */
394394

395395
/**
396396
* @param {string} type type
@@ -450,7 +450,7 @@ const createOverlay = (options) => {
450450
let containerElement;
451451
/** @type {HTMLDivElement | null | undefined} */
452452
let headerElement;
453-
/** @type {Array<(element: HTMLDivElement) => void>} */
453+
/** @type {((element: HTMLDivElement) => void)[]} */
454454
let onLoadQueue = [];
455455
/** @type {Omit<TrustedTypePolicy, "createScript" | "createScriptURL"> | undefined} */
456456
let overlayTrustedTypesPolicy;
@@ -589,7 +589,7 @@ const createOverlay = (options) => {
589589
* @param {string} type type
590590
* @param {(string | Message)[]} messages messages
591591
* @param {undefined | false | string} trustedTypesPolicyName trusted types policy name
592-
* @param {'build' | 'runtime'} messageSource message source
592+
* @param {"build" | "runtime"} messageSource message source
593593
*/
594594
function show(type, messages, trustedTypesPolicyName, messageSource) {
595595
ensureOverlayExists(() => {

client-src/utils/sendMessage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* global WorkerGlobalScope */
22

3-
/** @typedef {import("../index").EXPECTED_ANY} EXPECTED_ANY */
3+
/** @typedef {import("../index.js").EXPECTED_ANY} EXPECTED_ANY */
44

55
// Send messages to the outside, so plugins can consume it.
66
/**

client-src/webpack.config.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
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: {
9-
// type: "module",
10-
type: "commonjs",
10+
type: "module",
1111
},
1212
};
1313

1414
const baseForModules = {
1515
devtool: false,
1616
mode: "development",
17-
// TODO enable this in future after fix bug with `eval` in webpack
18-
// experiments: {
19-
// outputModule: true,
20-
// },
17+
experiments: {
18+
outputModule: true,
19+
},
2120
output: {
2221
path: path.resolve(__dirname, "../client/modules"),
2322
...library,
@@ -37,7 +36,7 @@ const baseForModules = {
3736
},
3837
};
3938

40-
module.exports = [
39+
export default [
4140
merge(baseForModules, {
4241
entry: path.join(__dirname, "modules/logger/index.js"),
4342
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],

0 commit comments

Comments
 (0)