webpack 在自定义加载器和插件之间共享数据 [英] webpack share data between custom loader and plugin

查看:35
本文介绍了webpack 在自定义加载器和插件之间共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 webpack 自定义加载器,负责解析 javascript 并使用正则表达式搜索某些文本.

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.

我可以使用模块在加载程序调用之间共享数据,但这些数据在插件中不可用.

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

我一直在研究提取文本插件的工作方式,因为它有一个加载器和一个插件,但是查看它们的源代码,它们声明模块的方式对我来说很奇怪,我可以't 真正掌握他们的策略.

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.

感谢您的帮助!

推荐答案

使用共享模块是一个可行的选择,我创建了一个加载器/插件组合,它从通过加载器导入的 svgs 创建一个 SVG 精灵,这里是存储库:https://github.com/crystal-ball/svg-symbol-sprite-装载机

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. 我将单例导出为 sprite-store
  2. loader 需要精灵存储并将任何导入的 SVG 添加到存储中.
  3. 在附加资产挂钩 plugin 从精灵存储中读取所有收集的数据.

这篇关于webpack 在自定义加载器和插件之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