-
Notifications
You must be signed in to change notification settings - Fork 7
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
commonjs-extension-resolution-loader: add experimental-specifier-resolution tests #11
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
35765c7
Cleanup existing code
GeoffreyBooth d17c0ea
Get commonjs-extension-resolution-loader working
GeoffreyBooth fea414b
Make way for porting Node's tests
GeoffreyBooth c6f86f3
Port experimental-specifier-resolution tests from Node codebase
GeoffreyBooth ed9a48e
Update loader to throw same error code as Node for module not found; …
GeoffreyBooth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,45 @@ | ||
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 | ||
}; | ||
let url; | ||
try { | ||
const resolution = await resolveAsync(specifier, { | ||
basedir: dirname(parentPath), | ||
// For whatever reason, --experimental-specifier-resolution=node doesn't search for .mjs extensions | ||
// but it does search for index.mjs files within directories | ||
extensions: ['.js', '.json', '.node', '.mjs'], | ||
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. Nit: no need to re-construct this list every time |
||
}); | ||
url = pathToFileURL(resolution).href; | ||
} catch (error) { | ||
if (error.code === 'MODULE_NOT_FOUND') { | ||
// Match Node's error code | ||
error.code = 'ERR_MODULE_NOT_FOUND'; | ||
} | ||
throw error; | ||
} | ||
|
||
// 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.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,11 @@ | |
"main": "./loader.js", | ||
"scripts": { | ||
"start": "npm test", | ||
"test": "node test.js" | ||
"test": "node test/basic.js && node test/test-esm-specifiers.js && node test/test-esm-specifiers-symlink.js" | ||
}, | ||
"author": "Geoffrey Booth <[email protected]>", | ||
"license": "MIT" | ||
"author": "Geoffrey Booth <[email protected]>", | ||
"license": "MIT", | ||
"dependencies": { | ||
"resolve": "^1.22.1" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { match } from 'assert'; | ||
import { spawn } from 'child_process'; | ||
import { execPath } from 'process'; | ||
|
||
|
||
// Run this test yourself with debugging mode via: | ||
// node --inspect-brk --loader ./loader.js ./fixtures/index.js | ||
|
||
const child = spawn(execPath, [ | ||
'--loader', | ||
'./loader.js', | ||
'./test/basic-fixtures/index.js' | ||
]); | ||
|
||
let stdout = ''; | ||
child.stdout.setEncoding('utf8'); | ||
child.stdout.on('data', data => { | ||
stdout += data; | ||
}); | ||
|
||
child.on('close', (_code, _signal) => { | ||
stdout = stdout.toString(); | ||
match(stdout, /hello from file\.js/); | ||
match(stdout, /hello from folder\/index\.js/); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Ported from https://github.com/nodejs/node/blob/45f2258f74117e27ffced434a98ad6babc14f7fa/test/common/index.js | ||
|
||
import { spawn } from 'node:child_process'; | ||
|
||
|
||
export function spawnPromisified(...args) { | ||
let stderr = ''; | ||
let stdout = ''; | ||
|
||
const child = spawn(...args); | ||
child.stderr.setEncoding('utf8'); | ||
child.stderr.on('data', (data) => { stderr += data; }); | ||
child.stdout.setEncoding('utf8'); | ||
child.stdout.on('data', (data) => { stdout += data; }); | ||
|
||
return new Promise((resolve, reject) => { | ||
child.on('close', (code, signal) => { | ||
resolve({ | ||
code, | ||
signal, | ||
stderr, | ||
stdout, | ||
}); | ||
}); | ||
child.on('error', (code, signal) => { | ||
reject({ | ||
code, | ||
signal, | ||
stderr, | ||
stdout, | ||
}); | ||
}); | ||
}); | ||
} |
11 changes: 11 additions & 0 deletions
11
commonjs-extension-resolution-loader/test/fixtures/es-module-specifiers/index.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import explicit from 'explicit-main'; | ||
import implicit from 'implicit-main'; | ||
import implicitModule from 'implicit-main-type-module'; | ||
import noMain from 'no-main-field'; | ||
|
||
function getImplicitCommonjs () { | ||
return import('implicit-main-type-commonjs'); | ||
} | ||
|
||
export {explicit, implicit, implicitModule, getImplicitCommonjs, noMain}; | ||
export default 'success'; |
1 change: 1 addition & 0 deletions
1
...extension-resolution-loader/test/fixtures/es-module-specifiers/package-type-commonjs/a.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = 'a'; |
1 change: 1 addition & 0 deletions
1
...xtension-resolution-loader/test/fixtures/es-module-specifiers/package-type-commonjs/b.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const b = 'b'; |
5 changes: 5 additions & 0 deletions
5
...xtension-resolution-loader/test/fixtures/es-module-specifiers/package-type-commonjs/c.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
one: 1, | ||
two: 2, | ||
three: 3 | ||
}; |
21 changes: 21 additions & 0 deletions
21
...sion-resolution-loader/test/fixtures/es-module-specifiers/package-type-commonjs/index.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// js file that is common.js | ||
import a from './a.js'; | ||
// ESM with named export | ||
import {b} from './b.mjs'; | ||
// import 'c.cjs'; | ||
import cjs from './c.cjs'; | ||
// proves cross boundary fun bits | ||
import jsAsEsm from '../package-type-module/a.js'; | ||
|
||
// named export from core | ||
import {strictEqual, deepStrictEqual} from 'assert'; | ||
|
||
strictEqual(a, jsAsEsm); | ||
strictEqual(b, 'b'); | ||
deepStrictEqual(cjs, { | ||
one: 1, | ||
two: 2, | ||
three: 3 | ||
}); | ||
|
||
export default 'commonjs'; |
3 changes: 3 additions & 0 deletions
3
...n-resolution-loader/test/fixtures/es-module-specifiers/package-type-commonjs/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "commonjs" | ||
} |
1 change: 1 addition & 0 deletions
1
...s-extension-resolution-loader/test/fixtures/es-module-specifiers/package-type-module/a.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default 'a' |
1 change: 1 addition & 0 deletions
1
...-extension-resolution-loader/test/fixtures/es-module-specifiers/package-type-module/b.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const b = 'b'; |
5 changes: 5 additions & 0 deletions
5
...-extension-resolution-loader/test/fixtures/es-module-specifiers/package-type-module/c.cjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
one: 1, | ||
two: 2, | ||
three: 3 | ||
}; |
21 changes: 21 additions & 0 deletions
21
...tension-resolution-loader/test/fixtures/es-module-specifiers/package-type-module/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// ESM with only default | ||
import a from './a.js'; | ||
// ESM with named export | ||
import {b} from './b.mjs'; | ||
// import 'c.cjs'; | ||
import cjs from './c.cjs'; | ||
// import across boundaries | ||
import jsAsCjs from '../package-type-commonjs/a.js' | ||
|
||
// named export from core | ||
import {strictEqual, deepStrictEqual} from 'assert'; | ||
|
||
strictEqual(a, jsAsCjs); | ||
strictEqual(b, 'b'); | ||
deepStrictEqual(cjs, { | ||
one: 1, | ||
two: 2, | ||
three: 3 | ||
}); | ||
|
||
export default 'module'; |
3 changes: 3 additions & 0 deletions
3
...ion-resolution-loader/test/fixtures/es-module-specifiers/package-type-module/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "module" | ||
} |
1 change: 1 addition & 0 deletions
1
commonjs-extension-resolution-loader/test/fixtures/es-module-specifiers/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
TLDR: you might need to wrap the
cwd()
in atry
/catch
.In
internal/process/esm_loader.js
, this cwd is wrapped in a try/catch. I don't remember why (I asked and Bradley answered, and I think I forgot document the reason in a comment).