jiek 2.2.5 → 2.2.7-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 (136) hide show
  1. package/bin/build.cjs +2 -0
  2. package/bin/index.cjs +2 -0
  3. package/bin-helper/index.cjs +31 -0
  4. package/bin-helper/index.d.cts +1 -0
  5. package/bin-helper/index.d.ts +1 -0
  6. package/bin-helper/package.json +5 -1
  7. package/dist/.internal/+/bin/parseArgv.cjs +33 -0
  8. package/dist/.internal/+/bin/parseArgv.d.cts +5 -0
  9. package/dist/.internal/+/bin/parseArgv.d.ts +5 -0
  10. package/dist/.internal/+/bin/parseArgv.js +27 -0
  11. package/dist/.internal/+/bridge.cjs +12 -0
  12. package/dist/.internal/+/bridge.d.cts +39 -0
  13. package/dist/.internal/+/bridge.d.ts +39 -0
  14. package/dist/.internal/+/bridge.js +9 -0
  15. package/dist/.internal/+/commands/build/analyzer.cjs +201 -0
  16. package/dist/.internal/+/commands/build/analyzer.d.cts +30 -0
  17. package/dist/.internal/+/commands/build/analyzer.d.ts +30 -0
  18. package/dist/.internal/+/commands/build/analyzer.js +195 -0
  19. package/dist/.internal/+/commands/descriptions.cjs +21 -0
  20. package/dist/.internal/+/commands/descriptions.d.cts +5 -0
  21. package/dist/.internal/+/commands/descriptions.d.ts +5 -0
  22. package/dist/.internal/+/commands/descriptions.js +17 -0
  23. package/dist/.internal/+/commands/meta.cjs +16 -0
  24. package/dist/.internal/+/commands/meta.d.cts +3 -0
  25. package/dist/.internal/+/commands/meta.d.ts +3 -0
  26. package/dist/.internal/+/commands/meta.js +15 -0
  27. package/dist/.internal/+/commands/utils/optionParser.cjs +8 -0
  28. package/dist/.internal/+/commands/utils/optionParser.d.cts +3 -0
  29. package/dist/.internal/+/commands/utils/optionParser.d.ts +3 -0
  30. package/dist/.internal/+/commands/utils/optionParser.js +6 -0
  31. package/dist/.internal/+/rollup/base.cjs +10 -0
  32. package/dist/{cli-only-build.d.cts → .internal/+/rollup/base.d.cts} +8 -15
  33. package/dist/{cli-only-build.d.ts → .internal/+/rollup/base.d.ts} +8 -15
  34. package/dist/.internal/+/rollup/base.js +7 -0
  35. package/dist/.internal/+/rollup/bundle-analyzer.cjs +51 -0
  36. package/dist/.internal/+/rollup/bundle-analyzer.d.cts +92 -0
  37. package/dist/.internal/+/rollup/bundle-analyzer.d.ts +92 -0
  38. package/dist/.internal/+/rollup/bundle-analyzer.js +45 -0
  39. package/dist/.internal/+/server.cjs +33 -0
  40. package/dist/.internal/+/server.d.cts +8 -0
  41. package/dist/.internal/+/server.d.ts +8 -0
  42. package/dist/.internal/+/server.js +27 -0
  43. package/dist/.internal/+/utils/checkDependency.cjs +39 -0
  44. package/dist/.internal/+/utils/checkDependency.d.cts +3 -0
  45. package/dist/.internal/+/utils/checkDependency.d.ts +3 -0
  46. package/dist/.internal/+/utils/checkDependency.js +34 -0
  47. package/dist/.internal/+/utils/filterSupport.cjs +20 -0
  48. package/dist/.internal/+/utils/filterSupport.d.cts +15 -0
  49. package/dist/.internal/+/utils/filterSupport.d.ts +15 -0
  50. package/dist/.internal/+/utils/filterSupport.js +10 -0
  51. package/dist/.internal/+/utils/getInternalModuleName.cjs +5 -0
  52. package/dist/.internal/+/utils/getInternalModuleName.d.cts +3 -0
  53. package/dist/.internal/+/utils/getInternalModuleName.d.ts +3 -0
  54. package/dist/.internal/+/utils/getInternalModuleName.js +3 -0
  55. package/dist/.internal/+/utils/getRoot.cjs +17 -0
  56. package/dist/.internal/+/utils/getRoot.d.cts +3 -0
  57. package/dist/.internal/+/utils/getRoot.d.ts +3 -0
  58. package/dist/.internal/+/utils/getRoot.js +11 -0
  59. package/dist/.internal/+/utils/getWD.cjs +15 -0
  60. package/dist/.internal/+/utils/getWD.d.cts +6 -0
  61. package/dist/.internal/+/utils/getWD.d.ts +6 -0
  62. package/dist/.internal/+/utils/getWD.js +10 -0
  63. package/dist/.internal/+/utils/intersection.cjs +5 -0
  64. package/dist/.internal/+/utils/intersection.d.cts +3 -0
  65. package/dist/.internal/+/utils/intersection.d.ts +3 -0
  66. package/dist/.internal/+/utils/intersection.js +3 -0
  67. package/dist/.internal/+/utils/loadConfig.cjs +100 -0
  68. package/dist/.internal/+/utils/loadConfig.d.cts +10 -0
  69. package/dist/.internal/+/utils/loadConfig.d.ts +10 -0
  70. package/dist/.internal/+/utils/loadConfig.js +94 -0
  71. package/dist/.internal/+/utils/recursiveListFiles.cjs +19 -0
  72. package/dist/.internal/+/utils/recursiveListFiles.d.cts +3 -0
  73. package/dist/.internal/+/utils/recursiveListFiles.d.ts +3 -0
  74. package/dist/.internal/+/utils/recursiveListFiles.js +13 -0
  75. package/dist/.internal/+/utils/resolveExports.cjs +118 -0
  76. package/dist/.internal/+/utils/resolveExports.d.cts +26 -0
  77. package/dist/.internal/+/utils/resolveExports.d.ts +26 -0
  78. package/dist/.internal/+/utils/resolveExports.js +111 -0
  79. package/dist/.internal/+/utils/ts.cjs +68 -0
  80. package/dist/.internal/+/utils/ts.d.cts +3 -0
  81. package/dist/.internal/+/utils/ts.d.ts +3 -0
  82. package/dist/.internal/+/utils/ts.js +62 -0
  83. package/dist/.internal/+/utils/tsRegister.cjs +25 -0
  84. package/dist/.internal/+/utils/tsRegister.d.cts +3 -0
  85. package/dist/.internal/+/utils/tsRegister.d.ts +3 -0
  86. package/dist/.internal/+/utils/tsRegister.js +26 -0
  87. package/dist/.internal/getWD-BRJ3PK1S.js +105 -0
  88. package/dist/.internal/getWD-Cmxzjf-f.js +95 -0
  89. package/dist/{cli.js → .internal/index-B6RQz1DZ.js} +1 -731
  90. package/dist/{cli.cjs → .internal/index-DlHFuTjM.js} +1 -753
  91. package/dist/.internal/jiek_create-require-CWFWNQHj.js +5 -0
  92. package/dist/.internal/jiek_create-require-CxSGbkTB.js +5 -0
  93. package/dist/.internal/package.json +10 -0
  94. package/dist/bin/build.cjs +505 -0
  95. package/dist/bin/index.cjs +488 -0
  96. package/dist/index.d.cts +1 -113
  97. package/dist/index.d.ts +1 -113
  98. package/dist/rollup/index.cjs +282 -4569
  99. package/dist/rollup/index.d.cts +7 -1
  100. package/dist/rollup/index.d.ts +7 -1
  101. package/dist/rollup/index.js +270 -4554
  102. package/package.json +25 -28
  103. package/src/bin/build.cts +11 -0
  104. package/src/bin/index.cts +6 -0
  105. package/src/bridge.ts +1 -1
  106. package/src/commands/base.ts +3 -3
  107. package/src/commands/build/analyzer.ts +5 -5
  108. package/src/commands/build/client/analyzer.tsx +1 -1
  109. package/src/commands/build/client/index.ts +5 -7
  110. package/src/commands/build.ts +218 -209
  111. package/src/commands/meta.ts +1 -1
  112. package/src/commands/publish.ts +101 -31
  113. package/src/rollup/base.ts +2 -2
  114. package/src/rollup/index.ts +309 -115
  115. package/src/rollup/plugins/with-external.ts +23 -0
  116. package/src/rollup/utils/externalResolver.ts +20 -8
  117. package/src/utils/checkDependency.ts +1 -1
  118. package/src/utils/filterSupport.ts +17 -16
  119. package/src/utils/getInternalModuleName.ts +5 -0
  120. package/src/utils/intersection.ts +1 -0
  121. package/src/utils/{recusiveListFiles.ts → recursiveListFiles.ts} +2 -2
  122. package/src/utils/{getExports.ts → resolveExports.ts} +16 -14
  123. package/bin/jiek-build.js +0 -16
  124. package/bin/jiek.js +0 -13
  125. package/bin-helper.cjs +0 -43
  126. package/cli/package.json +0 -1
  127. package/cli-only-build/package.json +0 -1
  128. package/dist/cli-only-build.cjs +0 -977
  129. package/dist/cli-only-build.js +0 -969
  130. package/dist/cli.d.cts +0 -14
  131. package/dist/cli.d.ts +0 -14
  132. package/src/bin/build.ts +0 -0
  133. package/src/cli-only-build.ts +0 -11
  134. package/src/cli.ts +0 -6
  135. /package/{bin-helper.js → bin-helper/index.js} +0 -0
  136. /package/src/{parseArgv.ts → bin/parseArgv.ts} +0 -0
