Search code examples
typescriptsystemjstsc

Specify systemJS configuration file for Node tsc compiler


Right now, the Node tsc compiler looks at the tsconfig.jsonfile which looks something like this:

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true
  }
}

Using the "module": "system" line, you can specify it to use SystemJS for managing modules. Is it possible to configure which configuration file SystemJS will use? I'd like to be able to specify custom defined modules so that Typescript won't complain about them, without having to create a separate .d.ts file for each one. It doesn't appear to be looking at either systemjs.config.js or system-config.js right now.


Solution

  • My comments on using the SystemJS config in the tsconfig.json file are still relevant as there's no option to do that.

    But, now that I understand what you want, then it's possible to do with just typescript, if you structure your code the right way.
    Let's assume the following module:

    • ModuleA
      • SubModuleOne
      • SubModuleTwo

    In dev you want to import from ./ModuleA/SubModuleOne but in prod it should be from ./ModuleA as you compile all the inner modules into one file of the same name.

    So how about always importing from ./ModuleA?
    According to How TypeScript resolves modules:

    an import statement like import { b } from "./moduleB" in /root/src/moduleA.ts would result in attempting the following locations for locating "./moduleB":

    1. /root/src/moduleB.ts
    2. /root/src/moduleB.tsx
    3. /root/src/moduleB.d.ts
    4. /root/src/moduleB/package.json (if it specifies a "typings" property)
    5. /root/src/moduleB/index.ts
    6. /root/src/moduleB/index.tsx
    7. /root/src/moduleB/index.d.ts

    So if you have a ./ModuleA/index.d.ts (or .ts then you can there include of all the inner modules and then just importing from ./ModuleA will work both in dev and prod.
    It's kind of like the python module with its __init__.py

    Hope that will work out for you.