简体   繁体   English

使用带有webpack的模块

[英]Using modules with webpack

I recently started using Webpack for frontend and came across the issue with modules. 我最近开始使用Webpack作为前端,并遇到了模块的问题。
For example, I have two modules, one uses another (to be specific its angular-bootstrap-slider and bootstrap-slider ). 例如,我有两个模块,一个使用另一个模块(具体是它的angular-bootstrap-sliderbootstrap-slider )。 angular-bootstrap-slider was failing to initialize due the fact that Slider function was undefined. 由于Slider功能未定义, angular-bootstrap-slider无法初始化。
Now I understand that I can either export Slider globally (which I did with jquery and angular libs) or import Slider in angular-bootstrap-slider (I picked that). 现在我明白我可以全局导出Slider (我使用jquery和angular libs)或者在angular-bootstrap-slider中导入Slider (我选择了它)。
I don't like both options, because global exports is one of the things I wanted to avoid using webpack and importing something in library means changing it's code. 我不喜欢这两个选项,因为全局导出是我想避免使用webpack并在库中导入某些东西意味着改变它的代码。
So am I missing something or maybe there is some best practice to deal with dependencies? 所以我错过了一些东西,或者有一些最好的做法来处理依赖关系?

What you are looking for is shimming modules . 您正在寻找的是填充模块

This allows you to declare that Slider is in fact to be imported from the module bootstrap-slider : 这允许您声明Slider实际上是从模块bootstrap-slider导入的:

...
plugins: [
  new webpack.ProvidePlugin({ 
    'Slider': 'bootstrap-slider'  
  })
]

I think you can use imports-loader : https://github.com/webpack/imports-loader 我认为你可以使用imports-loaderhttps//github.com/webpack/imports-loader

imports loader for webpack 为webpack导入加载程序

Can be used to inject variables into the scope of a module. 可用于将变量注入模块的范围。 This is especially useful if third-party modules are relying on global variables like $ or this being the window object. 如果第三方模块依赖于全局变量(如$或这是窗口对象),这将特别有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM