Skip to content

Commit d488958

Browse files
committed
feat: configs and stuff i dunno
1 parent c50a495 commit d488958

File tree

11 files changed

+141
-39
lines changed

11 files changed

+141
-39
lines changed

bin/build.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,8 @@ const config = esbuild.config({
1212
'src/esbuild/index.ts',
1313
'src/jest/index.ts',
1414
'src/utils/index.ts'
15-
],
16-
15+
]
1716
})
1817

19-
utils.clearPath(config.outdir)
20-
esbuild.build(config).then(() => {
21-
utils.typegen(config.entryPoints)
22-
})
18+
utils.clearPath(config.outdir!)
19+
esbuild.build(config)

src/esbuild/build.ts

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
1-
import { build as _build, analyzeMetafile, BuildOptions } from 'esbuild'
1+
import { build as _build, analyzeMetafile, BuildOptions, WatchMode } from 'esbuild'
2+
import { mapEntries, writeDefinition } from './definitions'
23

3-
const watch = process.argv.includes('--watch') || process.argv.includes('-w')
4+
/** Watch mode handler for adding actions on rebuild. */
5+
const handleRebuild: WatchMode['onRebuild'] = async (error, result) => {
6+
if (error) {
7+
console.error('👀❎ Watch build failed:', error)
8+
} else {
9+
const meta = await analyzeMetafile(result!.metafile!)
10+
console.log('👀✅ Watch build succeeded', meta)
11+
}
12+
}
413

5-
/** Build project with Esbuild. */
6-
export const build = async (opts: BuildOptions = {}) =>
7-
_build({
8-
watch: watch
9-
? {
10-
onRebuild: (error, result) => error
11-
? console.error('👀❎ Watch build failed:', error)
12-
: analyzeMetafile(result!.metafile!).then(
13-
(meta) => console.log('👀✅ Watch build succeeded', meta)
14-
)
15-
}
16-
: false,
17-
...opts
18-
})
14+
/** Build project with ES Build, adds reload handler in watch mode. */
15+
export const build = async (options: BuildOptions = {}) => {
16+
const entries = mapEntries(options)
17+
const writeDefinitions = () => Promise.all(entries.map(writeDefinition))
18+
const watchEnabled =
19+
options.watch ||
20+
process.argv.includes('--watch') ||
21+
process.argv.includes('-w')
22+
const watch: BuildOptions['watch'] = {
23+
onRebuild: async (error, result) => {
24+
await writeDefinitions()
25+
return handleRebuild(error, result)
26+
}
27+
}
28+
29+
return _build({...options, watch: watchEnabled ? watch : undefined })
1930
.then(({ metafile }) => analyzeMetafile(metafile!))
2031
.then(console.log)
32+
.then(writeDefinitions)
2133
.catch(_ => process.exit(1))
34+
}

src/esbuild/config.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export const defaults: Partial<BuildOptions> = {
1010
target: 'node14',
1111
outdir: 'dist',
1212
bundle: true,
13-
metafile: true
13+
metafile: true,
14+
outExtension: { '.js': '.mjs' }
1415
}
1516

1617
/** Generate ESbuild config, finding dependencies from package. */

src/esbuild/definitions.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { mapEntries } from './definitions'
2+
3+
describe('Cogs » esbuild » definitions', () => {
4+
describe('.mapEntries', () => {
5+
it('maps an array of entry points to js out files', () => {
6+
expect(mapEntries({
7+
outdir: 'dist',
8+
entryPoints: [
9+
'src/one/index.ts',
10+
'src/two/index.ts',
11+
]
12+
})/* ? */).toEqual([{
13+
entry: 'src/one/index.ts',
14+
outFile: 'dist/one/index.js'
15+
}, {
16+
entry: 'src/two/index.ts',
17+
outFile: 'dist/two/index.js'
18+
}])
19+
})
20+
it('maps an entry point object to js out files', () => {
21+
expect(mapEntries({
22+
outdir: 'dist',
23+
entryPoints: {
24+
one: 'src/one/index.ts',
25+
two: 'src/two/index.ts'
26+
}
27+
})).toEqual([{
28+
entry: 'src/one/index.ts',
29+
outFile: 'dist/one.js'
30+
}, {
31+
entry: 'src/two/index.ts',
32+
outFile: 'dist/two.js'
33+
}])
34+
})
35+
})
36+
})

