Skip to content

Commit

Permalink
fix: add sourceType: 'unambiguous' to babel preset (vuejs#4797)
Browse files Browse the repository at this point in the history
  • Loading branch information
haoqunjiang authored Nov 7, 2019
1 parent 759d77f commit e1d156f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
57 changes: 49 additions & 8 deletions packages/@vue/babel-preset-app/__tests__/babel-preset.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const defaultOptions = {

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

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

;({ code } = babel.transformSync(`
const a = new Map()
Expand All @@ -45,7 +45,7 @@ test('polyfill detection', () => {
// 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('import "core-js/modules/es.map"')
expect(code).toMatch('"core-js/modules/es.map"')
})

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

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

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

test('jsx', () => {
Expand Down Expand Up @@ -153,6 +153,47 @@ test('disable absoluteRuntime', () => {
filename: 'test-entry-file.js'
})

expect(code).toMatch('import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray"')
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', () => {
// TODO:
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, {
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,
presets: [[preset, {
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(')
})
1 change: 1 addition & 0 deletions packages/@vue/babel-preset-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ module.exports = (context, options = {}) => {
}])

return {
sourceType: 'unambiguous',
overrides: [{
exclude: [/@babel[\/|\\\\]runtime/, /core-js/],
presets,
Expand Down

0 comments on commit e1d156f

Please sign in to comment.