Is it possible to define a global variable with webpack to result something like this:
var myvar = {};
All of the examples I saw were using external file require("imports?$=jquery!./file.js")
Is it possible to define a global variable with webpack to result something like this:
var myvar = {};
All of the examples I saw were using external file require("imports?$=jquery!./file.js")
There are several way to approach globals:
1) Put your variables in a module.
Webpack evaluates modules only once, so your instance remains global and carries changes through from module to module. So if you create something like a
globals.js
and export an object of all your globals then you canimport './globals'
and read/write to these globals. You can import into one module, make changes to the object from a function and import into another module and read those changes in a function. Also remember the order things happen. Webpack will first take all the imports and load them up in order starting in yourentry.js
. Then it will executeentry.js
. So where you read/write to globals is important. Is it from the root scope of a module or in a function called later?Note: If you want the instance to be
new
each time, then use an ES6 class. Traditionally in JS you would capitalize classes (as opposed to the lowercase for objects) likeimport FooBar from './foo-bar' // <-- Usage: myFooBar = new FooBar()
2) Webpack's ProvidePlugin
Here's how you can do it using Webpack's ProvidePlugin (which makes a module available as a variable in every module and only those modules where you actually use it). This is useful when you don't want to keep typing
import Bar from 'foo'
again and again. Or you can bring in a package like jQuery or lodash as global here (although you might take a look at Webpack's Externals).Step 1) Create any module. For example, a global set of utilities would be handy:
utils.js
Step 2) Alias the module and add to ProvidePlugin:
webpack.config.js
Now just call
utils.sayHello()
in any js file and it should work. Make sure you restart your dev-server if you are using that with Webpack.Note: Don't forget to tell your linter about the global, so it won't complain. For example, see my answer for ESLint here.
3) Use Webpack's DefinePlugin
If you just want to use const with string values for your globals, then you can add this plugin to your list of Webpack plugins:
Use it like:
4) Use the global window object (or Node's global)
You'll see this commonly used for polyfills, for example:
window.Promise = Bluebird
5) Use a package like dotenv
(For server side projects) The dotenv package will take a local configuration file (which you could add to your .gitignore if there are any keys/credentials) and adds your configuration variables to Node's process.env object.
Create a
.env
file in the root directory of your project. Add environment-specific variables on new lines in the form ofNAME=VALUE
. For example:That's it.
process.env
now has the keys and values you defined in your.env
file.Notes:
Regarding Webpack's Externals, use it if you want to exclude some modules from being included in your built bundle. Webpack will make the module globally available but won't put it in your bundle. This is handy for big libraries like jQuery (because tree shaking external packages doesn't work in Webpack) where you have these loaded on your page already in separate script tags (perhaps from a CDN).
You can use define
window.myvar = {}
. When you want to use it, you can use likewindow.myvar = 1
I solved this issue by setting the global variables as a static properties on the classes to which they are most relevant. In ES5 it looks like this:
I was about to ask the very same question. After searching a bit further and decyphering part of webpack's documentation I think that what you want is the
output.library
andoutput.libraryTarget
in thewebpack.config.js
file.For example:
js/index.js:
webpack.config.js
Now if you link the generated
www/js/index.js
file in a html script tag you can access tomyLibrary.foo
from anywhere in your other scripts.Use DefinePlugin.
Example:
Usage: