-
Notifications
You must be signed in to change notification settings - Fork 414
/
shaker-plugin.ts
375 lines (321 loc) · 11.1 KB
/
shaker-plugin.ts
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import type core from '@babel/core';
import type { BabelFile, PluginObj, NodePath } from '@babel/core';
import type { Binding } from '@babel/traverse';
import type {
Identifier,
MemberExpression,
Program,
VariableDeclarator,
} from '@babel/types';
import { createCustomDebug } from '@linaria/logger';
import type { IExport, IMetadata, IReexport, IState } from '@linaria/utils';
import {
applyAction,
collectExportsAndImports,
dereference,
findActionForNode,
getFileIdx,
isRemoved,
reference,
removeWithRelated,
sideEffectImport,
} from '@linaria/utils';
import shouldKeepSideEffect from './utils/shouldKeepSideEffect';
type Core = typeof core;
export interface IShakerOptions {
keepSideEffects?: boolean;
ifUnknownExport?: 'error' | 'ignore' | 'reexport-all' | 'skip-shaking';
onlyExports: string[];
}
interface NodeWithName {
name: string;
}
function getBindingForExport(exportPath: NodePath): Binding | undefined {
if (exportPath.isIdentifier()) {
return exportPath.scope.getBinding(exportPath.node.name);
}
const variableDeclarator = exportPath.findParent((p) =>
p.isVariableDeclarator()
) as NodePath<VariableDeclarator> | undefined;
if (variableDeclarator) {
const id = variableDeclarator.get('id');
if (id.isIdentifier()) {
return variableDeclarator.scope.getBinding(id.node.name);
}
}
if (exportPath.isAssignmentExpression()) {
const left = exportPath.get('left');
if (left.isIdentifier()) {
return exportPath.scope.getBinding(left.node.name);
}
}
return undefined;
}
const withoutRemoved = <T extends { local: NodePath }>(items: T[]): T[] =>
items.filter(({ local }) => !isRemoved(local));
function rearrangeExports(
{ types: t }: Core,
root: NodePath<Program>,
exportRefs: Map<string, NodePath<MemberExpression>[]>,
exports: IExport[]
): IExport[] {
let rearranged = [...exports];
const rootScope = root.scope;
exportRefs.forEach((refs, name) => {
if (refs.length <= 1) {
return;
}
const uid = rootScope.generateUid(name);
// Define variable in the beginning
const [declaration] = root.unshiftContainer('body', [
t.variableDeclaration('var', [t.variableDeclarator(t.identifier(uid))]),
]);
rootScope.registerDeclaration(declaration);
// Replace every reference with defined variable
refs.forEach((ref) => {
const [replaced] = ref.replaceWith(t.identifier(uid));
if (replaced.isBindingIdentifier()) {
rootScope.registerConstantViolation(replaced);
} else {
reference(replaced);
}
});
// Assign defined variable to the export
const [pushed] = root.pushContainer('body', [
t.expressionStatement(
t.assignmentExpression(
'=',
t.memberExpression(t.identifier('exports'), t.identifier(name)),
t.identifier(uid)
)
),
]);
const local = pushed.get('expression.right') as NodePath<Identifier>;
reference(local);
rearranged = rearranged.map((exp) =>
exp.exported === name
? {
...exp,
local,
}
: exp
);
});
return rearranged;
}
export default function shakerPlugin(
babel: Core,
{
keepSideEffects = false,
ifUnknownExport = 'skip-shaking',
onlyExports,
}: IShakerOptions
): PluginObj<IState & { filename: string }> {
return {
name: '@linaria/shaker',
pre(file: BabelFile) {
this.filename = file.opts.filename!;
const log = createCustomDebug('shaker', getFileIdx(this.filename));
log('start', `${this.filename}, onlyExports: ${onlyExports.join(',')}`);
const onlyExportsSet = new Set(onlyExports);
const collected = collectExportsAndImports(file.path);
const sideEffectImports = collected.imports.filter(sideEffectImport);
log(
'import-and-exports',
[
`imports: ${collected.imports.length} (side-effects: ${sideEffectImports.length})`,
`exports: ${collected.exports.length}`,
`reexports: ${collected.reexports.length}`,
].join(', ')
);
// We cannot just throw out exports if they are referred in the code
// Let's dome some replacements
const exports = rearrangeExports(
babel,
file.path,
collected.exportRefs,
collected.exports
);
const findExport = (name: string) =>
exports.find((i) => i.exported === name);
collected.exports.forEach(({ local }) => {
if (local.isAssignmentExpression()) {
const left = local.get('left');
if (left.isIdentifier()) {
// For some reason babel does not mark id in AssignmentExpression as a reference
// So we need to do it manually
reference(left, left, true);
}
}
});
const hasLinariaPreval = findExport('__linariaPreval') !== undefined;
const hasDefault = findExport('default') !== undefined;
// If __linariaPreval is not exported, we can remove it from onlyExports
if (onlyExportsSet.has('__linariaPreval') && !hasLinariaPreval) {
onlyExportsSet.delete('__linariaPreval');
}
if (onlyExportsSet.size === 0) {
// Fast-lane: if there are no exports to keep, we can just shake out the whole file
this.imports = [];
this.exports = [];
this.reexports = [];
file.path.get('body').forEach((p) => {
p.remove();
});
return;
}
const importedAsSideEffect = onlyExportsSet.has('side-effect');
onlyExportsSet.delete('side-effect');
// Hackaround for packages which include a 'default' export without specifying __esModule; such packages cannot be
// shaken as they will break interopRequireDefault babel helper
// See example in shaker-plugin.test.ts
// Real-world example was found in preact/compat npm package
if (
onlyExportsSet.has('default') &&
hasDefault &&
!collected.isEsModule
) {
this.imports = collected.imports;
this.exports = exports;
this.reexports = collected.reexports;
return;
}
if (!onlyExportsSet.has('*')) {
const aliveExports = new Set<IExport | IReexport>();
const importNames = collected.imports.map(({ imported }) => imported);
exports.forEach((exp) => {
if (onlyExportsSet.has(exp.exported)) {
aliveExports.add(exp);
} else if (
importNames.includes((exp.local.node as NodeWithName).name || '')
) {
aliveExports.add(exp);
} else if (
[...aliveExports].some((liveExp) => liveExp.local === exp.local)
) {
// It's possible to export multiple values from a single variable initializer, e.g
// export const { foo, bar } = baz();
// We need to treat all of them as used if any of them are used, since otherwise
// we'll attempt to delete the baz() call
aliveExports.add(exp);
}
});
collected.reexports.forEach((exp) => {
if (onlyExportsSet.has(exp.exported)) {
aliveExports.add(exp);
}
});
const isAllExportsFound = aliveExports.size === onlyExportsSet.size;
if (!isAllExportsFound && ifUnknownExport !== 'ignore') {
if (ifUnknownExport === 'error') {
throw new Error(
`Unknown export(s) requested: ${onlyExports.join(',')}`
);
}
if (ifUnknownExport === 'reexport-all') {
// If there are unknown exports, we have keep alive all re-exports.
exports.forEach((exp) => {
if (exp.exported === '*') {
aliveExports.add(exp);
}
});
collected.reexports.forEach((exp) => {
if (exp.exported === '*') {
aliveExports.add(exp);
}
});
}
if (ifUnknownExport === 'skip-shaking') {
this.imports = collected.imports;
this.exports = exports;
this.reexports = collected.reexports;
return;
}
}
const forDeleting = [...exports, ...collected.reexports]
.filter((exp) => !aliveExports.has(exp))
.map((exp) => exp.local);
if (!keepSideEffects && !importedAsSideEffect) {
// Remove all imports that don't import something explicitly and should not be kept
sideEffectImports.forEach((i) => {
if (!shouldKeepSideEffect(i.source)) {
forDeleting.push(i.local);
}
});
}
const deleted = new Set<NodePath>();
const dereferenced: NodePath<Identifier>[] = [];
let changed = true;
while (changed && deleted.size < forDeleting.length) {
changed = false;
// eslint-disable-next-line no-restricted-syntax
for (const path of forDeleting) {
const binding = getBindingForExport(path);
const action = findActionForNode(path);
const parent = action?.[1];
const outerReferences = (binding?.referencePaths || []).filter(
(ref) => ref !== parent && !parent?.isAncestor(ref)
);
if (outerReferences.length > 0 && path.isIdentifier()) {
// Temporary deref it in order to simplify further checks.
dereference(path);
dereferenced.push(path);
}
if (
!deleted.has(path) &&
(!binding || outerReferences.length === 0)
) {
if (action) {
applyAction(action);
} else {
removeWithRelated([path]);
}
deleted.add(path);
changed = true;
}
}
}
dereferenced.forEach((path) => {
// If path is still alive, we need to reference it back
if (!isRemoved(path)) {
reference(path);
}
});
}
this.imports = withoutRemoved(collected.imports);
this.exports = withoutRemoved(exports);
this.reexports = withoutRemoved(collected.reexports);
},
visitor: {},
post(file: BabelFile) {
const log = createCustomDebug('shaker', getFileIdx(file.opts.filename!));
const processedImports = new Set<string>();
const imports = new Map<string, string[]>();
const addImport = ({
imported,
source,
}: {
imported: string;
source: string;
}) => {
if (processedImports.has(`${source}:${imported}`)) {
return;
}
if (!imports.has(source)) {
imports.set(source, []);
}
if (imported) {
imports.get(source)!.push(imported);
}
processedImports.add(`${source}:${imported}`);
};
this.imports.forEach(addImport);
this.reexports.forEach(addImport);
log('end', `remaining imports: %O`, imports);
// eslint-disable-next-line no-param-reassign
(file.metadata as IMetadata).linariaEvaluator = {
imports,
};
},
};
}