-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Get commonjs-extension-resolution-loader working
- Loading branch information
1 parent
35765c7
commit d17c0ea
Showing
4 changed files
with
149 additions
and
21 deletions.
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,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'], | ||
}); | ||
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.
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 |
---|---|---|
|
@@ -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" | ||
This comment has been minimized.
Sorry, 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
You may want to update to next (v2 prerelease) here