Skip to content

Commit

Permalink
fix(commonjs): prevent rewrite require.resolve (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
DorianBlues authored Jul 10, 2020
1 parent 3fb72b6 commit 30a8c91
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/commonjs/src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ export function transformCommonjs(
return true;
}

function isNodeRequireStatement(parent) {
const reservedMethod = ['resolve', 'cache', 'main'];
if (parent && parent.property && reservedMethod.indexOf(parent.property.name) > -1) {
return true;
}
return false;
}

function isIgnoredRequireStatement(requiredNode) {
return ignoreRequire(requiredNode.arguments[0].value);
}
Expand Down Expand Up @@ -353,6 +361,10 @@ export function transformCommonjs(
if (isReference(node, parent) && !scope.contains(node.name)) {
if (node.name in uses) {
if (isRequireIdentifier(node)) {
if (isNodeRequireStatement(parent)) {
return;
}

if (!isDynamicRequireModulesEnabled && isStaticRequireStatement(parent)) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.getFilePath = function getFilePath(someFile) {
return require.resolve(someFile);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var getFilePath = function getFilePath(someFile) {
return require.resolve(someFile);
};

var input = {
getFilePath: getFilePath
};

export default input;
export { input as __moduleExports };
export { getFilePath };

0 comments on commit 30a8c91

Please sign in to comment.