-
-
Notifications
You must be signed in to change notification settings - Fork 6
Fix commonjs-extension-resolution-loader #10
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,34 @@ | ||
import { existsSync } from 'fs'; | ||
import { createRequire } from 'module'; | ||
import { builtinModules } from 'node:module'; | ||
import { dirname } from 'path'; | ||
import { URL, fileURLToPath, pathToFileURL } from 'url'; | ||
import { cwd } from 'process'; | ||
import { fileURLToPath, pathToFileURL } from 'url'; | ||
import { promisify } from 'util'; | ||
|
||
const require = createRequire(import.meta.url); | ||
const baseURL = pathToFileURL(process.cwd() + '/').href; | ||
import resolveCallback from 'resolve/async.js'; | ||
|
||
export function resolve(specifier, context, defaultResolve) { | ||
const resolveAsync = promisify(resolveCallback); | ||
|
||
const baseURL = pathToFileURL(cwd() + '/').href; | ||
|
||
|
||
export async function resolve(specifier, context, next) { | ||
const { parentURL = baseURL } = context; | ||
|
||
// `require.resolve` works with paths, not URLs, so convert to and from | ||
if (specifier.startsWith('node:') || builtinModules.includes(specifier)) { | ||
return next(specifier, context); | ||
} | ||
|
||
// `resolveAsync` works with paths, not URLs | ||
if (specifier.startsWith('file://')) { | ||
specifier = fileURLToPath(specifier); | ||
} | ||
const basePath = dirname(fileURLToPath(parentURL)); | ||
const resolvedPath = require.resolve(specifier, {paths: [basePath]}); | ||
const parentPath = fileURLToPath(parentURL); | ||
|
||
if (existsSync(resolvedPath)) { | ||
return { | ||
url: pathToFileURL(resolvedPath).href | ||
}; | ||
} | ||
const resolution = await resolveAsync(specifier, { | ||
basedir: dirname(parentPath), | ||
extensions: ['.js', '.json', '.node'], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder if loaders should be able to expose their own hooks, for example to accept new extensions ... without that, a TS loader would have to implement the same thing but with a different extension, which would require to perform the same filesystem checks multiple times 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So ambient loaders weren’t enough, now we need loaders for our loaders . . . There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think the way to achieve this is for loaders to optionally load config files. There’s nothing stopping me from adding some code to the top of One loader could communicate with the following ones by attaching something to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the sort of problem that I'm referring to when I say that the "helper functions" need to compose like hooks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, they'll share globalThis (as long as we keep ambient loaders and lay loaders in the same thread). But There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, the blocker right now for the helper functions is the API design. I took a stab at it in nodejs/loaders#94 but I feel like that pass leaves a lot to be desired. A similar example is in https://github.com/browserify/resolve#resolveid-opts-cb: see the functions it accepts in the options to override its own lower-level operations. |
||
}); | ||
const url = pathToFileURL(resolution).href; | ||
|
||
// Let Node.js handle all other specifiers, such as package names | ||
return defaultResolve(specifier, context, defaultResolve); | ||
return next(url, context); | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,9 @@ | |
"start": "npm test", | ||
"test": "node test.js" | ||
}, | ||
"author": "Geoffrey Booth <[email protected]>", | ||
"license": "MIT" | ||
"author": "Geoffrey Booth <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"resolve": "^1.22.1" | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,25 @@ | ||
import { ok } from 'assert'; | ||
import { match } from 'assert'; | ||
import { spawn } from 'child_process'; | ||
import { execPath } from 'process'; | ||
|
||
|
||
// Run this test yourself with debugging mode via: | ||
// node --inspect-brk --experimental-loader ./loader.js ./fixtures/index.js | ||
// node --inspect-brk --loader ./loader.js ./fixtures/index.js | ||
|
||
const child = spawn(execPath, [ | ||
'--experimental-loader', | ||
'--loader', | ||
'./loader.js', | ||
'./fixtures/index.js' | ||
]); | ||
|
||
let stdout = ''; | ||
child.stdout.setEncoding('utf8'); | ||
child.stdout.on('data', (data) => { | ||
child.stdout.on('data', data => { | ||
stdout += data; | ||
}); | ||
|
||
child.on('close', (code, signal) => { | ||
stdout = stdout.toString(); | ||
ok(stdout.includes('hello from file.js')); | ||
ok(stdout.includes('hello from folder/index.js')); | ||
child.on('close', (_code, _signal) => { | ||
stdout = stdout.toString(); | ||
match(stdout, /hello from file\.js/); | ||
match(stdout, /hello from folder\/index\.js/); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: I think there's
module.isBuiltin
now