Skip to content

Commit 24345b6

Browse files
committed
build: remove assertions
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
1 parent ffff530 commit 24345b6

File tree

3 files changed

+185
-22
lines changed

3 files changed

+185
-22
lines changed

build.config.ts

Lines changed: 176 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,38 @@
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'
922
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'
1129
import util from 'node:util'
1230
import pkg from './package.json' assert { type: 'json' }
1331
import 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 (!/\.[cm]{0,1}js\.map$/.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 (/\.[cm]{0,1}js$/.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: [

src/unassert.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,10 @@ function unassert(this: void, tree: Program, options?: Nilable<Options>): void {
4747
})
4848
}
4949

50+
/**
51+
* @property {RegExp} MODULES_REGEX - Default regular expression used to match
52+
* assertion module ids
53+
*/
54+
unassert.MODULES_REGEX = MODULES_REGEX
55+
5056
export default unassert

yarn.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6986,11 +6986,11 @@ __metadata:
69866986
linkType: hard
69876987

69886988
"magic-string@npm:^0.30.5":
6989-
version: 0.30.6
6990-
resolution: "magic-string@npm:0.30.6"
6989+
version: 0.30.7
6990+
resolution: "magic-string@npm:0.30.7"
69916991
dependencies:
69926992
"@jridgewell/sourcemap-codec": "npm:^1.4.15"
6993-
checksum: 10/046fbf11614a271e7214d32ca02a2af8d18b268403db9d50580f421ff2222261f1cd8bb02a938206130e3c43deefa6423ee4622c4f161212298570f22b4059bb
6993+
checksum: 10/883eaaf6792a3263e44f4bcdcd35ace272268e4b98ed5a770ad711947958d2f9fc683e474945e306e2bdc152b7e44d369ee312690d87025b9879fc63fbe1409c
69946994
languageName: node
69956995
linkType: hard
69966996

0 commit comments

Comments
 (0)