Skip to content

Commit

Permalink
fix: get dependencies during evaluation (#338)
Browse files Browse the repository at this point in the history
closes #330
  • Loading branch information
satya164 authored and zamotany committed Mar 6, 2019
1 parent ba45d81 commit a2a98bc
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 37 deletions.
2 changes: 1 addition & 1 deletion src/__tests__/__snapshots__/preval.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ CSS:
;
}
Dependencies: NA
Dependencies: core-js/modules/es6.string.raw
`;
Expand Down
38 changes: 2 additions & 36 deletions src/babel/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,41 +107,6 @@ module.exports = function evaluate(
});
}

// Collect the list of dependencies that we import
const dependencies = requirements.reduce((deps, req) => {
if (t.isImportDeclaration(req.path.parentPath)) {
deps.push(req.path.parentPath.node.source.value);
} else {
req.path.traverse({
CallExpression(p) {
const { callee, arguments: args } = p.node;

let name;

if (callee.name === 'require' && args.length === 1) {
if (
args[0].type === 'Literal' ||
args[0].type === 'StringLiteral'
) {
name = args[0].value;
} else if (
args[0].type === 'TemplateLiteral' &&
args[0].quasis.length === 1
) {
name = args[0].quasis[0].value.cooked;
}
}

if (name) {
deps.push(name);
}
},
});
}

return deps;
}, []);

const expression = t.expressionStatement(
t.assignmentExpression(
'=',
Expand Down Expand Up @@ -182,6 +147,7 @@ module.exports = function evaluate(

const m = new Module(filename);

m.dependencies = [];
m.transform =
typeof transformer !== 'undefined'
? transformer
Expand Down Expand Up @@ -278,6 +244,6 @@ module.exports = function evaluate(

return {
value: m.exports,
dependencies,
dependencies: ((m.dependencies: any): string[]),
};
};
4 changes: 4 additions & 0 deletions src/babel/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ class Module {

extensions: string[];

dependencies: ?(string[]);

transform: ?(text: string) => { code: string };

constructor(filename: string) {
Expand Down Expand Up @@ -159,6 +161,8 @@ class Module {
);
}

this.dependencies && this.dependencies.push(id);

let m = cache[filename];

if (!m) {
Expand Down

0 comments on commit a2a98bc

Please sign in to comment.