@@ -11,30 +11,27 @@ import commonjs from '@rollup/plugin-commonjs'
11
11
  import inject from '@rollup/plugin-inject'
12
12
  import json from '@rollup/plugin-json'
13
13
  import { nodeResolve } from '@rollup/plugin-node-resolve'
14
- import { isMatch } from 'micromatch'
15
14
  import type { InputPluginOption, OutputOptions, OutputPlugin, OutputPluginOption, Plugin, RollupOptions } from 'rollup'
16
15
  import ts from 'typescript'
17
16
 
18
- import type { RollupBuildEntryCtx, RollupBuildEventMap } from '#~/bridge.ts'
19
- import { publish } from '#~/bridge.ts'
20
- import { bundleAnalyzer } from '#~/rollup/bundle-analyzer.ts'
21
- import { getExports, getOutDirs } from '#~/utils/getExports.ts'
22
- import { loadConfig } from '#~/utils/loadConfig.ts'
23
- import { recusiveListFiles } from '#~/utils/recusiveListFiles.ts'
24
- import { getCompilerOptionsByFilePath } from '#~/utils/ts.ts'
17
+ import type { RollupBuildEntryCtx, RollupBuildEventMap } from '#~/bridge'
18
+ import { publish } from '#~/bridge'
19
+ import { bundleAnalyzer } from '#~/rollup/bundle-analyzer'
20
+ import { getInternalModuleName } from '#~/utils/getInternalModuleName'
21
+ import { intersection } from '#~/utils/intersection'
22
+ import { loadConfig } from '#~/utils/loadConfig'
23
+ import { recursiveListFiles } from '#~/utils/recursiveListFiles'
24
+ import { getOutDirs, resolveExports } from '#~/utils/resolveExports'
25
+ import { getCompilerOptionsByFilePath } from '#~/utils/ts'
25
26
 
