Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack does not load "asset/resource" type

I've been trying to use Webpack in an electron-forge app to load assets (images, specifically) but I haven't succeeded.

The main issue is that when I use the example rule for png with "asset/resource" as a type, the app won't run and an error gets thrown.

My rules are as follows:

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'
  }
];

The error that gets thrown is the following:

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.

I've searched for similar errors and nothing comes up. The rule is copied directly from the official documentation (https://webpack.js.org/guides/asset-modules/). The webpack version being used is the latest one available as of right now.

I'm out of ideas now, maybe somebody with more experience can shine some light.

Thanks!

like image 625
Iscle Avatar asked Jul 03 '26 18:07

Iscle


1 Answers

Make sure you have the latest version of webpack installed using:

npm: npm install webpack@latest

Yarn: yarn add webpack@latest

You may be on version 4 currently, the default download (at least on npm) seems to be version 4 as of this post. This helped me just now, I hope it helps!

https://webpack.js.org/migrate/5/

like image 96
Namezore Avatar answered Jul 05 '26 08:07

Namezore