fix(vercel): CJS bundle fix (#3051)
* fix(vercel): CJS bundle fix * Changeset
This commit is contained in:
parent
abcee7c957
commit
b0ba22c5ff
2 changed files with 32 additions and 27 deletions
5
.changeset/little-numbers-buy.md
Normal file
5
.changeset/little-numbers-buy.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/vercel': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fixed issues when converting from ESM to CJS
|
|
@ -1,8 +1,8 @@
|
||||||
import type { AstroAdapter, AstroConfig, AstroIntegration, RouteData } from 'astro';
|
import type { AstroAdapter, AstroConfig, AstroIntegration, RouteData } from 'astro';
|
||||||
import type { PathLike } from 'fs';
|
import type { PathLike } from 'fs';
|
||||||
import fs from 'fs/promises';
|
import fs from 'fs/promises';
|
||||||
import esbuild from 'esbuild';
|
|
||||||
import { fileURLToPath } from 'url';
|
import { fileURLToPath } from 'url';
|
||||||
|
import esbuild from 'esbuild';
|
||||||
|
|
||||||
const writeJson = (path: PathLike, data: any) =>
|
const writeJson = (path: PathLike, data: any) =>
|
||||||
fs.writeFile(path, JSON.stringify(data), { encoding: 'utf-8' });
|
fs.writeFile(path, JSON.stringify(data), { encoding: 'utf-8' });
|
||||||
|
@ -19,6 +19,8 @@ export function getAdapter(): AstroAdapter {
|
||||||
|
|
||||||
export default function vercel(): AstroIntegration {
|
export default function vercel(): AstroIntegration {
|
||||||
let _config: AstroConfig;
|
let _config: AstroConfig;
|
||||||
|
let _serverEntry: URL;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
name: '@astrojs/vercel',
|
name: '@astrojs/vercel',
|
||||||
hooks: {
|
hooks: {
|
||||||
|
@ -29,44 +31,42 @@ export default function vercel(): AstroIntegration {
|
||||||
'astro:config:done': ({ setAdapter, config }) => {
|
'astro:config:done': ({ setAdapter, config }) => {
|
||||||
setAdapter(getAdapter());
|
setAdapter(getAdapter());
|
||||||
_config = config;
|
_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 }) => {
|
'astro:build:start': async ({ buildConfig }) => {
|
||||||
buildConfig.serverEntry = `${ENTRYFILE}.mjs`;
|
buildConfig.serverEntry = `${ENTRYFILE}.js`;
|
||||||
buildConfig.client = new URL('./static/', _config.outDir);
|
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 }) => {
|
'astro:build:done': async ({ routes }) => {
|
||||||
/*
|
// Bundle dependecies
|
||||||
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
|
|
||||||
await esbuild.build({
|
await esbuild.build({
|
||||||
entryPoints: [fileURLToPath(new URL(`./${ENTRYFILE}.mjs`, tmpDir))],
|
entryPoints: [fileURLToPath(_serverEntry)],
|
||||||
outfile: fileURLToPath(new URL(`./${ENTRYFILE}.js`, bundleDir)),
|
outfile: fileURLToPath(_serverEntry),
|
||||||
bundle: true,
|
bundle: true,
|
||||||
format: 'cjs',
|
format: 'cjs',
|
||||||
platform: 'node',
|
platform: 'node',
|
||||||
target: 'node14',
|
target: 'node14',
|
||||||
|
allowOverwrite: true,
|
||||||
|
minifyWhitespace: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
await fs.rm(tmpDir, { recursive: true });
|
|
||||||
|
|
||||||
let staticRoutes: RouteData[] = [];
|
let staticRoutes: RouteData[] = [];
|
||||||
let dynamicRoutes: RouteData[] = [];
|
let dynamicRoutes: RouteData[] = [];
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue