diff options
author | 2022-04-15 16:58:57 -0300 | |
---|---|---|
committer | 2022-04-15 15:58:57 -0400 | |
commit | b0ba22c5ffab6575706ae904d0ad8cadc3f48d43 (patch) | |
tree | a123bee80f04f3f6dcee241d6a0b93f5eb419b56 | |
parent | abcee7c957afb9e73fc65144054152b5d6a7a808 (diff) | |
download | astro-b0ba22c5ffab6575706ae904d0ad8cadc3f48d43.tar.gz astro-b0ba22c5ffab6575706ae904d0ad8cadc3f48d43.tar.zst astro-b0ba22c5ffab6575706ae904d0ad8cadc3f48d43.zip |
fix(vercel): CJS bundle fix (#3051)
* fix(vercel): CJS bundle fix
* Changeset
-rw-r--r-- | .changeset/little-numbers-buy.md | 5 | ||||
-rw-r--r-- | packages/integrations/vercel/src/index.ts | 54 |
2 files changed, 32 insertions, 27 deletions
diff --git a/.changeset/little-numbers-buy.md b/.changeset/little-numbers-buy.md new file mode 100644 index 000000000..f490e1737 --- /dev/null +++ b/.changeset/little-numbers-buy.md @@ -0,0 +1,5 @@ +--- +'@astrojs/vercel': patch +--- + +Fixed issues when converting from ESM to CJS diff --git a/packages/integrations/vercel/src/index.ts b/packages/integrations/vercel/src/index.ts index dea059b29..6c353a93d 100644 --- a/packages/integrations/vercel/src/index.ts +++ b/packages/integrations/vercel/src/index.ts @@ -1,8 +1,8 @@ import type { AstroAdapter, AstroConfig, AstroIntegration, RouteData } from 'astro'; import type { PathLike } from 'fs'; import fs from 'fs/promises'; -import esbuild from 'esbuild'; import { fileURLToPath } from 'url'; +import esbuild from 'esbuild'; const writeJson = (path: PathLike, data: any) => fs.writeFile(path, JSON.stringify(data), { encoding: 'utf-8' }); @@ -19,6 +19,8 @@ export function getAdapter(): AstroAdapter { export default function vercel(): AstroIntegration { let _config: AstroConfig; + let _serverEntry: URL; + return { name: '@astrojs/vercel', hooks: { @@ -29,44 +31,42 @@ export default function vercel(): AstroIntegration { 'astro:config:done': ({ setAdapter, config }) => { setAdapter(getAdapter()); _config = config; + _serverEntry = new URL(`./server/pages/${ENTRYFILE}.js`, config.outDir); + }, + 'astro:build:setup': ({ vite, target }) => { + if (target === 'server') { + vite.build!.rollupOptions = { + input: [], + output: { + format: 'cjs', + file: fileURLToPath(_serverEntry), + dir: undefined, + entryFileNames: undefined, + chunkFileNames: undefined, + assetFileNames: undefined, + inlineDynamicImports: true, + }, + }; + } }, 'astro:build:start': async ({ buildConfig }) => { - buildConfig.serverEntry = `${ENTRYFILE}.mjs`; + buildConfig.serverEntry = `${ENTRYFILE}.js`; buildConfig.client = new URL('./static/', _config.outDir); - buildConfig.server = new URL('./server/tmp/', _config.outDir); + buildConfig.server = new URL('./server/pages/', _config.outDir); }, 'astro:build:done': async ({ routes }) => { - /* - Why do we need two folders? Why don't we just generate all inside `server/pages/`? - When the app builds, it throws some metadata inside a `chunks/` folder. - - ./server/ - pages/ - __astro_entry.mjs - chunks/ - (lots of js files) - - Those chunks will count as serverless functions (which cost money), so we - need to bundle as much as possible in one file. Hence, the following code - */ - - const tmpDir = new URL('./server/tmp/', _config.outDir); - const bundleDir = new URL('./server/pages/', _config.outDir); - - await fs.mkdir(bundleDir, { recursive: true }); - - // Convert server entry to CommonJS + // Bundle dependecies await esbuild.build({ - entryPoints: [fileURLToPath(new URL(`./${ENTRYFILE}.mjs`, tmpDir))], - outfile: fileURLToPath(new URL(`./${ENTRYFILE}.js`, bundleDir)), + entryPoints: [fileURLToPath(_serverEntry)], + outfile: fileURLToPath(_serverEntry), bundle: true, format: 'cjs', platform: 'node', target: 'node14', + allowOverwrite: true, + minifyWhitespace: true, }); - await fs.rm(tmpDir, { recursive: true }); - let staticRoutes: RouteData[] = []; let dynamicRoutes: RouteData[] = []; |