Skip to content Skip to sidebar Skip to footer

Exported Typescript Class Not Included In Webpack Bundle If Not Used Directly

I'm converting a javascript project with Angular 1.x to WebPack and TypeScript (using ts-loader). I got it mostly working, but I'm running into trouble when ts-loader seems to be o

Solution 1:

Turns out you have to signal WebPack to explicitly include a module by adding an extra require call without import statement.

I'm not ready to mangle my .ts files by adding duplicate imports, so I made a generic solution for that using the preprocessor loader:

{
    "line": false,
    "file": true,
    "callbacks": {
    "fileName": "all",
    "scope": "line",
    "callback": "(function fixTs(line, fileName, lineNumber) { return line.replace(/^(import.*(require\\(.*?\\)))/g, '$2;$1'); })"
    }]
}

As a proof of concept, this regex version is very limited it only support the following format:

importClassA = require('ClassA');
// becomesrequire('ClassA');importClassA = require('ClassA');

But it works for me. Similarly, I'm adding the require shim:

{"fileName":"all","scope":"source","callback":"(function fixTs(source, fileName) { return 'declare var require: any;' + source; })"}

I made a sample project with this solution.

Post a Comment for "Exported Typescript Class Not Included In Webpack Bundle If Not Used Directly"