How to import custom function inside allmodules?

I have function:

function __extends(d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}

How i can pass with Webpack that function as variable __extends inside ALL modules?

Upvotes: 0

Views: 803

Answers (3)

Now __extends as node module!

https://www.npmjs.com/package/typescript-extends

// webpack configuration object

plugins: [
    new ProvidePlugin({
        __extends: 'typescript-extends'
    })
],

Upvotes: 0

dreyescat
dreyescat

Reputation: 13798

One way to make a function available in all modules is moving this function into a module and let webpack automatically load it when required. To do this you just need to:

1.- Move your __extends function into a module that exports this function:

extends.js

function __extends(d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}

module.exports = __extends;

2.- Configure webpack to automatically require/import this module whenever __extends is used as a free variable. For this just use the ProvidePlugin:

var webpack = require('webpack');

module.exports = {
  // Your other configuration
  plugins: [
    new webpack.ProvidePlugin({
      __extends: './extends'
    })
  ]
};

Now you are free to use __extends anywhere without having to explicitly import/require it. Webpack will take care for you.

Upvotes: 1

Hristo Deshev
Hristo Deshev

Reputation: 907

Is your custom function somewhere in the global scope? You can use the DefinePlugin to alias it:

new webpack.DefinePlugin({
__extends: "window.__extends"
})

Upvotes: 1

Related Questions