Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access the global object (window) using webpack?

I'm trying to interface ActionScript with JavaScript using ExternalInterface and webpack.

ExternalInterface can only provoked (call) functions found on the global object (window). How can I get a webpack module reference on window (global object)?

Allow me to elaborate some, I want to have a namespace for the company (window.companyName) with an interface for ExternalInterface:

window.companyName = { isReady: function() { ... },
                       driver1: function() { ... }, 
                       driver2: function() { ... } }

The ActionScript will drive my JavaScript. The more fundamental question is, how do I set globals using webpack so that ExternalInterface can see them (preferably as exports of a module)?

I've tried using expose-loader, exports-loader imports-loader with no luck. expose-loaderis ideally what I need, but doesn't seem to work. When I set window.companyName in my modules and try to verify it in my chrome console, it results in undefined.

like image 691
user2072912 Avatar asked Apr 24 '15 10:04

user2072912


People also ask

What is libraryTarget in webpack?

This is according to their documentation: "libraryTarget: "umd" - This exposes your library under all the module definitions, allowing it to work with CommonJS, AMD and as global variable." Also, I built the exact same code with Webpack 3 and it produced a proper bundle.

What is chunkFilename webpack?

So the filename is used for generating independent entry bundles, while chunkFilename is used for bundles that are auto-generated by Webpack during code splitting.

What is global window object?

In a web browser, any code which the script doesn't specifically start up as a background task has a Window as its global object.


3 Answers

Aren't you using webpack-dev-server?

Because when I try webpack command everything is working fine. I'm testing it by typing window.mySampleGlobalVariable in chrome developer tools.

BUT when I run webpack-dev-server command then my window variable is undefined.

I have this sample app:

app.js

window.mySampleGlobalVariable = 'TEST';
console.log('App is ready');

index.html

<!DOCTYPE HTML>
<html>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Webpack test</title>
    </head>

    <body>
        Webpack test
        <script src="bundle.js"></script>
    </body>

</html>

webpack.config.js

var path = require('path');

module.exports = {
    entry: './app.js',
    output: {
        filename: './bundle.js'
    },
    resolve: {
        extensions: ['', '.js']
    }
};
like image 198
machaj Avatar answered Oct 16 '22 16:10

machaj


You do have access to window object from your webpacked script. Webpack does not interfere with it since the wrapper function only injects module, exports and __webpack_require__ arguments.

Try it writing a script with a single line accessing window object and then check out the output script.

Your assignment should work, unless the execution never reaches it or some loader is altering the relevant code.

like image 39
Ricardo Stuven Avatar answered Oct 16 '22 15:10

Ricardo Stuven


If you running webpack-dev-server with the iframe you cannot access the variable via the Chrome console.

like image 44
Fabian Zeindl Avatar answered Oct 16 '22 15:10

Fabian Zeindl