webpack - 无法使用 copy-webpack-plugin 将静态文件添加到 electron-forge
问题描述
无法将静态文件添加到 electron-forge。
中创建assets
的文件夹src
。
修改 webpack.rules.js:
module.exports = [
// ... appended to default rules
{
test: /\.(jpg|png|svg|ico|icns)$/,
loader: 'file-loader',
options: {
name: '[path][name].[ext]',
publicPath: '../.', // move up from 'main_window'
context: 'src', // set relative working folder to src
},
},
];
修改 webpack.plugins.js:
const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const path = require('path');
module.exports = [
new ForkTsCheckerWebpackPlugin(),
new CopyPlugin({
patterns: [
{
from: path.join(__dirname, 'assets'),
to: 'assets/',
},
],
}),
];
修改后的 index.ts:
import { app, BrowserWindow, Menu, Tray } from 'electron';
// ...
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.
let tray = null;
app.whenReady().then(() => {
const trayPath = 'assets/iconTemplate.png';
console.log(trayPath);
tray = new Tray(trayPath);
const contextMenu = Menu.buildFromTemplate([
{ label: 'Item1', type: 'radio' },
{ label: 'Item2', type: 'radio' },
{ label: 'Item3', type: 'radio', checked: true },
{ label: 'Item4', type: 'radio' },
]);
tray.setToolTip('This is my application.');
tray.setContextMenu(contextMenu);
});
输出:
Hash: c7b3751b2a01093270d2
Version: webpack 4.43.0
Time: 634ms
Built at: 06/14/2020 9:27:17 PM
Asset Size Chunks Chunk Names
index.js 33.2 KiB main [emitted] main
index.js.map 39.3 KiB main [emitted] [dev] main
Entrypoint main = index.js index.js.map
[./node_modules/debug/src/browser.js] 5.69 KiB {main} [built]
[./node_modules/debug/src/common.js] 5.79 KiB {main} [built]
[./node_modules/debug/src/index.js] 314 bytes {main} [built]
[./node_modules/debug/src/node.js] 4.37 KiB {main} [built]
[./node_modules/electron-squirrel-startup/index.js] 1 KiB {main} [built]
[./node_modules/has-flag/index.js] 320 bytes {main} [built]
[./node_modules/ms/index.js] 2.95 KiB {main} [built]
[./node_modules/supports-color/index.js] 2.7 KiB {main} [optional] [built]
[./src/index.ts] 2.06 KiB {main} [built]
[child_process] external "child_process" 42 bytes {main} [built]
[electron] external "electron" 42 bytes {main} [built]
[os] external "os" 42 bytes {main} [built]
[path] external "path" 42 bytes {main} [built]
[tty] external "tty" 42 bytes {main} [built]
[util] external "util" 42 bytes {main} [built]
虽然没有找到文件
UnhandledPromiseRejectionWarning: Error: Image could not be created from /assets/iconTemplate.png
这是我用来解决此问题的所有参考资料:
解决方案
结果 CopyWebpackPlugin 需要一个模式列表:https ://webpack.js.org/plugins/copy-webpack-plugin/
还有一些我在问题中更新的目录路径错误。
推荐阅读
- ckeditor - Ckeditor 3 - 选择工具栏按钮时单击插入元素
- wpf - WPF 中的复杂标题样式
- c# - 在树视图中更改节点颜色
- python - Scrapy Python response.css 循环
- rest - 使用 Jive REST API 上传视频
- python - 仅一次使用 tf.gradients 的单个样本梯度(TypeError: Fetch argument None has invalid type
) - macos - 码头工人。如何使容器外部的文件在 Mac 上可用?
- python - Scipy ODE 时间步倒退
- javascript - 在 TypeScript 中使用来自 JavaScript 导入的扩展
- python - Huey,Flask:ModuleNotFoundError:没有名为“run_huey”的模块