Let I have a file index.js with content
var a = 1;
I have index.html file which include index.js using <script>
tag.
When I open index.html page in browser, then the a
variable can be accessed directly in browser console. Because a
have global scope and it is global variable.
Now I am using npm and webpack. My package.json file content is
"scripts": {
"build": "webpack"
},
and webpack.config.js file content is
module.exports = {
entry: './index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
}
Now I run npm build then new file is created in dist folder named bundle.js.
Now I replace index.js from script
tag of index.html file with dist/bundle.js .
Now I run index.html in browser, but variable a
is no longer access in browser console directly. It is not a global variable now.
My question is :
Is there a way by which we can access variable a
globally just like we access initially?
I have not found exact answer on internet. I found a loader expose-loader but it also can't solve my problem.