Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
liuxingbaoyu committed Jul 2, 2023
1 parent d875c5f commit 2bfda7d
Show file tree
Hide file tree
Showing 35 changed files with 129 additions and 76 deletions.
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",
"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}
}
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

0 comments on commit 2bfda7d

Please sign in to comment.