Webpack plugin: how can I modify and re-parse a module after compilation?

后端 未结 3 1145
粉色の甜心
粉色の甜心 2020-12-13 19:05

I\'m working on a webpack plugin and can\'t figure out how to modify a module during the build. What I\'m trying to do:

  • Collect data via a custom loader (fine)
3条回答
  •  旧巷少年郎
    2020-12-13 19:47

    Another idea comes to my mind. What if do some preprocessing before webpack compilation?

    1. Leverage babel + a custom plugin, or even a custom code parser based on babylon, to collect data you need, and then save to temp file.
    2. Run webpack build together with this temp file.

    This temp file could be a virtual file. May refer to https://github.com/rmarscher/virtual-module-webpack-plugin/ .

    ... be used if you generated file contents at build time that needs to be consumed as a module by your source code, but you don't want to write this file to disk.

提交回复
热议问题