Skip to content

Commit d243f3e

Browse files
authored
v0.3.0
1 parent 32071ce commit d243f3e

File tree

1 file changed

+63
-4
lines changed

1 file changed

+63
-4
lines changed

README.md

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
1-
# `stdlib`
1+
# `std.module.format`
2+
3+
- [`std.module.format`](#-stdmoduleformat-)
4+
* [Overview](#overview)
5+
* [Patterns: Prefer Named Exports](#avoid-default-exports-and-prefer-named-exports)
6+
+ [Context](#context)
7+
+ [Summary](#summary)
8+
* [File Extenstions](#decision)
9+
+ [ECMAScript Module Support in Node.js](#ecmascript-module-support-in-nodejs)
10+
+ [`.mjs`, `.cjs`, == `.mts`, `.cts` && `.d.mts` and `.d.cts`.](#-mjs----cjs-------mts----cts------dmts--and--dcts-)
11+
* [Anti-Patterns: Avoid Export Default](#avoid-export-default)
12+
+ [Poor Discoverability](#poor-discoverability)
13+
+ [Autocomplete](#autocomplete)
14+
+ [CommonJS interop](#commonjs-interop)
15+
+ [Typo Protection](#typo-protection)
16+
+ [TypeScript auto-import](#typescript-auto-import)
17+
+ [Re-exporting](#re-exporting)
18+
+ [Dynamic Imports](#dynamic-imports)
19+
+ [Needs two lines for non-class / non-function](#needs-two-lines-for-non-class---non-function)
220

321
## Overview
422

23+
524
Testing best way to support cjs and esm and how to structure project to enable that (see results.txt for more info)
625

726

8-
## Avoid Default Exports and Prefer Named Exports'
27+
## Avoid Default Exports and Prefer Named Exports
928

1029
### Context
1130

@@ -77,10 +96,49 @@ Imports that bypass an index file are discouraged, but may sometimes be necessar
7796
import { helperFunc } from '../../lib/UtilityX/helper';
7897
```
7998

99+
### ECMAScript Module Support in Node.js
100+
101+
> source https://devblogs.microsoft.com/typescript/announcing-typescript-4-5-beta/
102+
103+
For the last few years, Node.js has been working to support running ECMAScript modules (ESM). This has been a very difficult feature to support, since the foundation of the Node.js ecosystem is built on a different module system called CommonJS (CJS). Interoperating between the two brings large challenges, with many new features to juggle; however, support for ESM in Node.js is now largely implemented in Node.js 12 and later, and the dust has begun to settle.
104+
105+
That’s why TypeScript 4.5 brings two new module settings: node12 and nodenext.
106+
```json
107+
{
108+
"compilerOptions": {
109+
"module": "nodenext",
110+
}
111+
}
112+
```
113+
114+
These new modes bring a few high-level features which we’ll explore here.
115+
116+
type in package.json and New Extensions
117+
Node.js supports a new setting in package.json called type. "type" can be set to either "module" or "commonjs".
118+
```json
119+
{
120+
"name": "my-package",
121+
"type": "module",
122+
123+
"//": "...",
124+
"dependencies": {
125+
}
126+
}
127+
```
128+
### `.mjs`, `.cjs`, == `.mts`, `.cts` && `.d.mts` and `.d.cts`.
129+
130+
Node.js supports two extensions to help with this: `.mjs` and `.cjs`. `.mjs` files are always ES modules, and `.cjs` files are always CommonJS modules, and there’s no way to override these.
131+
132+
In turn, TypeScript supports two new source file extensions: .mts and `.cts`. When TypeScript emits these to JavaScript files, it will emit them to `.mjs` and `.cjs` respectively.
133+
134+
Furthermore, TypeScript also supports two new declaration file extensions: .d.mts and .d.cts. When TypeScript generates declaration files for `.mts` and `.cts`, their corresponding extensions will be `.d.mts` and `.d.cts`.
80135

136+
Using these extensions is entirely optional, but will often be useful even if you choose not to use them as part of your primary workflow.
81137

82138

83-
## Avoid Export Default - TypeScript Deep Dive
139+
## Avoid Export Default
140+
141+
> source: TypeScript Deep Dive
84142
85143
> [source https://basarat.gitbook.io/typescript/main-1/defaultisbad](https://basarat.gitbook.io/typescript/main-1/defaultisbad)
86144
@@ -101,8 +159,9 @@ There are a few maintainability concerns here:
101159

102160
For this reason I recommend simple exports + destructured import. E.g. `foo.ts`:
103161

162+
```typescript
104163
import { Foo } from "./foo";
105-
164+
```
106165
Below I also present a few more reasons.
107166

108167
### Poor Discoverability

0 commit comments

Comments
 (0)