4
4
* @see https://github.com/flex-development/mkbuild
5
5
*/
6
6
7
- import { defineBuildConfig , type Config } from '@flex-development/mkbuild'
8
- import { constant , define } from '@flex-development/tutils'
7
+ import { unassert } from '@flex-development/estree-util-unassert'
8
+ import {
9
+ defineBuildConfig ,
10
+ type Config ,
11
+ type OutputMetadata
12
+ } from '@flex-development/mkbuild'
13
+ import pathe from '@flex-development/pathe'
14
+ import {
15
+ DOT ,
16
+ constant ,
17
+ define ,
18
+ entries ,
19
+ get ,
20
+ overwrite
21
+ } from '@flex-development/tutils'
9
22
import { ok } from 'devlop'
10
- import type { BuildResult , PluginBuild } from 'esbuild'
23
+ import { fromJs } from 'esast-util-from-js'
24
+ import type { BuildResult , Metafile , OutputFile , PluginBuild } from 'esbuild'
25
+ import type { Program } from 'estree'
26
+ import { attachComments } from 'estree-util-attach-comments'
27
+ import { toJs } from 'estree-util-to-js'
28
+ import { visit } from 'estree-util-visit'
11
29
import util from 'node:util'
12
30
import pkg from './package.json' assert { type : 'json' }
13
31
import tsconfig from './tsconfig.build.json' assert { type : 'json' }
14
32
33
+ declare module 'estree' {
34
+ interface BaseNode {
35
+ position ?: import ( 'unist' ) . Position | undefined
36
+ }
37
+ }
38
+
15
39
/**
16
40
* Build configuration options.
17
41
*
@@ -23,23 +47,9 @@ const config: Config = defineBuildConfig({
23
47
{
24
48
dts : 'only'
25
49
} ,
26
- {
27
- dts : 'only' ,
28
- outdir : 'dist/dev'
29
- } ,
30
50
{
31
51
dts : false ,
32
- ignore : [ 'interfaces' ] ,
33
- outdir : 'dist/dev' ,
34
- sourcemap : true ,
35
- sourcesContent : false
36
- } ,
37
- {
38
- dts : false ,
39
- ignore : [ 'interfaces' ] ,
40
- plugins : [ ] ,
41
- sourcemap : true ,
42
- sourcesContent : false
52
+ ignore : [ 'interfaces' , 'types' ]
43
53
}
44
54
] ,
45
55
plugins : [
@@ -59,7 +69,7 @@ const config: Config = defineBuildConfig({
59
69
*/
60
70
setup ( build : PluginBuild ) : void {
61
71
/**
62
- * Regular expression used to fix module specifier .
72
+ * Regular expression used to fix module specifiers .
63
73
*
64
74
* @const {RegExp} regex
65
75
*/
@@ -80,6 +90,160 @@ const config: Config = defineBuildConfig({
80
90
}
81
91
} )
82
92
}
93
+ } ,
94
+ {
95
+ /**
96
+ * Plugin name.
97
+ */
98
+ name : unassert . name ,
99
+
100
+ /**
101
+ * Remove assertions.
102
+ *
103
+ * @this {void}
104
+ *
105
+ * @param {PluginBuild } build - esbuild plugin api
106
+ * @return {void } Nothing
107
+ */
108
+ setup ( build : PluginBuild ) : void {
109
+ const {
110
+ absWorkingDir : cwd = process . cwd ( ) ,
111
+ format,
112
+ outdir = DOT
113
+ } = build . initialOptions
114
+
115
+ /**
116
+ * Directory to store development output files.
117
+ *
118
+ * @const {string} devdir
119
+ */
120
+ const devdir : string = pathe . join ( outdir , 'dev' )
121
+
122
+ return void build . onEnd ( ( result : BuildResult ) : void => {
123
+ ok ( result . metafile , 'expected metafile' )
124
+ ok ( result . outputFiles , 'expected output files' )
125
+
126
+ /**
127
+ * Output file filter.
128
+ *
129
+ * @const {RegExp} filter
130
+ */
131
+ const filter : RegExp = / \. [ c m ] { 0 , 1 } j s $ /
132
+
133
+ /**
134
+ * Development output file metadata.
135
+ *
136
+ * @const {Metafile['outputs']} outputs
137
+ */
138
+ const outputs : Metafile [ 'outputs' ] = { }
139
+
140
+ /**
141
+ * Development output files.
142
+ *
143
+ * @const {OutputFile[]} outputs
144
+ */
145
+ const outputFiles : OutputFile [ ] = [ ]
146
+
147
+ // get development output file metadata
148
+ for ( const [ path , output ] of entries ( result . metafile . outputs ) ) {
149
+ if ( filter . test ( path ) ) {
150
+ define ( outputs , path . replace ( outdir , devdir ) , { value : output } )
151
+ }
152
+ }
153
+
154
+ // handle output files
155
+ for ( const output of result . outputFiles ) {
156
+ if ( filter . test ( output . path ) ) {
157
+ /**
158
+ * JavaScript syntax tree.
159
+ *
160
+ * @const {Program} tree
161
+ */
162
+ const tree : Program = fromJs ( output . text , {
163
+ module : format !== 'iife'
164
+ } )
165
+
166
+ // attach comments
167
+ visit ( tree , node => void ( node . loc = node . position ) )
168
+ attachComments ( tree , tree . comments )
169
+ delete tree . comments
170
+
171
+ // remove assertions
172
+ unassert ( tree )
173
+
174
+ /**
175
+ * Relative path to output file.
176
+ *
177
+ * @const {string} outfile
178
+ */
179
+ const outfile : string = output . path . replace ( cwd + pathe . sep , '' )
180
+
181
+ /**
182
+ * Path to development output file.
183
+ *
184
+ * @const {string} devpath
185
+ */
186
+ const devpath : string = pathe . resolve (
187
+ cwd ,
188
+ devdir ,
189
+ outfile . replace ( outdir + pathe . sep , '' )
190
+ )
191
+
192
+ /**
193
+ * Output file text.
194
+ *
195
+ * @const {string} text
196
+ */
197
+ const text : string = toJs ( tree ) . value
198
+
199
+ /**
200
+ * Output file contents.
201
+ *
202
+ * @const {Uint8Array} contents
203
+ */
204
+ const contents : Uint8Array = new util . TextEncoder ( ) . encode ( text )
205
+
206
+ /**
207
+ * Output file metadata.
208
+ *
209
+ * @const {OutputMetadata} metadata
210
+ */
211
+ const metadata : OutputMetadata = get (
212
+ result . metafile . outputs ,
213
+ outfile
214
+ )
215
+
216
+ // assert output file metadata
217
+ ok ( metadata , 'expected output file metadata' )
218
+
219
+ // add development output file
220
+ outputFiles . push (
221
+ define ( { contents : output . contents , path : devpath } , 'text' , {
222
+ get : constant ( output . text )
223
+ } )
224
+ )
225
+
226
+ // update output file
227
+ define ( output , 'text' , { get : constant ( text ) } )
228
+ output . contents = new util . TextEncoder ( ) . encode ( output . text )
229
+
230
+ // update output file metadata
231
+ define ( result . metafile . outputs , outfile , {
232
+ value : overwrite ( metadata , {
233
+ bytes : contents . byteLength ,
234
+ imports : metadata . imports . filter ( ( { path } ) => {
235
+ return ! unassert . MODULES_REGEX . test ( path )
236
+ } )
237
+ } )
238
+ } )
239
+ }
240
+ }
241
+
242
+ // update output files and metadata
243
+ result . outputFiles = [ ...outputFiles , ...result . outputFiles ]
244
+ result . metafile . outputs = { ...outputs , ...result . metafile . outputs }
245
+ } )
246
+ }
83
247
}
84
248
] ,
85
249
target : [
0 commit comments