-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,94 @@ | ||
import { type AstroConfig, type AstroIntegration } from "astro" | ||
import { fileURLToPath } from "url" | ||
import { build } from 'esbuild' | ||
import { type AstroConfig, type AstroIntegration } from 'astro' | ||
import * as fs from 'fs/promises' | ||
import fg from 'fast-glob' | ||
import path from 'path' | ||
import { getParsedCommandLineOfConfigFile } from "typescript" | ||
|
||
export default (): AstroIntegration => { | ||
let buildConfig: AstroConfig['build'] | ||
|
||
return { | ||
name: "cloudflare-adapter", | ||
name: 'cloudflare-adapter', | ||
hooks: { | ||
"astro:config:setup": ({ config }) => { | ||
config.vite.plugins = [ | ||
{ | ||
name: 'qwik-resolve', | ||
resolveId(source, importer, options) { | ||
if (source === '@qwik-client-manifest') { | ||
return source | ||
} | ||
'astro:config:setup': ({ config, command }) => { | ||
if (command === 'build') { | ||
config.vite.plugins = [ | ||
{ | ||
name: 'qwik-resolve', | ||
resolveId(source, importer, options) { | ||
if (source === '@qwik-client-manifest') { | ||
return source | ||
} | ||
}, | ||
async load(id, options) { | ||
if (id.startsWith('@qwik-client-manifest')) { | ||
const manifestPath = (await fg('.tmp-*/q-manifest.json'))[0]! | ||
const manifestJson = await fs.readFile(manifestPath, { | ||
encoding: 'utf-8' | ||
}) | ||
return `export const manifest = (${manifestJson})` | ||
} | ||
}, | ||
enforce: 'pre' | ||
}, | ||
async load(id, options) { | ||
if (id.startsWith('@qwik-client-manifest')) { | ||
const manifestPath = (await fg('.tmp-*/q-manifest.json'))[0]! | ||
const manifestJson = await fs.readFile(manifestPath, { encoding: 'utf-8' }) | ||
return `export const manifest = (${manifestJson})` | ||
} | ||
}, | ||
enforce: 'pre' | ||
}, | ||
...(config.vite.plugins ?? []) | ||
] | ||
...(config.vite.plugins ?? []) | ||
] | ||
} | ||
}, | ||
"astro:config:done": ({ setAdapter, config }) => { | ||
'astro:config:done': ({ setAdapter, config }) => { | ||
buildConfig = config.build | ||
setAdapter({ | ||
name: "cloudflare-adapter", | ||
serverEntrypoint: "./src/integrations/cloudflare/server/server.ts", | ||
name: 'cloudflare-adapter', | ||
serverEntrypoint: './src/integrations/cloudflare/server/server.ts', | ||
supportedAstroFeatures: { | ||
staticOutput: "stable", | ||
serverOutput: "stable", | ||
assets: { | ||
|
||
} | ||
staticOutput: 'stable', | ||
serverOutput: 'stable', | ||
assets: {} | ||
}, | ||
exports: ["fetch"] | ||
exports: ['fetch'] | ||
}) | ||
}, | ||
"astro:build:done": async () => { | ||
await fs.writeFile('dist/_worker.js', 'import { fetch } from "./server/entry.mjs"\nglobalThis.process={env:{}};\nexport default { fetch }') | ||
'astro:build:done': async () => { | ||
await fs.writeFile( | ||
'dist/_worker.js', | ||
'import { fetch } from "./server/entry.mjs"\nglobalThis.process={env:{}};\nexport default { fetch }' | ||
) | ||
const clientFiles = await fg('./dist/client/**/*') | ||
|
||
await fs.writeFile('dist/_routes.json', JSON.stringify({ | ||
version: 1, | ||
exclude: [...new Set([ | ||
...clientFiles.map(path => path.replace('./dist/client', '')), | ||
...clientFiles.map(path => path.replace('./dist/client', '').replace(/index\.html$/, '')) | ||
])], | ||
include: ['/*'] | ||
}, null, 2)) | ||
await fs.writeFile( | ||
'dist/_routes.json', | ||
JSON.stringify( | ||
{ | ||
version: 1, | ||
exclude: [ | ||
...new Set([ | ||
...clientFiles.map((path) => | ||
path.replace('./dist/client', '') | ||
), | ||
...clientFiles.map((path) => | ||
path | ||
.replace('./dist/client', '') | ||
.replace(/index\.html$/, '') | ||
) | ||
]) | ||
], | ||
include: ['/*'] | ||
}, | ||
null, | ||
2 | ||
) | ||
) | ||
|
||
for (const file of clientFiles) { | ||
const content = await Bun.file(file).arrayBuffer() | ||
await Bun.write(path.join('dist', file.replace('./dist/client/', '')), content, { | ||
createPath: true | ||
}) | ||
await Bun.write( | ||
path.join('dist', file.replace('./dist/client/', '')), | ||
content, | ||
{ | ||
createPath: true | ||
} | ||
) | ||
} | ||
} | ||
}, | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters