forked from vuejs/vue-cli
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use babel overrides to transpile babel runtime helpers (vue…
…js#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 vuejs#3928. Seems to have fixed vuejs#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
- Loading branch information
1 parent
0a5c79b
commit 759d77f
Showing
6 changed files
with
120 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/@vue/cli-plugin-babel/__tests__/babel-runtime.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters