Search code examples
webpackbuilddiskauto-generate

Webpack Write 'auto generated files' to disk before build


I am in the process of migrating from Grunt to Webpack. Almost done but i cannot seem to find a plugin that allows me to write files to disk.

Example: I have an auto generated file that lists my components: components.json

{
    "components": [
        "ComponentA",
        "ComponentB",
        "ComponentC"
    ]
}

In grunt i use the file creator task that loops over my js files via globbing to generate this file. (https://www.npmjs.com/package/grunt-file-creator). I cannot seem to find an equivalent for Webpack.

Does anyone uses a plugin for webpack that can write to disk like this?


Solution

  • Okay, after diving into the docs i found out the example i need is explained inside the 'Plugin' section of the docs: https://webpack.js.org/contribute/writing-a-plugin/#example

    You need to create a custom plugin that hooks into the emit.tabAsync and makes use of the compilation.assets.

    I think i will allow an array of functions passed to the plugin for generating files. This will allow for a more generic solution.