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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/alias/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.

"prerelease": "pnpm build",
"pretest": "pnpm build",
"release": "pnpm --workspace-root plugin:release --pkg $npm_package_name",
Expand Down
2 changes: 1 addition & 1 deletion packages/auto-install/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"prerelease": "pnpm build",
"pretest": "pnpm build",
"release": "pnpm --workspace-root plugin:release --pkg $npm_package_name",
Expand Down
2 changes: 1 addition & 1 deletion packages/buble/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"prerelease": "pnpm build",
"pretest": "pnpm build",
"release": "pnpm --workspace-root plugin:release --pkg $npm_package_name",
Expand Down
2 changes: 1 addition & 1 deletion packages/commonjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"ci:lint:commits": "commitlint --from=${CIRCLE_BRANCH} --to=${CIRCLE_SHA1}",
"ci:test": "pnpm test -- --verbose && pnpm test:ts",
"prebuild": "del-cli dist",
"prepare": "if [ ! -d 'dist' ]; then pnpm build; fi",
"prepare": "node -e \"fs.existsSync('./dist') || process.exit(1)\" || pnpm build",
"prepublishOnly": "pnpm build",
"prerelease": "pnpm build",
"pretest": "pnpm build",
Expand Down
11 changes: 7 additions & 4 deletions packages/commonjs/src/dynamic-modules.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export function getDynamicRequireModules(patterns, dynamicRequireRoot) {
};
}

const FAILED_REQUIRE_ERROR = `throw new Error('Could not dynamically require "' + path + '". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.');`;
const FAILED_REQUIRE_ERROR = `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.');`;

export const COMMONJS_REQUIRE_EXPORT = 'commonjsRequire';
export const CREATE_COMMONJS_REQUIRE_EXPORT = 'createCommonjsRequire';
Expand All @@ -73,9 +73,12 @@ export function getDynamicModuleRegistry(
ignoreDynamicRequires
) {
if (!isDynamicRequireModulesEnabled) {
return `export function ${COMMONJS_REQUIRE_EXPORT}(path) {
return `function ${COMMONJS_REQUIRE_EXPORT}(path) {
${FAILED_REQUIRE_ERROR}
}`;
}
${COMMONJS_REQUIRE_EXPORT}.resolve = ${COMMONJS_REQUIRE_EXPORT};
export {${COMMONJS_REQUIRE_EXPORT}};
`;
}
const dynamicModuleImports = [...dynamicRequireModules.values()]
.map(
Expand Down Expand Up @@ -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.

}
return handleRequire;
}
Expand Down
5 changes: 3 additions & 2 deletions packages/commonjs/src/transform-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,13 @@ export default async function transformCommonjs(

// Transform require.resolve
if (
isDynamicRequireModulesEnabled &&
node.callee.object &&
isRequire(node.callee.object, scope) &&
node.callee.property.name === 'resolve'
) {
checkDynamicRequire(node.start);
if (isDynamicRequireModulesEnabled) {
checkDynamicRequire(node.start);
}
uses.require = true;
const requireNode = node.callee.object;
replacedDynamicRequires.push(requireNode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ t.is(takeModule('submodule2.js'), 'submodule2');
t.is(takeModule('extramodule1.js'), 'extramodule1');
t.throws(() => takeModule('extramodule2.js'), {
message:
'Could not dynamically require "./extramodule2.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'
'Could not dynamically require/require.resolve "./extramodule2.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ function takeModule(withName) {

t.throws(() => takeModule('./dep.js'), {
message:
'Could not dynamically require "./dep.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'
'Could not dynamically require/require.resolve "./dep.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
description: 'throws when there is no fallback for a dynamic require.resolve call',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable import/no-dynamic-require, global-require */

function takeModule(withName) {
return require.resolve(withName);
}

t.throws(() => takeModule('./dep.js'), {
message:
'Could not dynamically require/require.resolve "./dep.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ function takeModule(withName) {
t.is(takeModule('./dep1.js'), 'dep');
t.throws(() => takeModule('./dep2.js'), {
message:
'Could not dynamically require "./dep2.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'
'Could not dynamically require/require.resolve "./dep2.js". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option of @rollup/plugin-commonjs appropriately for this require call to work.'
});
Loading
Loading