Closed5

[Electron] React x TypeScript を Rspack でビルドしてみる

はっぱはっぱ
  • rspack.config.ts
import type { Configuration } from "@rspack/cli";
import { CssExtractRspackPlugin, HtmlRspackPlugin } from "@rspack/core";

const isDev = process.env.NODE_ENV === "development";

const common: Configuration = {
  mode: isDev ? "development" : "production",
  externals: ["fsevents"],
  resolve: {
    extensions: [".js", ".ts", ".jsx", ".tsx", ".json"],
  },
  output: {
    publicPath: "./",
    assetModuleFilename: "assets/[name][ext]",
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        exclude: /node_modules/,
        loader: "builtin:swc-loader",
        options: {
          jsc: {
            parser: {
              syntax: "typescript",
            },
            transform: {
              react: {
                runtime: "automatic",
              },
            },
          },
        },
      },
      {
        test: /\.css$/,
        use: [CssExtractRspackPlugin.loader, "css-loader"],
        type: "javascript/auto",
      },
      {
        test: /\.(ico|png|svg|eot|woff?2?)$/,
        type: "asset/resource",
      },
    ],
  },
  watch: isDev,
  devtool: isDev ? "source-map" : false,
};

const main: Configuration = {
  ...common,
  target: "electron-main",
  entry: {
    main: "./src/main.ts",
  },
};

const preload: Configuration = {
  ...common,
  target: "electron-preload",
  entry: {
    preload: "./src/preload.ts",
  },
};

const renderer: Configuration = {
  ...common,
  target: "web",
  entry: {
    app: "./src/web/index.tsx",
  },
  plugins: [
    new CssExtractRspackPlugin(),
    new HtmlRspackPlugin({
      inject: "body",
      template: "./src/web/index.html",
    }),
  ],
};

export default [main, preload, renderer];
はっぱはっぱ
  • package.json
{
  "scripts": {
    "dev": "rimraf dist && run-p dev:rspack dev:electron",
    "dev:rspack": "rspack --node-env \"development\"",
    "dev:electron": "wait-on ./dist/index.html ./dist/main.js && electronmon .",
    "build": "rimraf dist && rspack --node-env \"production\""
  }
}
はっぱはっぱ
  • tsconfig.json
{
  "compilerOptions": {
    "target": "ESNext",
    "module": "ESNext",
    "moduleResolution": "bundler",
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "jsx": "react-jsx",
    "strict": true
  },
  "ts-node": {
    "compilerOptions": {
      "module": "CommonJS"
    }
  }
}
はっぱはっぱ
  • webpack
% time npm run build

> leafview@3.3.3 build
> rimraf dist && cross-env NODE_ENV="production" webpack --progress

npm run build  22.19s user 1.02s system 280% cpu 8.275 total
  • Rspack
% time npm run build

> leafview@3.3.3 build
> rimraf dist && rspack --node-env "production"

npm run build  3.21s user 0.31s system 215% cpu 1.634 total
このスクラップは2日前にクローズされました