Monday 1 May 2017

TypeScript AMD compilation and "barrel" modules

I'm trying to set up a Node.js + TypeScript project using Intern for testing. Everything works fine when I compile the project using "commonjs" (which I do for the normal build); and TypeScript is equally happy when compiling for "amd", which is required by Intern. However, when passing the tests with intern-client, it complains about a couple of things:

First, imports from "index.ts" files (so-called "barrel" modules) won't work. My setup is something like this (everything in the same directory):

// index.ts
export { x } from './x'

// x.ts
export function x() {}

// x.test.ts
import { x } from '.' // "Error: Failed to load module ..."

In fact, the generated JavaScript code looks something like this:

define(["require", "exports", "."], function (...) { ... })

And I'm not sure that AMD knows how to handle the ".".

The second issue happens under the same circunstances (TypeScript compiles happily, but intern-client complains). In summary, I get an error when doing:

import jsdom = require('jsdom')

Which I need to transform to:

const jsdom = require('jsdom')

For Intern to be able to deal with it.

Here is the tsconfig.json file I use to compile the tests:

{
    "compilerOptions": {
        "target": "es6",
        "module": "amd",
        "moduleResolution": "node",
        "sourceMap": true,
        "rootDir": "src",
        "outDir": "build/tests",
        "noImplicitAny": true,
        "suppressImplicitAnyIndexErrors": true
    }
}

And here is my intern.js configuration file, in case it helps:

define({
    suites: ['build/tests/**/*.test.js'],
    excludeInstrumentation: true,
    filterErrorStack: true
})



via DanielM

No comments:

Post a Comment