繁体   English   中英

将Webpack配置为使用Electron以使用ES6导入?

[英]Configure Webpack with Electron to use ES6 imports?

我想使用Webpack,因为我想在我的Electron应用程序中使用ES模块,但是有一些障碍。 我只想在main进程和renderer进程中使用import

我的应用程序结构如下-

- src/                  // contains basic html, css & js
  - index.html          // <h1>Hello World</h1>
  - style.css           // is empty
  - app.js              // console.log('it works 🙈')
- app/                  // contains electron code
  - main_window.js
  - custom_tray.js
- index.js              // entry point for electron application
- dist/                 // output bundle generated from webpack
  - bundle.js

我的index.js文件看起来像-

import path from "path";
import { app } from "electron";

import MainWindow from "./app/main_window";
import CustomTray from "./app/custom_tray";

let win = null,
    tray = null;

app.on("ready", () => {
    // app.dock.hide();
    win = new MainWindow(path.join("file://", __dirname, "/src/index.html"));

    win.on("closed", () => {
        win = null;
    });

    tray = new CustomTray(win);
});

我的main_window.js文件看起来像-

import { BrowserWindow } from "electron";

const config = {
    width: 250,
    height: 350,
    show: false,
    frame: false,
    radii: [500, 500, 500, 500],
    resizable: false,
    fullscreenable: false
};

class MainWindow extends BrowserWindow {
    constructor(url) {
        super(config);

        this.loadURL(url);
        this.on("blur", this.onBlur);
        this.show();
    }

    onBlur = () => {
        this.hide();
    };
}

export default MainWindow;

我的custom_tray.js看起来像-

import path from "path";
import { app, Tray, Menu } from "electron";

const iconPath = path.join(__dirname, "../src/assets/iconTemplate.png");

class CustomTray extends Tray {
    constructor(mainWindow) {
        super(iconPath);
        this.mainWindow = mainWindow;

        this.setToolTip("Thirsty");

        this.on("click", this.onClick);
        this.on("right-click", this.onRightClick);
    }

    onClick = (event, bounds) => {
        const { x, y } = bounds;
        const { width, height } = this.mainWindow.getBounds();

        const isMac = process.platform === "darwin";

        if (this.mainWindow.isVisible()) {
            this.mainWindow.hide();
        } else {
            this.mainWindow.setBounds({
                x: x - width / 2,
                y: isMac ? y : y - height,
                width,
                height
            });
            this.mainWindow.show();
        }
    };

    onRightClick = () => {
        const menuConfig = Menu.buildFromTemplate([
            {
                label: "Quit",
                click: () => app.quit()
            }
        ]);
        this.popUpContextMenu(menuConfig);
    };
}

export default CustomTray;

我的webpack.main.config.js看起来像-

const path = require("path");

const config = {
    entry: "./index.js",
    output: {
        path: path.resolve(__dirname, "dist"),
        filename: "bundle.js"
    },
    module: {
        rules: [{ test: /\.js$/, exclude: /node_modules/, use: "babel-loader" }]
    },
    stats: {
        colors: true
    },
    target: "electron-main",
    devtool: "source-map"
};

module.exports = config;

我的webpack.renderer.config.js看起来像-

const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");

const config = {
    entry: "./src/app.js",
    output: {
        path: path.resolve(__dirname, "dist/renderer"),
        filename: "app.js"
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /node_modules/,
                use: "babel-loader"
            },
            {
                test: /\.css$/,
                use: {
                    loader: "css-loader",
                    options: {
                        minimize: true
                    }
                }
            },
            {
                test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,
                use: {
                    loader: "url-loader",
                    query: {
                        limit: 10000,
                        name: "imgs/[name].[ext]"
                    }
                }
            },
            {
                test: /\.(woff2?|eot|ttf|otf)(\?.*)?$/,
                use: {
                    loader: "url-loader",
                    query: {
                        limit: 10000,
                        name: "fonts/[name].[ext]"
                    }
                }
            }
        ]
    },
    stats: {
        colors: true
    },
    target: "electron-renderer",
    devtool: "source-map",
    plugins: [
        new CopyWebpackPlugin([
            { from: "src/app.css" },
            { from: "src/assets", to: "assets/" }
        ]),
        new HtmlWebpackPlugin({
            filename: "index.html",
            template: path.resolve(__dirname, "./src/index.html"),
            minify: {
                collapseWhitespace: true,
                removeAttributeQuotes: true,
                removeComments: true
            }
        })
    ]
};

module.exports = config;

我在package.json中的 脚本看起来像

"scripts": {
    "dev:main": "webpack --mode development --config webpack.main.config.js",
    "dev:renderer": "webpack --mode development --config webpack.renderer.config.js",
    "dev:all": "npm run dev:main && npm run dev:renderer",
    "build:main": "webpack --mode production --config webpack.main.config.js",
    "build:renderer": "webpack --mode production --config webpack.renderer.config.js",
    "build:all": "npm run build:main && npm run build:renderer",
    "prestart": "npm run build:all",
    "electron": "electron dist/index.js",
    "start": "npm run electron",
}

当前,我的应用程序创建了dist / bundle.js,但是当我运行电子版dist / bundle.js时,它不起作用。 我明白了,可能是因为它不包含src文件夹,但是当我将src文件夹复制到dist时仍然无法正常工作。

首先,我运行npm run dev:main来生成dist/bundle.js然后运行npm run dev:renderer来生成dist/renderer/bundle.js ,然后运行npm run start来启动电子应用程序。

它给我的错误是“未捕获的异常:错误:需要在新的MainWindow上进行构造函数调用”,它在index.js ,在这里我将构造函数称为new MainWindow()

我只想在所有JS文件中使用ES6。 是否有任何样板,因为我发现的样板还有大量其他东西,例如React JS和所有其他东西,以及大量的优化方法?

8天后,我终于找到了答案。 它与Electron中的ESM一起使用。

我做了一个最小的仓库,让您用Electron编写ESM。

完整的代码可以在https://github.com/deadcoder0904/electron-webpack-sample中找到

它很小,所以应该很容易理解。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM