详细问题描述
在babel.config.js 文件中配置@babel/plugin-proposal-pipeline-operator 、@babel/plugin-proposal-do-expressions、@babel/plugin-proposal-optional-chaining等插件,在网页开发模式可以正确识别插件,在编译到小程序到时候会报错
[内容]
步骤1
修改babel.config.js 文件
const plugins = [
['@babel/plugin-proposal-pipeline-operator', { 'proposal': 'minimal' }],
'@babel/plugin-proposal-do-expressions',
'@babel/plugin-proposal-optional-chaining'
]
if (process.env.UNI_OPT_TREESHAKINGNG) {
plugins.push(require('@dcloudio/vue-cli-plugin-uni-optimize/packages/babel-plugin-uni-api/index.js'))
}
if (process.env.UNI_PLATFORM === 'app-plus' && process.env.UNI_USING_V8) {
const path = require('path')
const isWin = /^win/.test(process.platform)
const normalizePath = path => (isWin ? path.replace(/\/g, '/') : path)
const input = normalizePath(process.env.UNI_INPUT_DIR)
try {
plugins.push([
require('@dcloudio/vue-cli-plugin-hbuilderx/packages/babel-plugin-console'),
{
file (file) {
file = normalizePath(file)
if (file.indexOf(input) === 0) {
return path.relative(input, file)
}
return false
}
}
])
} catch (e) {}
}
process.UNI_LIBRARIES = process.UNI_LIBRARIES || ['@dcloudio/uni-ui']
process.UNI_LIBRARIES.forEach(libraryName => {
plugins.push([
'import',
{
'libraryName': libraryName,
'customName': (name) => {
return ${libraryName}/lib/${name}/${name}
}
}
])
})
module.exports = {
presets: [
[
'@vue/app',
{
modules: 'commonjs',
useBuiltIns: process.env.UNI_PLATFORM === 'h5' ? 'usage' : 'entry'
}
]
],
plugins
}
步骤2
在任意.Vue 文件中使用管道操作符编写代码
let a = function (i) {
return i+1
}
console.log(
123 |> a
)
[结果]
在网页开发模式中正常,小程序模式报错
Syntax Error: SyntaxError: This experimental syntax requires enabling the parser plugin: 'pipelineOperator' (35:9)
[期望]
小程序模式中也可以使用babel插件
IDE运行环境说明
WebStorm
联系方式
[QQ] 120267583
0 个回复