webpack-encore icon indicating copy to clipboard operation
webpack-encore copied to clipboard

asset/inline Asset Module complains about generator property which isn't there

Open dusandz opened this issue 2 years ago • 1 comments

I'm having an issue with asset/inline and would appreciate some help.

This is the rule I'm trying to add:

 .addRule({
        test: /my-module[/\\]static-assets/,
        type: 'asset/inline',
    })

And that triggers a warning when I try to build the assets:

warn  in ./assets/js/my-module/static-assets/ sync ^\.\/.*$                                                                                                                                     

Module not found: ValidationError: Invalid generator object. Asset Modules Plugin has been initialized using a generator object that does not match the API schema.
 - generator has an unknown property 'filename'. These properties are valid:
   object { dataUrl? }
   -> Generator options for asset/inline modules.

The error message makes sense (there are no filenames for inlined assets), but I'm not passing any filename parameters to the rule. Thinking that something might be happening under the hood, I tried modifying the config manually:

config = Encore.getWebpackConfig();
config.module.rules.push({
    test: /my-module[/\\]static-assets/,
    type: 'asset/inline',
})
module.exports = config

...but that didn't help. Just as a sanity check, I console-logged that part of the config:

  { test: /my-module[/\\]static-assets/, type: 'asset/inline' }

Looks good, but still complains about the filename. Then I tried writing generator directive explicitly (thinking that Webpack or Encore might be adding its own, if omitted):

{
    test: /my-module[/\\]static-assets/,
    type: 'asset/inline',
    generator: {
        dataUrl: content => {
            return content.toString('base64');
        }
    }
}

...to no avail. Tried on an isolated piece of code (just Webpack), worked fine there.

I can work around this and load the static assets manually using fetch, but I'm really curious to learn what did I do wrong.

dusandz avatar Oct 31 '23 18:10 dusandz

Thank you for this issue. There has not been a lot of activity here for a while. Has this been resolved?

carsonbot avatar Oct 01 '24 12:10 carsonbot

Hi, and thanks for your detailed report.

I was able to reproduce the issue by following code:

Encore.addRule({
    test: /my-module\/static-assets/,
    type: 'asset/inline',
    generator: {
        dataUrl: content => {
            return content.toString();
        }
    }
})

And I also have the issue:

Module not found: ValidationError: Invalid generator object. Asset Modules Plugin has been initialized using a generator object that does not match the API schema.
 - generator has an unknown property 'filename'. These properties are valid:
   object { binary?, dataUrl? }
   -> Generator options for asset/inline modules.

But when I log the generated Webpack configuration, I don't see this filename property:

// ...
  {
    test: /my-module\/static-assets/,
    type: 'asset/inline',
    generator: { dataUrl: [Function: dataUrl] }
  }
// ...

I'm not sure to understand why it happens, I will investigate more

Kocal avatar Feb 16 '25 21:02 Kocal

Note that the error does not happen when commenting the line: https://github.com/symfony/webpack-encore/blob/12c8144/lib/config-generator.js#L288

Kocal avatar Feb 16 '25 21:02 Kocal

After some investigations, it looks like Encore rules for images and fonts are also trying to handle your assets from /my-module\/static-assets/ directory.

I was able to make things work properly that way:

Encore
    .addRule({
        test: /my-module\/static-assets/,
        type: 'asset/inline',
        generator: {
            dataUrl: content => {
                return content.toString();
            }
        }
    })
    .configureLoaderRule('images', rule => {
        rule.exclude = /my-module\/static-assets/;
    })
    .configureLoaderRule('fonts', rule => {
        rule.exclude = /my-module\/static-assets/;
    })

Kocal avatar Feb 16 '25 21:02 Kocal