Schlaus
Schlaus

Reputation: 19182

Using a script that exposes a global var in a Webpack build

In my (first) Webpack build I'm having trouble comprehending how I should be loading a script that simply exposes a global var.

The script I'm trying to load is basically something like this:

//File: MyLibrary.js    

var MyLibrary = (function(window) {

    function MyLibrary() {
        // Do librarious stuff
    }

    return MyLibrary;

})(typeof window !== 'undefined' ? window : null);

I figured I should use the exports-loader since according to the docs it should be just the thing for this case:

The file sets a variable in the global context with var XModule = ....

var XModule = require("exports?XModule!./file.js")

So I put this in my config:

module: {
    loaders: [
        {
            test: /MyLibrary\.js$/,
            loader: "exports?MyLibrary!./MyLibrary.js"
        }
    ]
}

But this results in an error:

ERROR in Loader MyLibrary.js didn't return a function

which confuses me, since it's not supposed to return a function, that's the whole point why I'm using this particular loader...

So how should I load the script?

Upvotes: 1

Views: 1163

Answers (1)

Ayoub Kaanich
Ayoub Kaanich

Reputation: 1021

you don't specify the path to the library in loader property, simply:

module: {
    loaders: [
        {
            test: /MyLibrary\.js$/,
            loader: "exports?MyLibrary"
        }
    ]
}

Upvotes: 2

Related Questions