Skip to content

Commit 6a58956

Browse files
authored
feat: babel support new syntax and fine tuning compile (#342)
1 parent 51cc78b commit 6a58956

File tree

7 files changed

+149
-84
lines changed

7 files changed

+149
-84
lines changed

docs/recipes/javascript.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ We use a sane default preset for Babel, basically it:
99
- Compiles `object-rest-spread` to `Object.assign`.
1010
- Compiles `async/await` to Promise without regenerator using [babel-plugin-transform-async-to-promises](https://github.com/rpetrich/babel-plugin-transform-async-to-promises).
1111
- Compiles JSX.
12+
- Support [optional chaining](https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining) out of the box.
13+
- Support [nullish coalescing operator](https://babeljs.io/docs/en/babel-plugin-proposal-nullish-coalescing-operator) out of the box.
1214

1315
You can add a `.babelrc` file in your project to use your custom config instead. If you want to disable `.babelrc` in your project, pass `--no-babelrc` flag.
1416

package.json

+5-4
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,15 @@
3131
"node 6"
3232
],
3333
"dependencies": {
34-
"@babel/core": "^7.2.2",
34+
"@babel/core": "^7.9.6",
35+
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.8.3",
3536
"@babel/plugin-proposal-object-rest-spread": "^7.3.1",
36-
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
37+
"@babel/plugin-proposal-optional-chaining": "^7.9.0",
3738
"@babel/plugin-transform-react-jsx": "^7.3.0",
38-
"@babel/preset-env": "^7.3.1",
39+
"@babel/preset-env": "^7.9.6",
3940
"@babel/preset-typescript": "^7.1.0",
4041
"@rollup/plugin-replace": "^2.3.2",
41-
"babel-plugin-transform-async-to-promises": "^0.8.4",
42+
"babel-plugin-transform-async-to-promises": "^0.8.15",
4243
"chalk": "^2.4.2",
4344
"ora": "^3.0.0",
4445
"rollup": "^1.1.2",

src/babel/preset.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,22 @@ export default (
1111
minimal = process.env.BILI_MINIMAL
1212
} = {}
1313
) => {
14-
let presets: any[] = []
15-
let plugins: any[] = []
16-
17-
presets = [
18-
...presets,
14+
const presets = [
1915
!minimal && [
2016
require('@babel/preset-env').default,
2117
{
2218
modules: ENV === 'test' ? 'auto' : false,
23-
exclude: ['transform-regenerator', 'transform-async-to-generator']
19+
exclude: [
20+
'transform-regenerator',
21+
'transform-async-to-generator',
22+
'proposal-object-rest-spread'
23+
]
2424
}
2525
],
2626
require('@babel/preset-typescript')
2727
].filter(Boolean)
2828

29-
plugins = [
30-
...plugins,
31-
require('@babel/plugin-syntax-dynamic-import'),
29+
const plugins = [
3230
[
3331
require('@babel/plugin-transform-react-jsx'),
3432
{
@@ -42,6 +40,8 @@ export default (
4240
loose: true
4341
}
4442
],
43+
[require('@babel/plugin-proposal-optional-chaining')],
44+
[require('@babel/plugin-proposal-nullish-coalescing-operator')],
4545
[
4646
alterObjectAssign,
4747
{

src/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ export class Bundler {
186186
// [1] https://github.com/egoist/bili/issues/305
187187
const getObjectHashIgnoreUnknownHack = (): boolean => {
188188
try {
189-
const { version } = this.localRequire('rollup-plugin-typescript2/package.json')
189+
const { version } = this.localRequire(
190+
'rollup-plugin-typescript2/package.json'
191+
)
190192
const semver = require('semver')
191193
return semver.lt(version, '0.26.0')
192194
} catch (e) {
@@ -299,7 +301,7 @@ export class Bundler {
299301

300302
const env = Object.assign({}, config.env)
301303

302-
// drop precess.env.NODE_ENV from umd/iife
304+
// drop process.env.NODE_ENV from umd/iife
303305
if (
304306
['umd', 'umd-min', 'iife', 'iife-min'].includes(format) &&
305307
env.NODE_ENV === undefined

src/plugins/babel.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import babel from 'rollup-plugin-babel'
22
import preset from '../babel/preset'
33
import { BabelPresetOptions } from '../types'
44

5-
export default babel.custom((core: any) => {
6-
const presetItem = core.createConfigItem(preset, {
5+
export default babel.custom(babelCore => {
6+
const presetItem = babelCore.createConfigItem(preset, {
77
type: 'preset'
88
})
99

@@ -26,6 +26,7 @@ export default babel.custom((core: any) => {
2626
}
2727
},
2828

29+
// Passed Babel's 'PartialConfig' object.
2930
config(cfg: any, data: any) {
3031
if (cfg.hasFilesystemConfig()) {
3132
// Use the normal config

types.d.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@ declare module 'tinydate' {
66
export = tinydate
77
}
88

9-
declare module 'rollup-plugin-babel'
9+
declare module 'rollup-plugin-babel' {
10+
import { createConfigItem } from '@babel/core'
11+
interface BabelPlugin {
12+
custom: (
13+
callback: (
14+
babelCore: { createConfigItem: typeof createConfigItem }
15+
) => any
16+
) => any
17+
}
18+
const babelPlugin: BabelPlugin
19+
export default babelPlugin
20+
}
1021

1122
declare module 'babel-plugin-alter-object-assign'
1223

0 commit comments

Comments
 (0)