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';
|
2023-07-17 19:17:59 -05:00
|
|
|
import * as fs from 'node:fs';
|
|
|
|
import * as npath from 'node:path';
|
|
|
|
import { fileURLToPath } from 'node:url';
|
2022-03-30 07:42:19 -05:00
|
|
|
|
2022-10-12 16:25:51 -05:00
|
|
|
interface BuildConfig {
|
|
|
|
server: URL;
|
|
|
|
serverEntry: string;
|
2023-01-09 11:01:33 -05:00
|
|
|
assets: string;
|
2022-10-12 16:25:51 -05:00
|
|
|
}
|
|
|
|
|
2022-03-30 07:42:19 -05:00
|
|
|
interface Options {
|
|
|
|
port?: number;
|
|
|
|
hostname?: string;
|
|
|
|
}
|
|
|
|
|
2022-06-27 16:20:28 -05:00
|
|
|
const SHIM = `globalThis.process = {
|
|
|
|
argv: [],
|
|
|
|
env: Deno.env.toObject(),
|
2022-06-27 16:22:26 -05:00
|
|
|
};`;
|
2022-06-27 16:20:28 -05:00
|
|
|
|
2023-02-17 13:21:11 -05:00
|
|
|
const DENO_VERSION = `0.177.0`;
|
2023-06-15 09:20:35 -05:00
|
|
|
// REF: https://github.com/denoland/deno/tree/main/ext/node/polyfills
|
|
|
|
const COMPATIBLE_NODE_MODULES = [
|
|
|
|
'assert',
|
|
|
|
'assert/strict',
|
|
|
|
'async_hooks',
|
|
|
|
'buffer',
|
|
|
|
'child_process',
|
|
|
|
'cluster',
|
|
|
|
'console',
|
|
|
|
'constants',
|
|
|
|
'crypto',
|
|
|
|
'dgram',
|
|
|
|
'diagnostics_channel',
|
|
|
|
'dns',
|
|
|
|
'events',
|
|
|
|
'fs',
|
|
|
|
'fs/promises',
|
|
|
|
'http',
|
|
|
|
// 'http2',
|
|
|
|
'https',
|
|
|
|
'inspector',
|
|
|
|
'module',
|
|
|
|
'net',
|
|
|
|
'os',
|
|
|
|
'path',
|
|
|
|
'path/posix',
|
|
|
|
'path/win32',
|
|
|
|
'perf_hooks',
|
|
|
|
'process',
|
|
|
|
'punycode',
|
|
|
|
'querystring',
|
|
|
|
'readline',
|
|
|
|
'repl',
|
|
|
|
'stream',
|
|
|
|
'stream/promises',
|
|
|
|
'stream/web',
|
|
|
|
'string_decoder',
|
|
|
|
'sys',
|
|
|
|
'timers',
|
|
|
|
'timers/promises',
|
|
|
|
// 'tls',
|
|
|
|
'trace_events',
|
|
|
|
'tty',
|
|
|
|
'url',
|
|
|
|
'util',
|
|
|
|
'util/types',
|
|
|
|
// 'v8',
|
|
|
|
// 'vm',
|
|
|
|
// 'wasi',
|
|
|
|
// 'webcrypto',
|
|
|
|
'worker_threads',
|
|
|
|
'zlib',
|
|
|
|
];
|
2023-02-17 13:19:19 -05:00
|
|
|
|
|
|
|
// We shim deno-specific imports so we can run the code in Node
|
2023-02-17 13:21:11 -05:00
|
|
|
// to prerender pages. In the final Deno build, this import is
|
2023-02-17 13:19:19 -05:00
|
|
|
// replaced with the Deno-specific contents listed below.
|
|
|
|
const DENO_IMPORTS_SHIM = `@astrojs/deno/__deno_imports.js`;
|
|
|
|
const DENO_IMPORTS = `export { Server } from "https://deno.land/std@${DENO_VERSION}/http/server.ts"
|
|
|
|
export { serveFile } from 'https://deno.land/std@${DENO_VERSION}/http/file_server.ts';
|
2023-02-17 13:21:11 -05:00
|
|
|
export { fromFileUrl } from "https://deno.land/std@${DENO_VERSION}/path/mod.ts";`;
|
2023-02-17 13:19:19 -05:00
|
|
|
|
2022-03-30 07:42:19 -05:00
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-02-17 13:19:19 -05:00
|
|
|
const denoImportsShimPlugin = {
|
2023-02-17 13:21:11 -05:00
|
|
|
name: '@astrojs/deno:shim',
|
|
|
|
setup(build: esbuild.PluginBuild) {
|
2023-07-03 07:59:43 -05:00
|
|
|
build.onLoad({ filter: /__deno_imports\.js$/ }, async () => {
|
2023-02-17 13:21:11 -05:00
|
|
|
return {
|
|
|
|
contents: DENO_IMPORTS,
|
|
|
|
loader: 'js',
|
|
|
|
};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
2023-02-17 13:19:19 -05:00
|
|
|
|
2023-06-15 09:20:35 -05:00
|
|
|
const denoRenameNodeModulesPlugin = {
|
|
|
|
name: '@astrojs/esbuild-rename-node-modules',
|
|
|
|
setup(build: esbuild.PluginBuild) {
|
|
|
|
const filter = new RegExp(COMPATIBLE_NODE_MODULES.map((mod) => `(^${mod}$)`).join('|'));
|
|
|
|
build.onResolve({ filter }, (args) => ({ path: 'node:' + args.path, external: true }));
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-03-30 07:42:19 -05:00
|
|
|
export default function createIntegration(args?: Options): AstroIntegration {
|
2022-10-12 16:25:51 -05:00
|
|
|
let _buildConfig: BuildConfig;
|
2022-06-06 11:02:13 -05:00
|
|
|
let _vite: any;
|
2022-03-30 07:42:19 -05:00
|
|
|
return {
|
|
|
|
name: '@astrojs/deno',
|
|
|
|
hooks: {
|
2022-07-24 23:18:02 -05:00
|
|
|
'astro:config:done': ({ setAdapter, config }) => {
|
2022-03-30 07:42:19 -05:00
|
|
|
setAdapter(getAdapter(args));
|
2022-10-12 16:25:51 -05:00
|
|
|
_buildConfig = config.build;
|
2022-07-24 23:18:02 -05:00
|
|
|
|
2022-07-24 23:20:38 -05:00
|
|
|
if (config.output === 'static') {
|
2023-05-17 08:23:20 -05:00
|
|
|
console.warn(
|
|
|
|
`[@astrojs/deno] \`output: "server"\` or \`output: "hybrid"\` is required to use this adapter.`
|
|
|
|
);
|
2022-07-24 23:20:38 -05:00
|
|
|
console.warn(
|
|
|
|
`[@astrojs/deno] Otherwise, this adapter is not required to deploy a static site to Deno.`
|
|
|
|
);
|
2022-07-24 23:18:02 -05:00
|
|
|
}
|
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;
|
2023-02-17 13:19:19 -05:00
|
|
|
vite.resolve = vite.resolve ?? {};
|
|
|
|
vite.resolve.alias = vite.resolve.alias ?? {};
|
|
|
|
vite.build = vite.build ?? {};
|
|
|
|
vite.build.rollupOptions = vite.build.rollupOptions ?? {};
|
|
|
|
vite.build.rollupOptions.external = vite.build.rollupOptions.external ?? [];
|
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 = {
|
2023-06-15 09:20:35 -05:00
|
|
|
noExternal: COMPATIBLE_NODE_MODULES,
|
2022-03-30 07:42:19 -05:00
|
|
|
};
|
2023-02-17 13:19:19 -05:00
|
|
|
|
|
|
|
if (Array.isArray(vite.build.rollupOptions.external)) {
|
2023-02-17 13:21:11 -05:00
|
|
|
vite.build.rollupOptions.external.push(DENO_IMPORTS_SHIM);
|
2023-02-17 13:19:19 -05:00
|
|
|
} else if (typeof vite.build.rollupOptions.external !== 'function') {
|
2023-02-17 13:21:11 -05:00
|
|
|
vite.build.rollupOptions.external = [
|
|
|
|
vite.build.rollupOptions.external,
|
|
|
|
DENO_IMPORTS_SHIM,
|
|
|
|
];
|
2023-02-17 13:19:19 -05:00
|
|
|
}
|
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);
|
2023-02-17 10:58:53 -05:00
|
|
|
|
2022-06-06 11:02:13 -05:00
|
|
|
await esbuild.build({
|
2023-07-24 09:58:05 -05:00
|
|
|
target: 'esnext',
|
2022-06-06 11:02:13 -05:00
|
|
|
platform: 'browser',
|
|
|
|
entryPoints: [pth],
|
|
|
|
outfile: pth,
|
|
|
|
allowOverwrite: true,
|
|
|
|
format: 'esm',
|
|
|
|
bundle: true,
|
2023-06-15 09:20:35 -05:00
|
|
|
external: [
|
|
|
|
...COMPATIBLE_NODE_MODULES.map((mod) => `node:${mod}`),
|
|
|
|
'@astrojs/markdown-remark',
|
|
|
|
],
|
|
|
|
plugins: [denoImportsShimPlugin, denoRenameNodeModulesPlugin],
|
2022-06-27 16:20:28 -05:00
|
|
|
banner: {
|
|
|
|
js: SHIM,
|
2022-06-27 16:22:26 -05:00
|
|
|
},
|
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 =
|
2023-01-09 11:01:33 -05:00
|
|
|
_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
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|