|
1 | 1 | # `std.module.format` |
2 | 2 |
|
3 | | -> version 0.1.3 |
| 3 | +> version 0.1.5 |
4 | 4 |
|
5 | 5 | - [`std.module.format`](#-stdmoduleformat-) |
6 | 6 | * [Overview](#overview) |
@@ -43,21 +43,74 @@ Establishing the best way to support cjs and esm and how to structure project to |
43 | 43 |
|
44 | 44 | **disable** the following compiler options: |
45 | 45 |
|
46 | | -`esModuleInterop`: `false` |
47 | | -`allowSyntheticDefaultImports`: `false` |
48 | | - |
| 46 | +> `tsconfig.json` |
| 47 | +```jsonc |
| 48 | +"esModuleInterop": false, |
| 49 | +"allowSyntheticDefaultImports": false |
| 50 | +``` |
49 | 51 |
|
50 | 52 | The two flags `esModuleInterop` and `allowSyntheticDefaultImports` enable interoperation between ES Modules and CommonJS, AMD, and UMD modules for emit from TypeScript and type resolution by TypeScript respectively. |
51 | 53 |
|
52 | 54 | Unfortunately these options are viral: **enabling them in a package requires all downstream consumers to enable them as well** . The TLDR is due to the way CommonJS and ES Modules interoperate with bundlers (Webpack, Parcel, etc.). |
53 | 55 |
|
54 | 56 | ### Solution |
55 | 57 |
|
56 | | - set both `allowSyntheticDefaultImports` and `esModuleInterop` to `false`. |
| 58 | +>**Warning** |
| 59 | +> setting both `allowSyntheticDefaultImports` and `esModuleInterop` to `false` may break your build. |
57 | 60 |
|
58 | 61 | Consumers can now opt into these semantics, it also does not require them to do so. |
59 | 62 | Consumers **can always safely use alternative import syntaxes (including falling back to require() and import()),** or can enable these flags and opt into this behavior themselves. |
60 | 63 |
|
| 64 | +#### Other Compiler Options Affecting the Build Result |
| 65 | + |
| 66 | +- `extends` |
| 67 | +- `importsNotUsedAsValues` |
| 68 | +- `preserveValueImports` |
| 69 | +- `jsxFactory` |
| 70 | +- `jsxFragmentFactory` |
| 71 | + |
| 72 | +#### Suggested `tsconfig` values |
| 73 | + |
| 74 | +```jsonc |
| 75 | +"useDefineForClassFields": true, |
| 76 | +``` |
| 77 | +```jsonc |
| 78 | +"isolatedModules": true, |
| 79 | +``` |
| 80 | + |
| 81 | +> [source, vitejs developer guide: vitejs.dev/guide/features.html#typescript-compiler-options](https://vitejs.dev/guide/features.html#typescript-compiler-options) |
| 82 | +
|
| 83 | +## Side by Side Compare |
| 84 | + |
| 85 | +### node:assert |
| 86 | + |
| 87 | +>**Note** |
| 88 | +>[See the NodeJs Assertion Documentation for more information](https://nodejs.org/api/assert.html#assert) |
| 89 | + |
| 90 | +#### Modern |
| 91 | + |
| 92 | +```typescript |
| 93 | +import { strict as assert } from 'node:assert'; // ESM |
| 94 | +const assert = require('node:assert').strict; // CJS |
| 95 | +``` |
| 96 | + |
| 97 | +```typescript |
| 98 | +import assert from 'node:assert/strict'; // ESM |
| 99 | +const assert = require('node:assert/strict'); // CJS |
| 100 | +``` |
| 101 | + |
| 102 | +```typescript |
| 103 | +import { strict as assert } from 'node:assert'; // ESM |
| 104 | +const assert = require('node:assert/strict'); // CJS |
| 105 | +``` |
| 106 | + |
| 107 | +#### Legacy |
| 108 | + |
| 109 | +```typescript |
| 110 | +import assert from 'node:assert'; // ESM |
| 111 | +const assert = require('node:assert'); // CJS |
| 112 | +``` |
| 113 | + |
61 | 114 | ## TLDR |
62 | 115 |
|
63 | 116 | ```javascript |
@@ -136,23 +189,6 @@ export type { T } |
136 | 189 | ``` |
137 | 190 | > [source, typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export) |
138 | 191 |
|
139 | | -#### Other Compiler Options Affecting the Build Result |
140 | | -- `extends` |
141 | | -- `importsNotUsedAsValues` |
142 | | -- `preserveValueImports` |
143 | | -- `jsxFactory` |
144 | | -- `jsxFragmentFactory` |
145 | | -
|
146 | | -#### Suggested `tsconfig` values |
147 | | -```json |
148 | | -["useDefineForClassFields": true] |
149 | | -``` |
150 | | -```json |
151 | | -["isolatedModules": true] |
152 | | -``` |
153 | | -
|
154 | | -> [source, vitejs developer guide: vitejs.dev/guide/features.html#typescript-compiler-options](https://vitejs.dev/guide/features.html#typescript-compiler-options) |
155 | | -
|
156 | 192 | ## Avoid Default Exports and Prefer Named Exports |
157 | 193 |
|
158 | 194 | ### Context |
|
0 commit comments