Skip to content

Conversation

pabloerhard
Copy link

@pabloerhard pabloerhard commented Oct 2, 2025

This PR adds compatibility with specifiers, for example:

module.exports = require('#main-entry-point');

These specifiers are defined inside the package.json file and can reference either a local file or an external module.

The proposed solution works as follows: whenever a require call begins with # (all specifiers must start with #), locates the package.json, extracts the corresponding reference from the imports field, and sets the newUrl with the resolved path, maintains any original process after that.

jsumners-nr
jsumners-nr previously approved these changes Oct 2, 2025
Copy link
Contributor

@jsumners-nr jsumners-nr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks reasonable with linting fixed.


// Look for path inside packageJson
let resolvedPath
if (typeof imports == 'object') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A better check is Object.prototype.toString.call(imports) === '[object Object]'.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsumners-nr I am surprised by that suggestion (when I just looked at it, I wanted to recommend to check for typeof and not null). That should be much faster and should suffice for the overall check here, if I am not mistaken. Am I missing something / what would the benefit be for calling toString()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I understand, checking with toString() ensures we don't allow values such as [ ], null or Date, which are also of typeof 'object'

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand that it would not match these anymore. We just check for the property existence on these objects and they should never have the properties we are looking for. Due to that, I favor the faster check.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I'll add that into the latest changes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BridgeAR the original check was a coercive check for an Object. Various things, e.g Array, would mistakenly satisfy that check.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsumners-nr I reinstated it to the original check after considering that the code only performs operations based on certain properties, which are checked for existence before being used. This allows us to maintain the most efficient check.

@pabloerhard pabloerhard force-pushed the pabloerhard/fix-to-specifier-imports branch from 21de0c1 to a153042 Compare October 2, 2025 15:59
timfish
timfish previously approved these changes Oct 3, 2025
@timfish timfish changed the title Added compatibility for specifier imports feat: Specifier imports compatibility Oct 3, 2025
@timfish timfish changed the title feat: Specifier imports compatibility feat: Compatibility with specifier imports Oct 3, 2025
@timfish timfish requested a review from bengl October 4, 2025 10:11
AbhiPrasad
AbhiPrasad previously approved these changes Oct 5, 2025
return null
}
}
} catch (error) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} catch (error) {
} catch {

Alternatively, we could also just add the cause

@pabloerhard pabloerhard force-pushed the pabloerhard/fix-to-specifier-imports branch from 3a000d2 to 1f7a010 Compare October 6, 2025 21:20
@pabloerhard pabloerhard force-pushed the pabloerhard/fix-to-specifier-imports branch from 1f7a010 to 2022e5e Compare October 7, 2025 02:08
timfish
timfish previously approved these changes Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants