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.

Create asset module from custom loader

See original GitHub issue

Feature request

We have a @ecomfe/svg-mixed-loader which transforms an svg file to multiple formats, previously we use url-loader and file-loader to generate an DataURI or standalone asset according to user configuration.

Since url-loader and file-loader are deprecated and webpack recommends asset module instead, is it still possible to generate an asset/resource or asset/inline from my loader?

What is the expected behavior?

I’d prefer to have some helpers like generateAssetModule(this.resource, options) that can be used inside a loader implementation:

// Inside loader
const url = await generateAssetModule(this.resource, this.getOptions().asset);
return `export const url = ${JSON.stringify(url)}`;

What is motivation or use case for adding/changing the behavior?

Currently generating an asset involves end user configuration of module.resolve, we want it to be transparent to end user and encapsulated inside a loader

How should this be implemented in your opinion?

A helper function in webpack or loader-utils to manually generate asset from a file

Are you willing to work on this yourself?

Not familiar with that

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
otakustaycommented, Aug 2, 2021

Successfully implement it, works like a charm! Thanks a lot

0reactions
alexander-akaitcommented, Aug 2, 2021

In this case you should not add type: 'asset', you don’t need it, webpack itself take care about assets modules, your loader should generate this content:

const myURL = new URL('./image.svg?asset-module', import.meta.url);
const ReactComponent = props => /*#__PURE__*/external_React_default().createElement("svg", _extends({});

export default myURL;
export { ReactComponent }; 

And use this:

module: {
    rules: [
        {
            test: /\.svg$/,
            resourceQuery: { not: /asset-module/ },
            use: [
              "babel-loader", // optional, if you need convert new ECMA syntax
              "your-custom-loader.js" // you loader generate code above
            ],
        },
        {
            test: /\.svg$/,
            resourceQuery:  /asset-module/,
            type: 'asset',
        },
    ],
},

We use not to prevent overlapping between two rules with svg extension, also in near future we will have import assertions, so it can be simplified in future too

Read more comments on GitHub >

github_iconTop Results From Across the Web

Asset Modules - webpack
Asset Modules is a type of module that allows one to use asset files (fonts, icons, etc) without configuring additional loaders. Prior to...
Read more >
Writing a custom webpack loader - Redd Developer
Think of a loader as a function of a module's content that creates or augments that module's export: const styles = cssLoader(require('.
Read more >
Use the webpack asset module with a custom output filename
I want to put each one in a subdirectory called "fonts" and "images" under the assets folder. I know this is possible using...
Read more >
Customizing image bundling in webpack - Media Jams
During image bundle customization, asset modules achieve this by replacing the loaders (that is, url-loader or file-loader) with one of four (4) ...
Read more >
Webpack 5: Asset Modules - Medium
Prior to Webpack5 it was common to use: raw-loader, url-loader and file-loader. SPOILER ALERT! In Webpack5, you don't need them anymore! Asset modules...
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