Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node built-in modules support for Deno adapter #7288

Merged
merged 3 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/modern-lobsters-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/deno': minor
---

The deno adapter now supports npm package that require built-in node modules.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ jobs:
- name: Use Deno
uses: denoland/setup-deno@v1
with:
deno-version: v1.26.1
deno-version: v1.34.1

- name: Install dependencies
run: pnpm install
Expand Down
70 changes: 67 additions & 3 deletions packages/integrations/deno/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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,
},
Expand Down
6 changes: 6 additions & 0 deletions packages/integrations/deno/test/basics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ Deno.test({
assertEquals(h1!.innerText, 'test');
});

await t.step('node compatibility', async () => {
const resp = await fetch(new URL('/nodecompat', app.url));
assertEquals(resp.status, 200);
await resp.text();
});

app.stop();
},
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
// unprefixed node built-in module
import path from 'path'

// prefixed node built-in module
import os from 'node:os'
---
<body>
<a href={path.posix.basename('/public/myfile.html')}>Go to my file</a>
<details>
<summary>CPU Architecture</summary>
<code>{os.arch()}</code>
</details>
<p>Everything went fine.</p>
</body>
Loading