Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack share data between custom loader and plugin

Tags:

webpack

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!

like image 805
Erick M Avatar asked Sep 19 '17 19:09

Erick M


1 Answers

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.
like image 121
Dan Hedgecock Avatar answered Oct 23 '22 20:10

Dan Hedgecock