src/esbuild/definitions.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { BuildOptions } from 'esbuild'
2+
import { exec } from 'child_process'
3+
4+
type EntryMap = { entry: string, outFile: string }
5+
6+
/** Map entry points to js out files. */
7+
export const mapEntries = (options: BuildOptions, baseUrl: string = 'src') => {
8+
if (typeof options.entryPoints === 'undefined') return [] as EntryMap[]
9+
10+
const { entryPoints, outExtension, outdir = 'dist' } = options
11+
const entries = [] as EntryMap[]
12+
const ext = outExtension?.ext || 'js'
13+
14+
const getOutFile = (entry: string) =>
15+
`${outdir}/${entry.replace(new RegExp(`^${baseUrl}/`), '').replace(/\.ts$/, '')}.${ext}`
16+
17+
if (Array.isArray(entryPoints)) {
18+
for (const entry of entryPoints) {
19+
entries.push({ entry, outFile: getOutFile(entry) })
20+
}
21+
} else {
22+
for (const [out, entry] of Object.entries(entryPoints)) {
23+
entries.push({ entry, outFile: getOutFile(out) })
24+
}
25+
}
26+
return entries
27+
}
28+
29+
/** Write type definitions for entry points with js out files. */
30+
export const writeDefinition = ({ entry, outFile }: EntryMap) =>
31+
new Promise<string>((resolve, reject) =>
32+
exec(
33+
`yarn tsc ${entry} --outFile ${outFile} --declaration --emitDeclarationOnly -p .`,
34+
(error, stdout, stderr) => {
35+
if (error) {
36+
console.error(`❎ Writing definition for ${entry} failed:`, error)
37+
reject(stderr)
38+
} else {
39+
console.log(`✅ Writing definition for ${entry} succeeded`)
40+
resolve(stdout)
41+
}
42+
})
43+
)

src/typescript/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import ts from 'typescript'
2+
3+
const host: ts.ParseConfigFileHost = ts.sys as any;
4+
5+
ts.getParsedCommandLineOfConfigFile('tsconfig.json', {}, host)

src/utils/clear.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { rmdirSync, mkdirSync } from 'fs'
22

3-
export const clearPath = (path?: string) => {
4-
if (!path) return
3+
export const clearPath = (path: string) => {
54
rmdirSync(path, { recursive: true })
65
mkdirSync(path)
76
}

src/utils/package.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1+
import { fileURLToPath } from 'url'
12
import { readFileSync } from 'fs'
23
import { createRequire } from 'module'
3-
import { resolve } from 'path'
4+
import { dirname, resolve } from 'path'
5+
6+
export const resolvePath = (path: string) =>
7+
resolve(dirname(fileURLToPath(import.meta.url)), path)
48

59
export const readPackage = (path: string) =>
610
JSON.parse(readFileSync(resolve(path, 'package.json'), { encoding: 'utf-8' }))
@@ -10,3 +14,4 @@ export const requirePackage = (path: string) =>
1014

1115
export const readDependencies = (path: string) =>
1216
Object.keys(readPackage(path).dependencies)
17+

test/package/jest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { configure } from '@nested-code/cogs/jest/index.js'
1+
import jest from '@nested-code/cogs/jest'
22

3-
export default configure()
3+
export default jest.configure()

tsconfig.build.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"exclude": ["!./src"],
4+
"include": ["./src/jest"],
5+
"compilerOptions": {
6+
"declaration": true,
7+
"declarationDir": "dist",
8+
"declarationMap": true,
9+
"emitDeclarationOnly": true,
10+
"outFile": "./dist/jest/index.js",
11+
}
12+
}

tsconfig.json

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,6 @@
1414
"forceConsistentCasingInFileNames": true,
1515
"isolatedModules": true,
1616
"skipLibCheck": true,
17-
"strict": true,
18-
"declaration": true
19-
},
20-
"exclude": [
21-
"**/*.test.ts",
22-
"**/mocks",
23-
"**/coverage"
24-
],
25-
"include": [
26-
"**/*.ts"
27-
]
17+
"strict": true
18+
}
2819
}

0 commit comments

Comments
 (0)