summaryrefslogtreecommitdiff
path: root/packages/integrations/deno/src
diff options
context:
space:
mode:
authorGravatar Arsh <69170106+lilnasy@users.noreply.github.com> 2023-06-15 19:50:35 +0530
committerGravatar GitHub <noreply@github.com> 2023-06-15 10:20:35 -0400
commitd67ae846103c42ff0464c578813749940326e8ba (patch)
tree4e5790c75863a4b6bd5220b6b74f602c80a8fbfe /packages/integrations/deno/src
parent3f1cb6b1a001fb03419a313f72c9f4846b890fe0 (diff)
downloadastro-d67ae846103c42ff0464c578813749940326e8ba.tar.gz
astro-d67ae846103c42ff0464c578813749940326e8ba.tar.zst
astro-d67ae846103c42ff0464c578813749940326e8ba.zip
Node built-in modules support for Deno adapter (#7288)
* feature(deno adapter): allow built-in node modules * Add changeset * format
Diffstat (limited to 'packages/integrations/deno/src')
-rw-r--r--packages/integrations/deno/src/index.ts70
1 files changed, 67 insertions, 3 deletions
diff --git a/packages/integrations/deno/src/index.ts b/packages/integrations/deno/src/index.ts
index 7e4a520ca..8b1f38959 100644
--- a/packages/integrations/deno/src/index.ts
+++ b/packages/integrations/deno/src/index.ts
@@ -21,6 +21,59 @@ const SHIM = `globalThis.process = {
};`;
const DENO_VERSION = `0.177.0`;
+// 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',
+];
// We shim deno-specific imports so we can run the code in Node
// to prerender pages. In the final Deno build, this import is
@@ -51,6 +104,14 @@ const denoImportsShimPlugin = {
},
};
+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 }));
+ },
+};
+
export default function createIntegration(args?: Options): AstroIntegration {
let _buildConfig: BuildConfig;
let _vite: any;
@@ -89,7 +150,7 @@ export default function createIntegration(args?: Options): AstroIntegration {
}
}
vite.ssr = {
- noExternal: true,
+ noExternal: COMPATIBLE_NODE_MODULES,
};
if (Array.isArray(vite.build.rollupOptions.external)) {
@@ -114,8 +175,11 @@ export default function createIntegration(args?: Options): AstroIntegration {
allowOverwrite: true,
format: 'esm',
bundle: true,
- external: ['@astrojs/markdown-remark'],
- plugins: [denoImportsShimPlugin],
+ external: [
+ ...COMPATIBLE_NODE_MODULES.map((mod) => `node:${mod}`),
+ '@astrojs/markdown-remark',
+ ],
+ plugins: [denoImportsShimPlugin, denoRenameNodeModulesPlugin],
banner: {
js: SHIM,
},