webpack share data between custom loader and plugin

安稳与你 提交于 2021-02-07 06:45:08

问题


Explanation

I'm developing a webpack custom loader, that is in charge of parsing the javascript and search for certain text using regex.

I need to have a way to collect the data between each parsed file and finally pass that data to a related plugin, so that the plugin can make the appropriate action with that data.

My failed strategy

I can use a module to share data between loader calls, but this data is not available in the plugins.

Suggestions ?

I've been looking at the way the extract-text-plugin does it, since it has a loader and a plugin, but looking at their source code, the way they declare the module is strange to me and I can't really grasp their strategy.

Thanks for any help!


回答1:


Using a shared module is a viable option, I created a loader/plugin combo that creates an SVG sprite from svgs imported through the loader, here is the repository: https://github.com/crystal-ball/svg-symbol-sprite-loader

Here is how I setup the process:

  1. I export a singleton as the sprite-store
  2. The loader requires the sprite store and adds any imported SVG to the store.
  3. In the additional assets hook the plugin reads all of the collected data from the sprite store.


来源:https://stackoverflow.com/questions/46308248/webpack-share-data-between-custom-loader-and-plugin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!