fix(dev-infra): ensure ts-node is registered with commonjs as module (#37422)

We recently added support for automatic registration of `ts-node`
when the dev-infra configuration is loaded.

In addition to registering ts-node, we should also ensure that the
`commonjs` module is set up. By default, `ts-node` would use ES module
imports that are not supported by default in NodeJS.

PR Close #37422
This commit is contained in:
Paul Gschwendtner 2020-05-20 11:59:42 +02:00 committed by atscott
parent 954d002884
commit 9e28e14c08

View File

@ -7,7 +7,7 @@
*/
import {existsSync} from 'fs';
import {join} from 'path';
import {dirname, join} from 'path';
import {exec} from 'shelljs';
import {error} from './console';
@ -85,7 +85,12 @@ function readConfigFile(configPath: string): object {
// version of the given configuration seems to exist, set up `ts-node` if available.
if (require.extensions['.ts'] === undefined && existsSync(`${configPath}.ts`) &&
isTsNodeAvailable()) {
require('ts-node').register({skipProject: true, transpileOnly: true});
// Ensure the module target is set to `commonjs`. This is necessary because the
// dev-infra tool runs in NodeJS which does not support ES modules by default.
// Additionally, set the `dir` option to the directory that contains the configuration
// file. This allows for custom compiler options (such as `--strict`).
require('ts-node').register(
{dir: dirname(configPath), transpileOnly: true, compilerOptions: {module: 'commonjs'}});
}
try {