Create asset module from custom loader
See original GitHub issueFeature 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:
- Created 2 years ago
- Comments:11 (6 by maintainers)
Top GitHub Comments
Successfully implement it, works like a charm! Thanks a lot
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:And use this:
We use
not
to prevent overlapping between two rules withsvg
extension, also in near future we will haveimport assertions
, so it can be simplified in future too