Skip to content

Commit 7686654

Browse files
authored
Recommend rootDir and outDir in modules compiler option guide (#3330)
1 parent 5c3e830 commit 7686654

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

packages/documentation/copy/en/modules-reference/guides/Choosing Compiler Options.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ Choosing compilation settings as a library author is a fundamentally different p
142142
"verbatimModuleSyntax": true,
143143
"declaration": true,
144144
"sourceMap": true,
145-
"declarationMap": true
145+
"declarationMap": true,
146+
"rootDir": "src",
147+
"outDir": "dist"
146148
}
147149
}
148150
```
@@ -185,6 +187,7 @@ Let’s examine why we picked each of these settings:
185187
- **`verbatimModuleSyntax: true`**. This setting protects against a few module-related pitfalls that can cause problems for library consumers. First, it prevents writing any import statements that could be interpreted ambiguously based on the user’s value of `esModuleInterop` or `allowSyntheticDefaultImports`. Previously, it was often suggested that libraries compile without `esModuleInterop`, since its use in libraries could force users to adopt it too. However, it’s also possible to write imports that only work _without_ `esModuleInterop`, so neither value for the setting guarantees portability for libraries. `verbatimModuleSyntax` does provide such a guarantee.[^1] Second, it prevents the use of `export default` in modules that will be emitted as CommonJS, which can require bundler users and Node.js ESM users to consume the module differently. See the appendix on [ESM/CJS Interop](/docs/handbook/modules/appendices/esm-cjs-interop.html#library-code-needs-special-considerations) for more details.
186188
- **`declaration: true`** emits type declaration files alongside the output JavaScript. This is needed for consumers of the library to have any type information.
187189
- **`sourceMap: true`** and **`declarationMap: true`** emit source maps for the output JavaScript and type declaration files, respectively. These are only useful if the library also ships its source (`.ts`) files. By shipping source maps and source files, consumers of the library will be able to debug the library code somewhat more easily. By shipping declaration maps and source files, consumers will be able to see the original TypeScript sources when they run Go To Definition on imports from the libraries. Both of these represent a tradeoff between developer experience and library size, so it’s up to you whether to include them.
190+
- **`rootDir: true` and `outDir: true`**. Using a separate output directory is always a good idea, but it’s _necessary_ for libraries that publish their input files. Otherwise, [extension substitution](/docs/handbook/modules/reference.html#file-extension-substitution) will cause the library’s consumers to load the library’s `.ts` files instead of `.d.ts` files, causing type errors and performance problems.
188191

189192
### Considerations for bundling libraries
190193

0 commit comments

Comments
 (0)