@@ -64,32 +64,43 @@ touch loader.mjs
64
64
* @see https://nodejs.org/api/esm.html#loaders
65
65
*/
66
66
67
+ import { DECORATOR_REGEX } from ' @flex-development/decorator-regex'
67
68
import * as esm from ' @flex-development/esm-types'
68
69
import * as mlly from ' @flex-development/mlly'
69
70
import * as pathe from ' @flex-development/pathe'
71
+ import * as tscu from ' @flex-development/tsconfig-utils'
70
72
import * as tutils from ' @flex-development/tutils'
71
73
import * as esbuild from ' esbuild'
72
74
import { URL , fileURLToPath , pathToFileURL } from ' node:url'
73
- import tsconfig from ' ./tsconfig.json ' assert { type : ' json ' }
75
+ import ts from ' typescript '
74
76
75
77
// add support for extensionless files in "bin" scripts
76
78
// https://github.com/nodejs/modules/issues/488
77
79
mlly .EXTENSION_FORMAT_MAP .set (' ' , esm .Format .COMMONJS )
78
80
79
81
/**
80
- * {@linkcode URL } of current working directory .
82
+ * {@linkcode URL } of tsconfig file .
81
83
*
82
84
* @type {URL}
83
- * @const cwd
85
+ * @const tsconfig
84
86
*/
85
- const cwd = pathToFileURL (tsconfig .compilerOptions .baseUrl )
87
+ const tsconfig = mlly .toURL (' tsconfig.json' )
88
+
89
+ /**
90
+ * TypeScript compiler options.
91
+ *
92
+ * @type {tscu.CompilerOptions}
93
+ * @const compilerOptions
94
+ */
95
+ const compilerOptions = tscu .loadCompilerOptions (tsconfig)
86
96
87
97
/**
88
98
* Determines how the given module `url` should be interpreted, retrieved, and
89
99
* parsed.
90
100
*
91
101
* @see {@linkcode esm.LoadHookContext }
92
102
* @see {@linkcode esm.LoadHookResult }
103
+ * @see {@linkcode esm.LoadHook }
93
104
* @see {@linkcode esm.ResolvedModuleUrl }
94
105
* @see https://nodejs.org/api/esm.html#loadurl-context-nextload
95
106
*
@@ -132,11 +143,11 @@ export const load = async (url, context) => {
132
143
}
133
144
134
145
// resolve path aliases
135
- source = await mlly .resolveAliases (source, {
136
- aliases: tsconfig .compilerOptions .paths ,
146
+ source = await tscu .resolvePaths (source, {
137
147
conditions: context .conditions ,
138
- cwd,
139
- parent: url
148
+ ext: ' ' ,
149
+ parent: url,
150
+ tsconfig
140
151
})
141
152
142
153
// resolve modules
@@ -145,6 +156,16 @@ export const load = async (url, context) => {
145
156
parent: url
146
157
})
147
158
159
+ // emit decorator metadata
160
+ if (DECORATOR_REGEX .test (source)) {
161
+ const { outputText } = ts .transpileModule (source, {
162
+ compilerOptions: { ... compilerOptions, sourceMap: false },
163
+ fileName: url
164
+ })
165
+
166
+ source = outputText
167
+ }
168
+
148
169
// transpile source code
149
170
const { code } = await esbuild .transform (source, {
150
171
format: ' esm' ,
@@ -153,7 +174,7 @@ export const load = async (url, context) => {
153
174
sourcefile: fileURLToPath (url),
154
175
sourcemap: ' inline' ,
155
176
target: ` node${ process .versions .node } ` ,
156
- tsconfigRaw: { compilerOptions: tsconfig . compilerOptions }
177
+ tsconfigRaw: { compilerOptions }
157
178
})
158
179
159
180
// set source code to transpiled source
@@ -174,6 +195,7 @@ export const load = async (url, context) => {
174
195
*
175
196
* @see {@linkcode esm.ResolveHookContext }
176
197
* @see {@linkcode esm.ResolveHookResult }
198
+ * @see {@linkcode esm.ResolveHook }
177
199
* @see https://nodejs.org/api/esm.html#resolvespecifier-context-nextresolve
178
200
*
179
201
* @async
@@ -187,9 +209,9 @@ export const resolve = async (specifier, context) => {
187
209
188
210
// resolve path alias
189
211
specifier = await mlly .resolveAlias (specifier, {
190
- aliases: tsconfig . compilerOptions .paths ,
212
+ aliases: compilerOptions .paths ,
191
213
conditions,
192
- cwd,
214
+ cwd: pathToFileURL ( compilerOptions . baseUrl ) ,
193
215
parent
194
216
})
195
217
0 commit comments