开发者问题收集

Rollup、TypeScript、Electron、React 设置

2020-09-24
2450

我正在尝试使用 typescript 为 electron 和 react 创建汇总设置。我收到一个引用错误,因为 process 未在 /node_modules/react/index.js 中定义。

我创建了以下配置文件:

从 '@rollup/plugin-commonjs' 导入 commonjs 从 'rollup-plugin-copy' 导入 copy 从 '@rollup/plugin-typescript' 导入 typescript 从 '@rollup/plugin-node-resolve' 导入 { nodeResolve }

rollup-config.js:

export default [
   // electron
   {
      input: 'src/main.ts',
      output: [
         {
            file: 'build/main.js',
            format: 'cjs',
            sourcemap: true
         },
      ],
      plugins: [
         nodeResolve(),
         typescript({
            target: 'es5',
         }),
         commonjs({
            include: './node_modules/**',
         }),
         copy({
            targets: [
               { src: './src/index.html', dest: './build' }
            ]
         }),
      ]
   },
   // react
   {
      input: 'src/react/index.tsx',
      output: [
         {
            file: 'build/scripts.js',
            format: 'esm',
            sourcemap: true,
            globals: [
               'react',
            ],
         },
      ],
      plugins: [
         typescript({
            target: 'es5',
            module: 'ESNext',
         }),
         nodeResolve(),
         commonjs({
            include: './node_modules/**',
         }),
      ]
   }
]

tsconfig.json:

{
   "compilerOptions": {
      "target": "es5",
      "module": "commonjs",
      "lib": [
         "dom",
         "es2015",
         "es2016",
         "es2017"
      ],
      "allowJs": true,
      "moduleResolution": "node",
      "sourceMap": true,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "removeComments": true,
      "noImplicitAny": true,
      "suppressImplicitAnyIndexErrors": true,
      "forceConsistentCasingInFileNames": true,
      "importHelpers": true,
      "strict": true,
      "alwaysStrict": true,
      "strictNullChecks": true,
      "strictFunctionTypes": true,
      "strictBindCallApply": true,
      "strictPropertyInitialization": true,
      "noImplicitThis": true,
      "esModuleInterop": true,
      "jsx": "react",
   },
   "exclude": [
      "node_modules"
   ]
}

我希望你能帮我解决这个问题。遗憾的是,我不知道问题出在哪里。

问候

1个回答

我稍微摆弄了一下,让它按照我想要的方式工作:

rollup.config.js

import commonjs from '@rollup/plugin-commonjs'
import copy from 'rollup-plugin-copy'
import typescript from '@rollup/plugin-typescript'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'

export default [
   // electron
   {
      input: 'src/main.ts',
      output: [
         {
            file: 'build/main.js',
            format: 'cjs',
            sourcemap: true
         },
      ],
      plugins: [
         nodeResolve(),
         typescript(),
         commonjs({
            include: './node_modules/**',
         }),
         copy({
            targets: [
               { src: './src/index.html', dest: './build' }
            ]
         }),
      ]
   },
   // react
   {
      input: 'src/react/index.tsx',
      output: [
         {
            file: 'build/scripts.js',
            format: 'esm',
            sourcemap: true,
            globals: [
               'react',
            ],
         },
      ],
      plugins: [
         typescript({
            module: 'ESNext',
         }),
         commonjs({
            include: './node_modules/**',
         }),
         nodeResolve(),
         replace({
            'process.env.NODE_ENV': JSON.stringify('production')
         }),
      ]
   }
]

tsconfig.json

{
   "compilerOptions": {
      "target": "es5",
      "module": "commonjs",
      "lib": [
         "dom",
         "es2015",
         "es2016",
         "es2017"
      ],
      "allowJs": true,
      "moduleResolution": "node",
      "sourceMap": true,
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "removeComments": true,
      "noImplicitAny": true,
      "suppressImplicitAnyIndexErrors": true,
      "forceConsistentCasingInFileNames": true,
      "importHelpers": true,
      "strict": true,
      "alwaysStrict": true,
      "strictNullChecks": true,
      "strictFunctionTypes": true,
      "strictBindCallApply": true,
      "strictPropertyInitialization": true,
      "noImplicitThis": true,
      "esModuleInterop": true,
      "jsx": "react",
   },
   "exclude": [
      "node_modules"
   ]
}
pgalle
2020-09-25