From 25d07fbfdd24d33f1614e1426f67a60f39e066df Mon Sep 17 00:00:00 2001 From: Dario Piotrowicz Date: Sun, 12 Nov 2023 19:48:49 +0000 Subject: [PATCH] feat(cloudflare-pages, cloudflare-module): enable code splitting by default (#1905) --- .../2.deploy/20.providers/cloudflare.md | 31 +++---------------- src/presets/cloudflare-module.ts | 9 +----- src/presets/cloudflare-pages.ts | 15 ++------- test/presets/cloudflare-pages.test.ts | 2 +- 4 files changed, 9 insertions(+), 48 deletions(-) diff --git a/docs/content/2.deploy/20.providers/cloudflare.md b/docs/content/2.deploy/20.providers/cloudflare.md index 13e6d17657..5fc2a1c704 100644 --- a/docs/content/2.deploy/20.providers/cloudflare.md +++ b/docs/content/2.deploy/20.providers/cloudflare.md @@ -22,6 +22,10 @@ compatibility_date = "2022-09-10" account_id = "" route = "" +rules = [ + { type = "ESModule", globs = ["**/*.js", "**/*.mjs"]}, +] + [site] bucket = ".output/public" ``` @@ -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 = "" - route = "" -+ 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. diff --git a/src/presets/cloudflare-module.ts b/src/presets/cloudflare-module.ts index c23887fd8b..8aac3c5bea 100644 --- a/src/presets/cloudflare-module.ts +++ b/src/presets/cloudflare-module.ts @@ -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"), diff --git a/src/presets/cloudflare-pages.ts b/src/presets/cloudflare-pages.ts index 95b8a61be3..0448d7994e 100644 --- a/src/presets/cloudflare-pages.ts +++ b/src/presets/cloudflare-pages.ts @@ -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! @@ -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); }, diff --git a/test/presets/cloudflare-pages.test.ts b/test/presets/cloudflare-pages.test.ts index 52b844f1bf..41bca5afec 100644 --- a/test/presets/cloudflare-pages.test.ts +++ b/test/presets/cloudflare-pages.test.ts @@ -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: {