Skip to content

Commit

Permalink
fixup! expand changeset+formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
penalosa committed Mar 27, 2023
1 parent 88e98c4 commit b7e71a6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
44 changes: 42 additions & 2 deletions .changeset/beige-eagles-wave.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,58 @@ export default {
}
```

For JS modules, it's necessary to specify an additional [module rule](https://developers.cloudflare.com/workers/wrangler/configuration/#bundling) (or rules) in you `wrangler.toml` to configure whether your modules are ES modules or Common JS modules. For instance, to upload an additional module called `dep.js` which is an ES module, add the following module rule to your `wrangler.toml`, which tells Wrangler that all `**/*.js` files are ES modules.
For JS modules, it's necessary to specify an additional [module rule](https://developers.cloudflare.com/workers/wrangler/configuration/#bundling) (or rules) in your `wrangler.toml` to configure your modules as ES modules or Common JS modules. For instance, to upload additional JavaScript files as ES modules, add the following module rule to your `wrangler.toml`, which tells Wrangler that all `**/*.js` files are ES modules.

```toml
rules = [
{ type = "ESModule", globs = ["**/*.js"]},
]
```

If you have Common JS modules, you'd configure Wrangler with a CommonJS rule (the following rule tells Wrangler that all `.cjs` files are CommonJS):
If you have Common JS modules, you'd configure Wrangler with a CommonJS rule (the following rule tells Wrangler that all `.cjs` files are Common JS modules):

```toml
rules = [
{ type = "CommonJS", globs = ["**/*.cjs"]},
]
```

In most projects, adding a single rule will be sufficient. However, for advanced usecases where you're mixing ES modules and Common JS modules, you'll need to use multiple rule definitions. For instance, the following set of rules will match all `.mjs` files as ES modules, all `.cjs` files as Common JS modules, and the `nested/say-hello.js` file as Common JS.

```toml
rules = [
{ type = "CommonJS", globs = ["nested/say-hello.js", "**/*.cjs"]},
{ type = "ESModule", globs = ["**/*.mjs"]}
]
```

If multiple rules overlap, Wrangler will log a warning about the duplicate rules, and will discard additional rules that matches a module. For example, the following rule configuration classifies `dep.js` as both a Common JS module and an ES module:

```toml
rules = [
{ type = "CommonJS", globs = ["dep.js"]},
{ type = "ESModule", globs = ["dep.js"]}
]
```

Wrangler will treat `dep.js` as a Common JS module, since that was the first rule that matched, and will log the following warning:

```
▲ [WARNING] Ignoring duplicate module: dep.js (esm)
```

This also adds a new configuration option to `wrangler.toml`: `base_dir`. Defaulting to the directory of your Worker's main entrypoint, this tells Wrangler where your additional modules are located, and determines the module paths against which your module rule globs are matched.

For instance, given the following directory structure:

```
- wrangler.toml
- src/
- index.html
- vendor/
- dependency.js
- js/
- index.js
```

If your `wrangler.toml` had `main = "src/js/index.js"`, you would need to set `base_dir = "src"` in order to be able to import `src/vendor/dependency.js` and `src/index.html` from `src/js/index.js`.
2 changes: 1 addition & 1 deletion packages/wrangler/src/module-collection.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import crypto from "node:crypto";
import { readFile } from "node:fs/promises";
import path from "node:path";
import chalk from "chalk";
import globToRegExp from "glob-to-regexp";
import { logger } from "./logger";
import type { Config, ConfigModuleRuleType } from "./config";
import type { CfModule, CfModuleType, CfScriptFormat } from "./worker";
import type esbuild from "esbuild";
import chalk from "chalk";

function flipObject<
K extends string | number | symbol,
Expand Down

0 comments on commit b7e71a6

Please sign in to comment.