Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack and Typescript `__extends`

I write project with help of TypeScript. Project divided into many modules. I bundle all modules into one file with Webpack.

For each module-class, which extends from parent class, webpack added TypeScript __extends helper. As result - i got many repeated code.

/***/ },
/* 24 */
/***/ function(module, exports, __webpack_require__) {

"use strict";
var __extends = (this && this.__extends) || function (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 __());
};
var DeepExtend_1 = __webpack_require__(6);

//...

exports.SafariDetector = SafariDetector;


/***/ },
/* 25 */
/***/ function(module, exports, __webpack_require__) {

"use strict";
var __extends = (this && this.__extends) || function (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 __());
};
var DeepExtend_1 = __webpack_require__(6);

//...

exports.SafariMobileDetector = SafariMobileDetector;

So, any way to fix that?

like image 673
Качалов Тимофей Avatar asked Apr 11 '16 18:04

Качалов Тимофей


People also ask

How does webpack work with TypeScript?

webpack helps to compile and bundle modules into a single file, reducing HTTP requests and improving application performance as a result. With webpack, TypeScript code is compiled into a JavaScript file that is browser-friendly.

Does TSC use webpack?

Like Babel, Webpack depends on TSC to transpile TypeScript to JavaScript but as TSC doesn't have a clue about Webpack, hence Webpack needs a loader to talk to TSC. This is where the ts-loader comes into play. Webpack compiles a TypeScript file using ts-loader package which asks TSC to do the compilation.

Can webpack config be ts?

ts file directly as configuration for your project. Of course, because TypeScript is just a superset for Javascript, you can always use TypeScript to write your webpack. config.

Is Vite better than webpack?

Compared to Webpack 5 with lazy compilation, Vite has a slightly slower dev startup time and somewhat longer production build time even with code-splitting enabled. But one of the big reasons developers love Vite is the near-instant feedback loop between saving a file and seeing your changes in the browser.


2 Answers

Webpack's ProvidePlugin actually has an undocumented feature: you can configure it with an array instead of a string and it'll extract the given object path from the exports of a module.

This allows you to make use of TypeScript's official tslib module, which exports all the desired functions.

The following code works with [email protected]:

new webpack.ProvidePlugin({
    '__assign': ['tslib', '__assign'],
    '__extends': ['tslib', '__extends'],
})

Make sure to force Webpack to use the ES6 module version of tslib:

aliases: {
    'tslib$': 'tslib/tslib.es6.js',
}

And configure your tsc/tsconfig.json not to emit the helper functions for every module:

{
    "compilerOptions": {
        "noEmitHelpers": true,
    }
}

Edit: My pull-request for a documentation update has been merged, so you'll find this on the official website as well, now: https://webpack.js.org/guides/shimming/

like image 98
Kevin F Avatar answered Oct 11 '22 16:10

Kevin F


One possible solution would be cofiguring TypeScript to omit those helpers when compiling and write it by yourself, once and in a single file that will be later bundled by webpack.

Set compilerOptions.noEmitHelpers to true on your tsconfig.json file.

Create a extends.js with __extends function definition (typescript-helpers) and add it into your webpack bundle.

Never tried that, but I've done something similar here regarding __awaiter and code coverage.

like image 35
goenning Avatar answered Oct 11 '22 17:10

goenning