Files
vue-cli/packages/@vue/cli-plugin-babel/__tests__/babel-runtime.spec.js
Haoqun Jiang 759d77fd5c refactor: use babel overrides to transpile babel runtime helpers (#4777)
* refactor: use babel overrides to transpile babel runtime helpers

As recommended in babel/babel#9903.
Get rid of the module-resolver plugin, may fix #3928.
Seems to have fixed #4742 as well.

There may be a small breaking change: as we now use `excludes` & `includes`, babel requires `filename` option to be present (introduced in https://github.com/babel/babel/pull/10181/files). So users who call `babel.transformSync` directly may encounter an error.

However, as we explicitly stated that this preset is only used for Vue CLI internally, I don't expect too many such use cases there. And the error messages are clear enough.
Considering the benefits that this PR brings, I think it's an acceptable tradeoff.


 test: update tests for babel

* test: fix windows tests

* test: remove unused variables

* fix: fix scope package paths on Windows

* test: wait some time in router tests in case dom hasn't updated in time
2019-11-04 16:29:06 +08:00

78 lines
2.4 KiB
JavaScript

jest.setTimeout(80000)
const { defaultPreset } = require('@vue/cli/lib/options')
const create = require('@vue/cli-test-utils/createTestProject')
const serve = require('@vue/cli-test-utils/serveWithPuppeteer')
test('should add polyfills for code in @babel/runtime', async () => {
const project = await create('babel-runtime-polyfills', defaultPreset)
await project.write('src/main.js', `
const x = function () {
setTimeout(
// eslint-disable-next-line
() => console.log(...arguments), 100
);
}
x(1, 2)
`)
await project.run('vue-cli-service build --mode development')
const vendorFile = await project.read('dist/js/chunk-vendors.js')
// iterableToArray is used to transform `console.log(...arguments)`
expect(vendorFile).toMatch('iterableToArray')
// with inline helpers, preset-env can detect the symbol polyfill is required
// (because the implementation of `iterableToArray` relies on it)
// however, with transform-runtime plugin, helpers are only references to @babel/runtime modules
// so we need to make sure polyfill detection is enabled for @babel/runtime too
expect(vendorFile).toMatch('es.symbol')
})
test('should not transpile babel helpers multiple times', async () => {
const project = await create('babel-runtime-helpers', defaultPreset)
const mainjs = await project.read('src/main.js')
await project.write('src/main.js', `
// eslint-disable-next-line
console.log(typeof Symbol('a'))
${mainjs}
`)
// if the typeof symbol helper is transpiled recursively,
// there would be an error thrown and the page would be empty
await serve(
() => project.run('vue-cli-service serve'),
async ({ helpers }) => {
const msg = `Welcome to Your Vue.js App`
expect(await helpers.getText('h1')).toMatch(msg)
}
)
})
// #4742 core-js-pure imports are likely to be caused by
// incorrect configuration of @babel/plugin-transform-runtime
test('should not introduce polyfills from core-js-pure', async () => {
const project = await create('babel-runtime-core-js-pure', defaultPreset)
await project.write('src/main.js', `
import Vue from 'vue'
import App from './App.vue'
Vue.config.productionTip = false
new Vue({
render: h => h(App),
methods: {
myfunc: async function () {}
}
}).$mount('#app')
`)
await project.run('vue-cli-service build --mode development')
const vendorFile = await project.read('dist/js/chunk-vendors.js')
expect(vendorFile).not.toMatch('core-js-pure')
})