|
28 | 28 | * @module
|
29 | 29 | */
|
30 | 30 |
|
31 |
| -import { CONSTANTS, createContext } from './src/index.ts' |
| 31 | +import { createContainer } from './src/index.ts' |
| 32 | +import { analyzeMetafile } from 'esbuild' |
32 | 33 | import fs from 'node:fs'
|
33 | 34 | import path, { basename, dirname, join } from 'node:path'
|
34 | 35 | import { rollup } from 'rollup'
|
35 | 36 | import { dts } from 'rollup-plugin-dts'
|
36 | 37 | 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', |
48 | 51 | },
|
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...`)) |
90 | 79 | }
|
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))) |
94 | 83 | }
|
95 | 84 | },
|
| 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))) |
96 | 91 | }
|
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', |
112 | 98 | },
|
113 |
| - outdir: join(buildConfig.outdir, 'cjs'), |
114 | 99 | })
|
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', |
122 | 106 | },
|
123 |
| - outdir: join(buildConfig.outdir, 'esm'), |
124 | 107 | })
|
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 }) |
169 | 109 | }
|
170 | 110 |
|
171 | 111 | function generateTypes({ buildConfig } = {}) {
|
|
0 commit comments