Skip to content

Commit 40efc5e

Browse files
committed
feat: add vue-cli3 source
1 parent 29c27c1 commit 40efc5e

File tree

744 files changed

+43708
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

744 files changed

+43708
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__tests__
2+
__mocks__
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# @vue/babel-preset-app
2+
3+
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.**
4+
5+
## Included Features
6+
7+
### [@babel/preset-env](https://new.babeljs.io/docs/en/next/babel-preset-env.html)
8+
9+
`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.
10+
11+
- `modules: false`
12+
- auto set to `'commonjs'` in Jest tests
13+
- [`useBuiltIns: 'usage'`](#usebuiltins)
14+
- `targets` is determined:
15+
- using `browserslist` field in `package.json` when building for browsers
16+
- set to `{ node: 'current' }` when running unit tests in Node.js
17+
- Includes `Promise` polyfill by default so that they are usable even in non-transpiled dependencies (only for environments that need it)
18+
19+
### Stage 3 or Below
20+
21+
Only the following stage 3 or below features are supported (object reset spread is supported as part of `preset-env`):
22+
23+
- [Dynamic Import Syntax](https://github.com/tc39/proposal-dynamic-import)
24+
- [Proposal Class Properties](https://babeljs.io/docs/en/next/babel-plugin-proposal-class-properties.html)
25+
- [Proposal Decorators (legacy)](https://babeljs.io/docs/en/next/babel-plugin-proposal-decorators.html)
26+
27+
If you need additional stage 3 or below features, you need to install and configure it yourself.
28+
29+
### Vue JSX support
30+
31+
- [@babel/plugin-syntax-jsx](https://github.com/babel/babel/tree/master/packages/babel-plugin-syntax-jsx)
32+
- [babel-plugin-transform-vue-jsx](https://github.com/vuejs/babel-plugin-transform-vue-jsx)
33+
- ~~[babel-plugin-jsx-event-modifiers](https://github.com/nickmessing/babel-plugin-jsx-event-modifiers)~~ (temporarily disabled until fixed for Babel 7)
34+
- ~~[babel-plugin-jsx-v-model](https://github.com/nickmessing/babel-plugin-jsx-v-model)~~ (temporarily disabled until fixed for Babel 7)
35+
36+
### [@babel/plugin-transform-runtime](https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-runtime)
37+
38+
`transform-runtime` avoids inlining helpers in every file. This is enabled for helpers only, since polyfills are handled by `babel-preset-env`.
39+
40+
## Options
41+
42+
- 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.
43+
44+
### modules
45+
46+
- Default:
47+
- `false` when building with webpack
48+
- `'commonjs'` when running tests in Jest.
49+
50+
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.
51+
52+
### targets
53+
54+
- Default:
55+
- determined from `browserslist` field in `package.json` when building for browsers
56+
- set to `{ node: 'current' }` when running unit tests in Node.js
57+
58+
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.
59+
60+
### useBuiltIns
61+
62+
- Default: `'usage'`
63+
- Allowed values: `'usage' | 'entry' | false`
64+
65+
Explicitly set `useBuiltIns` option for `babel-preset-env`.
66+
67+
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.
68+
69+
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.
70+
71+
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:
72+
73+
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.
74+
75+
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.
76+
77+
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.
78+
79+
See [@babel/preset-env docs](https://new.babeljs.io/docs/en/next/babel-preset-env.html#usebuiltins-usage) for more details.
80+
81+
### polyfills
82+
83+
- Default: `['es6.array.iterator', 'es6.promise', 'es7.promise.finally']`
84+
85+
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**.
86+
87+
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).
88+
89+
### jsx
90+
91+
- Default: `true`.
92+
93+
Set to `false` to disable JSX support.
94+
95+
### loose
96+
97+
- Default: `false`.
98+
99+
Setting this to `true` will generate code that is more performant but less spec-compliant.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
const babel = require('@babel/core')
2+
const preset = require('../index')
3+
const defaultOptions = {
4+
babelrc: false,
5+
presets: [preset]
6+
}
7+
8+
test('polyfill detection', () => {
9+
let { code } = babel.transformSync(`
10+
const a = new Map()
11+
`.trim(), {
12+
babelrc: false,
13+
presets: [[preset, {
14+
targets: { node: 'current' }
15+
}]]
16+
})
17+
// default includes
18+
expect(code).not.toMatch(`import "core-js/modules/es6.promise"`)
19+
// usage-based detection
20+
expect(code).not.toMatch(`import "core-js/modules/es6.map"`)
21+
22+
;({ code } = babel.transformSync(`
23+
const a = new Map()
24+
`.trim(), {
25+
babelrc: false,
26+
presets: [[preset, {
27+
targets: { ie: 9 }
28+
}]]
29+
}))
30+
// default includes
31+
expect(code).toMatch(`import "core-js/modules/es6.promise"`)
32+
// promise polyfill alone doesn't work in IE, needs this as well. fix: #1642
33+
expect(code).toMatch(`import "core-js/modules/es6.array.iterator"`)
34+
// usage-based detection
35+
expect(code).toMatch(`import "core-js/modules/es6.map"`)
36+
})
37+
38+
test('modern mode always skips polyfills', () => {
39+
process.env.VUE_CLI_MODERN_BUILD = true
40+
let { code } = babel.transformSync(`
41+
const a = new Map()
42+
`.trim(), {
43+
babelrc: false,
44+
presets: [[preset, {
45+
targets: { ie: 9 },
46+
useBuiltIns: 'usage'
47+
}]]
48+
})
49+
// default includes
50+
expect(code).not.toMatch(`import "core-js/modules/es6.promise"`)
51+
// usage-based detection
52+
expect(code).not.toMatch(`import "core-js/modules/es6.map"`)
53+
54+
;({ code } = babel.transformSync(`
55+
const a = new Map()
56+
`.trim(), {
57+
babelrc: false,
58+
presets: [[preset, {
59+
targets: { ie: 9 },
60+
useBuiltIns: 'entry'
61+
}]]
62+
}))
63+
// default includes
64+
expect(code).not.toMatch(`import "core-js/modules/es6.promise"`)
65+
// usage-based detection
66+
expect(code).not.toMatch(`import "core-js/modules/es6.map"`)
67+
delete process.env.VUE_CLI_MODERN_BUILD
68+
})
69+
70+
test('object spread', () => {
71+
const { code } = babel.transformSync(`
72+
const a = { ...b }
73+
`.trim(), defaultOptions)
74+
expect(code).toMatch(`import _objectSpread from`)
75+
expect(code).toMatch(`var a = _objectSpread({}, b)`)
76+
})
77+
78+
test('dynamic import', () => {
79+
expect(() => {
80+
babel.transformSync(`const Foo = () => import('./Foo.vue')`, defaultOptions)
81+
}).not.toThrow()
82+
})
83+
84+
test('async/await', () => {
85+
const { code } = babel.transformSync(`
86+
async function hello () {
87+
await Promise.resolve()
88+
}
89+
hello()
90+
`.trim(), defaultOptions)
91+
expect(code).toMatch(`import "core-js/modules/es6.promise"`)
92+
// should use regenerator runtime
93+
expect(code).toMatch(`import "regenerator-runtime/runtime"`)
94+
// should use required helper instead of inline
95+
expect(code).toMatch(/@babel.*runtime\/helpers\/.*asyncToGenerator/)
96+
})
97+
98+
test('jsx', () => {
99+
const { code } = babel.transformSync(`
100+
export default {
101+
render () {
102+
return <div>bar</div>
103+
}
104+
}
105+
`.trim(), defaultOptions)
106+
expect(code).toMatch(`var h = arguments[0]`)
107+
expect(code).toMatch(`return h("div", ["bar"])`)
108+
})
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
const path = require('path')
2+
3+
const defaultPolyfills = [
4+
// promise polyfill alone doesn't work in IE,
5+
// needs this as well. see: #1642
6+
'es6.array.iterator',
7+
// this is required for webpack code splitting, vuex etc.
8+
'es6.promise',
9+
// #2012 es6.promise replaces native Promise in FF and causes missing finally
10+
'es7.promise.finally'
11+
]
12+
13+
function getPolyfills (targets, includes, { ignoreBrowserslistConfig, configPath }) {
14+
const { isPluginRequired } = require('@babel/preset-env')
15+
const builtInsList = require('@babel/preset-env/data/built-ins.json')
16+
const getTargets = require('@babel/preset-env/lib/targets-parser').default
17+
const builtInTargets = getTargets(targets, {
18+
ignoreBrowserslistConfig,
19+
configPath
20+
})
21+
22+
return includes.filter(item => {
23+
return isPluginRequired(builtInTargets, builtInsList[item])
24+
})
25+
}
26+
27+
module.exports = (context, options = {}) => {
28+
const presets = []
29+
const plugins = []
30+
31+
// JSX
32+
if (options.jsx !== false) {
33+
plugins.push(
34+
require('@babel/plugin-syntax-jsx'),
35+
require('babel-plugin-transform-vue-jsx')
36+
// require('babel-plugin-jsx-event-modifiers'),
37+
// require('babel-plugin-jsx-v-model')
38+
)
39+
}
40+
41+
const {
42+
polyfills: userPolyfills,
43+
loose = false,
44+
debug = false,
45+
useBuiltIns = 'usage',
46+
modules = false,
47+
targets: rawTargets,
48+
spec,
49+
ignoreBrowserslistConfig = !!process.env.VUE_CLI_MODERN_BUILD,
50+
configPath,
51+
include,
52+
exclude,
53+
shippedProposals,
54+
forceAllTransforms,
55+
decoratorsLegacy
56+
} = options
57+
58+
// resolve targets
59+
let targets
60+
if (process.env.VUE_CLI_BABEL_TARGET_NODE) {
61+
// running tests in Node.js
62+
targets = { node: 'current' }
63+
} else if (process.env.VUE_CLI_BUILD_TARGET === 'wc' || process.env.VUE_CLI_BUILD_TARGET === 'wc-async') {
64+
// targeting browsers that at least support ES2015 classes
65+
// https://github.com/babel/babel/blob/master/packages/babel-preset-env/data/plugins.json#L52-L61
66+
targets = {
67+
browsers: [
68+
'Chrome >= 49',
69+
'Firefox >= 45',
70+
'Safari >= 10',
71+
'Edge >= 13',
72+
'iOS >= 10',
73+
'Electron >= 0.36'
74+
]
75+
}
76+
} else if (process.env.VUE_CLI_MODERN_BUILD) {
77+
// targeting browsers that support <script type="module">
78+
targets = { esmodules: true }
79+
} else {
80+
targets = rawTargets
81+
}
82+
83+
// included-by-default polyfills. These are common polyfills that 3rd party
84+
// dependencies may rely on (e.g. Vuex relies on Promise), but since with
85+
// useBuiltIns: 'usage' we won't be running Babel on these deps, they need to
86+
// be force-included.
87+
let polyfills
88+
const buildTarget = process.env.VUE_CLI_BUILD_TARGET || 'app'
89+
if (
90+
buildTarget === 'app' &&
91+
useBuiltIns === 'usage' &&
92+
!process.env.VUE_CLI_BABEL_TARGET_NODE &&
93+
!process.env.VUE_CLI_MODERN_BUILD
94+
) {
95+
polyfills = getPolyfills(targets, userPolyfills || defaultPolyfills, {
96+
ignoreBrowserslistConfig,
97+
configPath
98+
})
99+
plugins.push([require('./polyfillsPlugin'), { polyfills }])
100+
} else {
101+
polyfills = []
102+
}
103+
104+
const envOptions = {
105+
spec,
106+
loose,
107+
debug,
108+
modules,
109+
targets,
110+
useBuiltIns,
111+
ignoreBrowserslistConfig,
112+
configPath,
113+
include,
114+
exclude: polyfills.concat(exclude || []),
115+
shippedProposals,
116+
forceAllTransforms
117+
}
118+
119+
// cli-plugin-jest sets this to true because Jest runs without bundling
120+
if (process.env.VUE_CLI_BABEL_TRANSPILE_MODULES) {
121+
envOptions.modules = 'commonjs'
122+
// necessary for dynamic import to work in tests
123+
plugins.push(require('babel-plugin-dynamic-import-node'))
124+
}
125+
126+
// pass options along to babel-preset-env
127+
presets.push([require('@babel/preset-env'), envOptions])
128+
129+
// additional <= stage-3 plugins
130+
// Babel 7 is removing stage presets altogether because people are using
131+
// too many unstable proposals. Let's be conservative in the defaults here.
132+
plugins.push(
133+
require('@babel/plugin-syntax-dynamic-import'),
134+
[require('@babel/plugin-proposal-decorators'), { legacy: decoratorsLegacy !== false }],
135+
[require('@babel/plugin-proposal-class-properties'), { loose }],
136+
)
137+
138+
// transform runtime, but only for helpers
139+
plugins.push([require('@babel/plugin-transform-runtime'), {
140+
regenerator: useBuiltIns !== 'usage',
141+
corejs: useBuiltIns !== false ? false : 2,
142+
helpers: useBuiltIns === 'usage',
143+
useESModules: !process.env.VUE_CLI_BABEL_TRANSPILE_MODULES,
144+
absoluteRuntime: path.dirname(require.resolve('@babel/runtime/package.json'))
145+
}])
146+
147+
return {
148+
presets,
149+
plugins
150+
}
151+
}

0 commit comments

Comments
 (0)