@@ -64,32 +64,43 @@ touch loader.mjs
6464 * @see https://nodejs.org/api/esm.html#loaders
6565 */
6666
67+ import { DECORATOR_REGEX } from ' @flex-development/decorator-regex'
6768import * as esm from ' @flex-development/esm-types'
6869import * as mlly from ' @flex-development/mlly'
6970import * as pathe from ' @flex-development/pathe'
71+ import * as tscu from ' @flex-development/tsconfig-utils'
7072import * as tutils from ' @flex-development/tutils'
7173import * as esbuild from ' esbuild'
7274import { URL , fileURLToPath , pathToFileURL } from ' node:url'
73- import tsconfig from ' ./tsconfig.json ' assert { type : ' json ' }
75+ import ts from ' typescript '
7476
7577// add support for extensionless files in "bin" scripts
7678// https://github.com/nodejs/modules/issues/488
7779mlly .EXTENSION_FORMAT_MAP .set (' ' , esm .Format .COMMONJS )
7880
7981/**
80- * {@linkcode URL } of current working directory .
82+ * {@linkcode URL } of tsconfig file .
8183 *
8284 * @type {URL}
83- * @const cwd
85+ * @const tsconfig
8486 */
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)
8696
8797/**
8898 * Determines how the given module `url` should be interpreted, retrieved, and
8999 * parsed.
90100 *
91101 * @see {@linkcode esm.LoadHookContext }
92102 * @see {@linkcode esm.LoadHookResult }
103+ * @see {@linkcode esm.LoadHook }
93104 * @see {@linkcode esm.ResolvedModuleUrl }
94105 * @see https://nodejs.org/api/esm.html#loadurl-context-nextload
95106 *
@@ -132,11 +143,11 @@ export const load = async (url, context) => {
132143 }
133144
134145 // resolve path aliases
135- source = await mlly .resolveAliases (source, {
136- aliases: tsconfig .compilerOptions .paths ,
146+ source = await tscu .resolvePaths (source, {
137147 conditions: context .conditions ,
138- cwd,
139- parent: url
148+ ext: ' ' ,
149+ parent: url,
150+ tsconfig
140151 })
141152
142153 // resolve modules
@@ -145,6 +156,16 @@ export const load = async (url, context) => {
145156 parent: url
146157 })
147158
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+
148169 // transpile source code
149170 const { code } = await esbuild .transform (source, {
150171 format: ' esm' ,
@@ -153,7 +174,7 @@ export const load = async (url, context) => {
153174 sourcefile: fileURLToPath (url),
154175 sourcemap: ' inline' ,
155176 target: ` node${ process .versions .node } ` ,
156- tsconfigRaw: { compilerOptions: tsconfig . compilerOptions }
177+ tsconfigRaw: { compilerOptions }
157178 })
158179
159180 // set source code to transpiled source
@@ -174,6 +195,7 @@ export const load = async (url, context) => {
174195 *
175196 * @see {@linkcode esm.ResolveHookContext }
176197 * @see {@linkcode esm.ResolveHookResult }
198+ * @see {@linkcode esm.ResolveHook }
177199 * @see https://nodejs.org/api/esm.html#resolvespecifier-context-nextresolve
178200 *
179201 * @async
@@ -187,9 +209,9 @@ export const resolve = async (specifier, context) => {
187209
188210 // resolve path alias
189211 specifier = await mlly .resolveAlias (specifier, {
190- aliases: tsconfig . compilerOptions .paths ,
212+ aliases: compilerOptions .paths ,
191213 conditions,
192- cwd,
214+ cwd: pathToFileURL ( compilerOptions . baseUrl ) ,
193215 parent
194216 })
195217
0 commit comments