forked from standard-things/esm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
.babel.config.js
159 lines (137 loc) · 3.36 KB
/
.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
"use strict"
const arrayRemove = require("./script/array-remove.js")
const babel = require("@babel/core")
function BabelEqEqEqPlugin() {
return {
visitor: {
BinaryExpression({ node }) {
const { operator } = node
if ((operator === "==" ||
operator === "!=") &&
node.left.type !== "NullLiteral" &&
node.right.type !== "NullLiteral") {
node.operator += "="
}
}
}
}
}
function BabelModePlugin({ types }) {
// Based on `isInStrictMode()`.
// Copyright Sebastian McKenzie and other contributors. Released under MIT license:
// https://github.com/babel/babel/blob/master/packages/babel-traverse/src/path/introspection.js
function isInMode(path) {
const parent = path.find((path) => {
if (path.isClass()) {
return true
}
const isFunc = path.isFunction()
if (! isFunc &&
! path.isProgram()) {
return false
}
const node = isFunc
? path.node.body
: path.node
const { directives } = node
let { length } = directives
while (length--) {
const { value } = directives[length].value
if (value === "use sloppy" ||
value === "use strict") {
return true
}
}
})
return parent !== null
}
function isSimpleParameterList(params) {
return params.every(({ type }) => type === "Identifier")
}
function enterFunction(path) {
const { node } = path
const { directives } = node.body
if (directives &&
isSimpleParameterList(node.params) &&
! isInMode(path)) {
directives.push(types.directive(types.directiveLiteral("use strict")))
}
}
return {
visitor: {
ArrowFunctionExpression: enterFunction,
FunctionDeclaration: enterFunction,
FunctionExpression: enterFunction
}
}
}
function BabelOrderPlugin(plugins) {
return {
visitor: {
Program(path) {
for (const plugin of plugins) {
path.traverse(plugin(babel).visitor)
}
}
}
}
}
function BabelRemoveSloppyPlugin() {
function enterFunction({ node: { body: directives } }) {
arrayRemove(directives, ({ value: { value } }) => value === "use sloppy")
}
return {
visitor: {
ArrowFunctionExpression: enterFunction,
FunctionDeclaration: enterFunction,
FunctionExpression: enterFunction
}
}
}
const isTest = /test/.test(process.env.ESM_ENV)
module.exports = {
env: {
production: {
plugins: [
["transform-remove-console", {
exclude: ["error"]
}],
"transform-remove-debugger"
]
}
},
plugins: [
["@babel/proposal-class-properties", {
loose: true
}],
"@babel/proposal-optional-catch-binding",
["@babel/transform-arrow-functions", {
spec: false
}],
["@babel/transform-block-scoping", {
throwIfClosureRequired: false
}],
["transform-for-of-as-array", {
loose: true
}],
BabelOrderPlugin([
BabelEqEqEqPlugin,
BabelModePlugin,
BabelRemoveSloppyPlugin
])
],
presets: [
["@babel/env", {
debug: isTest,
exclude: [
"transform-async-to-generator",
"transform-for-of",
"transform-function-name"
],
loose: true,
modules: false,
targets: { node: 6 }
}]
],
sourceMaps: false
}