Skip to content

Commit e1d156f

Browse files
authored
fix: add sourceType: 'unambiguous' to babel preset (#4797)
fixes #4773
1 parent 759d77f commit e1d156f

File tree

2 files changed

+50
-8
lines changed

2 files changed

+50
-8
lines changed

packages/@vue/babel-preset-app/__tests__/babel-preset.spec.js

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const defaultOptions = {
99

1010
const getAbsolutePolyfill = mod => {
1111
// expected to include a `node_modules` in the import path because we use absolute path for core-js
12-
return new RegExp(`import "${['.*node_modules', 'core-js', 'modules', mod].join(`[\\${path.sep}]+`)}`)
12+
return new RegExp(`"${['.*node_modules', 'core-js', 'modules', mod].join(`[\\${path.sep}]+`)}`)
1313
}
1414

1515
beforeEach(() => {
@@ -29,7 +29,7 @@ test('polyfill detection', () => {
2929
// default includes
3030
expect(code).not.toMatch(getAbsolutePolyfill('es.promise'))
3131
// usage-based detection
32-
expect(code).not.toMatch('import "core-js/modules/es.map"')
32+
expect(code).not.toMatch('"core-js/modules/es.map"')
3333

3434
;({ code } = babel.transformSync(`
3535
const a = new Map()
@@ -45,7 +45,7 @@ test('polyfill detection', () => {
4545
// promise polyfill alone doesn't work in IE, needs this as well. fix: #1642
4646
expect(code).toMatch(getAbsolutePolyfill('es.array.iterator'))
4747
// usage-based detection
48-
expect(code).toMatch('import "core-js/modules/es.map"')
48+
expect(code).toMatch('"core-js/modules/es.map"')
4949
})
5050

5151
test('modern mode always skips polyfills', () => {
@@ -63,7 +63,7 @@ test('modern mode always skips polyfills', () => {
6363
// default includes
6464
expect(code).not.toMatch(getAbsolutePolyfill('es.promise'))
6565
// usage-based detection
66-
expect(code).not.toMatch('import "core-js/modules/es.map"')
66+
expect(code).not.toMatch('"core-js/modules/es.map"')
6767

6868
;({ code } = babel.transformSync(`
6969
const a = new Map()
@@ -78,7 +78,7 @@ test('modern mode always skips polyfills', () => {
7878
// default includes
7979
expect(code).not.toMatch(getAbsolutePolyfill('es.promise'))
8080
// usage-based detection
81-
expect(code).not.toMatch('import "core-js/modules/es.map"')
81+
expect(code).not.toMatch('"core-js/modules/es.map"')
8282
delete process.env.VUE_CLI_MODERN_BUILD
8383
})
8484

@@ -105,9 +105,9 @@ test('async/await', () => {
105105
`.trim(), defaultOptions)
106106
expect(code).toMatch(getAbsolutePolyfill('es.promise'))
107107
// should use regenerator runtime
108-
expect(code).toMatch(`import "regenerator-runtime/runtime"`)
108+
expect(code).toMatch(`"regenerator-runtime/runtime"`)
109109
// should use required helper instead of inline
110-
expect(code).toMatch(/import _asyncToGenerator from ".*runtime\/helpers\/esm\/asyncToGenerator\"/)
110+
expect(code).toMatch(/".*runtime\/helpers\/asyncToGenerator\"/)
111111
})
112112

113113
test('jsx', () => {
@@ -153,6 +153,47 @@ test('disable absoluteRuntime', () => {
153153
filename: 'test-entry-file.js'
154154
})
155155

156-
expect(code).toMatch('import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"')
156+
expect(code).toMatch('"@babel/runtime/helpers/toConsumableArray"')
157157
expect(code).not.toMatch(getAbsolutePolyfill('es.promise'))
158158
})
159+
160+
test('should inject polyfills / helpers using "require" statements for a umd module', () => {
161+
// TODO:
162+
const { code } = babel.transformSync(`
163+
(function (global, factory) {
164+
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
165+
typeof define === 'function' && define.amd ? define(factory) :
166+
(global = global || self, global.Vue = factory());
167+
}(this, function () {
168+
const a = [...arr]
169+
new Promise()
170+
}))
171+
`.trim(), {
172+
babelrc: false,
173+
presets: [[preset, {
174+
absoluteRuntime: false
175+
}]],
176+
filename: 'test-entry-file.js'
177+
})
178+
expect(code).toMatch('require("@babel/runtime/helpers/toConsumableArray")')
179+
expect(code).toMatch('require("core-js/modules/es.promise")')
180+
expect(code).not.toMatch('import ')
181+
})
182+
183+
test('should inject polyfills / helpers using "import" statements for an es module', () => {
184+
const { code } = babel.transformSync(`
185+
import Vue from 'vue'
186+
const a = [...arr]
187+
new Promise()
188+
`.trim(), {
189+
babelrc: false,
190+
presets: [[preset, {
191+
absoluteRuntime: false
192+
}]],
193+
filename: 'test-entry-file.js'
194+
})
195+
196+
expect(code).toMatch('import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"')
197+
expect(code).toMatch('import "core-js/modules/es.promise"')
198+
expect(code).not.toMatch('require(')
199+
})

packages/@vue/babel-preset-app/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ module.exports = (context, options = {}) => {
184184
}])
185185

186186
return {
187+
sourceType: 'unambiguous',
187188
overrides: [{
188189
exclude: [/@babel[\/|\\\\]runtime/, /core-js/],
189190
presets,

0 commit comments

Comments
 (0)