8

I have realized for my library projects that it is best if I use TypeScript to transpile to ES2015 format and then I bili to convert that to alternative formats (aka, UMD and CJS). So that means that my main tsconfig.json is:

{
  "compilerOptions": {
    "declaration": true,
    "module": "es2015",
    "target": "es2015",
    "lib": ["es2015", "esnext.asynciterable", "es2015.reflect"],
    "moduleResolution": "node",
    "sourceMap": true,
    "noImplicitAny": true,
    "outDir": "./lib",
    "removeComments": false,
    "typeRoots": ["./node_modules/@types"]
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules", "**/*-spec.ts"]
}

This does what I need it to for transpiling but for years now I've been enjoying my mocha-tests run via ts-node:

ts-node --require ts-node/register 'test/**/*-spec.ts'

I pretty quickly realized that all my TS based test scripts were now falling over due to their use of import statements and the like but I was able to address this by adding the following:

TS_NODE_PROJECT="tsconfig.test.json" ts-node --require ts-node/register 'test/**/*-spec.ts'

Where the alternative tsconfig.test.json is:

{
  "compilerOptions": {
    "module": "commonjs",
    "moduleResolution": "node",
    "target": "es5",
    "lib": ["es2015"],
    "declaration": false,
    "noImplicitAny": false,
    "removeComments": true,
    "inlineSourceMap": true
  },
  "include": ["scripts/**/*.ts"]
}

This does advance things to some degree in that my test files -- which are TypeScript files -- do run now BUT all is not well as I switched from lodash to lodash-es (for hopefully obvious reasons) and now I get errors within lodash-es:

enter image description here

so in essence it seems that the following ES formatted files ARE being transpiled:

  • my test script test-spec.ts
  • my source files src/my-file.ts

but my source files dependencies like "lodash-es" which are in ES2015 are NOT being transpiled. Is there any way for me to address this?


Update

I found this post very useful: Tree shake Lodash with Webpack, Jest and Typescript

While the example is using Jest, I believe Mocha is behaving the same way as is described. What I don't know is ... is there a way to get Mocha configured like Jest is in this article.

ken
  • 8,763
  • 11
  • 72
  • 133

0 Answers0