Babel.js 导入第三方库时出现CommonJS汇总插件语法错误,主要与“进程”有关

erhoui1w  于 2022-12-08  发布在  Babel
关注(0)|答案(1)|浏览(389)

我一直在做一个定制的汇总配置,它接受一个React项目,并将js和css内联在index.html中。
当我导入一些第三方React库(比如material-ui-color)时,我遇到了CommonJS的一个问题,它说有一个语法错误。在我尝试安装的库中,“process”这个词是最常见的。下面是material-ui-color问题的日志:

[!] (plugin commonjs) SyntaxError: Unexpected token (205:28) in /Users/meyerm/Documents/GitHub/button-generator-figma-plugin/node_modules/jss/dist/jss.esm.js
node_modules/jss/dist/jss.esm.js (205:28)
203:     var newValue = value;
204:
205:     if (!options || options.process !== false) {
                                 ^

我已经包括了rollup-plugin-replace来修复任何process.env.NODE_ENV的出现,但这似乎是一个不同的问题。
以下是我的汇总配置:

import resolve from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import babel from '@rollup/plugin-babel';
import livereload from 'rollup-plugin-livereload';
import replace from '@rollup/plugin-replace';
import { terser } from 'rollup-plugin-terser';
import postcss from 'rollup-plugin-postcss';
import html from 'rollup-plugin-bundle-html-plus';
import typescript from 'rollup-plugin-typescript';
import svgr from '@svgr/rollup';

const production = !process.env.ROLLUP_WATCH;

export default [
  /* 
  Transpiling React code and injecting into index.html for Figma  
  */
  {
    input: 'src/app/index.tsx',
    output: {
      name: 'ui',
      file: 'dist/bundle.js',
      format: 'umd',
    },
    plugins: [
      // What extensions is rollup looking for
      resolve({
        extensions: ['.jsx', '.js', '.json', '.ts', '.tsx'],
      }),

      // Manage process.env
      replace({
        preventAssignment: true,
        process: JSON.stringify({
          env: {
            isProd: production,
          },
        }),
        'process.env.NODE_ENV': JSON.stringify(production),
      }),

      typescript({ sourceMap: !production }),

      // Babel config to support React
      babel({
        presets: ['@babel/preset-react', '@babel/preset-env'],
        babelHelpers: 'runtime',
        plugins: ['@babel/plugin-transform-runtime'],
        extensions: ['.js', '.ts', 'tsx', 'jsx'],
        compact: true,
        exclude: 'node_modules/**',
      }),

      commonjs({
        include: 'node_modules/**',
      }),

      svgr(),

      // Config to allow sass and css modules
      postcss({
        extensions: ['.css, .scss, .sass'],
        modules: true,
        use: ['sass'],
      }),

      // Injecting UI code into ui.html
      html({
        template: 'src/app/index.html',
        dest: 'dist',
        filename: 'index.html',
        inline: true,
        inject: 'body',
        ignore: /code.js/,
      }),

      // If dev mode, serve and livereload
      !production && serve(),
      !production && livereload('dist'),

      // If prod mode, minify
      production && terser(),
    ],
    watch: {
      clearScreen: true,
    },
  },

  /* 
  Main Figma plugin code
  */
  {
    input: 'src/plugin/controller.ts',
    output: {
      file: 'dist/code.js',
      format: 'iife',
      name: 'code',
    },
    plugins: [resolve(), typescript(), commonjs({ transformMixedEsModules: true }), production && terser()],
  },
];

function serve() {
  let started = false;

  return {
    writeBundle() {
      if (!started) {
        started = true;

        // Start localhost dev server on port 5000 to work on the UI in the browser
        require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
          stdio: ['ignore', 'inherit', 'inherit'],
          shell: true,
        });
      }
    },
  };
}

有什么想法,我可以克服这一点,并导入第三方库到项目容易?

g6ll5ycj

g6ll5ycj1#

我建议按以下方式重写配置:

replace({
  "process.env.isProd": production,
}),

相关问题