electron / forge

:electron: A complete tool for building and publishing Electron applications
https://electronforge.io
MIT License
6.41k stars 506 forks source link

Use Esbuild in webpack plugin #2127

Closed linonetwo closed 3 years ago

linonetwo commented 3 years ago

Preflight Checklist

Problem Description

ESBuild is super fast, and is becoming popular ( See https://2020.stateofjs.com/en-US/technologies/build-tools/ )

Hope electron forge webpack can utilize esbuild to speed up.

Proposed Solution

Replace ts-loader with esbuild directly, see link for details:

esbuild:https://esbuild.github.io/ esbuild-loader:https://github.com/privatenumber/esbuild-loader

Alternatives Considered

Maybe an esbuild plugin? But I think this is not necessary, because esbuild can be used in webpack.

Additional Information

An expert in ByteDance uses esbuild to speed up his building up to 144% ! (From 71s to 29s, with 0.05MLoc )

linonetwo commented 3 years ago

esbuild doesn't support decorator, so this can't be a drop'in replacement for every user. https://github.com/evanw/esbuild/pull/509

But for people searched here, you can easily do so to adopt esbuild, if you are not using Decorator Metadata:

  1. prepare a new tsconfig for esbuild, because 1. it don'r understand json with comment https://github.com/privatenumber/esbuild-loader/issues/58#issuecomment-760702588 2. it don't support all configs

webpack.rules.js

  {
    test: /\.tsx?$/,
    exclude: /(node_modules|\.webpack)/,
    use: {
      loader: 'esbuild-loader',
      options: {
        loader: 'tsx',
        tsconfigRaw: require('./tsconfig.esbuild.json'),
        target: 'es2015',
      },
    },
  },

webpack.plugins.js

const { ESBuildPlugin } = require('esbuild-loader');

module.exports = [
  new ESBuildPlugin(),

webpack.main.config.js

const webpack = require('webpack');
const { ESBuildPlugin } = require('esbuild-loader');
const { webpackAlias } = require('./webpack.alias');

module.exports = {
  /**
   * This is the main entry point for your application, it's the first file
   * that runs in the main process.
   */
  entry: './src/main/main.ts',
  // Put your normal webpack config below here
  module: {
    rules: require('./webpack.rules'),
  },
  resolve: {
    alias: webpackAlias,
    extensions: ['.js', '.ts', '.jsx', '.tsx', '.css', '.json'],
  },
  plugins: [new ESBuildPlugin()],
};
linonetwo commented 3 years ago

And this might be a workaround https://github.com/evanw/esbuild/issues/257#issuecomment-658069578

malept commented 3 years ago

I'm not inclined to add yet another template just for esbuild + webpack support. It's possible to create a third party template with this configuration if you wish to make it.

RamK777-stack commented 1 year ago

@linonetwo hot reload is not working. when reloading getting blank white screen. any ideas ?

I used the same configuration. as you mentioned above.

in main.ts I am using mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY); is this a reason ? but for first time while starting app everything rendered correctly. while reloading screen gets white.

linonetwo commented 1 year ago

I use mainWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY) too, so I don't know. Please look at my code to check differences. Mine can hot reload renderer process, but main process don't have hot reload. https://github.com/tiddly-gittly/TidGi-Desktop

RamK777-stack commented 1 year ago

Thank !! @linonetwo I have customized header menu options, so In the reload action I am using BrowserWindow.getFocusedWindow()?.webContents.reload() . while doing this getting blank white screen. previously it was worked.

linonetwo commented 1 year ago

I'm doing the exactly same thing, https://github.com/tiddly-gittly/TidGi-Desktop/blob/feat/execute-graph/src/services/view/index.ts#L195 but works very fine.

And MAIN_WINDOW_WEBPACK_ENTRY

https://github.com/tiddly-gittly/TidGi-Desktop/blob/feat/execute-graph/src/services/windows/index.ts#L233

RamK777-stack commented 12 months ago

Thanks for the reply @linonetwo, I tried everything, and when I reloaded all the files in the source tab, and hangs showing only white screen. is this because of large bundle ? .webpack folder size is 136 MB

image