Skip to content

Commit

Permalink
build: remove assertions
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Mar 3, 2024
1 parent ffff530 commit 24345b6
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 22 deletions.
195 changes: 176 additions & 19 deletions build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,38 @@
* @see https://github.com/flex-development/mkbuild
*/

import { defineBuildConfig, type Config } from '@flex-development/mkbuild'
import { constant, define } from '@flex-development/tutils'
import { unassert } from '@flex-development/estree-util-unassert'
import {
defineBuildConfig,
type Config,
type OutputMetadata
} from '@flex-development/mkbuild'
import pathe from '@flex-development/pathe'
import {
DOT,
constant,
define,
entries,
get,
overwrite
} from '@flex-development/tutils'
import { ok } from 'devlop'
import type { BuildResult, PluginBuild } from 'esbuild'
import { fromJs } from 'esast-util-from-js'
import type { BuildResult, Metafile, OutputFile, PluginBuild } from 'esbuild'
import type { Program } from 'estree'
import { attachComments } from 'estree-util-attach-comments'
import { toJs } from 'estree-util-to-js'
import { visit } from 'estree-util-visit'
import util from 'node:util'
import pkg from './package.json' assert { type: 'json' }
import tsconfig from './tsconfig.build.json' assert { type: 'json' }

declare module 'estree' {
interface BaseNode {
position?: import('unist').Position | undefined
}
}

/**
* Build configuration options.
*
Expand All @@ -23,23 +47,9 @@ const config: Config = defineBuildConfig({
{
dts: 'only'
},
{
dts: 'only',
outdir: 'dist/dev'
},
{
dts: false,
ignore: ['interfaces'],
outdir: 'dist/dev',
sourcemap: true,
sourcesContent: false
},
{
dts: false,
ignore: ['interfaces'],
plugins: [],
sourcemap: true,
sourcesContent: false
ignore: ['interfaces', 'types']
}
],
plugins: [
Expand All @@ -59,7 +69,7 @@ const config: Config = defineBuildConfig({
*/
setup(build: PluginBuild): void {
/**
* Regular expression used to fix module specifier.
* Regular expression used to fix module specifiers.
*
* @const {RegExp} regex
*/
Expand All @@ -80,6 +90,153 @@ const config: Config = defineBuildConfig({
}
})
}
},
{
/**
* Plugin name.
*/
name: unassert.name,

/**
* Remove assertions.
*
* @this {void}
*
* @param {PluginBuild} build - esbuild plugin api
* @return {void} Nothing
*/
setup(build: PluginBuild): void {
const {
absWorkingDir: cwd = process.cwd(),
format,
outdir = DOT
} = build.initialOptions

/**
* Directory to store development output files.
*
* @const {string} devdir
*/
const devdir: string = pathe.join(outdir, 'dev')

return void build.onEnd((result: BuildResult): void => {
ok(result.metafile, 'expected metafile')
ok(result.outputFiles, 'expected output files')

/**
* Development output metadata.
*
* @const {Metafile['outputs']} outputs
*/
const outputs: Metafile['outputs'] = {}

/**
* Development output files.
*
* @const {OutputFile[]} outputs
*/
const outputFiles: OutputFile[] = []

// get development output file metadata
for (const [path, output] of entries(result.metafile.outputs)) {
define(outputs, path.replace(outdir, devdir), { value: output })
}

// handle output files
for (const output of result.outputFiles) {
/**
* Relative path to output file.
*
* @const {string} outfile
*/
const outfile: string = output.path.replace(cwd + pathe.sep, '')

if (!/\.[cm]{0,1}js\.map$/.test(output.path)) {
/**
* Path to development output file.
*
* @const {string} devpath
*/
const devpath: string = pathe.resolve(
cwd,
devdir,
outfile.replace(outdir + pathe.sep, '')
)

// add development output file
outputFiles.push(
define({ contents: output.contents, path: devpath }, 'text', {
get: constant(output.text)
})
)
}

if (/\.[cm]{0,1}js$/.test(output.path)) {
/**
* JavaScript syntax tree.
*
* @const {Program} tree
*/
const tree: Program = fromJs(output.text, {
module: format !== 'iife'
})

// attach comments
visit(tree, node => void (node.loc = node.position))
attachComments(tree, tree.comments)
delete tree.comments

// remove assertions
unassert(tree)

/**
* Output file text.
*
* @const {string} text
*/
const text: string = toJs(tree).value

/**
* Output file contents.
*
* @const {Uint8Array} contents
*/
const contents: Uint8Array = new util.TextEncoder().encode(text)

/**
* Output file metadata.
*
* @const {OutputMetadata} metadata
*/
const metadata: OutputMetadata = get(
result.metafile.outputs,
outfile
)

// assert output file metadata
ok(metadata, 'expected output file metadata')

// update output file
define(output, 'text', { get: constant(text) })
output.contents = new util.TextEncoder().encode(output.text)

// update output file metadata
define(result.metafile.outputs, outfile, {
value: overwrite(metadata, {
bytes: contents.byteLength,
imports: metadata.imports.filter(({ path }) => {
return !unassert.MODULES_REGEX.test(path)
})
})
})
}
}

// update output files and metadata
result.outputFiles = [...outputFiles, ...result.outputFiles]
result.metafile.outputs = { ...outputs, ...result.metafile.outputs }
})
}
}
],
target: [
Expand Down
6 changes: 6 additions & 0 deletions src/unassert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,10 @@ function unassert(this: void, tree: Program, options?: Nilable<Options>): void {
})
}

/**
* @property {RegExp} MODULES_REGEX - Default regular expression used to match
* assertion module ids
*/
unassert.MODULES_REGEX = MODULES_REGEX

export default unassert
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6986,11 +6986,11 @@ __metadata:
linkType: hard

"magic-string@npm:^0.30.5":
version: 0.30.6
resolution: "magic-string@npm:0.30.6"
version: 0.30.7
resolution: "magic-string@npm:0.30.7"
dependencies:
"@jridgewell/sourcemap-codec": "npm:^1.4.15"
checksum: 10/046fbf11614a271e7214d32ca02a2af8d18b268403db9d50580f421ff2222261f1cd8bb02a938206130e3c43deefa6423ee4622c4f161212298570f22b4059bb
checksum: 10/883eaaf6792a3263e44f4bcdcd35ace272268e4b98ed5a770ad711947958d2f9fc683e474945e306e2bdc152b7e44d369ee312690d87025b9879fc63fbe1409c
languageName: node
linkType: hard

Expand Down

0 comments on commit 24345b6

Please sign in to comment.