@@ -33,13 +33,16 @@ Now you can publish your library with `npm run ubik publish`.
33
33
34
34
### Fixing extensions
35
35
36
- The recommended way to do this is by publishing your package with:
36
+ ` tsc ` outputs invalid JavaScript when building ESM libraries. [ Read more] ( ./docs/extensions.md )
37
+
38
+ The recommended way to fix this with Ubik is by using the following command
39
+ instead of ` npm publish ` to publish your package to NPM:
37
40
38
41
``` sh
39
42
npm run ubik publish
40
43
```
41
44
42
- You can also do:
45
+ You can also do this to apply the fix in place :
43
46
44
47
``` sh
45
48
npm run ubik compile
@@ -54,7 +57,12 @@ For best experience, add to `tsconfig.json`:
54
57
{
55
58
"exclude" : [
56
59
" dist/**/*"
57
- ]
60
+ ],
61
+ "compilerOptions" : {
62
+ "target" : " esnext" ,
63
+ "module" : " esnext" ,
64
+ "moduleResolution" : " node10"
65
+ }
58
66
}
59
67
```
60
68
@@ -69,39 +77,12 @@ dist/
69
77
*.dist.d.ts
70
78
```
71
79
72
- This assumes that the entrypoint of your package (` main ` in ` package.json ` )
80
+ ** KNOWN ISSUE: ** This operation assumes that the entrypoint of your package (` main ` in ` package.json ` )
73
81
is at the top level of your source code tree, e.g. ` ./index.ts ` or ` ./src/index.ts ` -
74
82
but not e.g. ` ./src/foo/index.ts ` next to ` ./src/bar/somethingelse.ts ` (the latter
75
83
would probably fail to compile all files or will place them in inappropriate locations -
76
84
good matter for a pull request.)
77
85
78
- #### Fixing extensions - rationale
79
-
80
- TypeScript wants you to import modules with:
81
-
82
- ``` ts
83
- import { foo } from " ./foo"
84
- ```
85
-
86
- but the ECMAScript specification expects:
87
-
88
- ``` js
89
- import { foo } from " ./foo.js"
90
- ```
91
-
92
- TypeScript doesn't add the extensions; it also doesn't provide an extensibility hook
93
- which could be used to add them. [ The documentation] ( https://www.typescriptlang.org/docs/handbook/esm-node.html )
94
- is confusing and unhelpful; the solution that it suggests is ridiculous and unacceptable.
95
- On GitHub issues, the TypeScript developers have repeatedly reacted with indifference.
96
-
97
- ** Before Node.js 16, this was less of a problem:** imports without extensions still
98
- worked. However, since Node 16 started enforcing the ES Modules spec (which requires
99
- extensions), the code that ` tsc ` outputs became effectively ** invalid and impossible to run**
100
- (unless you were writing your program in a single file).
101
-
102
- What, were you expecting the compiler for evertone's favorite "strict superset of JS"
103
- to, idk, output correct JS that is ready to execute? You crazy person!
104
-
105
86
### Splitting away type imports
106
87
107
88
``` sh
@@ -110,8 +91,13 @@ to, idk, output correct JS that is ready to execute? You crazy person!
110
91
111
92
### Fixing CommonJS star imports
112
93
94
+ When targeting ESM on Node, CommonJS imports are wrapped in an extra ` default ` key,
95
+ of which TypeScript is unaware. [ Read more] ( ./docs/split-stars.md )
96
+
97
+ Let's rewrite the imports so that both work:
98
+
113
99
``` sh
114
- // TODO
100
+ npm exec ubik split-stars ./src -- protobufjs
115
101
```
116
102
117
103
### Others
0 commit comments