1

I'm trying to expose some libraries like lodash to be available to the outside world via window object,

I've tried the shimming way but doesn't work https://webpack.js.org/guides/shimming/

   plugins: [
     new webpack.ProvidePlugin({
       _: 'lodash',
     }),
   ],

And i also tried the expose loader https://www.npmjs.com/package/expose-loader but it doesn't seem to work in webpack 4.

How to expose some libraries like lodash to be available to the outside world via window object ?

Thanks in advance

Thomas Martin
  • 666
  • 2
  • 6
  • 26
Adel Hamad
  • 23
  • 1
  • 5
  • Maybe you should get the library from [cdn](https://stackoverflow.com/a/31575848/1641941)? – HMR Mar 10 '20 at 11:14
  • it's a valid option but I like to keep it in a webpack standard way, I'm trying to load javascript bundle into my main app so don't want to bundle the used packages again – Adel Hamad Mar 10 '20 at 11:16

1 Answers1

1

Why not just import the library inside your code, and then expose it? Is there any benefit to using Lodash as a plugin?

npm i lodash

 import _ from 'lodash'
 window._ = _;

To be honest it seems Lodash even exposes this object to the global scope automatically(worked for me even without the last line)

i.brod
  • 3,993
  • 11
  • 38
  • 74
  • thank you for your answer :) actually there is another way also by writing this line in the entry file ``` import "expose-loader?_!lodash" ``` but I like to keep it the webpack standard way – Adel Hamad Mar 10 '20 at 11:14