Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(babel-preset-taro): fix #9659 #9660

Merged
merged 6 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/babel-preset-taro/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ module.exports = {
presets: [
// 已经填上的参数是默认值
['taro', {
// 框架:`nerv`, `react`, `vue` 三选一
// 框架:`nerv`, `react`, `vue`, `vue3` 四选一
framework: 'react',
// 是否使用 TypeScript,当值为 true 时,使用 `@babel/preset-typescript` 编译
ts: true,
// 是否使用 `@vue/babel-preset-jsx` (当`framework`为`vue`时)
// 或 `@vue/babel-plugin-jsx` (当`framework`为`vue3`时)来支持`jsx`编译
// 该参数配置方式同 `@vue/babel-preset-app`的`jsx`参数
// https://github.com/vuejs/vue-cli/blob/dev/packages/%40vue/babel-preset-app/README.md#jsx
vueJsx: true,
// 以下参数为 @babel/preset-env 的参数:
// https://babeljs.io/docs/en/babel-preset-env
loose: false,
Expand Down
54 changes: 50 additions & 4 deletions packages/babel-preset-taro/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,51 @@ describe('babel-preset-taro', () => {

const [override] = config.overrides

const [, preset] = override.presets
expect(preset).toBeUndefined()
const [, [jsxPreset, jsxOptions]] = override.presets
expect(jsxPreset === require('@vue/babel-preset-jsx')).toBeTruthy()
expect(jsxOptions).toEqual({})
})

it('vue3', () => {
const config = babelPresetTaro({}, {
framework: 'vue3'
})

expect(config.sourceType).toBe('unambiguous')

const [override] = config.overrides

const [[jsxPlugin, jsxOptions]] = override.plugins
expect(jsxPlugin === require('@vue/babel-plugin-jsx')).toBeTruthy()
expect(jsxOptions).toEqual({})
})

it('vue without jsx', () => {
const config = babelPresetTaro({}, {
framework: 'vue',
vueJsx: false
})

expect(config.sourceType).toBe('unambiguous')

const [override] = config.overrides

const [, jsxPreset] = override.presets
expect(jsxPreset).toBeUndefined()
})

it('vue3 without jsx', () => {
const config = babelPresetTaro({}, {
framework: 'vue3',
vueJsx: false
})

expect(config.sourceType).toBe('unambiguous')

const [override] = config.overrides

const [[jsxPlugin, jsxOptions]] = override.plugins
expect(jsxPlugin === require('@vue/babel-plugin-jsx')).toBeFalsy()
})

it('typescript react', () => {
Expand Down Expand Up @@ -71,10 +114,13 @@ describe('babel-preset-taro', () => {
ts: true
})

const [override] = config.overrides
const [, [ts, tsConfig]] = override.presets
const [override, vueOverride] = config.overrides
const [, , [ts, tsConfig]] = override.presets

expect(typeof ts.default === 'function').toBeTruthy()
expect(tsConfig.hasOwnProperty('jsxPragma') === false).toBeTruthy()

expect(vueOverride.include.test('a.vue')).toBeTruthy()
})

it('can change env options', () => {
Expand Down
19 changes: 17 additions & 2 deletions packages/babel-preset-taro/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = (_, options = {}) => {
}
const presets = []
const plugins = []
const overrides = []
const isReact = options.framework === 'react'
const isNerv = options.framework === 'nerv'
const isVue = options.framework === 'vue'
Expand All @@ -30,13 +31,27 @@ module.exports = (_, options = {}) => {
}
}

if (isVue || isVue3) {
if (options.vueJsx !== false) {
const jsxOptions = typeof options.vueJsx === 'object' ? options.vueJsx : {}
if (isVue) {
presets.push([require('@vue/babel-preset-jsx'), jsxOptions])
} else {
plugins.push([require('@vue/babel-plugin-jsx'), jsxOptions])
}
}
}

if (options.ts) {
const config = {}
if (isNerv || isReact) {
config.jsxPragma = moduleName
}
if (isVue || isVue3) {
config.allExtensions = true
overrides.push({
Sociosarbis marked this conversation as resolved.
Show resolved Hide resolved
include: /\.vue$/,
presets: [[require('@babel/preset-typescript'), { allExtensions: true, isTSX: true }]]
})
}
presets.push([require('@babel/preset-typescript'), config])
}
Expand Down Expand Up @@ -144,6 +159,6 @@ module.exports = (_, options = {}) => {
exclude: [/@babel[/|\\\\]runtime/, /core-js/],
presets,
plugins
}]
}, ...overrides]
}
}
2 changes: 2 additions & 0 deletions packages/babel-preset-taro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
"@babel/runtime": "^7.11.2",
"@tarojs/helper": "3.3.0",
"@tarojs/taro-h5": "3.3.0",
"@vue/babel-plugin-jsx": "^1.0.6",
"@vue/babel-preset-jsx": "^1.2.4",
"babel-plugin-dynamic-import-node": "^2.3.3",
"babel-plugin-global-define": "^1.0.3",
"babel-plugin-transform-imports-api": "^1.0.0",
Expand Down
Loading