1

I'm trying to create an application in Electron and I'd like to use Typescript for this. Knowing Typescript just boils down to JavaScript in the end, thats no problem, and having used TypeScript before, that "just works".

My scenario at the moment is I'm trying to create a single, monolithic .js file from the TypeScript (using the outFile setting), but separating my .ts code a little into files containing classes (namespaced where appropriate).

I know I can use the references in TypeScript and using the namespace keyword to break apart my code into sensible chunks. I've also been using "export class" to give me access to what I need. However, I walk into a problem whenever I want to import fs or lodash. Then start getting all kinds of errors about "define is not defined". So, I try and import AMD loader or RequireJS, but none of this seems to work.

I then read that I shouldn't mix Internal and External modules together in TypeScript 1.8.

So, the big question is.. how should I layout my application code in TypeScript... How do I separate out my code sensibly into .ts chunks containing sensible classes which compile into a single .js file that I can obfuscate successfully? Using references? Exporting? Importing? Which module type?

Cheers in advance.

Schodemeiss
  • 1,144
  • 3
  • 16
  • 37

1 Answers1

1

You have several options here:

  1. Use external modules with -outFile and module set to AMD or system. Its possible since typescript 1.8. More info
  2. Use external modules and tools like webpack(example)/browserify(example) that will allow you to concat resulting js into one (or more files).
  3. Do not use namespaces. Some links: link, link

Hope this helps.

Community
  • 1
  • 1
Amid
  • 21,508
  • 5
  • 57
  • 54
  • I converted my project to just use External Modules (basically, works like plain Node.JS would). The next question is, how do I invoke a module from that single file? Especially in the realms of Electron? I keep getting "define is undefined". Or, when I include requirejs, I get "path is not valid". – Schodemeiss Apr 11 '16 at 10:31
  • I do not have experience with Electron in particular. But error message clearly indicates that you need to have a loader that will during runtime load your external modules. It can be requirejs, webpack, etc, but I personally prefer systemjs. For example of its setup you can check this section: https://angular.io/docs/ts/latest/quickstart.html#!#systemjs. Don not pay attension that its for angular. The systemjs setup is the same. – Amid Apr 11 '16 at 12:31