Skip to content

Instantly share code, notes, and snippets.

@SMotaal
Created April 25, 2018 16:53
Show Gist options
  • Save SMotaal/4f9458cf43cd7a55a12d5c476c15400e to your computer and use it in GitHub Desktop.
Save SMotaal/4f9458cf43cd7a55a12d5c476c15400e to your computer and use it in GitHub Desktop.
TypeScript: Support for experimental ".mjs" output

Getting Started

Installing the forked typescript

  yarn add smotaal/TypeScript

Installing typescript-local

  yarn add smotaal/typescript-local

Using the custom build

  // replace this
  import ts from 'typescript';

  // with this
  import ts from 'typescript-local';

You need to explicitly call tsc-local to run the custom compiler build and import from 'typescript-local' to use the custom build in Node.js.


Warning: Running `tsc` calls the original unmodified build to avoid unexpected outcomes.

Working with MJS Modules

Importing from MJS modules into TS files

  // file: test.ts
  import { runner } from './runner'; // -> runner.mjs

Importing from from MJS modules into MJS files

  // file: test.mjs
  import { runner } from './runner.mjs'; // -> runner.mjs

Compiling MJS Modules

Transpiling TypeScript to Node.js ES modules (.mjs)

  yarn tsc-local --module "esnext" --target "esnext" --allowSyntheticDefaultImports --esModuleInterop --outDir "build" ./test.ts

Transpiling TypeScript & JavaScript to Node.js ES modules (.mjs)

  yarn tsc-local --module "esnext" --target "esnext" --allowJS --allowSyntheticDefaultImports --esModuleInterop --outDir "build" ./test.ts

Transpiling Node.js ES modules (.mjs) to Standard ES2015 modules (.js)

  yarn tsc-local --module "es2015" --target "esnext" --allowJS --outDir "build" ./test.mjs

Transpiling Node.js ES modules (.mjs) to Node.js CommonJS modules (.js)

  yarn tsc-local --allowJS --outDir "build" ./test.mjs
@SMotaal
Copy link
Author

SMotaal commented Apr 25, 2018

Visual Studio Code's workspace './.vscode/settings.json'

  typescript.tsdk: "./packages/typescript-local/lib/"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment