Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit a1cff3c

Browse files
mrbbotKikobeats
andcommitted
Switch back to CommonJS modules
A primary goal of Miniflare is to provide a *fun* developer experience. Currently, this is not the case when using Miniflare with a certain popular testing framework (**cough**, ~~Jest~~, **cough**), because it and its ecosystem's support for ESM isn't great yet. This also makes it easier to upgrade from Miniflare 1. Node lets you import CommonJS modules in ES modules so this won't break those. We'll look at switching to ES modules again later... - Switch esbuild format to `cjs` and set platform to `node` - Replace `"type": "module"` and `"exports"` with `"main"` in `package.json`s - Replace all dynamic `import()`s with `require()`s - Remove all uses of `import.meta.url` - Use `@ava/typescript` for tests instead of custom TypeScript loader Closes #119 Co-authored-by: Kikobeats <[email protected]>
1 parent 0ab1f32 commit a1cff3c

File tree

50 files changed

+178
-196
lines changed

Some content is hidden

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

50 files changed

+178
-196
lines changed

.eslintrc.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
parser: "@typescript-eslint/parser",
33
extends: ["plugin:prettier/recommended"],
4-
plugins: ["import"],
4+
plugins: ["import", "es"],
55
rules: {
66
"import/order": ["warn", { alphabetize: { order: "asc" } }],
77
"no-undef-init": "off",
@@ -17,10 +17,12 @@ module.exports = {
1717
"@typescript-eslint/no-empty-function": "off",
1818
"@typescript-eslint/no-explicit-any": "off",
1919
"@typescript-eslint/no-non-null-assertion": "off",
20+
"@typescript-eslint/no-var-requires": "off",
2021
"@typescript-eslint/no-unused-vars": [
2122
"warn",
2223
{ argsIgnorePattern: "^_" },
2324
],
25+
"es/no-dynamic-import": "error",
2426
},
2527
},
2628
],

ava.config.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

ava.config.mjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { pkgsList } from "./scripts/common.mjs";
2+
3+
const rewritePaths = Object.fromEntries(
4+
pkgsList.map((pkgName) => [
5+
`packages/${pkgName}/test/`,
6+
`packages/${pkgName}/dist/test/`,
7+
])
8+
);
9+
10+
export default {
11+
nonSemVerExperiments: {
12+
nextGenConfig: true,
13+
},
14+
files: ["packages/*/test/**/*.spec.ts"],
15+
timeout: "5m",
16+
nodeArguments: ["--no-warnings", "--experimental-vm-modules"],
17+
typescript: {
18+
compile: false,
19+
rewritePaths,
20+
},
21+
};

package-lock.json

Lines changed: 55 additions & 25 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"esbuild": "^0.12.20",
4545
"eslint": "^7.32.0",
4646
"eslint-config-prettier": "^8.3.0",
47+
"eslint-plugin-es": "^4.1.0",
4748
"eslint-plugin-import": "^2.24.2",
4849
"eslint-plugin-prettier": "^3.4.1",
4950
"esm": "^3.2.25",

packages/cache/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
],
1212
"author": "MrBBot <[email protected]>",
1313
"license": "MIT",
14-
"type": "module",
15-
"exports": "./dist/src/index.js",
14+
"main": "./dist/src/index.js",
1615
"types": "./dist/src/index.d.ts",
1716
"files": [
1817
"dist/src"

packages/cli-parser/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
],
1212
"author": "MrBBot <[email protected]>",
1313
"license": "MIT",
14-
"type": "module",
15-
"exports": "./dist/src/index.js",
14+
"main": "./dist/src/index.js",
1615
"types": "./dist/src/index.d.ts",
1716
"files": [
1817
"dist/src"

packages/cli-parser/src/parse.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { readFileSync } from "fs";
22
import path from "path";
3-
import { fileURLToPath } from "url";
43
import {
54
MiniflareError,
65
OptionMetadata,
@@ -92,8 +91,6 @@ export function parseArgv<Plugins extends PluginSignatures>(
9291
// If help or version requested, ignore other options
9392
if (parsed.help) throw new ParseError("ERR_HELP");
9493
if (parsed.version) {
95-
const __filename = fileURLToPath(import.meta.url);
96-
const __dirname = path.dirname(__filename);
9794
const pkgPath = path.join(__dirname, "..", "..", "package.json");
9895
const pkg = JSON.parse(readFileSync(pkgPath, "utf8"));
9996
throw new ParseError("ERR_VERSION", pkg.version);

packages/core/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
],
1212
"author": "MrBBot <[email protected]>",
1313
"license": "MIT",
14-
"type": "module",
15-
"exports": "./dist/src/index.js",
14+
"main": "./dist/src/index.js",
1615
"types": "./dist/src/index.d.ts",
1716
"files": [
1817
"dist/src"

packages/core/src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ export class MiniflareCore<
326326
this.#wranglerConfigPath = configPath;
327327
try {
328328
const configData = await fs.readFile(configPath, "utf8");
329-
const toml = await import("@iarna/toml");
329+
const toml: typeof import("@iarna/toml") = require("@iarna/toml");
330330
const config: WranglerConfig = toml.parse(configData);
331331
if (configEnv && config.env && configEnv in config.env) {
332332
// TODO: take into account option inheritance properly
@@ -730,9 +730,11 @@ export class MiniflareCore<
730730
let watcher = this.#watcher;
731731
// Make sure we've created the watcher
732732
if (!watcher) {
733-
const { Watcher } = await import("@miniflare/watcher");
733+
const {
734+
Watcher,
735+
}: typeof import("@miniflare/watcher") = require("@miniflare/watcher");
734736
this.#watcherCallbackMutex = new Mutex();
735-
watcher = new Watcher(this.#watcherCallback.bind(this)); // , this.log
737+
watcher = new Watcher(this.#watcherCallback.bind(this));
736738
this.#watcher = watcher;
737739
}
738740

0 commit comments

Comments
 (0)