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(register): skip load files in node_modules #828

Merged
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
1 change: 1 addition & 0 deletions packages/integrate-module/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@types/react-dom": "^18.3.0",
"esmock": "^2.6.6",
"ipaddr.js": "^2.2.0",
"postgres": "^3.4.4",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"simple-git": "^3.25.0",
Expand Down
7 changes: 7 additions & 0 deletions packages/integrate-module/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { supportedExtensions } from 'file-type'
import { renderToString } from 'react-dom/server'
import { simpleGit } from 'simple-git'
import ipaddr from 'ipaddr.js'
import postgres from 'postgres'

import { CompiledClass } from './compiled.js'
import cjs from './cjs'
Expand Down Expand Up @@ -90,3 +91,9 @@ await test('esmock should work', async () => {

assert.strictEqual(main.pathbasenamewrap(), 'hello')
})

await test('postgres should work', async () => {
postgres({
host: 'postgres://localhost',
})
})
19 changes: 14 additions & 5 deletions packages/register/esm.mts
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,7 @@ export const resolve: ResolveHook = async (specifier, context, nextResolve) => {
}

// local project file
if (
path &&
((process.platform !== 'win32' && !path.includes('/node_modules/')) ||
(process.platform === 'win32' && !path.includes('\\node_modules\\')))
) {
if (path && isPathNotInNodeModules(path)) {
debug('resolved: typescript', specifier, path)
const url = new URL(join('file://', path))
return addShortCircuitSignal({
Expand Down Expand Up @@ -272,6 +268,12 @@ export const load: LoadHook = async (url, context, nextLoad) => {
return nextLoad(url, context)
}

if (url.includes('/node_modules/')) {
debug('skip load: node_modules', url)

return nextLoad(url, context)
}

if (['builtin', 'json', 'wasm'].includes(context.format)) {
debug('loaded: internal format', url)
return nextLoad(url, context)
Expand Down Expand Up @@ -300,3 +302,10 @@ export const load: LoadHook = async (url, context, nextLoad) => {
source: compiled,
})
}

function isPathNotInNodeModules(path: string) {
return (
(process.platform !== 'win32' && !path.includes('/node_modules/')) ||
(process.platform === 'win32' && !path.includes('\\node_modules\\'))
)
}
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

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