Skip to content

Commit

Permalink
fix(register): skip load files in node_modules (#828)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn authored Jul 17, 2024
1 parent af98621 commit f6816c1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
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.

0 comments on commit f6816c1

Please sign in to comment.