Skip to content

Commit c2a6597

Browse files
committed
docs: use
Signed-off-by: Lexus Drumgold <[email protected]>
1 parent 126e1b2 commit c2a6597

File tree

3 files changed

+117
-1
lines changed

3 files changed

+117
-1
lines changed

README.md

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,114 @@ yarn add @flex-development/ext-regex@flex-development/ext-regex
6565

6666
## Use
6767

68-
**TODO**: usage example.
68+
```typescript
69+
import { DECORATOR_REGEX } from '@flex-development/decorator-regex'
70+
import { EXT_DTS_REGEX, EXT_TS_REGEX } from '@flex-development/ext-regex'
71+
import * as mlly from '@flex-development/mlly'
72+
import pathe from '@flex-development/pathe'
73+
import * as tscu from '@flex-development/tsconfig-utils'
74+
import type { Nullable } from '@flex-development/tutils'
75+
import type {
76+
OnLoadArgs,
77+
OnLoadOptions,
78+
OnLoadResult,
79+
Plugin,
80+
PluginBuild
81+
} from 'esbuild'
82+
import type { URL } from 'node:url'
83+
84+
/**
85+
* Returns a plugin that allows esbuild to handle [`emitDecoratorMetadata`][1].
86+
*
87+
* [1]: https://www.typescriptlang.org/tsconfig#emitDecoratorMetadata
88+
*
89+
* @param {tscu.LoadTsconfigOptions?} [options] - Plugin options
90+
* @return {Plugin} Decorator metadata plugin
91+
*/
92+
const plugin = (options?: tscu.LoadTsconfigOptions): Plugin => ({
93+
name: 'decorators',
94+
setup: async ({ initialOptions, onLoad }: PluginBuild): Promise<void> => {
95+
const { absWorkingDir = '.', tsconfig = 'tsconfig.json' } = initialOptions
96+
97+
/**
98+
* User compiler options.
99+
*
100+
* @const {tscu.CompilerOptions} compilerOptions
101+
*/
102+
const compilerOptions: tscu.CompilerOptions = tscu.loadCompilerOptions(
103+
pathe.resolve(absWorkingDir, tsconfig),
104+
options
105+
)
106+
107+
// exit early if decorator metadata should not be emitted
108+
if (!compilerOptions.emitDecoratorMetadata) return void 0
109+
110+
/**
111+
* TypeScript module.
112+
*
113+
* @const {typeof import('typescript')} ts
114+
*/
115+
const ts: typeof import('typescript') = (await import('typescript')).default
116+
117+
/**
118+
* {@linkcode onLoad} callback options.
119+
*
120+
* @const {OnLoadOptions}
121+
*/
122+
const opts: OnLoadOptions = { filter: /.*/ }
123+
124+
// transpile typescript modules containing decorators
125+
onLoad(opts, async (args: OnLoadArgs): Promise<Nullable<OnLoadResult>> => {
126+
/**
127+
* Callback result.
128+
*
129+
* @var {Nullable<OnLoadResult>} result
130+
*/
131+
let result: Nullable<OnLoadResult> = null
132+
133+
// transpile typescript modules, but skip typescript declaration modules
134+
if (EXT_TS_REGEX.test(args.path) && !EXT_DTS_REGEX.test(args.path)) {
135+
/**
136+
* URL of module to load.
137+
*
138+
* @const {URL} url
139+
*/
140+
const url: URL = mlly.toURL(args.path)
141+
142+
/**
143+
* File content at {@linkcode args.path}.
144+
*
145+
* @const {string} source
146+
*/
147+
const source: string = (await mlly.getSource(url)) as string
148+
149+
// do nothing if module does not use decorators
150+
if (!DECORATOR_REGEX.test(source)) return null
151+
152+
// transpile module to emit decorator metadata
153+
const { outputText: contents } = ts.transpileModule(source, {
154+
compilerOptions: tscu.normalizeCompilerOptions(compilerOptions)
155+
})
156+
157+
result = { contents }
158+
}
159+
160+
return result
161+
})
162+
163+
return void 0
164+
}
165+
})
166+
167+
export default plugin
168+
```
169+
170+
<blockquote>
171+
<small>
172+
Looking for a plugin like this? Check out&nbsp;
173+
<a href='https://github.com/flex-development/mkbuild'><code>mkbuild</code></a> 😉
174+
</small>
175+
</blockquote>
69176

70177
## API
71178

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
"devDependencies": {
7676
"@commitlint/cli": "17.4.3",
7777
"@commitlint/config-conventional": "17.4.3",
78+
"@flex-development/decorator-regex": "1.0.0",
7879
"@flex-development/mkbuild": "1.0.0-alpha.11",
7980
"@flex-development/mlly": "1.0.0-alpha.11",
8081
"@flex-development/tsconfig-utils": "1.1.1",

yarn.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,13 @@ __metadata:
11341134
languageName: node
11351135
linkType: hard
11361136

1137+
"@flex-development/decorator-regex@npm:1.0.0":
1138+
version: 1.0.0
1139+
resolution: "@flex-development/decorator-regex@npm:1.0.0::__archiveUrl=https%3A%2F%2Fnpm.pkg.github.com%2Fdownload%2F%40flex-development%2Fdecorator-regex%2F1.0.0%2Fe485b45a5c66ca688d77188039bb5efa90ca3fb8"
1140+
checksum: 3200659769897add6af31341c391a58a4edc7920b81042a7117a95c50ae0375da2f62c6869060ce91507a29ac9dbd06a96e440fe30b438536685f7ce36a1daca
1141+
languageName: node
1142+
linkType: hard
1143+
11371144
"@flex-development/errnode@npm:1.2.0":
11381145
version: 1.2.0
11391146
resolution: "@flex-development/errnode@npm:1.2.0::__archiveUrl=https%3A%2F%2Fnpm.pkg.github.com%2Fdownload%2F%40flex-development%2Ferrnode%2F1.2.0%2F8e54e66e637d392299cd2d422224da9cf5d16cab"
@@ -1183,6 +1190,7 @@ __metadata:
11831190
dependencies:
11841191
"@commitlint/cli": "npm:17.4.3"
11851192
"@commitlint/config-conventional": "npm:17.4.3"
1193+
"@flex-development/decorator-regex": "npm:1.0.0"
11861194
"@flex-development/errnode": "npm:1.5.0"
11871195
"@flex-development/mkbuild": "npm:1.0.0-alpha.11"
11881196
"@flex-development/mlly": "npm:1.0.0-alpha.11"

0 commit comments

Comments
 (0)