-
Notifications
You must be signed in to change notification settings - Fork 38
feat: Compatibility with specifier imports #211
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
Open
pabloerhard
wants to merge
5
commits into
nodejs:main
Choose a base branch
from
pabloerhard:pabloerhard/fix-to-specifier-imports
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+131
−3
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c9b2a33
Added compatibility for specifier imports
pabloerhard a153042
Fixed lint issues and failing tests
pabloerhard 6e3fe6a
Fixed issues when nested folders and added test for specifier and esm
pabloerhard 2022e5e
Reinstated existsSync approach and added suggested improvements
pabloerhard f9c7f54
Added extra check for imports object
pabloerhard 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 hidden or 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 | ||||
---|---|---|---|---|---|---|
|
@@ -2,10 +2,10 @@ | |||||
|
||||||
const getEsmExports = require('./get-esm-exports.js') | ||||||
const { parse: parseCjs } = require('cjs-module-lexer') | ||||||
const { readFileSync } = require('fs') | ||||||
const { readFileSync, existsSync } = require('fs') | ||||||
const { builtinModules } = require('module') | ||||||
const { fileURLToPath, pathToFileURL } = require('url') | ||||||
const { dirname } = require('path') | ||||||
const { dirname, join } = require('path') | ||||||
|
||||||
function addDefault (arr) { | ||||||
return new Set(['default', ...arr]) | ||||||
|
@@ -27,6 +27,63 @@ function getExportsForNodeBuiltIn (name) { | |||||
|
||||||
const urlsBeingProcessed = new Set() // Guard against circular imports. | ||||||
|
||||||
/** | ||||||
* This function looks for the package.json which contains the specifier trying to resolve. | ||||||
* Once the package.json file has been found, we extract the file path from the specifier | ||||||
* @param {string} specifier The specifier that is being search for inside the imports object | ||||||
* @param {URL|string} fromUrl The url from which the search starts from | ||||||
* @returns array with url and resolvedExport | ||||||
*/ | ||||||
function resolvePackageImports (specifier, fromUrl) { | ||||||
try { | ||||||
const fromPath = fileURLToPath(fromUrl) | ||||||
let currentDir = dirname(fromPath) | ||||||
|
||||||
// search for package.json file which has the real url to export | ||||||
while (currentDir !== dirname(currentDir)) { | ||||||
const packageJsonPath = join(currentDir, 'package.json') | ||||||
|
||||||
if (existsSync(packageJsonPath)) { | ||||||
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8')) | ||||||
if (packageJson.imports && packageJson.imports[specifier]) { | ||||||
const imports = packageJson.imports[specifier] | ||||||
|
||||||
// Look for path inside packageJson | ||||||
let resolvedExport | ||||||
if (typeof imports === 'object') { | ||||||
const requireExport = imports.require | ||||||
const importExport = imports.import | ||||||
// look for the possibility of require and import which is standard for CJS/ESM | ||||||
if (requireExport || importExport) { | ||||||
// trying to resolve based on order of importance | ||||||
resolvedExport = requireExport.node || requireExport.default || importExport.node || importExport.default | ||||||
} else if (imports.node || imports.default) { | ||||||
resolvedExport = imports.node || imports.default | ||||||
} | ||||||
} else if (typeof imports === 'string') { | ||||||
resolvedExport = imports | ||||||
} | ||||||
|
||||||
if (resolvedExport) { | ||||||
const url = resolvedExport.startsWith('.') | ||||||
? pathToFileURL(join(currentDir, resolvedExport)) | ||||||
: fromUrl | ||||||
return [url, resolvedExport] | ||||||
} | ||||||
} | ||||||
// return if we find a package.json but did not find an import | ||||||
return null | ||||||
} | ||||||
|
||||||
currentDir = dirname(currentDir) | ||||||
} | ||||||
} catch (error) { | ||||||
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.
Suggested change
Alternatively, we could also just add the |
||||||
throw new Error(`Failed to find export: ${specifier}`) | ||||||
} | ||||||
|
||||||
return null | ||||||
} | ||||||
|
||||||
async function getCjsExports (url, context, parentLoad, source) { | ||||||
if (urlsBeingProcessed.has(url)) { | ||||||
return [] | ||||||
|
@@ -46,7 +103,14 @@ async function getCjsExports (url, context, parentLoad, source) { | |||||
if (re === '.') { | ||||||
re = './' | ||||||
} | ||||||
// Resolve the re-exported module relative to the current module. | ||||||
|
||||||
// Entries in the import field should always start with # | ||||||
if (re.startsWith('#')) { | ||||||
const resolved = resolvePackageImports(re, url) | ||||||
if (!resolved) return | ||||||
[url, re] = resolved | ||||||
} | ||||||
|
||||||
const newUrl = pathToFileURL(require.resolve(re, { paths: [dirname(fileURLToPath(url))] })).href | ||||||
|
||||||
if (newUrl.endsWith('.node') || newUrl.endsWith('.json')) { | ||||||
|
This file contains hidden or 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 = { ...require('#main-entry-point') } |
This file contains hidden or 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,17 @@ | ||
{ | ||
"name": "test-fixtures", | ||
"imports": { | ||
"#main-entry-point": { | ||
"require": { | ||
"node": "./something.js", | ||
"default": "./something.js" | ||
}, | ||
"import": { | ||
"node":"./something.mjs", | ||
"default": "./something.mjs" | ||
} | ||
}, | ||
"#main-entry-point-string" : "./something.js", | ||
"#main-entry-point-external" : "some-external-cjs-module" | ||
} | ||
} |
This file contains hidden or 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 = { ...require('#main-entry-point-external') } |
This file contains hidden or 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 = { ...require('#main-entry-point-string') } |
This file contains hidden or 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 * from '#main-entry-point' |
This file contains hidden or 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 { foo } from '../fixtures/specifier-external.js' | ||
import Hook from '../../index.js' | ||
import { strictEqual } from 'assert' | ||
|
||
Hook((exports, name) => { | ||
if (name.endsWith('fixtures/specifier-external.js')) { | ||
exports.foo = 'bar2' | ||
} | ||
}) | ||
|
||
strictEqual(foo, 'bar2') |
This file contains hidden or 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,10 @@ | ||
import { foo } from '../fixtures/specifier.mjs' | ||
import Hook from '../../index.js' | ||
import { strictEqual } from 'assert' | ||
Hook((exports, name) => { | ||
if (name.endsWith('fixtures/specifier.mjs')) { | ||
exports.foo = 1 | ||
} | ||
}) | ||
|
||
strictEqual(foo, 1) |
This file contains hidden or 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 { foo } from '../fixtures/nested-folder/specifier.js' | ||
import Hook from '../../index.js' | ||
import { strictEqual } from 'assert' | ||
|
||
Hook((exports, name) => { | ||
if (name.endsWith('fixtures/nested-folder/specifier.js')) { | ||
exports.foo = 1 | ||
} | ||
}) | ||
|
||
strictEqual(foo, 1) |
This file contains hidden or 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 { foo } from '../fixtures/specifier-string.js' | ||
import Hook from '../../index.js' | ||
import { strictEqual } from 'assert' | ||
|
||
Hook((exports, name) => { | ||
if (name.endsWith('fixtures/specifier-string.js')) { | ||
exports.foo = 1 | ||
} | ||
}) | ||
|
||
strictEqual(foo, 1) |
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.
Uh oh!
There was an error while loading. Please reload this page.