jiek 2.0.1 → 2.0.2-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/package.json +8 -5
  2. package/README.md +0 -78
  3. package/bin/jiek-build.js +0 -16
  4. package/bin/jiek.js +0 -13
  5. package/dist/cli-only-build.cjs +0 -743
  6. package/dist/cli-only-build.d.cts +0 -91
  7. package/dist/cli-only-build.d.ts +0 -91
  8. package/dist/cli-only-build.js +0 -735
  9. package/dist/cli.cjs +0 -4888
  10. package/dist/cli.d.cts +0 -67
  11. package/dist/cli.d.ts +0 -67
  12. package/dist/cli.js +0 -4857
  13. package/dist/index.cjs +0 -5
  14. package/dist/index.d.cts +0 -165
  15. package/dist/index.d.ts +0 -165
  16. package/dist/index.js +0 -3
  17. package/dist/rollup/index.cjs +0 -4893
  18. package/dist/rollup/index.d.cts +0 -10
  19. package/dist/rollup/index.d.ts +0 -10
  20. package/dist/rollup/index.js +0 -4880
  21. package/src/cli-only-build.ts +0 -7
  22. package/src/cli.ts +0 -3
  23. package/src/commands/base.ts +0 -18
  24. package/src/commands/build.ts +0 -462
  25. package/src/commands/descriptions.ts +0 -17
  26. package/src/commands/init.ts +0 -373
  27. package/src/commands/meta.ts +0 -5
  28. package/src/commands/publish.ts +0 -204
  29. package/src/index.ts +0 -8
  30. package/src/inner.ts +0 -11
  31. package/src/rollup/base.ts +0 -137
  32. package/src/rollup/index.ts +0 -565
  33. package/src/rollup/plugins/progress.ts +0 -26
  34. package/src/rollup/plugins/skip.ts +0 -21
  35. package/src/rollup/utils/commonOptions.ts +0 -9
  36. package/src/rollup/utils/externalResolver.ts +0 -35
  37. package/src/rollup/utils/globalResolver.ts +0 -13
  38. package/src/rollup/utils/withMinify.ts +0 -18
  39. package/src/utils/filterSupport.ts +0 -91
  40. package/src/utils/getExports.ts +0 -140
  41. package/src/utils/getRoot.ts +0 -16
  42. package/src/utils/getWD.ts +0 -31
  43. package/src/utils/loadConfig.ts +0 -111
  44. package/src/utils/recusiveListFiles.ts +0 -13
  45. package/src/utils/ts.ts +0 -94
  46. package/src/utils/tsRegister.ts +0 -26
