Skip to content Skip to sidebar Skip to footer

Webpack Loaders - Create And Write Files Inside Loader

Pretty much what the title suggests. I want to create a metadata file alongside every javascript file in my project. So I need a webpack loader that doesn't change the content at a

Solution 1:

So this took a while to find and seems rarely mentioned, but webpack loaders actually have a method this.emitFile specifically for this purpose. Here is some example usage:

function extractMeta(content) {
    // extract and return metadata for file
}

module.exports = function(content) {
    console.log('extracting metadata and writing to file');

    // get source filename, strip file extension and append ".meta"
    const filename = this.resourcePath.split('/').pop().split('.')[0] + '.meta'

    this.emitFile(filename, extractMeta(content));
    return content; // return the source file unchanged
};

Post a Comment for "Webpack Loaders - Create And Write Files Inside Loader"