4

我正在使用 electron-forge 构建一个电子应用程序,我正在使用 webpack 模板(https://www.electronforge.io/templates/webpack-template)。在我的应用程序中,我需要包含静态资产(javascript、css 和图像)

为了做到这一点,我copy-webpack-plugin在 render.config.js 文件中包含了 ,这在测试应用程序时有效,electron-forge start但是在尝试编译应用程序时electron-forge build,资产不包含在构建中。

module.exports = {
  // Put your normal webpack config below here
  module: {
    rules
  },
  plugins: [
    new CopyPlugin([
      { from: './src/images', to: 'images' },
      { from: './src/js', to: 'js' },
      { from: './src/css', to: 'css' },
      { from: './src/webfont', to: 'webfont' }
    ])
  ]
}

我希望 css 和其他资产以与我测试应用程序时的捆绑方式相同的方式捆绑在我的应用程序中。

4

2 回答 2

0
  1. 将 CopyPlugin 配置放在 webpack.plugins.js 文件中,而不是 render.config.js
  2. 您的配置似乎不正确,因为它错过了模式选项。
  3. 您必须提供 CopyPlugin 的完整路径。

样本 :

module.exports = [
  new CopyPlugin({
    patterns: [
      {
        from: path.resolve(__dirname, '../app/localization/locales'),
        to: path.resolve(__dirname, '../.webpack/locales')
      }
    ]
  })
]
于 2020-05-28T07:50:15.740 回答
0

实际上,我使用electron-forge + webpack + typescriptCopyWebpackPlugin复制一些资源文件时遇到了同样的问题。

webpack.main.config.js

const CopyPlugin = require("copy-webpack-plugin");

module.exports = {
  entry: './src/index.ts',
  module: {
    rules: require('./webpack.rules')
  },
  resolve: {
    extensions: ['.js', '.ts', '.jsx', '.tsx', '.css', '.json']
  },
  plugins: [
    new CopyPlugin([
      { from: 'resources/credentials.json', to: 'resources/credentials.json'}
    ]),
  ]
}

在我的一个源文件中,我尝试从文件中读取。

const credentialsFile = fs.readFileSync('resources/credentials.json'));
const credentials = JSON.parse(credentialsFile.toString());

这在使用 运行时有效electron-forge start,但是当使用 构建二进制文件时electron-forge package,出现以下异常:-

Uncaught Exception:
Error: ENOENT: no such file or directory, open 'resources/credentials.json'
    at Object.openSync (fs.js:476:3)
...

原来我只需要__dirname在构建路径时添加。

const credentialsFile = fs.readFileSync(path.join(__dirname, 'resources', 'credentials.json'));
于 2021-04-25T07:11:34.720 回答