question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Loading assets via a loader's `this.loadModule()` doesn't emit asset files

See original GitHub issue

Bug report

What is the current behavior? When calling this.loadModule( "some-path", callback ) from a loader while some-path is defined as an asset in the module rules (eg. type: "asset/source"), webpack won’t emit some-path as an asset.

If the current behavior is a bug, please provide the steps to reproduce. See my simple reproduction repo here: https://github.com/bastimeyer/webpack-load-module-asset

Relevant files

Build log (verbose stats): https://github.com/bastimeyer/webpack-load-module-asset/runs/2794477055?check_suite_focus=true#step:5:1

What is the expected behavior? Webpack should emit the asset loaded from within the loader just like when loading the asset from an import / require call / etc, or when using the file-loader for emitting resolved asset files.

Other relevant information: webpack version: 5.38.1 (currently the latest version) Node.js version: 16.3.0 (irrelevant) Operating System: Arch Linux (irrelevant) Additional tools: yarn (irrelevant)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
vankopcommented, Mar 25, 2022

we discussed this internally… In my opinion there is no need to create specific api for that. mostly you always need an url to this asset. So in case:

Just as a short description how I’ve been running into this problem: I’m using a custom loader for parsing JSON files which have entries for certain file paths which need to be resolved and emitted as asset files. Webpack5 doesn’t allow me to do this without the deprecated file-loader. Declaring those file paths as assets in the webpack5 config does not work, hence this bug report. The “specific syntax” mentioned above does not work and it shouldn’t be required either, as described in my previous post.

You can transform your json to js json

{ "asset": "./a.txt" }

transformed

import a from "./a.txt";

export default { "asset": a }
0reactions
decademooncommented, Mar 22, 2022

@bastimeyer That’s literally my exact use case too.

See https://github.com/usmanyunusov/webpack-webmanifest-loader for a webpack 5 loader which does a similar thing. It loads a webmanifest file (json) and replaces paths to image assets with the final bundled path of that asset. It uses child compilers and stuff which goes over my head.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Webpack file-loader not loading assets folder in production build
You need to copy the image files to the build directory. To do that, install webpack copy plugin: copy-webpack-plugin
Read more >
Asset Modules - webpack
asset /resource emits a separate file and exports the URL. ... When using the old assets loaders (i.e. file-loader / url-loader / raw-loader...
Read more >
Stats Data - webpack 3 documentation
Each assets object represents an output file emitted from the compilation. ... asset contains "emitted": true, // Indicates whether or not the asset...
Read more >
Webpack & CSS - 5. Resolving assets with file-loader - YouTube
Referencing assets in our CSS now can get a little tricky, let's use the file - loader to resolve those files !
Read more >
Bundling non-JavaScript resources - web.dev
Learn how to import and bundle various types of assets from JavaScript in a way that works both in browsers and bundlers.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found