Skip to content

Commit 85dd6de

Browse files
committed
rebuild the readme
1 parent 1bc470d commit 85dd6de

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

README.md

+52
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ Native ESM support is currently experimental. For usage, limitations, and to pro
7272
* [Third-party transpilers](#third-party-transpilers)
7373
* [Bundled `swc` integration](#bundled-swc-integration)
7474
* [Writing your own integration](#writing-your-own-integration)
75+
* [Module type overrides](#module-type-overrides)
76+
* [Caveats](#caveats)
7577
* [Recipes](#recipes)
7678
* [Watching and Restarting](#watching-and-restarting)
7779
* [AVA](#ava)
@@ -321,6 +323,9 @@ node --trace-deprecation --abort-on-uncaught-exception -r ts-node/register ./ind
321323
* `-r, --require [path]` Require a node module before execution
322324
* `--cwd` Behave as if invoked in this working directory <br/>*Default:* `process.cwd()`<br/>*Environment:* `TS_NODE_CWD`
323325
* `--emit` Emit output files into `.ts-node` directory <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_EMIT`
326+
* `--scope` Scope compiler to files within `scopeDir`. Anything outside this directory is ignored. <br/>\*Default: `false` <br/>*Environment:* `TS_NODE_SCOPE`
327+
* `--scopeDir` Directory within which compiler is limited when `scope` is enabled. <br/>*Default:* First of: `tsconfig.json` "rootDir" if specified, directory containing `tsconfig.json`, or cwd if no `tsconfig.json` is loaded.<br/>*Environment:* `TS_NODE_SCOPE_DIR`
328+
* `moduleType` Override the module type of certain files, ignoring the `package.json` `"type"` field. See [Module type overrides](#module-type-overrides) for details.<br/>*Default:* obeys `package.json` `"type"` and `tsconfig.json` `"module"` <br/>*Can only be specified via `tsconfig.json` or API.*
324329
* `TS_NODE_HISTORY` Path to history file for REPL <br/>*Default:* `~/.ts_node_repl_history`<br/>
325330

326331
## API
@@ -671,6 +676,53 @@ To write your own transpiler integration, check our [API docs](https://typestron
671676
Integrations are `require()`d, so they can be published to npm. The module must export a `create` function matching the
672677
[`TranspilerModule`](https://typestrong.org/ts-node/api/interfaces/TranspilerModule.html) interface.
673678
679+
## Module type overrides
680+
681+
When deciding between CommonJS and native ECMAScript modules, `ts-node` defaults to matching vanilla `node` and `tsc`
682+
behavior. This means TypeScript files are transformed according to your `tsconfig.json` `"module"` option and executed
683+
according to node's rules for the `package.json` `"type"` field.
684+
685+
In some projects you may need to override this behavior for some files. For example, in a webpack
686+
project, you may have `package.json` configured with `"type": "module"` and `tsconfig.json` with
687+
`"module": "esnext"`. However, webpack uses our CommonJS hook to execute your `webpack.config.ts`,
688+
so you need to force your webpack config and any supporting scripts to execute as CommonJS.
689+
690+
In these situations, our `moduleTypes` option lets you override certain files, forcing execution as
691+
CommonJS or ESM. Node supports similar overriding via `.cjs` and `.mjs` file extensions, but `.ts` files cannot use them.
692+
`moduleTypes` achieves the same effect, and *also* overrides your `tsconfig.json` `"module"` config appropriately.
693+
694+
The following example tells `ts-node` to execute a webpack config as CommonJS:
695+
696+
```jsonc title=tsconfig.json
697+
{
698+
"ts-node": {
699+
"transpileOnly": true,
700+
"moduleTypes": {
701+
"webpack.config.ts": "cjs",
702+
// Globs are also supported with the same behavior as tsconfig "include"
703+
"webpack-config-scripts/**/*": "cjs"
704+
}
705+
},
706+
"compilerOptions": {
707+
"module": "es2020",
708+
"target": "es2020"
709+
}
710+
}
711+
```
712+
713+
Each key is a glob pattern with the same syntax as tsconfig's `"include"` array.
714+
When multiple patterns match the same file, the last pattern takes precedence.
715+
716+
* `cjs` overrides matches files to compile and execute as CommonJS.
717+
* `esm` overrides matches files to compile and execute as native ECMAScript modules.
718+
* `package` resets either of the above to default behavior, which obeys `package.json` `"type"` and `tsconfig.json` `"module"` options.
719+
720+
### Caveats
721+
722+
Files with an overridden module type are transformed with the same limitations as [`isolatedModules`](https://www.typescriptlang.org/tsconfig#isolatedModules). This will only affect rare cases such as using `const enum`s with [`preserveConstEnums`](https://www.typescriptlang.org/tsconfig#preserveConstEnums) disabled.
723+
724+
This feature is meant to faciliate scenarios where normal `compilerOptions` and `package.json` configuration is not possible. For example, a `webpack.config.ts` cannot be given its own `package.json` to override `"type"`. Wherever possible you should favor using traditional `package.json` and `tsconfig.json` configurations.
725+
674726
# Recipes
675727

676728
## Watching and Restarting

0 commit comments

Comments
 (0)