-
Notifications
You must be signed in to change notification settings - Fork 411
Expand file tree
/
Copy pathindex.ts
More file actions
67 lines (57 loc) · 1.9 KB
/
index.ts
File metadata and controls
67 lines (57 loc) · 1.9 KB
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
import shake from './shaker';
import { debug } from '../../utils/logger';
import generator from '@babel/generator';
import { Evaluator, StrictOptions } from '../../types';
import { transformSync, types } from '@babel/core';
import buildOptions, { mergeOrPrependPlugin } from '../buildOptions';
function prepareForShake(
filename: string,
options: StrictOptions,
code: string
): types.Program {
const transformOptions = buildOptions(filename, options);
transformOptions.ast = true;
transformOptions.presets = mergeOrPrependPlugin(transformOptions.presets!, [
'@babel/preset-env',
{
targets: 'ie 11',
// we need this plugin so we list it explicitly, explanation in `evaluators/extractor/index`
include: ['@babel/plugin-transform-template-literals'],
},
]);
transformOptions.presets = mergeOrPrependPlugin(transformOptions.presets!, [
require.resolve('../preeval'),
options,
]);
transformOptions.plugins = mergeOrPrependPlugin(
transformOptions.plugins!,
'transform-react-remove-prop-types'
);
transformOptions.plugins = mergeOrPrependPlugin(transformOptions.plugins!, [
'@babel/plugin-transform-runtime',
{ useESModules: false },
]);
debug(
'evaluator:shaker:transform',
`Transform ${filename} with options ${JSON.stringify(
transformOptions,
null,
2
)}`
);
const transformed = transformSync(code, transformOptions);
if (transformed === null || !transformed.ast) {
throw new Error(`${filename} cannot be transformed`);
}
return transformed.ast.program;
}
const shaker: Evaluator = (filename, options, text, only = null) => {
const [shaken, imports] = shake(
prepareForShake(filename, options, text),
only
);
debug('evaluator:shaker:generate', `Generate shaken source code ${filename}`);
const { code: shakenCode } = generator(shaken!);
return [shakenCode, imports];
};
export default shaker;