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

fix(edge): externalize node builtins #317

Merged
merged 3 commits into from
May 13, 2024
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
2 changes: 1 addition & 1 deletion .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx --no -- commitlint --edit ${1}
pnpm exec -- commitlint --edit
2 changes: 2 additions & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public-hoist-pattern[]=@commitlint*
public-hoist-pattern[]=commitlint
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
},
"homepage": "https://github.com/netlify/remix-compute#readme",
"devDependencies": {
"@commitlint/cli": "^19.3.0",
"@netlify/eslint-config-node": "^7.0.1",
"@playwright/test": "^1.44.0",
"@remix-run/eslint-config": "^2.8.1",
"@types/node": "^20.0.0",
"@typescript-eslint/eslint-plugin": "^5.50.0",
"@typescript-eslint/parser": "^5.50.0",
"commitlint": "^19.0.0",
"eslint": "^8.33.0",
"eslint-plugin-playwright": "^1.6.0",
"eslint-prettier-config": "^1.0.1",
Expand Down
61 changes: 38 additions & 23 deletions packages/remix-edge-adapter/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { writeFile, mkdir, readdir } from 'node:fs/promises'
import { join, relative, sep } from 'node:path'
import { sep as posixSep } from 'node:path/posix'
import { version, name } from '../package.json'
import { isBuiltin } from 'node:module'

const SERVER_ID = 'virtual:netlify-server'
const RESOLVED_SERVER_ID = `\0${SERVER_ID}`
Expand Down Expand Up @@ -46,7 +47,8 @@ export function netlifyPlugin(): Plugin {
config.ssr = {
...config.ssr,
target: 'webworker',
noExternal: true,
// Only externalize Node builtins
noExternal: /^(?!node:).*$/,
}
// We need to add an extra entrypoint, as we need to compile
// the server entrypoint too. This is because it uses virtual
Expand All @@ -67,29 +69,42 @@ export function netlifyPlugin(): Plugin {
async configResolved(config) {
resolvedConfig = config
},
async resolveId(source, importer, options) {
// Conditionally resolve the server entry based on the command.
// The Vite dev server uses Node, so we use a different entrypoint
if (source === 'virtual:netlify-server-entry') {
if (currentCommand === 'build' && options.ssr) {
// This is building for edge functions, so use the edge adapter
return this.resolve('@netlify/remix-edge-adapter/entry.server', importer, {
...options,
skipSelf: true,
})
} else {
// This is building for the dev server, so use the Node adapter
return this.resolve('@remix-run/dev/dist/config/defaults/entry.server.node', importer, {
...options,
skipSelf: true,
})

resolveId: {
order: 'pre',
async handler(source, importer, options) {
// Conditionally resolve the server entry based on the command.
// The Vite dev server uses Node, so we use a different entrypoint
if (source === 'virtual:netlify-server-entry') {
if (currentCommand === 'build' && options.ssr) {
// This is building for edge functions, so use the edge adapter
return this.resolve('@netlify/remix-edge-adapter/entry.server', importer, {
...options,
skipSelf: true,
})
} else {
// This is building for the dev server, so use the Node adapter
return this.resolve('@remix-run/dev/dist/config/defaults/entry.server.node', importer, {
...options,
skipSelf: true,
})
}
}
}
// Our virtual entrypoint module
if (source === SERVER_ID) {
return RESOLVED_SERVER_ID
}
return null
// Our virtual entrypoint module
if (source === SERVER_ID) {
return RESOLVED_SERVER_ID
}

if (isSsr && isBuiltin(source)) {
return {
// Deno needs Node builtins to be prefixed
id: source.startsWith('node:') ? source : `node:${source}`,
external: true,
moduleSideEffects: false,
}
}
return null
},
},
load(id) {
if (id === RESOLVED_SERVER_ID) {
Expand Down
18 changes: 3 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading