diff options
author | 2022-10-12 17:25:51 -0400 | |
---|---|---|
committer | 2022-10-12 17:25:51 -0400 | |
commit | e55af8a23233b6335f45b7a04b9d026990fb616c (patch) | |
tree | 62f47ae6e1fa56c04c045318c3a0d34674cb4a63 /packages/integrations/cloudflare/src/index.ts | |
parent | 2b7fb848bbe18942960c17a135c5a3769780512b (diff) | |
download | astro-e55af8a23233b6335f45b7a04b9d026990fb616c.tar.gz astro-e55af8a23233b6335f45b7a04b9d026990fb616c.tar.zst astro-e55af8a23233b6335f45b7a04b9d026990fb616c.zip |
Node.js standalone mode + support for astro preview (#5056)
* wip
* Deprecate buildConfig and move to config.build
* Implement the standalone server
* Stay backwards compat
* Add changesets
* correctly merge URLs
* Get config earlier
* update node tests
* Return the preview server
* update remaining tests
* swap usage and config ordering
* Update packages/astro/src/@types/astro.ts
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* Update .changeset/metal-pumas-walk.md
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* Update .changeset/metal-pumas-walk.md
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* Update .changeset/stupid-points-refuse.md
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* Update .changeset/stupid-points-refuse.md
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
* Link to build.server config
Co-authored-by: Fred K. Schott <fkschott@gmail.com>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Diffstat (limited to 'packages/integrations/cloudflare/src/index.ts')
-rw-r--r-- | packages/integrations/cloudflare/src/index.ts | 34 |
1 files changed, 27 insertions, 7 deletions
diff --git a/packages/integrations/cloudflare/src/index.ts b/packages/integrations/cloudflare/src/index.ts index 13c8578ee..9cf6412b8 100644 --- a/packages/integrations/cloudflare/src/index.ts +++ b/packages/integrations/cloudflare/src/index.ts @@ -1,4 +1,4 @@ -import type { AstroAdapter, AstroConfig, AstroIntegration, BuildConfig } from 'astro'; +import type { AstroAdapter, AstroConfig, AstroIntegration } from 'astro'; import esbuild from 'esbuild'; import * as fs from 'fs'; import { fileURLToPath } from 'url'; @@ -7,6 +7,12 @@ type Options = { mode: 'directory' | 'advanced'; }; +interface BuildConfig { + server: URL; + client: URL; + serverEntry: string; +} + export function getAdapter(isModeDirectory: boolean): AstroAdapter { return isModeDirectory ? { @@ -29,14 +35,26 @@ const SHIM = `globalThis.process = { export default function createIntegration(args?: Options): AstroIntegration { let _config: AstroConfig; let _buildConfig: BuildConfig; + let needsBuildConfig = false; const isModeDirectory = args?.mode === 'directory'; return { name: '@astrojs/cloudflare', hooks: { + 'astro:config:setup': ({ config, updateConfig }) => { + needsBuildConfig = !config.build.client; + updateConfig({ + build: { + client: new URL('./static/', config.outDir), + server: new URL('./', config.outDir), + serverEntry: '_worker.js', + } + }); + }, 'astro:config:done': ({ setAdapter, config }) => { setAdapter(getAdapter(isModeDirectory)); _config = config; + _buildConfig = config.build; if (config.output === 'static') { throw new Error(` @@ -45,12 +63,6 @@ export default function createIntegration(args?: Options): AstroIntegration { `); } }, - 'astro:build:start': ({ buildConfig }) => { - _buildConfig = buildConfig; - buildConfig.client = new URL('./static/', _config.outDir); - buildConfig.serverEntry = '_worker.js'; - buildConfig.server = new URL('./', _config.outDir); - }, 'astro:build:setup': ({ vite, target }) => { if (target === 'server') { vite.resolve = vite.resolve || {}; @@ -69,6 +81,14 @@ export default function createIntegration(args?: Options): AstroIntegration { vite.ssr.target = vite.ssr.target || 'webworker'; } }, + 'astro:build:start': ({ buildConfig }) => { + // Backwards compat + if(needsBuildConfig) { + buildConfig.client = new URL('./static/', _config.outDir); + buildConfig.server = new URL('./', _config.outDir); + buildConfig.serverEntry = '_worker.js'; + } + }, 'astro:build:done': async () => { const entryUrl = new URL(_buildConfig.serverEntry, _buildConfig.server); const pkg = fileURLToPath(entryUrl); |