-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
804 changed files
with
49,192 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
__tests__ | ||
__mocks__ |
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,103 @@ | ||
# @vue/babel-preset-app | ||
|
||
This is the default Babel preset used in all Vue CLI projects. **Note: this preset is meant to be used exclusively in projects created via Vue CLI and does not consider external use cases.** | ||
|
||
## Included Features | ||
|
||
### [@babel/preset-env](https://new.babeljs.io/docs/en/next/babel-preset-env.html) | ||
|
||
`preset-env` automatically determines the transforms and polyfills to apply based on your browser target. See [Browser Compatibility](https://cli.vuejs.org/guide/browser-compatibility.html) section in docs for more details. | ||
|
||
- `modules: false` | ||
- auto set to `'commonjs'` in Jest tests | ||
- [`useBuiltIns: 'usage'`](#usebuiltins) | ||
- `targets` is determined: | ||
- using `browserslist` field in `package.json` when building for browsers | ||
- set to `{ node: 'current' }` when running unit tests in Node.js | ||
- Includes `Promise` polyfill by default so that they are usable even in non-transpiled dependencies (only for environments that need it) | ||
|
||
### Stage 3 or Below | ||
|
||
Only the following stage 3 or below features are supported (object rest spread is supported as part of `preset-env`): | ||
|
||
- [Dynamic Import Syntax](https://github.com/tc39/proposal-dynamic-import) | ||
- [Proposal Class Properties](https://babeljs.io/docs/en/next/babel-plugin-proposal-class-properties.html) | ||
- [Proposal Decorators (legacy)](https://babeljs.io/docs/en/next/babel-plugin-proposal-decorators.html) | ||
|
||
If you need additional stage 3 or below features, you need to install and configure it yourself. | ||
|
||
### Vue JSX support | ||
|
||
- [@babel/plugin-syntax-jsx](https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-jsx) | ||
- [@vue/babel-preset-jsx](https://github.com/vuejs/jsx) | ||
|
||
### [@babel/plugin-transform-runtime](https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-runtime) | ||
|
||
`transform-runtime` avoids inlining helpers in every file. This is enabled for helpers only, since polyfills are handled by `babel-preset-env`. | ||
|
||
## Options | ||
|
||
- All options from [@babel/preset-env](https://babeljs.io/docs/en/next/babel-preset-env.html) are supported, with some of them having smarter defaults. | ||
|
||
### modules | ||
|
||
- Default: | ||
- `false` when building with webpack | ||
- `'commonjs'` when running tests in Jest. | ||
|
||
Explicitly set `modules` option for `babel-preset-env`. See [babel-preset-env docs](https://github.com/babel/babel/tree/master/packages/babel-preset-env#modules) for more details. | ||
|
||
### targets | ||
|
||
- Default: | ||
- determined from `browserslist` field in `package.json` when building for browsers | ||
- set to `{ node: 'current' }` when running unit tests in Node.js | ||
|
||
Explicitly set `targets` option for `babel-preset-env`. See [babel-preset-env docs](https://github.com/babel/babel/tree/master/packages/babel-preset-env#targets) for more details. | ||
|
||
### useBuiltIns | ||
|
||
- Default: `'usage'` | ||
- Allowed values: `'usage' | 'entry' | false` | ||
|
||
Explicitly set `useBuiltIns` option for `babel-preset-env`. | ||
|
||
The default value is `'usage'`, which adds imports to polyfills based on the usage in transpiled code. For example, if you use `Object.assign` in your code, the corresponding polyfill will be auto-imported if your target environment does not supports it. | ||
|
||
If you are building a library or web component instead of an app, you probably want to set this to `false` and let the consuming app be responsible for the polyfills. | ||
|
||
Note that the usage detection does not apply to your dependencies (which are excluded by `cli-plugin-babel` by default). If one of your dependencies need polyfills, you have a few options: | ||
|
||
1. **If the dependency is written in an ES version that your target environments do not support:** Add that dependency to the `transpileDependencies` option in `vue.config.js`. This would enable both syntax transforms and usage-based polyfill detection for that dependency. | ||
|
||
2. **If the dependency ships ES5 code and explicitly lists the polyfills needed:** you can pre-include the needed polyfills using the [polyfills](#polyfills) option for this preset. | ||
|
||
3. **If the dependency ships ES5 code, but uses ES6+ features without explicitly listing polyfill requirements (e.g. Vuetify):** Use `useBuiltIns: 'entry'` and then add `import '@babel/polyfill'` to your entry file. This will import **ALL** polyfills based on your `browserslist` targets so that you don't need to worry about dependency polyfills anymore, but will likely increase your final bundle size with some unused polyfills. | ||
|
||
See [@babel/preset-env docs](https://new.babeljs.io/docs/en/next/babel-preset-env.html#usebuiltins-usage) for more details. | ||
|
||
### polyfills | ||
|
||
- Default: `['es6.array.iterator', 'es6.promise', 'es6.object.assign', 'es7.promise.finally']` | ||
|
||
A list of [core-js](https://github.com/zloirock/core-js) polyfills to pre-include when using `useBuiltIns: 'usage'`. **These polyfills are automatically excluded if they are not needed for your target environments**. | ||
|
||
Use this option when you have 3rd party dependencies that are not processed by Babel but have specific polyfill requirements (e.g. Axios and Vuex require Promise support). | ||
|
||
### jsx | ||
|
||
- Default: `true`. | ||
|
||
Set to `false` to disable JSX support. Or you can toggle [@vue/babel-preset-jsx](https://github.com/vuejs/jsx/tree/dev/packages/babel-preset-jsx) features here. | ||
|
||
### loose | ||
|
||
- Default: `false`. | ||
|
||
Setting this to `true` will generate code that is more performant but less spec-compliant. | ||
|
||
### entryFiles | ||
|
||
- Default: `[]` | ||
|
||
Multi page repo use `entryFiles` to ensure inject polyfills to all entry file. |
157 changes: 157 additions & 0 deletions
157
4.Vue-cli/packages/@vue/babel-preset-app/__tests__/babel-preset.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,157 @@ | ||
const path = require('path') | ||
const babel = require('@babel/core') | ||
const preset = require('../index') | ||
const defaultOptions = { | ||
babelrc: false, | ||
presets: [preset], | ||
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(`import "${['.*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('import "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('import "core-js/modules/es.map"') | ||
}) | ||
|
||
test('modern mode always skips polyfills', () => { | ||
process.env.VUE_CLI_MODERN_BUILD = true | ||
let { code } = babel.transformSync(` | ||
const a = new Map() | ||
`.trim(), { | ||
babelrc: false, | ||
presets: [[preset, { | ||
targets: { ie: 9 }, | ||
useBuiltIns: 'usage' | ||
}]], | ||
filename: 'test-entry-file.js' | ||
}) | ||
// default includes | ||
expect(code).not.toMatch(getAbsolutePolyfill('es.promise')) | ||
// usage-based detection | ||
expect(code).not.toMatch('import "core-js/modules/es.map"') | ||
|
||
;({ code } = babel.transformSync(` | ||
const a = new Map() | ||
`.trim(), { | ||
babelrc: false, | ||
presets: [[preset, { | ||
targets: { ie: 9 }, | ||
useBuiltIns: 'entry' | ||
}]], | ||
filename: 'test-entry-file.js' | ||
})) | ||
// default includes | ||
expect(code).not.toMatch(getAbsolutePolyfill('es.promise')) | ||
// usage-based detection | ||
expect(code).not.toMatch('import "core-js/modules/es.map"') | ||
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(`import "regenerator-runtime/runtime"`) | ||
// should use required helper instead of inline | ||
expect(code).toMatch(/import _asyncToGenerator from ".*runtime-corejs3\/helpers\/esm\/asyncToGenerator\"/) | ||
}) | ||
|
||
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 | ||
} | ||
}]] | ||
}) | ||
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, { | ||
absoluteRuntime: false | ||
}]], | ||
filename: 'test-entry-file.js' | ||
}) | ||
|
||
expect(code).toMatch('import _toConsumableArray from "@babel/runtime-corejs3/helpers/esm/toConsumableArray"') | ||
expect(code).not.toMatch(getAbsolutePolyfill('es.promise')) | ||
}) |
Oops, something went wrong.