2

Say I have two files

//a.ts
export class A {
    public print() {
        console.log("A!");
    }
}

and

//app.ts
import A from "./a";
new A().print();

How would I go about setting up the tsconfig so that it outputs both to one file? i.e. kinda like so

class A {
    print() {
        console.log("A!");
    }
}
new A().print();

I can't seem to get outfile to work correctly and when I set module to "AMD", I can't run the code in browser because it's missing "define"

I've tried Typescript compile to single file but that seems to generate define related things that doesn't run on it's own in browser without require.js, when all I want is to condense self contained code into one file? Thank you!

  • If you want to bundle for browser usage, I strongly suggest chaining the typescript compiler into a bundling tool like webpack or roll up. – pascalpuetz Dec 12 '19 at 01:44
  • I was afraid of that, thank you, I'll go the web pack route if no one has knows of other ways. –  Dec 12 '19 at 01:48
  • what about `outfile` doesn't work correctly? are you compiling from the command line, or VS? – Dave Cousineau Dec 12 '19 at 02:21
  • @DaveCousineau it does compile but in a way that I can't run natively in Browser. Always get output using the define keyword that requires require.js. Was looking for a solution that didn't have that extra overhead, just condensing both files together without the other stuff. Will be looking into web pack rn –  Dec 12 '19 at 02:31
  • Please check below post. you may have to change target version:: https://stackoverflow.com/questions/42415942/typescript-what-is-target-in-tsconfig – xdeepakv Dec 12 '19 at 02:41

1 Answers1

1

Something like this, though it might depend on your module setting, not sure.

"compilerOptions": {
   "module": "none",
   "outFile": "Scripts/Output/MyOutputFile.js"
}, "exclude": [
   "Scripts/Output/MyOutputFile.d.ts",
   "obj/**/*"
]

To avoid problems with the ordering of the source files, make sure to use reference path statements to identify local dependencies in each file. Without this, a dependent file might appear before what it depends on, which leads to unexpected client errors.

/// <reference path="Folder/File.ts"/>

Since I have encountered no end of problems getting the Visual Studio TypeScript compiler to work properly, I manually compile from the command line. This actually led to using MAKE in order to compile all my TypeScript projects in the right order and at the right times. MAKE is pretty complicated, so I'm not sure if I recommend going that route or not.

Dave Cousineau
  • 12,154
  • 8
  • 64
  • 80
  • Interesting, I'll try out this method. Thank you very much! –  Dec 12 '19 at 02:35
  • This works perfectly! Thank thank thank you so much! It compiles perfectly into one file without the define junk. Seems that my mistake was using `imports` instead of reference paths like you did –  Dec 12 '19 at 02:38
  • @SebastianFMunoz np, glad to help – Dave Cousineau Dec 12 '19 at 02:40