Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cloudflare-module, cloudflare-pages): experimental dynamic imports #1172

Merged
merged 11 commits into from
Aug 21, 2023
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