This repository has been archived by the owner on May 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
get-babel-config.js
159 lines (147 loc) · 4.31 KB
/
get-babel-config.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
/** Copyright (c) 2018 Uber Technologies, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
/* eslint-env node */
/*::
type Target = "node-native" | "node-bundled" | "browser-modern" | "browser-legacy";
type JSXTransformOpts = {
pragma?: string,
pragmaFrag?: string,
};
type BabelConfigOpts =
| {|
specOnly: true,
target: Target,
plugins?: Array<any>,
presets?: Array<any>,
|}
| {|
specOnly: false,
dev: boolean,
target: Target,
plugins?: Array<any>,
presets?: Array<any>,
jsx?: JSXTransformOpts,
assumeNoImportSideEffects?: boolean,
fusionTransforms: boolean
|};
*/
module.exports = function getBabelConfig(opts /*: BabelConfigOpts */) {
const {target, plugins, presets} = opts;
// Shared base env preset options
let envPresetOpts /*: Object*/ = {
useBuiltIns: 'entry',
};
// Shared base configuration
let config = {
plugins: [
require.resolve('@babel/plugin-syntax-dynamic-import'),
[
require.resolve('@rtsao/plugin-proposal-class-properties'),
{loose: false},
],
opts.dev &&
require.resolve('babel-plugin-transform-styletron-display-name'),
].filter(Boolean),
presets: [[require.resolve('@babel/preset-env'), envPresetOpts]],
babelrc: false,
};
if (opts.specOnly === false) {
let {jsx, assumeNoImportSideEffects, dev, fusionTransforms} = opts;
if (!jsx) {
jsx = {};
}
config.presets.push([
require.resolve('@babel/preset-react'),
{
pragma: jsx.pragma,
pragmaFrag: jsx.pragmaFrag,
development: dev,
},
]);
config.plugins.unshift(
require.resolve('@babel/plugin-transform-flow-strip-types')
);
if (fusionTransforms) {
config.presets.push([fusionPreset, {target, assumeNoImportSideEffects}]);
} else {
config.plugins.push(require.resolve('./babel-plugins/babel-plugin-gql'));
}
}
if (target === 'node-native') {
envPresetOpts.modules = 'commonjs';
envPresetOpts.targets = {
node: 'current',
};
config.plugins.push(require.resolve('babel-plugin-dynamic-import-node'));
} else if (target === 'node-bundled') {
envPresetOpts.modules = false;
envPresetOpts.targets = {
node: 'current',
};
} else if (target === 'browser-modern') {
envPresetOpts.modules = false;
envPresetOpts.targets = {
esmodules: true,
};
envPresetOpts.useBuiltIns = 'entry';
} else if (target === 'browser-legacy') {
envPresetOpts.modules = false;
envPresetOpts.targets = {
ie: 9,
};
}
if (plugins) {
// Note: babel plugins run first to last, so custom plugins go first
config.plugins.unshift(...plugins);
}
if (presets) {
// Note: babel presets run last to first, so custom plugins go last
config.presets.push(...presets);
}
return config;
};
/*::
type FusionPresetOpts = {
target: Target,
assumeNoImportSideEffects: boolean,
};
*/
/**
* This is abstracted into preset for the following reasoning:
* The tree shake plugin needs to run after JSX transform.
* However, the JSX transform is inside the React preset.
* Because plugins run before presets, the tree shake plugin
* must also live in a preset.
*/
function fusionPreset(
context /*: any */,
{target, assumeNoImportSideEffects} /*: FusionPresetOpts */
) {
const targetEnv =
target === 'node-native' || target === 'node-bundled' ? 'node' : 'browser';
return {
plugins: [
require.resolve('./babel-plugins/babel-plugin-gql'),
require.resolve('./babel-plugins/babel-plugin-asseturl'),
require.resolve('./babel-plugins/babel-plugin-pure-create-plugin'),
require.resolve('./babel-plugins/babel-plugin-sync-chunk-ids'),
require.resolve('./babel-plugins/babel-plugin-sw'),
require.resolve('./babel-plugins/babel-plugin-sync-chunk-paths'),
require.resolve('./babel-plugins/babel-plugin-chunkid'),
require.resolve('./babel-plugins/babel-plugin-workerurl'),
[
require.resolve('babel-plugin-transform-cup-globals'),
{target: targetEnv},
],
assumeNoImportSideEffects && [
require.resolve('./babel-plugins/babel-plugin-transform-tree-shake'),
{target: targetEnv},
],
].filter(Boolean),
};
}