S
S
sanex33392016-04-12 12:51:50
typescript
sanex3339, 2016-04-12 12:51:50

How to import a function as a variable in all modules?

When Webpack builds Typescript modules, it pushes Typescript's __extends helper into each module.
It turns out a lot of repeating helpers in each module.
I'm trying to take out this helper now as a separate f-ii, cut off the generation of helpers through noEmitHelpers.

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 __());
}

Now about the problem: how to properly import this helper into all modules as a variable using webpack? I would very much like this variable not to be global. I would also very much like not to use any plugins, tk. gulp-webpack doesn't support them.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sanex3339, 2016-04-12
@sanex3339

extends.ts:

declare let module: any;

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;

In the webpack config:
resolve: {
               modulesDirectories: ['node_modules', '.']
           },.
plugins: [
               new webpack.ProvidePlugin({
                   __extends: 'src/ts/ts-helpers/extends' //src/ts/ts-helpers/extens.ts
               })
           ]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question