Skip to content

Commit 924fb76

Browse files
authored
feat: createContainer (#150)
* feat: add `createContainer` * test(container): basic tests * chore: update readme * chore: update readme * chore: pkg lock update
1 parent cfeea38 commit 924fb76

9 files changed

+1221
-141
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
node_modules
22
/dist
33
.DS_Store
4+
5+
/coverage

build.mjs

+64-124
Original file line numberDiff line numberDiff line change
@@ -28,144 +28,84 @@
2828
* @module
2929
*/
3030

31-
import { CONSTANTS, createContext } from './src/index.ts'
31+
import { createContainer } from './src/index.ts'
32+
import { analyzeMetafile } from 'esbuild'
3233
import fs from 'node:fs'
3334
import path, { basename, dirname, join } from 'node:path'
3435
import { rollup } from 'rollup'
3536
import { dts } from 'rollup-plugin-dts'
3637
import ts from 'typescript'
37-
38-
await defineBuild({
39-
input: ['src/index.ts', 'src/watcher.ts'],
40-
tsconfig: './tsconfig.json',
41-
outdir: 'dist',
42-
tmpDir: '.tmp-build',
43-
dtsInDev: true,
44-
isDev: process.argv.includes('--dev'),
45-
esbuild: {
46-
platform: 'node',
47-
external: ['tiny-glob', 'defu', 'esbuild', 'chokidar'],
38+
import k from 'kleur'
39+
const ctxContainer = createContainer()
40+
41+
ctxContainer.createContext('esm', {
42+
entryPoints: ['src/index.ts'],
43+
format: 'esm',
44+
bundle: true,
45+
platform: 'node',
46+
metafile: true,
47+
logLevel: 'info',
48+
external: ['esbuild', 'chokidar'],
49+
outExtension: {
50+
'.js': '.mjs',
4851
},
49-
}).build()
50-
51-
/**
52-
* @param {object} options
53-
* @param {string} options.input
54-
* @param {string} options.tsconfig
55-
* @param {string} options.outdir
56-
* @param {string} options.tmpDir
57-
* @param {boolean} options.dtsInDev
58-
* @param {boolean} options.isDev
59-
* @param {import("esbuild").BuildOptions} options.esbuild
60-
* @returns
61-
*/
62-
function defineBuild(options) {
63-
return {
64-
...options,
65-
build: async () => {
66-
process.on('SIGINT', function () {
67-
console.log('Cleaning Up')
68-
if (fs.existsSync(options.tmpDir)) {
69-
fs.rmSync(options.tmpDir, { recursive: true, force: true })
70-
}
71-
process.exit()
72-
})
73-
74-
const ctx = await bundleCode({
75-
watch: true,
76-
buildConfig: options,
77-
onRebuild: async () => {
78-
if (options.dtsInDev) {
79-
console.log('Generating Type Bundle')
80-
generateTypes({ buildConfig: options })
81-
await bundleTypes({ buildConfig: options })
82-
}
83-
},
84-
})
85-
if (!options.isDev) {
86-
console.log('Generating Type Bundle')
87-
generateTypes({ buildConfig: options })
88-
await bundleTypes({ buildConfig: options })
89-
fs.rmSync(options.tmpDir, { recursive: true, force: true })
52+
outdir: './dist/esm',
53+
})
54+
55+
ctxContainer.createContext('cjs', {
56+
entryPoints: ['src/index.ts'],
57+
format: 'cjs',
58+
bundle: true,
59+
metafile: true,
60+
external: ['esbuild', 'chokidar'],
61+
platform: 'node',
62+
logLevel: 'info',
63+
outExtension: {
64+
'.js': '.cjs',
65+
},
66+
outdir: './dist/cjs',
67+
})
68+
const tmpDir = './.tmp-build'
69+
70+
if (process.argv.slice(2).includes('--dev')) {
71+
await ctxContainer.dev({
72+
dirs: ['./src'],
73+
async onBuild(result, triggeredBy) {
74+
if (!triggeredBy) {
75+
console.log(k.cyan('Initial Build'))
76+
} else {
77+
console.log(`${triggeredBy.eventLabel}: ${k.cyan(triggeredBy.file)}`)
78+
console.log(k.dim(`Building...`))
9079
}
91-
92-
if (!options.isDev) {
93-
await ctx.dispose()
80+
for (let ctxName in result) {
81+
console.log(`Building context ${k.cyan(ctxName)}`)
82+
console.log(k.dim(await analyzeMetafile(result[ctxName].metafile)))
9483
}
9584
},
85+
})
86+
} else {
87+
const result = await ctxContainer.build()
88+
for (let ctxName in result) {
89+
console.log(`Building context ${k.cyan(ctxName)}`)
90+
console.log(k.dim(await analyzeMetafile(result[ctxName].metafile)))
9691
}
97-
}
98-
99-
async function bundleCode({
100-
watch = false,
101-
buildConfig,
102-
onRebuild = () => {},
103-
} = {}) {
104-
const buildCtx = createContext()
105-
buildCtx.add('cjs', {
106-
...buildConfig.esbuild,
107-
entryPoints: [].concat(buildConfig.input),
108-
format: 'cjs',
109-
bundle: true,
110-
outExtension: {
111-
'.js': '.cjs',
92+
generateTypes({
93+
buildConfig: {
94+
input: ['src/index.ts'],
95+
tsconfig: './tsconfig.json',
96+
tmpDir: tmpDir,
97+
outdir: './dist',
11298
},
113-
outdir: join(buildConfig.outdir, 'cjs'),
11499
})
115-
buildCtx.add('esm', {
116-
...buildConfig.esbuild,
117-
entryPoints: [].concat(buildConfig.input),
118-
format: 'esm',
119-
bundle: true,
120-
outExtension: {
121-
'.js': '.mjs',
100+
await bundleTypes({
101+
buildConfig: {
102+
input: ['src/index.ts'],
103+
tsconfig: './tsconfig.json',
104+
tmpDir: tmpDir,
105+
outdir: './dist',
122106
},
123-
outdir: join(buildConfig.outdir, 'esm'),
124107
})
125-
126-
const debouncedRebuild = (fn => {
127-
let handle
128-
return (...args) => {
129-
if (handle) clearTimeout(handle)
130-
handle = setTimeout(() => {
131-
fn(...args)
132-
}, 500)
133-
}
134-
})(onRebuild)
135-
136-
buildCtx.hook('esm:complete', () => {
137-
process.stdout.write('[custom-builder] ESM Built\n')
138-
debouncedRebuild()
139-
})
140-
141-
buildCtx.hook('cjs:complete', () => {
142-
process.stdout.write('[custom-builder] CJS Built\n')
143-
debouncedRebuild()
144-
})
145-
146-
buildCtx.hook('esm:error', async errors => {
147-
process.stdout.write('[custom-builder] ESM Error:\n')
148-
errors.map(x => console.error(x))
149-
})
150-
151-
buildCtx.hook('cjs:error', async errors => {
152-
process.stdout.write('[custom-builder] CJS Error:\n')
153-
errors.map(x => console.error(x))
154-
})
155-
156-
buildCtx.hook(CONSTANTS.BUILD_COMPLETE, () => {
157-
console.log('Bundled')
158-
})
159-
160-
buildCtx.hook(CONSTANTS.ERROR, errors => {
161-
console.error(errors)
162-
})
163-
164-
if (watch) {
165-
await buildCtx.watch()
166-
}
167-
await buildCtx.build()
168-
return buildCtx
108+
fs.rmSync(tmpDir, { recursive: true })
169109
}
170110

171111
function generateTypes({ buildConfig } = {}) {

0 commit comments

Comments
 (0)