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-pages, cloudflare-module): enable code splitting by default #1905

Merged
merged 4 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]},
]

Comment on lines +25 to +28
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added those here to simplify the documentation and not requiring people to set them only when using the cloudflare_module preset.

The the service worker output these are not going to do anything but they are not breaking anything either (as you can see from this example), that's why I think it's simpler to just include them here, but let me know what you think, I don't mind moving it into the cloudflare_module preset section.

[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
14 changes: 3 additions & 11 deletions src/presets/cloudflare-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,13 @@ export const cloudflarePages = defineNitroPreset({
},
rollupConfig: {
output: {
entryFileNames: "_worker.js",
entryFileNames: "index.js",
dir: resolve("{{ output.dir }}", "_worker.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
Loading