Skip to content

Commit 595c754

Browse files
committed
build: remove assertions
- arethetypeswrong/arethetypeswrong.github.io#112 - unfortunately, attw/no-resolution needs to be disabled entirely. manual review is required Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
1 parent ffff530 commit 595c754

File tree

4 files changed

+191
-22
lines changed

4 files changed

+191
-22
lines changed

.attw.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"color": true,
33
"emoji": true,
44
"format": "ascii",
5-
"ignoreRules": ["cjs-resolves-to-esm"],
5+
"ignoreRules": ["cjs-resolves-to-esm", "no-resolution"],
66
"summary": true
77
}

build.config.ts

Lines changed: 183 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,160 @@ 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+
* Output file filter.
128+
*
129+
* @const {RegExp} filter
130+
*/
131+
const filter: RegExp = /\.[cm]{0,1}js$/
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+
}
83247
}
84248
],
85249
target: [

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,12 @@
3232
],
3333
"exports": {
3434
".": {
35+
"types": "./dist/index.d.mts",
3536
"development": "./dist/dev/index.mjs",
3637
"default": "./dist/index.mjs"
3738
},
3839
"./package.json": "./package.json"
3940
},
40-
"module": "./dist/index.mjs",
41-
"types": "./dist/index.d.mts",
4241
"scripts": {
4342
"build": "node --loader=./loader.mjs --enable-source-maps ./build",
4443
"check:ci": "yarn dedupe --check && yarn check:format && yarn check:lint && yarn check:spelling && yarn typecheck && yarn test:cov && yarn pack && yarn check:types:build && attw package.tgz && yarn clean:pack",

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

0 commit comments

Comments
 (0)