-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbabel-plugin-macro.js
65 lines (57 loc) · 1.33 KB
/
babel-plugin-macro.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
const babel = require('babel-core');
const { transform, transformAsync, transformSync } = require('@babel/core');
const babelGenerator = require('babel-generator').default;
const fs = require('fs-extra');
const path = require('path');
const run = () => {
const input = `
import scope from 'scope.macro'
import ms from 'ms.macro'
function add100(a) {
const oneHundred = 100
scope('Add 100 to another number')
const ONE_DAY = ms('1 day');
const TWO_DAYS = ms('2 days');
return add(a, oneHundred)
}
function add(a, b) {
return a + b
}
add100(20220);
`;
const { ast } = babel.transform(input, {
sourceMap: true,
env: {},
presets: ['es2015'],
plugins: [
[
'transform-runtime',
{
"corejs": 2,
"helpers": true,
"regenerator": true,
"useESModules": false
}
],
'macros',
],
});
const { ast } = transformSync(input, {
ast: true,
code: false,
sourceMap: true,
babelrc: false,
configFile: false,
presets: ['@babel/env'],
filename: 'unknown',
plugins: [
'macros',
],
});
const { code: babelCode } = babelGenerator(ast, {
minified: false,
});
fs.writeFileSync(path.join(__dirname, './test.js'), babelCode);
};
module.exports = run;
run();