我一直在尝试在 Electron 锻造应用程序中使用 Webpack 来加载 Assets (特别是图像),但我没有成功。
主要问题是,当我使用带有“ Assets /资源”作为类型的 png 示例规则时,应用程序将无法运行并引发错误。
我的规则如下:
module.exports = [
// Add support for native node modules
{
test: /\.node$/,
use: 'node-loader',
},
{
test: /\.(m?js|node)$/,
parser: { amd: false },
use: {
loader: '@marshallofsound/webpack-asset-relocator-loader',
options: {
outputAssetBase: 'native_modules',
},
},
},
{
test: /\.tsx?$/,
exclude: /(node_modules|\.webpack)/,
use: {
loader: 'ts-loader',
options: {
transpileOnly: true
}
}
},
{
test: /\.(png|svg|jpg|jpeg|gif)$/i,
type: 'asset/resource'
}
];
引发的错误如下:An unhandled error has occurred inside Forge:
Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
- configuration.module.rules[3].type should be one of these:
"javascript/auto" | "javascript/dynamic" | "javascript/esm" | "json" | "webassembly/experimental"
-> Module type to use for the module
WebpackOptionsValidationError: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
- configuration.module.rules[3].type should be one of these:
"javascript/auto" | "javascript/dynamic" | "javascript/esm" | "json" | "webassembly/experimental"
-> Module type to use for the module
at webpack (C:\Users\Iscle\Documents\Work\TFG\gestishop-forge\node_modules\@electron-forge\plugin-webpack\node_modules\webpack\lib\webpack.js:31:9)
at C:\Users\Iscle\Documents\Work\TFG\gestishop-forge\node_modules\@electron-forge\plugin-webpack\src\WebpackPlugin.ts:236:26
at new Promise (<anonymous>)
at C:\Users\Iscle\Documents\Work\TFG\gestishop-forge\node_modules\@electron-forge\plugin-webpack\src\WebpackPlugin.ts:235:13
error Command failed with exit code 1.
我搜索了类似的错误,但没有出现。该规则直接从官方文档 (https://webpack.js.org/guides/asset-modules/) 中复制而来。正在使用的 webpack 版本是目前可用的最新版本。我现在没有想法,也许有更多经验的人可以发光。
谢谢!
最佳答案
确保您使用以下命令安装了最新版本的 webpack:npm: npm install webpack@latest
Yarn: yarn add webpack@latest
您当前可能使用的是第 4 版,截至本文,默认下载(至少在 npm 上)似乎是第 4 版。这对我有帮助,我希望它有帮助!
https://webpack.js.org/migrate/5/
关于javascript - Webpack 不加载 "asset/resource"类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66444612/