-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c50a495
commit d488958
Showing
11 changed files
with
141 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,34 @@ | ||
import { build as _build, analyzeMetafile, BuildOptions } from 'esbuild' | ||
import { build as _build, analyzeMetafile, BuildOptions, WatchMode } from 'esbuild' | ||
import { mapEntries, writeDefinition } from './definitions' | ||
|
||
const watch = process.argv.includes('--watch') || process.argv.includes('-w') | ||
/** Watch mode handler for adding actions on rebuild. */ | ||
const handleRebuild: WatchMode['onRebuild'] = async (error, result) => { | ||
if (error) { | ||
console.error('👀❎ Watch build failed:', error) | ||
} else { | ||
const meta = await analyzeMetafile(result!.metafile!) | ||
console.log('👀✅ Watch build succeeded', meta) | ||
} | ||
} | ||
|
||
/** Build project with Esbuild. */ | ||
export const build = async (opts: BuildOptions = {}) => | ||
_build({ | ||
watch: watch | ||
? { | ||
onRebuild: (error, result) => error | ||
? console.error('👀❎ Watch build failed:', error) | ||
: analyzeMetafile(result!.metafile!).then( | ||
(meta) => console.log('👀✅ Watch build succeeded', meta) | ||
) | ||
} | ||
: false, | ||
...opts | ||
}) | ||
/** Build project with ES Build, adds reload handler in watch mode. */ | ||
export const build = async (options: BuildOptions = {}) => { | ||
const entries = mapEntries(options) | ||
const writeDefinitions = () => Promise.all(entries.map(writeDefinition)) | ||
const watchEnabled = | ||
options.watch || | ||
process.argv.includes('--watch') || | ||
process.argv.includes('-w') | ||
const watch: BuildOptions['watch'] = { | ||
onRebuild: async (error, result) => { | ||
await writeDefinitions() | ||
return handleRebuild(error, result) | ||
} | ||
} | ||
|
||
return _build({...options, watch: watchEnabled ? watch : undefined }) | ||
.then(({ metafile }) => analyzeMetafile(metafile!)) | ||
.then(console.log) | ||
.then(writeDefinitions) | ||
.catch(_ => process.exit(1)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { mapEntries } from './definitions' | ||
|
||
describe('Cogs » esbuild » definitions', () => { | ||
describe('.mapEntries', () => { | ||
it('maps an array of entry points to js out files', () => { | ||
expect(mapEntries({ | ||
outdir: 'dist', | ||
entryPoints: [ | ||
'src/one/index.ts', | ||
'src/two/index.ts', | ||
] | ||
})/* ? */).toEqual([{ | ||
entry: 'src/one/index.ts', | ||
outFile: 'dist/one/index.js' | ||
}, { | ||
entry: 'src/two/index.ts', | ||
outFile: 'dist/two/index.js' | ||
}]) | ||
}) | ||
it('maps an entry point object to js out files', () => { | ||
expect(mapEntries({ | ||
outdir: 'dist', | ||
entryPoints: { | ||
one: 'src/one/index.ts', | ||
two: 'src/two/index.ts' | ||
} | ||
})).toEqual([{ | ||
entry: 'src/one/index.ts', | ||
outFile: 'dist/one.js' | ||
}, { | ||
entry: 'src/two/index.ts', | ||
outFile: 'dist/two.js' | ||
}]) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { BuildOptions } from 'esbuild' | ||
import { exec } from 'child_process' | ||
|
||
type EntryMap = { entry: string, outFile: string } | ||
|
||
/** Map entry points to js out files. */ | ||
export const mapEntries = (options: BuildOptions, baseUrl: string = 'src') => { | ||
if (typeof options.entryPoints === 'undefined') return [] as EntryMap[] | ||
|
||
const { entryPoints, outExtension, outdir = 'dist' } = options | ||
const entries = [] as EntryMap[] | ||
const ext = outExtension?.ext || 'js' | ||
|
||
const getOutFile = (entry: string) => | ||
`${outdir}/${entry.replace(new RegExp(`^${baseUrl}/`), '').replace(/\.ts$/, '')}.${ext}` | ||
|
||
if (Array.isArray(entryPoints)) { | ||
for (const entry of entryPoints) { | ||
entries.push({ entry, outFile: getOutFile(entry) }) | ||
} | ||
} else { | ||
for (const [out, entry] of Object.entries(entryPoints)) { | ||
entries.push({ entry, outFile: getOutFile(out) }) | ||
} | ||
} | ||
return entries | ||
} | ||
|
||
/** Write type definitions for entry points with js out files. */ | ||
export const writeDefinition = ({ entry, outFile }: EntryMap) => | ||
new Promise<string>((resolve, reject) => | ||
exec( | ||
`yarn tsc ${entry} --outFile ${outFile} --declaration --emitDeclarationOnly -p .`, | ||
(error, stdout, stderr) => { | ||
if (error) { | ||
console.error(`❎ Writing definition for ${entry} failed:`, error) | ||
reject(stderr) | ||
} else { | ||
console.log(`✅ Writing definition for ${entry} succeeded`) | ||
resolve(stdout) | ||
} | ||
}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import ts from 'typescript' | ||
|
||
const host: ts.ParseConfigFileHost = ts.sys as any; | ||
|
||
ts.getParsedCommandLineOfConfigFile('tsconfig.json', {}, host) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
import { rmdirSync, mkdirSync } from 'fs' | ||
|
||
export const clearPath = (path?: string) => { | ||
if (!path) return | ||
export const clearPath = (path: string) => { | ||
rmdirSync(path, { recursive: true }) | ||
mkdirSync(path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { configure } from '@nested-code/cogs/jest/index.js' | ||
import jest from '@nested-code/cogs/jest' | ||
|
||
export default configure() | ||
export default jest.configure() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"exclude": ["!./src"], | ||
"include": ["./src/jest"], | ||
"compilerOptions": { | ||
"declaration": true, | ||
"declarationDir": "dist", | ||
"declarationMap": true, | ||
"emitDeclarationOnly": true, | ||
"outFile": "./dist/jest/index.js", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters