汇总+ React无法编译JSX

时间:2018-10-19 00:21:25

标签: reactjs jsx rollup

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

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

我有一个repo会触发错误。我使用Rollup + React找到的所有文档/示例都没有使用最新的Babel,因此可能与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"]
}

1 个答案:

答案 0 :(得分:2)

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

来自:

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