Skip to content

Commit

Permalink
feat(commonjs): prevent rewrite require.resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
DorianBlues committed Jun 10, 2020
1 parent 39eb61c commit a6fee67
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
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
@@ -0,0 +1,3 @@
module.exports.getFilePath = function getFilePath(someFile) {
return require.resolve(someFile);
};
@@ -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 a6fee67

Please sign in to comment.