Skip to content

Commit

Permalink
feat(cloudflare-pages, cloudflare-module): enable code splitting by d…
Browse files Browse the repository at this point in the history
…efault (#1905)
  • Loading branch information
dario-piotrowicz authored Nov 12, 2023
1 parent 265e719 commit 25d07fb
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 48 deletions.
31 changes: 4 additions & 27 deletions docs/content/2.deploy/20.providers/cloudflare.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ 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"
```
Expand Down Expand Up @@ -296,33 +300,6 @@ 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"
```

### Local Wrangler Dev builds

By default `wrangler dev` requires nitro to be built before it can be served by wrangler.
Expand Down
9 changes: 1 addition & 8 deletions src/presets/cloudflare-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,10 @@ export const cloudflareModule = defineNitroPreset({
output: {
format: "esm",
exports: "named",
inlineDynamicImports: false,
},
},
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
15 changes: 3 additions & 12 deletions src/presets/cloudflare-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const cloudflarePages = defineNitroPreset({
output: {
dir: "{{ rootDir }}/dist",
publicDir: "{{ output.dir }}",
serverDir: "{{ output.dir }}",
serverDir: "{{ output.dir }}/_worker.js",
},
alias: {
// Hotfix: Cloudflare appends /index.html if mime is not found and things like ico are not in standard lite.js!
Expand All @@ -30,21 +30,12 @@ export const cloudflarePages = defineNitroPreset({
},
rollupConfig: {
output: {
entryFileNames: "_worker.js",
entryFileNames: "index.js",
format: "esm",
inlineDynamicImports: false,
},
},
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
2 changes: 1 addition & 1 deletion test/presets/cloudflare-pages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe("nitro:preset:cloudflare-pages", async () => {
testNitro(ctx, () => {
const mf = new Miniflare({
modules: true,
scriptPath: resolve(ctx.outDir, "_worker.js"),
scriptPath: resolve(ctx.outDir, "_worker.js", "index.js"),
globals: { __env__: {} },
compatibilityFlags: ["streams_enable_constructors"],
bindings: {
Expand Down

0 comments on commit 25d07fb

Please sign in to comment.