Search code examples
moduletypescriptamdbundling-and-minificationtsc

TypeScript bundling not working for External Modules


I've a sample TypeScript code and I'm trying to bundle multiple ts/tsx files using typescript compiler (tsc).

Here is the code:

File: ISample.ts

    class ISample{
        constructor(public value:string){
        }
    }
    export = ISample;

File: Sample.ts

    import ISample = require('./ISample');

    class SampleImpl{
        value: ISample;
        constructor(sample:number){
            this.value = new ISample(sample+'');
        }
    }

File: tsconfig.json

    {
        "compilerOptions": {
            "module": "amd",
            "noImplicitAny": true,
            "removeComments": true,
            "preserveConstEnums": true,
            "jsx": "react",
            "outFile": "./dist/bundle.js",
            "target": "es3",
            "listFiles": true,
            "sourceMap": false
        },
        "files": [
            "./src/Sample.ts",
            "./src/ISample.ts"
        ]
    }

When I run the command:

    tsc

bundle.js is generated but it is completely blank.

Observations:

  1. The problem doesn't occur when I move the code to Internal Modules

  2. The problem also doesn't occur when I omit the import/require statement and use a declaration for ISample class, but in that case bundle.js does not contain the code of ISample class

Any ideas why this is happening?


Solution

  • This is not supported for now: Suggestion: multi-file external modules.

    There are a set of workarounds, e.g. Compile TypeScript with modules and bundle to one file