Skip to content
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

feat: make require.resolve respect ignoreDynamicRequires #1530

Closed
wants to merge 1 commit into from

Conversation

liuxingbaoyu
Copy link

Rollup Plugin Name: @rollup/plugin-commonjs

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.

(I'm not sure if this is a breaking change, since it seems unnecessary to allow require.resolve when disallowing require)

List any relevant issue numbers:
Fixes #1472
Fixes babel/babel#14301

Description

PR consists of two parts
The first part is to allow the project to run pnpm build on Windows.
The second part is to make require.resolve respect ignoreDynamicRequires.

@@ -30,7 +30,7 @@
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
"ci:test": "pnpm test -- --verbose",
"prebuild": "del-cli dist",
"prepare": "if [ ! -d 'dist' ]; then pnpm build; fi",
"prepare": "node -e \"fs.existsSync('./dist') || process.exit(1)\" || pnpm build",
Copy link
Member

Choose a reason for hiding this comment

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

This is a great idea to make it platform independent, but this will mess up release as all plugins will get a new release with a confusing error message. This should be put into a separate PR.

}␊
handleRequire.resolve = function (path) {␊
var resolvedPath = commonjsResolve(path, originalModuleDir);␊
if (resolvedPath !== null) {␊
return resolvedPath;␊
}␊
return require.resolve(path);␊
throw new Error('Could not dynamically require/require.resolve "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');␊
Copy link
Member

Choose a reason for hiding this comment

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

IMO it is unfortunate to repeat this huge error message twice in the user's code. And if we do, then we do not need the require/require.resolve duality in the message because we can use two separate messages. But I would prefer to find a way to not repeat it (and possibly shorten it if this is possible without loss in information).

@@ -116,7 +119,7 @@ export function ${CREATE_COMMONJS_REQUIRE_EXPORT}(originalModuleDir) {
if (resolvedPath !== null) {
return resolvedPath;
}
return require.resolve(path);
${ignoreDynamicRequires ? 'return require.resolve(path);' : FAILED_REQUIRE_ERROR}
Copy link
Member

Choose a reason for hiding this comment

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

This makes it a breaking change. I.e. where before we had a fallback to require.resolve, we now need to potentially configure more require targets. Which is fine, but we need to take care to make this a major release.

@shellscape
Copy link
Collaborator

@liuxingbaoyu please see the previous review. If this has no action after 60 days we'll consider it abandoned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants