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

fix: get dependencies during evaluation #338

Merged
merged 1 commit into from
Mar 6, 2019
Merged
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 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