2022-03-30 07:42:19 -05:00
|
|
|
import type { AstroAdapter, AstroIntegration } from 'astro';
|
2022-06-06 11:02:13 -05:00
|
|
|
import esbuild from 'esbuild';
|
|
|
|
import * as fs from 'fs';
|
2022-06-06 11:49:53 -05:00
|
|
|
import * as npath from 'path';
|
|
|
|
import { fileURLToPath } from 'url';
|
2022-03-30 07:42:19 -05:00
|
|
|
|
|
|
|
interface Options {
|
|
|
|
port?: number;
|
|
|
|
hostname?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getAdapter(args?: Options): AstroAdapter {
|
|
|
|
return {
|
|
|
|
name: '@astrojs/deno',
|
|
|
|
serverEntrypoint: '@astrojs/deno/server.js',
|
|
|
|
args: args ?? {},
|
2022-04-15 16:01:33 -05:00
|
|
|
exports: ['stop', 'handle', 'start', 'running'],
|
2022-03-30 07:42:19 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function createIntegration(args?: Options): AstroIntegration {
|
2022-06-06 11:02:13 -05:00
|
|
|
let _buildConfig: any;
|
|
|
|
let _vite: any;
|
2022-03-30 07:42:19 -05:00
|
|
|
return {
|
|
|
|
name: '@astrojs/deno',
|
|
|
|
hooks: {
|
|
|
|
'astro:config:done': ({ setAdapter }) => {
|
|
|
|
setAdapter(getAdapter(args));
|
|
|
|
},
|
2022-06-06 11:02:13 -05:00
|
|
|
'astro:build:start': ({ buildConfig }) => {
|
|
|
|
_buildConfig = buildConfig;
|
|
|
|
},
|
2022-03-30 07:42:19 -05:00
|
|
|
'astro:build:setup': ({ vite, target }) => {
|
2022-03-30 07:43:13 -05:00
|
|
|
if (target === 'server') {
|
2022-06-06 11:02:13 -05:00
|
|
|
_vite = vite;
|
2022-04-21 11:10:06 -05:00
|
|
|
vite.resolve = vite.resolve || {};
|
|
|
|
vite.resolve.alias = vite.resolve.alias || {};
|
2022-06-02 12:54:35 -05:00
|
|
|
|
|
|
|
const aliases = [{ find: 'react-dom/server', replacement: 'react-dom/server.browser' }];
|
|
|
|
|
|
|
|
if (Array.isArray(vite.resolve.alias)) {
|
|
|
|
vite.resolve.alias = [...vite.resolve.alias, ...aliases];
|
|
|
|
} else {
|
|
|
|
for (const alias of aliases) {
|
|
|
|
(vite.resolve.alias as Record<string, string>)[alias.find] = alias.replacement;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 07:42:19 -05:00
|
|
|
vite.ssr = {
|
2022-03-30 07:43:13 -05:00
|
|
|
noExternal: true,
|
2022-03-30 07:42:19 -05:00
|
|
|
};
|
|
|
|
}
|
2022-03-30 07:43:13 -05:00
|
|
|
},
|
2022-06-06 11:02:13 -05:00
|
|
|
'astro:build:done': async () => {
|
|
|
|
const entryUrl = new URL(_buildConfig.serverEntry, _buildConfig.server);
|
|
|
|
const pth = fileURLToPath(entryUrl);
|
|
|
|
await esbuild.build({
|
|
|
|
target: 'es2020',
|
|
|
|
platform: 'browser',
|
|
|
|
entryPoints: [pth],
|
|
|
|
outfile: pth,
|
|
|
|
allowOverwrite: true,
|
|
|
|
format: 'esm',
|
|
|
|
bundle: true,
|
2022-06-06 11:03:17 -05:00
|
|
|
external: ['@astrojs/markdown-remark'],
|
2022-06-06 11:02:13 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
// Remove chunks, if they exist. Since we have bundled via esbuild these chunks are trash.
|
|
|
|
try {
|
2022-06-06 11:03:17 -05:00
|
|
|
const chunkFileNames =
|
|
|
|
_vite?.build?.rollupOptions?.output?.chunkFileNames ?? 'chunks/chunk.[hash].mjs';
|
2022-06-06 11:02:13 -05:00
|
|
|
const chunkPath = npath.dirname(chunkFileNames);
|
|
|
|
const chunksDirUrl = new URL(chunkPath + '/', _buildConfig.server);
|
|
|
|
await fs.promises.rm(chunksDirUrl, { recursive: true, force: true });
|
|
|
|
} catch {}
|
2022-06-06 11:03:17 -05:00
|
|
|
},
|
2022-03-30 07:42:19 -05:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|