-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
Copy pathbabel-preset.spec.js
230 lines (211 loc) · 6.5 KB
/
babel-preset.spec.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
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
const path = require('path')
const babel = require('@babel/core')
const preset = require('../index')
const defaultOptions = {
babelrc: false,
presets: [[preset, { targets: { ie: 9 } }]],
filename: 'test-entry-file.js'
}
const getAbsolutePolyfill = mod => {
// expected to include a `node_modules` in the import path because we use absolute path for core-js
return new RegExp(`"${['.*node_modules', 'core-js', 'modules', mod].join(`[\\${path.sep}]+`)}`)
}
beforeEach(() => {
process.env.VUE_CLI_ENTRY_FILES = JSON.stringify([path.join(process.cwd(), 'test-entry-file.js')])
})
test('polyfill detection', () => {
let { code } = babel.transformSync(`
const a = new Map()
`.trim(), {
babelrc: false,
presets: [[preset, {
targets: { node: 'current' }
}]],
filename: 'test-entry-file.js'
})
// default includes
expect(code).not.toMatch(getAbsolutePolyfill('es.promise'))
// usage-based detection
expect(code).not.toMatch('core-js/modules/es.map')
;({ code } = babel.transformSync(`
const a = new Map()
`.trim(), {
babelrc: false,
presets: [[preset, {
targets: { ie: 9 }
}]],
filename: 'test-entry-file.js'
}))
// default includes
expect(code).toMatch(getAbsolutePolyfill('es.promise'))
// promise polyfill alone doesn't work in IE, needs this as well. fix: #1642
expect(code).toMatch(getAbsolutePolyfill('es.array.iterator'))
// usage-based detection
expect(code).toMatch('core-js/modules/es.map')
})
test('modern mode always skips unnecessary polyfills', () => {
process.env.VUE_CLI_MODERN_BUILD = true
let { code } = babel.transformSync(`
const a = new Map()
console.log(globalThis)
`.trim(), {
babelrc: false,
presets: [[preset, {
targets: { ie: 9, safari: '12' },
useBuiltIns: 'usage'
}]],
filename: 'test-entry-file.js'
})
// default includes that are supported in all modern browsers should be skipped
expect(code).not.toMatch('es.assign')
// though es.promise is not supported in all modern browsers
// (modern: safari >= 10.1, es.promise: safrai >= 11)
// the custom configuration only expects to support safari >= 12
// so it can be skipped
expect(code).not.toMatch('es.promise[^.]')
// es.promise.finally is supported in safari >= 13.0.3
// so still needs to be included
expect(code).toMatch('es.promise.finally')
// usage-based detection
// Map is supported in all modern browsers
expect(code).not.toMatch('es.map')
// globalThis is not supported until safari 12.1
expect(code).toMatch('es.global-this')
;({ code } = babel.transformSync(`
const a = new Map()
`.trim(), {
babelrc: false,
presets: [[preset, {
targets: { ie: 9, safari: '12' },
useBuiltIns: 'entry'
}]],
filename: 'test-entry-file.js'
}))
// default includes
expect(code).not.toMatch('es.promise[^.]')
expect(code).not.toMatch('es.promise.finally')
// usage-based detection
expect(code).not.toMatch('core-js/modules/es.map')
expect(code).not.toMatch('es.global-this')
delete process.env.VUE_CLI_MODERN_BUILD
})
test('object spread', () => {
const { code } = babel.transformSync(`
const a = { ...b }
`.trim(), defaultOptions)
// expect(code).toMatch(`import _objectSpread from`)
expect(code).toMatch(`var a = _objectSpread({}, b)`)
})
test('dynamic import', () => {
expect(() => {
babel.transformSync(`const Foo = () => import('./Foo.vue')`, defaultOptions)
}).not.toThrow()
})
test('async/await', () => {
const { code } = babel.transformSync(`
async function hello () {
await Promise.resolve()
}
hello()
`.trim(), defaultOptions)
expect(code).toMatch(getAbsolutePolyfill('es.promise'))
// should use regenerator runtime
expect(code).toMatch(`regenerator-runtime/runtime`)
})
test('jsx', () => {
const { code } = babel.transformSync(`
export default {
render () {
return <div>bar</div>
}
}
`.trim(), defaultOptions)
expect(code).toMatch(`var h = arguments[0]`)
expect(code).toMatch(`return h("div", ["bar"])`)
})
test('jsx options', () => {
const { code } = babel.transformSync(`
export default {
render () {
return <div>bar</div>
}
}
`.trim(), {
babelrc: false,
presets: [[preset, {
jsx: {
injectH: false
}
}]],
filename: 'test-entry-file.js'
})
expect(code).not.toMatch(`var h = arguments[0]`)
expect(code).toMatch(`return h("div", ["bar"])`)
})
test('disable absoluteRuntime', () => {
const { code } = babel.transformSync(`
const a = [...arr]
`.trim(), {
babelrc: false,
presets: [[preset, {
targets: { ie: 9 },
absoluteRuntime: false
}]],
filename: 'test-entry-file.js'
})
expect(code).toMatch('@babel/runtime/helpers/toConsumableArray')
expect(code).not.toMatch(getAbsolutePolyfill('es.promise'))
})
test('should inject polyfills / helpers using "require" statements for a umd module', () => {
const { code } = babel.transformSync(`
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global = global || self, global.Vue = factory());
}(this, function () {
const a = [...arr]
new Promise()
}))
`.trim(), {
babelrc: false,
presets: [[preset, {
targets: { ie: 9 },
absoluteRuntime: false
}]],
filename: 'test-entry-file.js'
})
expect(code).toMatch('require("@babel/runtime/helpers/toConsumableArray')
expect(code).toMatch('require("core-js/modules/es.promise')
expect(code).not.toMatch('import ')
})
test('should inject polyfills / helpers using "import" statements for an es module', () => {
const { code } = babel.transformSync(`
import Vue from 'vue'
const a = [...arr]
new Promise()
`.trim(), {
babelrc: false,
configFile: false,
presets: [[preset, {
targets: { ie: 9 },
absoluteRuntime: false
}]],
filename: 'test-entry-file.js'
})
expect(code).toMatch('import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray')
expect(code).toMatch('import "core-js/modules/es.promise')
expect(code).not.toMatch('require(')
})
test('should not inject excluded polyfills', () => {
const { code } = babel.transformSync(`
new Promise()
`.trim(), {
babelrc: false,
presets: [[preset, {
exclude: ['es.promise'],
polyfills: ['es.array.iterator', 'es.object.assign']
}]],
filename: 'test-entry-file.js'
})
expect(code).not.toMatch('es.promise')
})