@@ -1,7 +0,0 @@
1
- import './utils/filterSupport'
2
- import './commands/base'
3
- import './commands/build'
4
-
5
- import { program } from 'commander'
6
-
7
- program.parse(process.argv)
package/src/cli.ts DELETED
@@ -1,3 +0,0 @@
1
- import './commands/init'
2
- import './commands/publish'
3
- import 'jiek/cli-only-build'
@@ -1,18 +0,0 @@
1
- import { program } from 'commander'
2
- import pkg from 'jiek/package.json'
3
-
4
- import { filterDescription } from '#~/commands/descriptions.ts'
5
- import { IS_WORKSPACE } from '#~/commands/meta.ts'
6
- import { type } from '#~/utils/filterSupport.ts'
7
-
8
- program
9
- .name('jk/jiek')
10
- .version(pkg.version)
11
- .description(`${pkg.description} - Version ${pkg.version}`)
12
- .option('--root <root>', 'The root path of the project')
13
- .option('-c, --config-path <configPath>', 'Custom jiek config path')
14
-
15
- if (type !== '' && IS_WORKSPACE) {
16
- program
17
- .option('-f, --filter <filter>', filterDescription)
18
- }
@@ -1,462 +0,0 @@
1
- import fs from 'node:fs'
2
- import { createRequire } from 'node:module'
3
- import path from 'node:path'
4
-
5
- import { MultiBar, Presets } from 'cli-progress'
6
- import { program } from 'commander'
7
- import { execaCommand } from 'execa'
8
-
9
- import { entriesDescription, filterDescription, outdirDescription } from '#~/commands/descriptions.ts'
10
- import { IS_WORKSPACE } from '#~/commands/meta.ts'
11
- import type { ProjectsGraph } from '#~/utils/filterSupport.ts'
12
- import { filterPackagesGraph, getSelectedProjectsGraph } from '#~/utils/filterSupport.ts'
13
- import { getWD } from '#~/utils/getWD.ts'
14
- import { loadConfig } from '#~/utils/loadConfig.ts'
15
- import { tsRegisterName } from '#~/utils/tsRegister.ts'
16
-
17
- import type { RollupProgressEvent, TemplateOptions } from '../rollup/base'
18
- import { BUILDER_TYPE_PACKAGE_NAME_MAP, BUILDER_TYPES } from '../rollup/base'
19
-
20
- declare module 'jiek' {
21
- export interface Config {
22
- build?: TemplateOptions & {
23
- /**
24
- * Whether to run in silent mode, only active when configured in the workspace root or cwd.
25
- *
26
- * @default false
27
- */
28
- silent?: boolean
29
- }
30
- }
31
- }
32
-
33
- const FILE_TEMPLATE = (manifest: unknown) => (`
34
- module.exports = require('jiek/rollup').template(${JSON.stringify(manifest, null, 2)})
35
- `.trimStart())
36
-
37
- const require = createRequire(import.meta.url)
38
-
39
- const isDefault = process.env.JIEK_IS_ONLY_BUILD === 'true'
40
-
41
- const description = `
42
- Build the package according to the 'exports' field from the package.json.
43
- If you want to rewrite the \`rollup\` command options, you can pass the options after '--'.
44
- ${isDefault ? 'This command is the default command.' : ''}
45
- `.trim()
46
-
47
- interface BuildOptions {
48
- /**
49
- * Auto-detect the builder from the installed dependencies.
50
- * If the builder is not installed, it will prompt the user to install it.
51
- * If exists multiple builders, it will fall back to the 'esbuild'.
52
- */
53
- type?: typeof BUILDER_TYPES[number]
54
- /**
55
- * The output directory of the build, which relative to the target subpackage root directory.
56
- * Support with variables: 'PKG_NAME',
57
- * .e.g. 'dist/{{PKG_NAME}}'.
58
- *
59
- * @default 'dist'
60
- */
61
- outdir: string
62
- watch: boolean
63
- silent: boolean
64
- verbose: boolean
65
- entries?: string
66
- external?: string
67
- noJs: boolean
68
- noDts: boolean
69
- noMin: boolean
70
- /**
71
- * Do not clean the output directory before building.
72
- */
73
- noClean: boolean
74
- onlyMin: boolean
75
- /**
76
- * The type of minify, support 'terser' and 'builder'.
77
- *
78
- * @default 'builder'
79
- */
80
- minType?: string
81
- /**
82
- * The path of the tsconfig file which is used to generate js and dts files.
83
- * If not specified, it will be loaded from:
84
- * - ./tsconfig.json
85
- * - ./tsconfig.dts.json
86
- * - ./tsconfig.build.json
87
- */
88
- tsconfig?: string
89
- /**
90
- * The path of the tsconfig file which is used to generate dts files.
91
- * If not specified, it will be loaded from:
92
- * - ./tsconfig.json
93
- * - ./tsconfig.dts.json
94
- */
95
- dtsconfig?: string
96
- }
97
-
98
- async function checkDependency(dependency: string) {
99
- try {
100
- require.resolve(dependency)
101
- } catch (e) {
102
- console.error(`The package '${dependency}' is not installed, please install it first.`)
103
- const answer = prompt('Do you want to install it now? (Y/n)', 'Y')
104
- const { notWorkspace } = getWD()
105
- if (answer === 'Y') {
106
- await execaCommand(`pnpm install -${notWorkspace ? '' : 'w'}D ${dependency}`)
107
- } else {
108
- return
109
- }
110
- }
111
- }
112
-
113
- let DEFAULT_BUILDER_TYPE: typeof BUILDER_TYPES[number]
114
- Object.entries(BUILDER_TYPE_PACKAGE_NAME_MAP).forEach(([type, packageName]) => {
115
- try {
116
- require.resolve(packageName)
117
- DEFAULT_BUILDER_TYPE = type as typeof BUILDER_TYPES[number]
118
- } catch { /* empty */ }
119
- })
120
- if (!DEFAULT_BUILDER_TYPE!) {
121
- DEFAULT_BUILDER_TYPE = 'esbuild'
122
- }
123
-
124
- function parseBoolean(v?: unknown) {
125
- if (v === undefined) return true
126
- return Boolean(v)
127
- }
128
-
129
- const buildFilterDescription = `
130
- ${filterDescription}
131
- If you pass the --filter option, it will merge into the filters of the command.
132
- `.trim()
133
-
134
- const buildEntriesDescription = `
135
- ${entriesDescription}
136
- If you pass the --entries option, it will merge into the entries of the command.
137
- `.trim()
138
-
139
- const command = isDefault
140
- ? program
141
- .name('jb/jiek-build')
142
- .helpCommand(false)
143
- : program
144
-
145
- if (IS_WORKSPACE) {
146
- if (isDefault) {
147
- command.argument('[filters]', buildFilterDescription)
148
- } else {
149
- command.command('build [filters]')
150
- }
151
- } else {
152
- if (isDefault) {
153
- command.argument('[entries]', buildEntriesDescription)
154
- } else {
155
- command.command('build [entries]')
156
- }
157
- }
158
-
159
- command
160
- .description(description)
161
- .option('-t, --type <TYPE>', `The type of build, support ${BUILDER_TYPES.map(s => `"${s}"`).join(', ')}.`, v => {
162
- if (!BUILDER_TYPES.includes(v as any)) {
163
- throw new Error(`The value of 'type' must be ${BUILDER_TYPES.map(s => `"${s}"`).join(', ')}`)
164
- }
165
- return String(v)
166
- }, 'esbuild')
167
- .option('-o, --outdir <OUTDIR>', outdirDescription, String, 'dist')
168
- .option('-e, --entries <ENTRIES>', entriesDescription)
169
- .option('--external <EXTERNAL>', 'Specify the external dependencies of the package.', String)
170
- .option('-nj, --noJs', 'Do not output js files.', parseBoolean)
171
- .option('-nd, --noDts', 'Do not output dts files.', parseBoolean)
172
- .option('-nm, --noMin', 'Do not output minify files.', parseBoolean)
173
- .option(
174
- '--minType <MINTYPE>',
175
- 'The type of minify, support "builder" and "terser".',
176
- v => {
177
- if (!['builder', 'terser'].includes(v)) {
178
- throw new Error('The value of `minType` must be "builder" or "terser"')
179
- }
180
- return String(v)
181
- }
182
- )
183
- .option('-nc, --noClean', 'Do not clean the output directory before building.', parseBoolean)
184
- .option(
185
- '-om, --onlyMin',
186
- 'Only output minify files, but dts files will still be output, it only replaces the js files.',
187
- parseBoolean
188
- )
189
- .option('--tsconfig <TSCONFIG>', 'The path of the tsconfig file which is used to generate js and dts files.', String)
190
- .option('--dtsconfig <DTSCONFIG>', 'The path of the tsconfig file which is used to generate dts files.', String)
191
- .option('-w, --watch', 'Watch the file changes.', parseBoolean)
192
- .option('-s, --silent', "Don't display logs.", parseBoolean)
193
- .option('-v, --verbose', 'Display debug logs.', parseBoolean)
194
- .action(async (commandFiltersOrEntries: string | undefined, options: BuildOptions) => {
195
- /* eslint-disable prefer-const */
196
- let {
197
- type,
198
- outdir,
199
- watch,
200
- silent,
201
- verbose,
202
- entries: optionEntries,
203
- external,
204
- noJs: withoutJs,
205
- noDts: withoutDts,
206
- noMin: withoutMin,
207
- minType: minifyType,
208
- noClean,
209
- onlyMin,
210
- tsconfig,
211
- dtsconfig
212
- } = options
213
- /* eslint-enable prefer-const */
214
- const resolvedType = type ?? DEFAULT_BUILDER_TYPE
215
- if (!withoutJs) {
216
- await checkDependency(BUILDER_TYPE_PACKAGE_NAME_MAP[resolvedType]!)
217
- if (minifyType === 'builder') {
218
- minifyType = resolvedType
219
- }
220
- }
221
- if (!withoutMin) {
222
- await checkDependency(
223
- {
224
- ...BUILDER_TYPE_PACKAGE_NAME_MAP,
225
- terser: '@rollup/plugin-terser'
226
- }[resolvedType]!
227
- )
228
- }
229
- let shouldPassThrough = false
230
-
231
- const passThroughOptions = process.argv
232
- .reduce(
233
- (acc, value) => {
234
- if (shouldPassThrough) {
235
- acc.push(value)
236
- }
237
- if (value === '--') {
238
- shouldPassThrough = true
239
- }
240
- return acc
241
- },
242
- [] as string[]
243
- )
244
- const { build } = loadConfig()
245
- silent = silent ?? build?.silent ?? false
246
-
247
- if (withoutMin && onlyMin) {
248
- throw new Error('Cannot use both --without-minify and --only-minify')
249
- }
250
- if (onlyMin && withoutJs) {
251
- throw new Error('Cannot use --without-js and --only-minify at the same time')
252
- }
253
-
254
- let entries: string | undefined = [
255
- optionEntries,
256
- IS_WORKSPACE ? undefined : commandFiltersOrEntries
257
- ].filter(Boolean).join(',')
258
- if (entries.length === 0) {
259
- entries = undefined
260
- }
261
- const env = {
262
- ...process.env,
263
- JIEK_BUILDER: type,
264
- JIEK_OUT_DIR: outdir,
265
- JIEK_CLEAN: String(!noClean),
266
- JIEK_ENTRIES: entries,
267
- JIEK_EXTERNAL: external,
268
- JIEK_WITHOUT_JS: String(withoutJs),
269
- JIEK_WITHOUT_DTS: String(withoutDts),
270
- JIEK_WITHOUT_MINIFY: String(withoutMin),
271
- JIEK_ONLY_MINIFY: String(onlyMin),
272
- JIEK_MINIFY_TYPE: minifyType,
273
- JIEK_TSCONFIG: tsconfig,
274
- JIEK_DTSCONFIG: dtsconfig
275
- }
276
-
277
- const multiBars = new MultiBar({
278
- clearOnComplete: false,
279
- hideCursor: true,
280
- format: '- {bar} | {status} | {pkgName} | {input} | {message}'
281
- }, Presets.shades_classic)
282
-
283
- const buildPackage = async ({
284
- wd,
285
- value = {}
286
- }: ProjectsGraph) => {
287
- if (Object.keys(value).length === 0) {
288
- throw new Error('no package found')
289
- }
290
- const wdNodeModules = path.resolve(wd, 'node_modules')
291
- if (!fs.existsSync(wdNodeModules)) {
292
- fs.mkdirSync(wdNodeModules)
293
- }
294
- const jiekTempDir = (...paths: string[]) => path.resolve(wdNodeModules, '.jiek', ...paths)
295
- if (!fs.existsSync(jiekTempDir())) {
296
- fs.mkdirSync(jiekTempDir())
297
- }
298
-
299
- const rollupBinaryPath = require.resolve('rollup')
300
- .replace(/dist\/rollup.js$/, 'dist/bin/rollup')
301
- let i = 0
302
- await Promise.all(
303
- Object.entries(value).map(async ([dir, manifest]) => {
304
- if (!manifest.name) {
305
- throw new Error('package.json must have a name field')
306
- }
307
-
308
- // TODO support auto build child packages in workspaces
309
- const escapeManifestName = manifest.name.replace(/^@/g, '').replace(/\//g, '+')
310
- const configFile = jiekTempDir(
311
- `${escapeManifestName ?? `anonymous-${i++}`}.rollup.config.js`
312
- )
313
- fs.writeFileSync(configFile, FILE_TEMPLATE(manifest))
314
- const command = [rollupBinaryPath, '--silent', '-c', configFile]
315
- if (tsRegisterName) {
316
- command.unshift(`node -r ${tsRegisterName}`)
317
- }
318
- if (watch) {
319
- command.push('--watch')
320
- }
321
- command.push(...passThroughOptions)
322
- const child = execaCommand(command.join(' '), {
323
- ipc: true,
324
- cwd: dir,
325
- env: {
326
- ...env,
327
- JIEK_NAME: manifest.name,
328
- JIEK_ROOT: wd
329
- }
330
- })
331
- const bars: Record<string, ReturnType<typeof multiBars.create>> = {}
332
- const times: Record<string, number> = {}
333
- const locks: Record<string, boolean> = {}
334
- let inputMaxLen = 10
335
- child.on('message', (e: RollupProgressEvent) => {
336
- if (e.type === 'debug') console.log(...(Array.isArray(e.data) ? e.data : [e.data]))
337
- })
338
- !silent && child.on('message', (e: RollupProgressEvent) => {
339
- if (e.type === 'init') {
340
- const { leafMap, targetsLength } = e.data
341
- const leafs = Array
342
- .from(leafMap.entries())
343
- .flatMap(([input, pathAndCondiions]) =>
344
- pathAndCondiions.map(([path, ...conditions]) => ({
345
- input,
346
- path,
347
- conditions
348
- }))
349
- )
350
- let initMessage = `Package '${manifest.name}' has ${targetsLength} targets to build`
351
- if (watch) {
352
- initMessage += ' and watching...'
353
- }
354
- console.log(initMessage)
355
- leafs.forEach(({ input }) => {
356
- inputMaxLen = Math.max(inputMaxLen, input.length)
357
- })
358
- leafs.forEach(({ input, path }) => {
359
- const key = `${input}:${path}`
360
- if (bars[key]) return
361
- bars[key] = multiBars.create(50, 0, {
362
- pkgName: manifest.name,
363
- input: input.padEnd(inputMaxLen + 5),
364
- status: 'waiting'.padEnd(10)
365
- }, {
366
- barsize: 20,
367
- linewrap: true
368
- })
369
- })
370
- }
371
- if (e.type === 'progress') {
372
- const {
373
- path,
374
- tags,
375
- input,
376
- event,
377
- message
378
- } = e.data
379
- const bar = bars[`${input}:${path}`]
380
- if (!bar) return
381
- const time = times[`${input}:${path}`]
382
- bar.update(
383
- {
384
- start: 0,
385
- resolve: 20,
386
- end: 50
387
- }[event ?? 'start'] ?? 0,
388
- {
389
- input: (
390
- time
391
- ? `${input}(x${time.toString().padStart(2, '0')})`
392
- : input
393
- ).padEnd(inputMaxLen + 5),
394
- status: event?.padEnd(10),
395
- message: `${tags?.join(', ')}: ${message}`
396
- }
397
- )
398
- }
399
- if (e.type === 'watchChange') {
400
- const {
401
- path,
402
- input
403
- } = e.data
404
- const key = `${input}:${path}`
405
- const bar = bars[key]
406
- if (!bar) return
407
- let time = times[key] ?? 1
408
- if (!locks[key]) {
409
- time += 1
410
- times[key] = time
411
- setTimeout(() => {
412
- locks[key] = false
413
- }, 100)
414
- bar.update(0, {
415
- input: `${input}(x${time.toString().padStart(2, '0')})`.padEnd(inputMaxLen + 5),
416
- status: 'watching'.padEnd(10),
417
- message: 'watching...'
418
- })
419
- }
420
- locks[key] = true
421
- }
422
- })
423
- await new Promise<void>((resolve, reject) => {
424
- let errorStr = ''
425
- child.stderr?.on('data', (data) => {
426
- errorStr += data
427
- })
428
- child.once('exit', (code) =>
429
- code === 0
430
- ? resolve()
431
- : reject(new Error(`rollup build failed:\n${errorStr}`)))
432
- verbose && child.stdout?.pipe(process.stdout)
433
- })
434
- })
435
- )
436
- }
437
- const commandFilters = IS_WORKSPACE ? commandFiltersOrEntries : undefined
438
- const filters = [
439
- ...new Set([
440
- ...(program.getOptionValue('filter') as string | undefined)
441
- ?.split(',')
442
- .map(s => s.trim())
443
- .filter(s => s.length > 0)
444
- ?? [],
445
- ...commandFilters
446
- ?.split(',')
447
- .map(s => s.trim())
448
- .filter(s => s.length > 0)
449
- ?? []
450
- ])
451
- ]
452
- try {
453
- if (filters.length > 0) {
454
- const packages = await filterPackagesGraph(filters)
455
- await Promise.all(packages.map(buildPackage))
456
- } else {
457
- await buildPackage(await getSelectedProjectsGraph())
458
- }
459
- } finally {
460
- multiBars.stop()
461
- }
462
- })
@@ -1,17 +0,0 @@
1
- export const entriesDescription = `
2
- Specify the build entry-points of the package.json's 'exports' field.
3
- Support glob pattern and array.
4
- .e.g. '.', './*', './sub/*', './a,./b'.
5
- `.trim()
6
-
7
- export const filterDescription = `
8
- Filter the packages from the workspace.
9
- Support fuzzy match and array.
10
- .e.g. 'core,utils'.
11
- `.trim()
12
-
13
- export const outdirDescription = `
14
- The output directory of the build, which relative to the target subpackage root directory.
15
- Support with variables: 'PKG_NAME',
16
- .e.g. 'dist/{{PKG_NAME}}'.
17
- `.trim()