-
Notifications
You must be signed in to change notification settings - Fork 413
/
evaluate.js
191 lines (165 loc) · 4.92 KB
/
evaluate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* @flow */
import type { Options as PluginOptions } from './extract';
const generator = require('@babel/generator').default;
const babel = require('@babel/core');
const Module = require('./module');
const resolve = (path, t, requirements) => {
const binding = path.scope.getBinding(path.node.name);
if (
path.isReferenced() &&
binding &&
binding.kind !== 'param' &&
!requirements.some(req => req.path === binding.path)
) {
let result;
switch (binding.kind) {
case 'module':
if (t.isImportSpecifier(binding.path)) {
result = t.importDeclaration(
[binding.path.node],
binding.path.parentPath.node.source
);
} else {
result = binding.path.parentPath.node;
}
break;
case 'const':
case 'let':
case 'var': {
result = t.variableDeclaration(binding.kind, [binding.path.node]);
break;
}
default:
result = binding.path.node;
break;
}
const { loc } = binding.path.node;
requirements.push({
result,
path: binding.path,
start: loc.start,
end: loc.end,
});
binding.path.traverse({
Identifier(p) {
resolve(p, t, requirements);
},
});
}
};
module.exports = function evaluate(
path: any,
t: any,
filename: string,
transformer?: (text: string) => { code: string },
options?: PluginOptions
) {
const requirements = [];
if (t.isIdentifier(path)) {
resolve(path, t, requirements);
} else {
path.traverse({
Identifier(p) {
resolve(p, t, requirements);
},
});
}
// 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(
'=',
t.memberExpression(t.identifier('module'), t.identifier('exports')),
path.node
)
);
// Preserve source order
requirements.sort((a, b) => {
if (a.start.line === b.start.line) {
return a.start.column - b.start.column;
}
return a.start.line - b.start.line;
});
// We'll wrap each code in a block to avoid collisions in variable names
// We separate out the imports since they cannot be inside blocks
const { imports, others } = requirements.reduce(
(acc, curr) => {
if (t.isImportDeclaration(curr.path.parentPath)) {
acc.imports.push(curr.result);
} else {
// Add these in reverse because we'll need to wrap in block statements in reverse
acc.others.unshift(curr.result);
}
return acc;
},
{ imports: [], others: [] }
);
const wrapped = others.reduce(
(acc, curr) => t.blockStatement([curr, acc]),
t.blockStatement([expression])
);
const m = new Module(filename);
m.transform =
typeof transformer !== 'undefined'
? transformer
: function transform(text) {
if (options && options.ignore && options.ignore.test(this.filename)) {
return { code: text };
}
return babel.transformSync(text, {
caller: { name: 'linaria', evaluate: true },
filename: this.filename,
presets: [[require.resolve('./index'), options]],
plugins: [
// Include this plugin to avoid extra config when using { module: false } for webpack
'@babel/plugin-transform-modules-commonjs',
'@babel/plugin-proposal-export-namespace-from',
// We don't support dynamic imports when evaluating, but don't wanna syntax error
// This will replace dynamic imports with an object that does nothing
require.resolve('./dynamic-import-noop'),
],
});
};
m.evaluate(
[
// Use String.raw to preserve escapes such as '\n' in the code
// Flow doesn't understand template tags: https://github.com/facebook/flow/issues/2616
/* $FlowFixMe */
imports.map(node => String.raw`${generator(node).code}`).join('\n'),
/* $FlowFixMe */
String.raw`${generator(wrapped).code}`,
].join('\n')
);
return {
value: m.exports,
dependencies,
};
};