44 * @see https://github.com/flex-development/mkbuild
55 */
66
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'
922import { 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'
1129import util from 'node:util'
1230import pkg from './package.json' assert { type : 'json' }
1331import tsconfig from './tsconfig.build.json' assert { type : 'json' }
1432
33+ declare module 'estree' {
34+ interface BaseNode {
35+ position ?: import ( 'unist' ) . Position | undefined
36+ }
37+ }
38+
1539/**
1640 * Build configuration options.
1741 *
@@ -23,23 +47,9 @@ const config: Config = defineBuildConfig({
2347 {
2448 dts : 'only'
2549 } ,
26- {
27- dts : 'only' ,
28- outdir : 'dist/dev'
29- } ,
3050 {
3151 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' ]
4353 }
4454 ] ,
4555 plugins : [
@@ -59,7 +69,7 @@ const config: Config = defineBuildConfig({
5969 */
6070 setup ( build : PluginBuild ) : void {
6171 /**
62- * Regular expression used to fix module specifier .
72+ * Regular expression used to fix module specifiers .
6373 *
6474 * @const {RegExp} regex
6575 */
@@ -80,6 +90,153 @@ const config: Config = defineBuildConfig({
8090 }
8191 } )
8292 }
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+ * Development output metadata.
128+ *
129+ * @const {Metafile['outputs']} outputs
130+ */
131+ const outputs : Metafile [ 'outputs' ] = { }
132+
133+ /**
134+ * Development output files.
135+ *
136+ * @const {OutputFile[]} outputs
137+ */
138+ const outputFiles : OutputFile [ ] = [ ]
139+
140+ // get development output file metadata
141+ for ( const [ path , output ] of entries ( result . metafile . outputs ) ) {
142+ define ( outputs , path . replace ( outdir , devdir ) , { value : output } )
143+ }
144+
145+ // handle output files
146+ for ( const output of result . outputFiles ) {
147+ /**
148+ * Relative path to output file.
149+ *
150+ * @const {string} outfile
151+ */
152+ const outfile : string = output . path . replace ( cwd + pathe . sep , '' )
153+
154+ if ( ! / \. [ c m ] { 0 , 1 } j s \. m a p $ / . test ( output . path ) ) {
155+ /**
156+ * Path to development output file.
157+ *
158+ * @const {string} devpath
159+ */
160+ const devpath : string = pathe . resolve (
161+ cwd ,
162+ devdir ,
163+ outfile . replace ( outdir + pathe . sep , '' )
164+ )
165+
166+ // add development output file
167+ outputFiles . push (
168+ define ( { contents : output . contents , path : devpath } , 'text' , {
169+ get : constant ( output . text )
170+ } )
171+ )
172+ }
173+
174+ if ( / \. [ c m ] { 0 , 1 } j s $ / . test ( output . path ) ) {
175+ /**
176+ * JavaScript syntax tree.
177+ *
178+ * @const {Program} tree
179+ */
180+ const tree : Program = fromJs ( output . text , {
181+ module : format !== 'iife'
182+ } )
183+
184+ // attach comments
185+ visit ( tree , node => void ( node . loc = node . position ) )
186+ attachComments ( tree , tree . comments )
187+ delete tree . comments
188+
189+ // remove assertions
190+ unassert ( tree )
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+ // update output file
220+ define ( output , 'text' , { get : constant ( text ) } )
221+ output . contents = new util . TextEncoder ( ) . encode ( output . text )
222+
223+ // update output file metadata
224+ define ( result . metafile . outputs , outfile , {
225+ value : overwrite ( metadata , {
226+ bytes : contents . byteLength ,
227+ imports : metadata . imports . filter ( ( { path } ) => {
228+ return ! unassert . MODULES_REGEX . test ( path )
229+ } )
230+ } )
231+ } )
232+ }
233+ }
234+
235+ // update output files and metadata
236+ result . outputFiles = [ ...outputFiles , ...result . outputFiles ]
237+ result . metafile . outputs = { ...outputs , ...result . metafile . outputs }
238+ } )
239+ }
83240 }
84241 ] ,
85242 target : [
0 commit comments