Search code examples
reactjstypescriptelectronrollup

Rollup, TypeScript, Electron, React setup


I'm trying to create a rollup setup for electron and react using typescript. That I get an Reference Error for process not being defined in /node_modules/react/index.js.

I created the following configuration files:

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'

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"
   ]
}

I hope you can help me with this. I sadly can't figure out what the problem is.

regards


Solution

  • I fiddled a little and got it working the way I want it to:

    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"
       ]
    }