Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lose require jsc in renderer entry file #21

Open
jjj201200 opened this issue Feb 2, 2023 · 2 comments
Open

Lose require jsc in renderer entry file #21

jjj201200 opened this issue Feb 2, 2023 · 2 comments
Labels
electron-builder needs clarification needs reproduction The author did not provide a reproduction repository potential bug

Comments

@jjj201200
Copy link

Hi, thanks for your work. I am using this plugin with electron-builder.

It works fine in electron-main, but not renderer.

Specifically, this plugin can output both main and renderer jsc files and the entry file.

The entry file for main is ok, it contains the coderequire("./main.compiled.jsc").

But the renderer entry file does not contain the require code, but any other file, such as the css file import in js.

WHY?

In order to run, i add following code in index.html to load renderer's jsc file and it works fine:

if (process.env.NODE_ENV !== 'development') {
  require.extensions[".css"] = () => {
    return;
  };
  require('./main.compiled.jsc');
}

the browser code:

mainWindow = new BrowserWindow({
      show: false,
      width: 1024,
      height: 728,
      icon: getAssetPath('icon.png'),
      webPreferences: {
          nodeIntegration: true,
          nodeIntegrationInWorker: true,
          contextIsolation: false,
          devTools: true,
          preload: app.isPackaged
              ? path.join(__dirname, 'preload.js')
              : path.join(__dirname, '../../.erb/dll/preload.js'),
      },
  });

The renderer webpack config is similar to main's not special:

/**
 * Build config for electron renderer process
 */

import path from 'path';
import webpack from 'webpack';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import {BundleAnalyzerPlugin} from 'webpack-bundle-analyzer';
import CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
import {merge} from 'webpack-merge';
import TerserPlugin from 'terser-webpack-plugin';
import {BytenodeWebpackPlugin} from '@herberttn/bytenode-webpack-plugin';
import baseConfig from './webpack.config.base';
import webpackPaths from './webpack.paths';
import checkNodeEnv from '../scripts/check-node-env';
import deleteSourceMaps from '../scripts/delete-source-maps';

checkNodeEnv('production');
deleteSourceMaps();

const configuration: webpack.Configuration = {
    devtool: 'source-map',

    mode: 'production',

    target: 'electron-renderer',

    entry: {
        main: path.join(webpackPaths.srcRendererPath, 'main.ts'),
    },

    output: {
        path: webpackPaths.distRendererPath,
        publicPath: './',
        filename: '[name].js',
        library: {
            type: 'umd',
        },
    },

    module: {
        rules: [
            {
                test: /\.s?(a|c|le)ss$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    {
                        loader: 'css-loader',
                        options: {
                            modules: true,
                            sourceMap: true,
                            importLoaders: 1,
                        },
                    },
                    'sass-loader',
                ],
                include: /\.module\.s?(c|a)ss$/,
            },
            {
                test: /\.s?(a|c|le)ss$/,
                use: [MiniCssExtractPlugin.loader, 'css-loader', 'sass-loader'],
                exclude: /\.module\.s?(c|a)ss$/,
            },
            // Fonts
            {
                test: /\.(woff|woff2|eot|ttf|otf)$/i,
                type: 'asset/resource',
            },
            // Images
            {
                test: /\.(png|jpg|jpeg|gif)$/i,
                type: 'asset/resource',
            },
            // SVG
            {
                test: /\.svg$/,
                use: [
                    {
                        loader: '@svgr/webpack',
                        options: {
                            prettier: false,
                            svgo: false,
                            svgoConfig: {
                                plugins: [{removeViewBox: false}],
                            },
                            titleProp: true,
                            ref: true,
                        },
                    },
                    'file-loader',
                ],
            },
        ],
    },

    optimization: {
        minimize: true,
        minimizer: [
            new TerserPlugin({
                parallel: true,
            }),
            new CssMinimizerPlugin(),
        ],
    },

    plugins: [
        /**
         * Create global constants which can be configured at compile time.
         *
         * Useful for allowing different behaviour between development builds and
         * release builds
         *
         * NODE_ENV should be production so that modules do not perform certain
         * development checks
         */
        new webpack.EnvironmentPlugin({
            NODE_ENV: 'production',
            DEBUG_PROD: false,
        }),

        new MiniCssExtractPlugin({
            filename: 'style.css',
        }),

        new BundleAnalyzerPlugin({
            analyzerMode: process.env.ANALYZE === 'true' ? 'server' : 'disabled',
            analyzerPort: 8889,
        }),

        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: path.join(webpackPaths.srcRendererPath, 'index.ejs'),
            minify: {
                collapseWhitespace: true,
                removeAttributeQuotes: true,
                removeComments: true,
            },
            isBrowser: false,
            isDevelopment: process.env.NODE_ENV !== 'production',
        }),

        new webpack.DefinePlugin({
            'process.type': '"renderer"',
        }),
        new BytenodeWebpackPlugin({
            compileForElectron: true,
        }),
    ],
};

export default merge(baseConfig, configuration);
@herberttn
Copy link
Owner

Hey @jjj201200 I did not understand the problem, can you explain in more detail?
Also, I need a reproduction repo to test and fix the issue, can you make one?

@herberttn herberttn added electron-builder needs reproduction The author did not provide a reproduction repository bug Something isn't working needs clarification potential bug and removed bug Something isn't working labels Feb 27, 2023
@herberttn
Copy link
Owner

@jjj201200 does this still happen? If so, can you create a reproduction repo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
electron-builder needs clarification needs reproduction The author did not provide a reproduction repository potential bug
Projects
None yet
Development

No branches or pull requests

2 participants