26
27
  import type { ConfigGenerateContext, TemplateOptions } from './base'
27
28
  import createRequire, { CREATE_REQUIRE_VIRTUAL_MODULE_NAME } from './plugins/create-require'
28
29
  import progress from './plugins/progress'
29
30
  import skip from './plugins/skip'
31
+ import withExternal from './plugins/with-external.ts'
32
+ import type { PackageJSON } from './utils/externalResolver'
30
33
  import externalResolver from './utils/externalResolver'
31
34
 
32
- interface PackageJSON {
33
- name?: string
34
- type?: string
35
- exports?: Record<string, unknown> | string | string[]
36
- }
37
-
38
35
  const {
39
36
  JIEK_ROOT,
40
37
  JIEK_NAME,
@@ -48,7 +45,8 @@ const {
48
45
  JIEK_CLEAN,
49
46
  JIEK_ONLY_MINIFY,
50
47
  JIEK_TSCONFIG,
51
- JIEK_DTSCONFIG
48
+ JIEK_DTSCONFIG,
49
+ JIEK_FEATURES
52
50
  } = process.env
53
51
 
54
52
  const resolveArrayString = (str: string | undefined) => {
@@ -74,6 +72,8 @@ const COMMON_PLUGINS = [
74
72
  json()
75
73
  ]
76
74
 
75
+ const INTERNAL_MODULE_NAME = getInternalModuleName(JIEK_NAME!)
76
+
77
77
  const WITHOUT_JS = JIEK_WITHOUT_JS === 'true'
78
78
  const WITHOUT_DTS = JIEK_WITHOUT_DTS === 'true'
79
79
  const WITHOUT_MINIFY = JIEK_WITHOUT_MINIFY === 'true'
@@ -82,6 +82,8 @@ const ONLY_MINIFY = JIEK_ONLY_MINIFY === 'true'
82
82
 
83
83
  const CLEAN = JIEK_CLEAN === 'true'
84
84
 
85
+ const FEATURES = JSON.parse(JIEK_FEATURES ?? '{}') as Record<string, unknown>
86
+
85
87
  const MINIFY_DEFAULT_VALUE = WITHOUT_MINIFY
86
88
  ? false
87
89
  : ONLY_MINIFY
@@ -116,6 +118,10 @@ if (CLEAN) {
116
118
 
117
119
  const STYLE_REGEXP = /\.(css|s[ac]ss|less|styl)$/
118
120
 
121
+ const CWD_FILES = recursiveListFiles(process.cwd())
122
+ .filter(p => /(?<!\.d)\.[cm]?tsx?$/.test(p))
123
+ .map(p => relative(process.cwd(), p))
124
+
119
125
  const resolveBuildPlugins = (context: ConfigGenerateContext, plugins: TemplateOptions['plugins']): {
120
126
  js: InputPluginOption
121
127
  dts: InputPluginOption
@@ -197,9 +203,12 @@ const withMinify = (
197
203
  output: OutputOptions & {
198
204
  plugins?: OutputPlugin[]
199
205
  },
206
+ disableMinify?: boolean,
200
207
  onlyOncePlugins: OutputPluginOption[] = []
201
208
  ): OutputOptions[] => {
202
- const minify = build?.output?.minify ?? MINIFY_DEFAULT_VALUE
209
+ const minify = disableMinify !== undefined
210
+ ? !disableMinify
211
+ : build?.output?.minify ?? MINIFY_DEFAULT_VALUE
203
212
 
204
213
  output.plugins = output.plugins ?? []
205
214
  const notOnlyOncePlugins = [...output.plugins]
@@ -226,7 +235,7 @@ const withMinify = (
226
235
  typeof output.entryFileNames === 'function'
227
236
  ? output.entryFileNames(chunkInfo)
228
237
  : (() => {
229
- throw new Error('entryFileNames must be a function')
238
+ throw new TypeError('entryFileNames must be a function')
230
239
  })(),
231
240
  plugins: [
232
241
  ...output.plugins,
@@ -253,7 +262,25 @@ const withMinify = (
253
262
  ]
254
263
  }
255
264
 
256
- const generateConfigs = (context: ConfigGenerateContext, options: TemplateOptions = {}): RollupOptions[] => {
265
+ interface GenerateConfigsOptions {
266
+ internalModuleCollect?: (id: string) => void
267
+ commonPlugins?: InputPluginOption[]
268
+ disableDTS?: boolean
269
+ disableMinify?: boolean
270
+ disableCollectInternalModule?: boolean
271
+ }
272
+
273
+ const generateConfigs = (
274
+ context: ConfigGenerateContext,
275
+ {
276
+ internalModuleCollect,
277
+ commonPlugins: inputCommonPlugins = [],
278
+ disableDTS = false,
279
+ disableMinify,
280
+ disableCollectInternalModule
281
+ }: GenerateConfigsOptions = {}
282
+ ): RollupOptions[] => {
283
+ const buildOptions: TemplateOptions = build
257
284
  const {
258
285
  path,
259
286
  name,
@@ -263,7 +290,7 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
263
290
  pkgIsModule,
264
291
  conditionals
265
292
  } = context
266
- const external = [...inputExternal, ...(options.external ?? []), ...(commandExternal ?? [])]
293
+ const external = [...inputExternal, ...(buildOptions.external ?? []), ...(commandExternal ?? [])]
267
294
  const isModule = conditionals.includes('import')
268
295
  const isCommonJS = conditionals.includes('require')
269
296
  const isBrowser = conditionals.includes('browser')
@@ -314,6 +341,23 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
314
341
  delete compilerOptions.composite
315
342
  }
316
343
  const exportConditions = [...conditionals, ...(compilerOptions.customConditions ?? [])]
344
+ const nodeResolvePluginInstance = nodeResolve({
345
+ exportConditions,
346
+ extensions: [
347
+ '.js',
348
+ '.cjs',
349
+ '.mjs',
350
+ '.jsx',
351
+ '.cjsx',
352
+ '.mjsx',
353
+ '.ts',
354
+ '.cts',
355
+ '.mts',
356
+ '.tsx',
357
+ '.ctsx',
358
+ '.mtsx'
359
+ ]
360
+ })
317
361
  const publishInEntry = <K extends keyof RollupBuildEventMap>(
318
362
  type: K,
319
363
  data: Omit<RollupBuildEventMap[K], keyof RollupBuildEntryCtx>
@@ -336,12 +380,15 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
336
380
  'input should not include "**", please read the [documentation](https://nodejs.org/api/packages.html#subpath-patterns).'
337
381
  )
338
382
  }
383
+ const reg = new RegExp(`^${
384
+ input
385
+ .slice(2)
386
+ .replace(/\./g, '\\.')
387
+ .replace(/\*/g, '.*')
388
+ }$`)
339
389
  const inputObj = !input.includes('*')
340
390
  ? input
341
- : recusiveListFiles(process.cwd())
342
- .filter(p => /(?<!\.d)\.[cm]?tsx?$/.test(p))
343
- .map(p => relative(process.cwd(), p))
344
- .filter(p => isMatch(p, input.slice(2)))
391
+ : CWD_FILES.filter(p => reg.test(p))
345
392
  const globCommonDir = input.includes('*')
346
393
  ? input.split('*')[0].replace('./', '')
347
394
  : ''
@@ -359,32 +406,59 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
359
406
  const { js: jsOutput, dts: dtsOutput } = resolveOutputControls(context, build.output)
360
407
  const rollupOptions: RollupOptions[] = []
361
408
 
362
- const commonPlugins: Plugin[] = [
363
- nodeResolve({
364
- exportConditions,
365
- extensions: [
366
- '.js',
367
- '.cjs',
368
- '.mjs',
369
- '.jsx',
370
- '.cjsx',
371
- '.mjsx',
372
- '.ts',
373
- '.cts',
374
- '.mts',
375
- '.tsx',
376
- '.ctsx',
377
- '.mtsx'
378
- ]
379
- })
409
+ const commonPlugins: InputPluginOption[] = [
410
+ ...inputCommonPlugins,
411
+ withExternal(),
412
+ !disableCollectInternalModule && {
413
+ name: 'jiek:collect-internal-module',
414
+ resolveId: {
415
+ order: 'pre',
416
+ async handler(source, importer, options) {
417
+ if (!source.startsWith('#')) return
418
+
419
+ if (!nodeResolvePluginInstance.resolveId || !('handler' in nodeResolvePluginInstance.resolveId)) {
420
+ throw new Error('nodeResolvePluginInstance.resolveId is not a plugin instance')
421
+ }
422
+ let resolved = await nodeResolvePluginInstance
423
+ .resolveId
424
+ .handler
425
+ .call(this, source, importer, options)
426
+ if (typeof resolved === 'string') {
427
+ resolved = { id: resolved }
428
+ }
429
+ if (!resolved || !('id' in resolved)) {
430
+ throw new Error('nodeResolvePluginInstance.resolveId.handler did not return a resolved object')
431
+ }
432
+ internalModuleCollect?.(relative(process.cwd(), resolved.id))
433
+ return {
434
+ id: source
435
+ .replaceAll('#', `${INTERNAL_MODULE_NAME}/`)
436
+ .replaceAll('~', '+'),
437
+ external: true
438
+ }
439
+ }
440
+ }
441
+ }
380
442
  ]
443
+ const features = Object.assign(
444
+ {
445
+ keepImportAttributes: false
446
+ },
447
+ build.features,
448
+ FEATURES
449
+ )
450
+ const importAttributesKey = (
451
+ features.keepImportAttributes === false
452
+ || features.keepImportAttributes === undefined
453
+ )
454
+ ? undefined
455
+ : features.keepImportAttributes === true
456
+ ? 'with'
457
+ : features.keepImportAttributes
381
458
  if (jsOutput && !WITHOUT_JS) {
382
- const sourcemap = typeof options?.output?.sourcemap === 'object'
383
- ? options.output.sourcemap.js
384
- : options?.output?.sourcemap
385
- const features = Object.assign({
386
- keepImportAttributes: true
387
- }, build.features)
459
+ const sourcemap = typeof buildOptions?.output?.sourcemap === 'object'
460
+ ? buildOptions.output.sourcemap.js
461
+ : buildOptions?.output?.sourcemap
388
462
  const builder = resolvedBuilderOptions.type === 'esbuild'
389
463
  ? import('rollup-plugin-esbuild').then(({ default: esbuild }) =>
390
464
  esbuild({
@@ -402,8 +476,8 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
402
476
  },
403
477
  ...noTypeResolvedBuilderOptions,
404
478
  supported: {
405
- 'import-attributes': features.keepImportAttributes !== false,
406
- ...resolvedBuilderOptions.supported
479
+ ...resolvedBuilderOptions.supported,
480
+ 'import-attributes': true
407
481
  }
408
482
  })
409
483
  )
@@ -431,7 +505,7 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
431
505
  },
432
506
  experimental: {
433
507
  ...resolvedBuilderOptions.jsc?.experimental,
434
- keepImportAttributes: features.keepImportAttributes !== false
508
+ keepImportAttributes: true
435
509
  }
436
510
  }
437
511
  })
@@ -444,36 +518,36 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
444
518
  input: inputObj,
445
519
  external,
446
520
  output: [
447
- ...withMinify({
448
- dir: jsOutdir,
449
- name,
450
- interop: 'auto',
451
- entryFileNames: (chunkInfo) => (
452
- Array.isArray(inputObj)
453
- ? chunkInfo.facadeModuleId!.replace(`${process.cwd()}/`, '')
454
- .replace(globCommonDir, pathCommonDir)
455
- .replace(/(\.[cm]?)ts$/, jsOutputSuffix)
456
- : output.replace(`${jsOutdir}/`, '')
457
- ),
458
- sourcemap,
459
- format,
460
- strict: typeof options?.output?.strict === 'object'
461
- ? options.output.strict.js
462
- : options?.output?.strict,
463
- externalImportAttributes: features.keepImportAttributes !== false,
464
- importAttributesKey: (
465
- features.keepImportAttributes === false
466
- || features.keepImportAttributes === undefined
467
- )
468
- ? undefined
469
- : features.keepImportAttributes === true
470
- ? 'with'
471
- : features.keepImportAttributes,
472
- plugins: []
473
- }, onlyOncePlugins)
521
+ ...withMinify(
522
+ {
523
+ dir: jsOutdir,
524
+ name,
525
+ interop: 'auto',
526
+ entryFileNames: (chunkInfo) => {
527
+ return Array.isArray(inputObj)
528
+ ? chunkInfo.facadeModuleId!
529
+ .replace(`${process.cwd()}/`, '')
530
+ .replace(globCommonDir, pathCommonDir)
531
+ .replace(/(\.[cm]?)ts$/, jsOutputSuffix)
532
+ : output.replace(`${jsOutdir}/`, '')
533
+ },
534
+ sourcemap,
535
+ format,
536
+ strict: typeof buildOptions?.output?.strict === 'object'
537
+ ? buildOptions.output.strict.js
538
+ : buildOptions?.output?.strict,
539
+ externalImportAttributes: features.keepImportAttributes === true
540
+ ? true
541
+ : features.keepImportAttributes,
542
+ importAttributesKey
543
+ },
544
+ disableMinify,
545
+ onlyOncePlugins
546
+ )
474
547
  ],
475
548
  plugins: [
476
549
  ...commonPlugins,
550
+ nodeResolvePluginInstance,
477
551
  import('rollup-plugin-postcss')
478
552
  .then(({ default: postcss }) =>
479
553
  postcss({
@@ -502,10 +576,10 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
502
576
  })
503
577
  }
504
578
 
505
- if (dtsOutput && !WITHOUT_DTS) {
506
- const sourcemap = typeof options?.output?.sourcemap === 'object'
507
- ? options.output.sourcemap.dts
508
- : options?.output?.sourcemap
579
+ if (!disableDTS && dtsOutput && !WITHOUT_DTS) {
580
+ const sourcemap = typeof buildOptions?.output?.sourcemap === 'object'
581
+ ? buildOptions.output.sourcemap.dts
582
+ : buildOptions?.output?.sourcemap
509
583
  rollupOptions.push({
510
584
  input: inputObj,
511
585
  external,
@@ -522,9 +596,11 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
522
596
  .replace(`${jsOutdir}/`, '')
523
597
  .replace(/(\.[cm]?)js$/, tsOutputSuffix)
524
598
  ),
525
- strict: typeof options?.output?.strict === 'object'
526
- ? options.output.strict.dts
527
- : options?.output?.strict
599
+ strict: typeof buildOptions?.output?.strict === 'object'
600
+ ? buildOptions.output.strict.dts
601
+ : buildOptions?.output?.strict,
602
+ externalImportAttributes: features.keepImportAttributes !== false,
603
+ importAttributesKey
528
604
  }
529
605
  ],
530
606
  plugins: [
@@ -566,57 +642,174 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
566
642
  }
567
643
 
568
644
  export function template(packageJSON: PackageJSON): RollupOptions[] {
569
- const { name, type, exports: entrypoints } = packageJSON
645
+ const {
646
+ name,
647
+ type,
648
+ bin,
649
+ exports: entrypoints,
650
+ imports: internalEntrypoints
651
+ } = packageJSON
570
652
  const pkgIsModule = type === 'module'
571
- if (!name) throw new Error('package.json name is required')
572
- if (!entrypoints) throw new Error('package.json exports is required')
573
-
574
653
  const packageName = pascalCase(name)
575
-
576
- const external = externalResolver(packageJSON as Record<string, unknown>)
577
-
578
- const [filteredResolvedEntrypoints, exports] = getExports({
579
- entrypoints,
580
- pkgIsModule,
581
- entries,
582
- pkgName: JIEK_NAME!,
583
- outdir: jsOutdir,
584
- config
585
- })
586
-
587
654
  const leafMap = new Map<string, string[][]>()
588
- getAllLeafs(filteredResolvedEntrypoints as RecursiveRecord<string>, ({ keys, value }) => {
589
- if (typeof value === 'string') {
590
- const keysArr = leafMap.get(value) ?? []
591
- leafMap.set(value, keysArr)
592
- keysArr.push(keys)
655
+ const inputTags = new Map<string, string>()
656
+ const inputExports = new Map<string, Record<string, unknown>>()
657
+ const configs: RollupOptions[] = []
658
+ const external = externalResolver(packageJSON)
659
+
660
+ let collectConfigTotal = 0
661
+ const collected = Promise.withResolvers<void>()
662
+ const internalModules = new Set<string>()
663
+ const internalModuleCollect = (id?: string) => {
664
+ if (!id) return
665
+ internalModules.add(id)
666
+ }
667
+ const collectPlugin: Plugin = {
668
+ name: 'jiek:collect',
669
+ buildStart() {
670
+ collectConfigTotal++
671
+ },
672
+ buildEnd() {
673
+ if (--collectConfigTotal === 0) {
674
+ collected.resolve()
675
+ }
593
676
  }
594
- return false
595
- })
677
+ }
678
+
679
+ if (entrypoints) {
680
+ const [filteredResolvedEntrypoints, exports] = resolveExports({
681
+ entrypoints,
682
+ pkgIsModule,
683
+ entries,
684
+ pkgName: JIEK_NAME!,
685
+ outdir: jsOutdir,
686
+ config
687
+ })
688
+ getAllLeafs(filteredResolvedEntrypoints as RecursiveRecord<string>, ({ keys, value }) => {
689
+ if (typeof value === 'string') {
690
+ const keysArr = leafMap.get(value) ?? []
691
+ leafMap.set(value, keysArr)
692
+ inputExports.set(value, exports)
693
+ keysArr.push(keys)
694
+ }
695
+ return false
696
+ })
697
+ }
698
+ if (bin) {
699
+ ;[...new Set(typeof bin === 'string' ? [bin] : Object.values(bin))]
700
+ .filter(binFile => binFile.startsWith('bin'))
701
+ .map(binFile => [
702
+ `./src/${binFile.replace(/(\.[cm]?)js$/, '$1ts')}`,
703
+ `./dist/${binFile}`
704
+ ])
705
+ .forEach(([input, output]) => {
706
+ configs.push(...generateConfigs({
707
+ path: output,
708
+ name,
709
+ input,
710
+ output,
711
+ external,
712
+ pkgIsModule,
713
+ conditionals: output.endsWith('.mjs')
714
+ ? ['import']
715
+ : output.endsWith('.cjs')
716
+ ? ['require']
717
+ : []
718
+ }, {
719
+ internalModuleCollect,
720
+ disableDTS: true,
721
+ disableMinify: true,
722
+ commonPlugins: [
723
+ collectPlugin
724
+ ]
725
+ }))
726
+ leafMap.set(input, [[output]])
727
+ inputTags.set(input, 'binary')
728
+ })
729
+ }
730
+ if (internalEntrypoints) {
731
+ const [filteredResolvedInternalEntrypoints, imports] = resolveExports({
732
+ entrypoints: internalEntrypoints,
733
+ pkgIsModule,
734
+ pkgName: JIEK_NAME!,
735
+ outdir: `${jsOutdir}/.internal`,
736
+ config
737
+ })
738
+ getAllLeafs(filteredResolvedInternalEntrypoints as RecursiveRecord<string>, ({ keys, value }) => {
739
+ if (typeof value === 'string') {
740
+ const keysArr = leafMap.get(value) ?? []
741
+ leafMap.set(value, keysArr)
742
+ inputExports.set(value, imports)
743
+ inputTags.set(value, 'internal')
744
+ keysArr.push(keys)
745
+ }
746
+ return false
747
+ })
748
+ }
749
+
750
+ leafMap.forEach((keysArr, input) => {
751
+ if (inputTags.get(input) === 'binary') return
752
+ const isInternal = inputTags.get(input) === 'internal'
753
+ const exports = inputExports.get(input)!
596
754
 
597
- const configs: RollupOptions[] = []
598
- leafMap.forEach((keysArr, input) =>
599
755
  keysArr.forEach((keys) => {
600
756
  const [path, ...conditionals] = keys
601
757
 
602
758
  const name = packageName + (path === '.' ? '' : pascalCase(path))
603
759
  const keyExports = reveal(exports, keys)
604
- const commonOptions = {
760
+ const commonContext = {
605
761
  path,
606
762
  name,
607
763
  input,
608
764
  external,
609
765
  pkgIsModule
610
766
  }
767
+ const commonOptions: GenerateConfigsOptions = isInternal
768
+ ? {
769
+ commonPlugins: [
770
+ {
771
+ name: 'jiek:loadInternalModules',
772
+ async options(inputOptions) {
773
+ await collected.promise
774
+ inputOptions.input = [...intersection(
775
+ inputOptions.input as string[],
776
+ internalModules
777
+ )]
778
+ return inputOptions
779
+ },
780
+ outputOptions(outputOptions) {
781
+ outputOptions.dir = `${outputOptions.dir}/.internal`
782
+ const oldEntryFileNames = outputOptions.entryFileNames
783
+ outputOptions.entryFileNames = (chunkInfo) => {
784
+ if (typeof oldEntryFileNames !== 'function') {
785
+ throw new TypeError('entryFileNames must be a function')
786
+ }
787
+ const oldFileName = oldEntryFileNames(chunkInfo)
788
+ return oldFileName
789
+ .replaceAll('#', '')
790
+ .replaceAll('~', '+')
791
+ }
792
+ return outputOptions
793
+ }
794
+ }
795
+ ],
796
+ disableCollectInternalModule: true
797
+ }
798
+ : {
799
+ internalModuleCollect,
800
+ commonPlugins: [
801
+ collectPlugin
802
+ ]
803
+ }
611
804
 
612
805
  // eslint-disable-next-line ts/switch-exhaustiveness-check
613
806
  switch (typeof keyExports) {
614
807
  case 'string': {
615
808
  configs.push(...generateConfigs({
616
- ...commonOptions,
809
+ ...commonContext,
617
810
  output: keyExports,
618
811
  conditionals
619
- }, build))
812
+ }, commonOptions))
620
813
  break
621
814
  }
622
815
  case 'object': {
@@ -624,10 +817,10 @@ export function template(packageJSON: PackageJSON): RollupOptions[] {
624
817
  const allConditionals = [...new Set([...conditionals, ...nextKeys])]
625
818
  if (typeof value === 'string') {
626
819
  configs.push(...generateConfigs({
627
- ...commonOptions,
820
+ ...commonContext,
628
821
  output: value,
629
822
  conditionals: allConditionals
630
- }, build))
823
+ }, commonOptions))
631
824
  }
632
825
  return false
633
826
  })
@@ -635,7 +828,8 @@ export function template(packageJSON: PackageJSON): RollupOptions[] {
635
828
  }
636
829
  }
637
830
  })
638
- )
831
+ })
832
+
639
833
  void publish('init', { leafMap, targetsLength: configs.length })
640
834
  return configs.map(c => ({
641
835
  ...COMMON_OPTIONS,
@@ -0,0 +1,23 @@
1
+ import type { PluginImpl } from 'rollup'
2
+
3
+ export default (() => ({
4
+ name: 'jiek:with-external',
5
+ resolveId: {
6
+ order: 'pre',
7
+ handler: (source, _, { attributes }) => {
8
+ if (
9
+ 'external' in attributes || 'bundle' in attributes
10
+ ) {
11
+ delete attributes.external
12
+ delete attributes.bundle
13
+ return {
14
+ id: source,
15
+ external: attributes.external === 'true'
16
+ ? true
17
+ : attributes.bundle !== 'true',
18
+ attributes
19
+ }
20
+ }
21
+ }
22
+ }
23
+ })) as PluginImpl<{}>
@@ -1,5 +1,6 @@
1
1
  import fs from 'node:fs'
2
2
  import { builtinModules } from 'node:module'
3
+ import process from 'node:process'
3
4
 
4
5
  const EXCLUDE_SUFFIX = [
5
6
  'te?xt',
@@ -7,21 +8,32 @@ const EXCLUDE_SUFFIX = [
7
8
  '(css|s[ac]ss|less|styl)'
8
9
  ]
9
10
 
10
- export default function(json: Record<string, unknown>): (string | RegExp)[]
11
+ export interface PackageJSON {
12
+ name: string
13
+ type?: string
14
+ bin?: string | Record<string, string>
15
+ exports?: Record<string, unknown> | string | string[]
16
+ imports?: Record<string, unknown>
17
+ dependencies?: Record<string, unknown>
18
+ peerDependencies?: Record<string, unknown>
19
+ optionalDependencies?: Record<string, unknown>
20
+ }
21
+
22
+ export default function(json: PackageJSON): (string | RegExp)[]
11
23
  export default function(path?: string): (string | RegExp)[]
12
- export default function(jsonOrPath: string | Record<string, unknown> = process.cwd()): (string | RegExp)[] {
13
- const pkg = typeof jsonOrPath === 'string'
24
+ export default function(jsonOrPath: string | PackageJSON = process.cwd()): (string | RegExp)[] {
25
+ const pkg: PackageJSON = typeof jsonOrPath === 'string'
14
26
  ? fs.existsSync(`${jsonOrPath}/package.json`)
15
- ? JSON.parse(fs.readFileSync(`${jsonOrPath}/package.json`, 'utf-8'))
16
- : {}
27
+ ? JSON.parse(fs.readFileSync(`${jsonOrPath}/package.json`, 'utf-8')) as PackageJSON
28
+ : {} as PackageJSON
17
29
  : jsonOrPath
18
30
  const { name, dependencies = {}, peerDependencies = {}, optionalDependencies = {} } = pkg
19
- if (!name) {
31
+ if (name == null) {
20
32
  throw new Error('package.json must have a name field')
21
33
  }
22
34
 
23
- const external = <(string | RegExp)[]> Object
24
- .keys(dependencies)
35
+ const external = (<(string | RegExp)[]> [])
36
+ .concat(Object.keys(dependencies))
25
37
  .concat(Object.keys(peerDependencies))
26
38
  .concat(Object.keys(optionalDependencies))
27
39
  .concat(builtinModules)
@@ -3,7 +3,7 @@ import process from 'node:process'
3
3
 
4
4
  import { confirm } from '@inquirer/prompts'
5
5
 
6
- import { getWD } from '#~/utils/getWD.ts'
6
+ import { getWD } from '#~/utils/getWD'
7
7
 
8
8
  export async function checkDependency(dependency: string) {
9
9
  try {