汇总+ React无法编译JSX [英] Rollup + React not compiling JSX

查看:128
本文介绍了汇总+ React无法编译JSX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用汇总+ React,但是遇到汇总时遇到了JSX错误.

I'm trying to use Rollup + React but I'm encounting an error when rollup encounters JSX.

Unexpected token... export default () => <p>M...

我有一个 repo 会触发错误.我使用Rollup + React找到的所有文档/示例都没有使用最新的Babel,因此可能与Babel有关.

I have a repo that triggers the error. All documentation/examples I've found using Rollup + React don't use the latest Babel so it might have something to do with Babel.

rollup.config.js:

import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import babel from 'rollup-plugin-babel';
import pkg from './package.json';

export default [{
        input: 'src/index.js',
        output: {
        name: 'index',
        file: pkg.main,
        format: 'umd'
    },
    plugins: [
        resolve(),
        commonjs(),
        babel({ 
            exclude: 'node_modules/**',
            presets: ['@babel/env', '@babel/preset-react']
        })
    ],
    external: [
        'react',
        'prop-types',
    ],
    globals: {
        react: "React"
    }
},
];

.babelrc:

{
  "presets": [
  ["@babel/env", { "modules": false }], "@babel/preset-react"]
}

推荐答案

解决方案是两个交换2个插件的顺序

The solution to this is two swap the order of 2 of the plugins

来自:

plugins: [
    resolve(),
    commonjs(),
    babel({ 
        exclude: 'node_modules/**',
        presets: ['@babel/env', '@babel/preset-react']
    })
],

收件人:

plugins: [
    resolve(),
    babel({ 
        exclude: 'node_modules/**',
        presets: ['@babel/env', '@babel/preset-react']
    }),
    commonjs()
],

感谢 vladshcherbin .

这篇关于汇总+ React无法编译JSX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