Skip to content

Commit

Permalink
feat(cloudflare-module, cloudflare-pages): experimental dynamic impor…
Browse files Browse the repository at this point in the history
…ts (#1172)

Co-authored-by: Pooya Parsa <pooya@pi0.io>
Co-authored-by: Dario Piotrowicz <dario.piotrowicz@gmail.com>
  • Loading branch information
3 people authored Aug 21, 2023
1 parent 6a1ea5c commit 345d0cc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
27 changes: 27 additions & 0 deletions docs/content/2.deploy/providers/cloudflare.md
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,31 @@ NITRO_HELLO_THERE="captain"
SECRET="top-secret"
```

## Advanced

### Experimental Dynamic Imports

By default cloudflare presets output to a single bundle file.

In order to try experimental dynamic imports you need to set the `NITRO_EXP_CLOUDFLARE_DYNAMIC_IMPORTS` environment variable for build command.

::alert{type="warning"}
This is an experimental mode and is likely not working at the moment!
::

With `cloudflare_module` preset, you need to add the following rule to your `wrangler.toml` file:

```diff [wrangler.toml]
name = "playground"
main = "./.output/server/index.mjs"
workers_dev = true
compatibility_date = "2022-09-10"
account_id = "<the account_id you obtained (optional)>"
route = "<mainly useful when you want to setup custom domains (optional too)>"
+ rules = [
+ { type = "ESModule", globs = ["**/*.js", "**/*.mjs"]},
+ ]

[site]
bucket = ".output/public"
```
9 changes: 9 additions & 0 deletions src/presets/cloudflare-module.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { readFile } from "node:fs/promises";
import { resolve } from "pathe";
import { writeFile } from "../utils";
import { defineNitroPreset } from "../preset";
Expand All @@ -19,6 +20,14 @@ export const cloudflareModule = defineNitroPreset({
},
},
hooks: {
"rollup:before"(_nitro, rollupConfig) {
if (process.env.NITRO_EXP_CLOUDFLARE_DYNAMIC_IMPORTS) {
rollupConfig.output = {
...rollupConfig.output,
inlineDynamicImports: false,
};
}
},
async compiled(nitro: Nitro) {
await writeFile(
resolve(nitro.options.output.dir, "package.json"),
Expand Down
10 changes: 10 additions & 0 deletions src/presets/cloudflare-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ export const cloudflarePages = defineNitroPreset({
},
},
hooks: {
"rollup:before"(nitro, rollupConfig) {
if (process.env.NITRO_EXP_CLOUDFLARE_DYNAMIC_IMPORTS) {
rollupConfig.output = {
...rollupConfig.output,
entryFileNames: "index.js",
dir: resolve(nitro.options.output.serverDir, "_worker.js"),
inlineDynamicImports: false,
};
}
},
async compiled(nitro: Nitro) {
await writeCFRoutes(nitro);
},
Expand Down

0 comments on commit 345d0cc

Please sign in to comment.