jiek 2.2.7 → 2.3.0

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 (100) hide show
  1. package/bin/build.cjs +2 -0
  2. package/bin/index.cjs +2 -0
  3. package/bin-helper/index.cjs +19 -31
  4. package/dist/.internal/.chunks/filterSupport.4rM7f6jB.cjs +114 -0
  5. package/dist/.internal/.chunks/filterSupport.BXWpLBpT.js +104 -0
  6. package/dist/{cli.js → .internal/.chunks/index.B6RQz1DZ.js} +1 -737
  7. package/dist/.internal/.chunks/index.BHuJRlvZ.js +9059 -0
  8. package/dist/.internal/.chunks/index.Daihq2aI.cjs +9071 -0
  9. package/dist/{cli.cjs → .internal/.chunks/index.DlHFuTjM.cjs} +1 -759
  10. package/dist/.internal/.chunks/loadConfig.CFfzkm_p.js +110 -0
  11. package/dist/.internal/.chunks/loadConfig.OOKddvHF.cjs +117 -0
  12. package/dist/{cli-only-build.cjs → .internal/bin/common.cjs} +225 -444
  13. package/dist/.internal/bin/common.d.cts +18 -0
  14. package/dist/.internal/bin/common.d.ts +18 -0
  15. package/dist/{cli-only-build.js → .internal/bin/common.js} +220 -438
  16. package/dist/.internal/bin/parseArgv.cjs +33 -0
  17. package/dist/.internal/bin/parseArgv.d.cts +5 -0
  18. package/dist/.internal/bin/parseArgv.d.ts +5 -0
  19. package/dist/.internal/bin/parseArgv.js +27 -0
  20. package/dist/.internal/bridge.cjs +30 -0
  21. package/dist/.internal/bridge.d.cts +39 -0
  22. package/dist/.internal/bridge.d.ts +39 -0
  23. package/dist/.internal/bridge.js +27 -0
  24. package/dist/.internal/rollup/base.cjs +10 -0
  25. package/dist/{cli-only-build.d.cts → .internal/rollup/base.d.cts} +8 -15
  26. package/dist/{cli-only-build.d.ts → .internal/rollup/base.d.ts} +8 -15
  27. package/dist/.internal/rollup/base.js +7 -0
  28. package/dist/.internal/rollup/bundle-analyzer.cjs +51 -0
  29. package/dist/.internal/rollup/bundle-analyzer.d.cts +92 -0
  30. package/dist/.internal/rollup/bundle-analyzer.d.ts +92 -0
  31. package/dist/.internal/rollup/bundle-analyzer.js +45 -0
  32. package/dist/.internal/utils/filterSupport.cjs +18 -0
  33. package/dist/.internal/utils/filterSupport.d.cts +15 -0
  34. package/dist/.internal/utils/filterSupport.d.ts +15 -0
  35. package/dist/.internal/utils/filterSupport.js +8 -0
  36. package/dist/.internal/utils/getInternalModuleName.cjs +5 -0
  37. package/dist/.internal/utils/getInternalModuleName.d.cts +3 -0
  38. package/dist/.internal/utils/getInternalModuleName.d.ts +3 -0
  39. package/dist/.internal/utils/getInternalModuleName.js +3 -0
  40. package/dist/.internal/utils/intersection.cjs +5 -0
  41. package/dist/.internal/utils/intersection.d.cts +3 -0
  42. package/dist/.internal/utils/intersection.d.ts +3 -0
  43. package/dist/.internal/utils/intersection.js +3 -0
  44. package/dist/.internal/utils/loadConfig.cjs +14 -0
  45. package/dist/.internal/utils/loadConfig.d.cts +10 -0
  46. package/dist/.internal/utils/loadConfig.d.ts +10 -0
  47. package/dist/.internal/utils/loadConfig.js +9 -0
  48. package/dist/.internal/utils/recursiveListFiles.cjs +19 -0
  49. package/dist/.internal/utils/recursiveListFiles.d.cts +3 -0
  50. package/dist/.internal/utils/recursiveListFiles.d.ts +3 -0
  51. package/dist/.internal/utils/recursiveListFiles.js +13 -0
  52. package/dist/.internal/utils/resolveExports.cjs +118 -0
  53. package/dist/.internal/utils/resolveExports.d.cts +26 -0
  54. package/dist/.internal/utils/resolveExports.d.ts +26 -0
  55. package/dist/.internal/utils/resolveExports.js +111 -0
  56. package/dist/.internal/utils/ts.cjs +68 -0
  57. package/dist/.internal/utils/ts.d.cts +3 -0
  58. package/dist/.internal/utils/ts.d.ts +3 -0
  59. package/dist/.internal/utils/ts.js +62 -0
  60. package/dist/bin/build.cjs +10 -0
  61. package/dist/bin/index.cjs +501 -0
  62. package/dist/index.d.cts +12 -114
  63. package/dist/index.d.ts +12 -114
  64. package/dist/rollup/index.cjs +280 -4573
  65. package/dist/rollup/index.d.cts +7 -1
  66. package/dist/rollup/index.d.ts +7 -1
  67. package/dist/rollup/index.js +268 -4558
  68. package/package.json +15 -23
  69. package/src/bin/build.cts +5 -0
  70. package/src/bin/common.ts +3 -0
  71. package/src/bin/index.cts +7 -0
  72. package/src/bridge.ts +1 -1
  73. package/src/commands/base.ts +3 -3
  74. package/src/commands/build/analyzer.ts +5 -5
  75. package/src/commands/build/client/analyzer.tsx +1 -1
  76. package/src/commands/build/client/index.ts +5 -7
  77. package/src/commands/build.ts +226 -211
  78. package/src/commands/meta.ts +1 -1
  79. package/src/commands/publish.ts +113 -32
  80. package/src/index.ts +6 -1
  81. package/src/rollup/base.ts +2 -2
  82. package/src/rollup/index.ts +321 -124
  83. package/src/rollup/plugins/with-external.ts +23 -0
  84. package/src/rollup/utils/externalResolver.ts +20 -8
  85. package/src/utils/checkDependency.ts +1 -1
  86. package/src/utils/filterSupport.ts +17 -16
  87. package/src/utils/getInternalModuleName.ts +5 -0
  88. package/src/utils/intersection.ts +1 -0
  89. package/src/utils/{recusiveListFiles.ts → recursiveListFiles.ts} +2 -2
  90. package/src/utils/{getExports.ts → resolveExports.ts} +16 -14
  91. package/bin/jiek-build.js +0 -16
  92. package/bin/jiek.js +0 -13
  93. package/cli/package.json +0 -1
  94. package/cli-only-build/package.json +0 -1
  95. package/dist/cli.d.cts +0 -14
  96. package/dist/cli.d.ts +0 -14
  97. package/src/cli-only-build.ts +0 -11
  98. package/src/cli.ts +0 -6
  99. /package/{src/bin/build.ts → .jiek-production-tag} +0 -0
  100. /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
@@ -103,7 +105,7 @@ const MINIFY_OPTIONS = {
103
105
  const config = loadConfig({
104
106
  root: WORKSPACE_ROOT
105
107
  }) ?? {}
106
- const { build = {} } = config
108
+ const { experimental, build = {} } = config
107
109
  const { js: jsOutdir, dts: dtsOutdir } = getOutDirs({
108
110
  config,
109
111
  pkgName: JIEK_NAME
@@ -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,
@@ -244,6 +253,12 @@ const withMinify = (
244
253
  : (() => {
245
254
  throw new Error('entryFileNames must be a function')
246
255
  })(),
256
+ chunkFileNames: chunkInfo =>
257
+ typeof output.chunkFileNames === 'function'
258
+ ? output.chunkFileNames(chunkInfo).replace(/(\.[cm]?js)$/, '.min$1')
259
+ : (() => {
260
+ throw new Error('chunkFileNames must be a function')
261
+ })(),
247
262
  file: output.file?.replace(/(\.[cm]?js)$/, '.min$1'),
248
263
  plugins: [
249
264
  ...notOnlyOncePlugins,
@@ -253,7 +268,25 @@ const withMinify = (
253
268
  ]
254
269
  }
255
270
 
256
- const generateConfigs = (context: ConfigGenerateContext, options: TemplateOptions = {}): RollupOptions[] => {
271
+ interface GenerateConfigsOptions {
272
+ internalModuleCollect?: (id: string) => void
273
+ commonPlugins?: InputPluginOption[]
274
+ disableDTS?: boolean
275
+ disableMinify?: boolean
276
+ disableCollectInternalModule?: boolean
277
+ }
278
+
279
+ const generateConfigs = (
280
+ context: ConfigGenerateContext,
281
+ {
282
+ internalModuleCollect,
283
+ commonPlugins: inputCommonPlugins = [],
284
+ disableDTS = false,
285
+ disableMinify,
286
+ disableCollectInternalModule
287
+ }: GenerateConfigsOptions = {}
288
+ ): RollupOptions[] => {
289
+ const buildOptions: TemplateOptions = build
257
290
  const {
258
291
  path,
259
292
  name,
@@ -263,7 +296,7 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
263
296
  pkgIsModule,
264
297
  conditionals
265
298
  } = context
266
- const external = [...inputExternal, ...(options.external ?? []), ...(commandExternal ?? [])]
299
+ const external = [...inputExternal, ...(buildOptions.external ?? []), ...(commandExternal ?? [])]
267
300
  const isModule = conditionals.includes('import')
268
301
  const isCommonJS = conditionals.includes('require')
269
302
  const isBrowser = conditionals.includes('browser')
@@ -314,6 +347,23 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
314
347
  delete compilerOptions.composite
315
348
  }
316
349
  const exportConditions = [...conditionals, ...(compilerOptions.customConditions ?? [])]
350
+ const nodeResolvePluginInstance = nodeResolve({
351
+ exportConditions,
352
+ extensions: [
353
+ '.js',
354
+ '.cjs',
355
+ '.mjs',
356
+ '.jsx',
357
+ '.cjsx',
358
+ '.mjsx',
359
+ '.ts',
360
+ '.cts',
361
+ '.mts',
362
+ '.tsx',
363
+ '.ctsx',
364
+ '.mtsx'
365
+ ]
366
+ })
317
367
  const publishInEntry = <K extends keyof RollupBuildEventMap>(
318
368
  type: K,
319
369
  data: Omit<RollupBuildEventMap[K], keyof RollupBuildEntryCtx>
@@ -336,21 +386,24 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
336
386
  'input should not include "**", please read the [documentation](https://nodejs.org/api/packages.html#subpath-patterns).'
337
387
  )
338
388
  }
389
+ const reg = new RegExp(`^\./${
390
+ input
391
+ .slice(2)
392
+ .replace(/\./g, '\\.')
393
+ .replace(/\*/g, '.*')
394
+ }$`)
339
395
  const inputObj = !input.includes('*')
340
396
  ? 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)))
397
+ : CWD_FILES.filter(p => reg.test(p))
345
398
  const globCommonDir = input.includes('*')
346
- ? input.split('*')[0].replace('./', '')
399
+ ? input.split('*')[0]
347
400
  : ''
348
- const pathCommonDir = path.includes('*')
349
- ? path.split('*')[0].replace('./', '')
401
+ const outputCommonDir = output.includes('*')
402
+ ? output.split('*')[0]
350
403
  : ''
351
404
  if (
352
- (globCommonDir.length > 0 && pathCommonDir.length === 0)
353
- || (globCommonDir.length === 0 && pathCommonDir.length > 0)
405
+ (globCommonDir.length > 0 && outputCommonDir.length === 0)
406
+ || (globCommonDir.length === 0 && outputCommonDir.length > 0)
354
407
  ) {
355
408
  throw new Error('input and path should both include "*" or not include "*"')
356
409
  }
@@ -359,32 +412,61 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
359
412
  const { js: jsOutput, dts: dtsOutput } = resolveOutputControls(context, build.output)
360
413
  const rollupOptions: RollupOptions[] = []
361
414
 
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
- })
415
+ const commonPlugins: InputPluginOption[] = [
416
+ ...inputCommonPlugins,
417
+ withExternal(),
418
+ !disableCollectInternalModule && {
419
+ name: 'jiek:collect-internal-module',
420
+ resolveId: {
421
+ order: 'pre',
422
+ async handler(source, importer, options) {
423
+ if (!source.startsWith('#')) return
424
+
425
+ if (!nodeResolvePluginInstance.resolveId || !('handler' in nodeResolvePluginInstance.resolveId)) {
426
+ throw new Error('nodeResolvePluginInstance.resolveId is not a plugin instance')
427
+ }
428
+ let resolved = await nodeResolvePluginInstance
429
+ .resolveId
430
+ .handler
431
+ .call(this, source, importer, options)
432
+ if (typeof resolved === 'string') {
433
+ resolved = { id: resolved }
434
+ }
435
+ if (!resolved || !('id' in resolved)) {
436
+ throw new Error('nodeResolvePluginInstance.resolveId.handler did not return a resolved object')
437
+ }
438
+ internalModuleCollect?.(`./${relative(process.cwd(), resolved.id)}`)
439
+ return {
440
+ id: experimental?.importsDowngrade
441
+ ? source
442
+ .replaceAll('#', `${INTERNAL_MODULE_NAME}/`)
443
+ .replaceAll('~', '+')
444
+ : source,
445
+ external: true
446
+ }
447
+ }
448
+ }
449
+ }
380
450
  ]
451
+ const features = Object.assign(
452
+ {
453
+ keepImportAttributes: false
454
+ },
455
+ build.features,
456
+ FEATURES
457
+ )
458
+ const importAttributesKey = (
459
+ features.keepImportAttributes === false
460
+ || features.keepImportAttributes === undefined
461
+ )
462
+ ? undefined
463
+ : features.keepImportAttributes === true
464
+ ? 'with'
465
+ : features.keepImportAttributes
381
466
  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)
467
+ const sourcemap = typeof buildOptions?.output?.sourcemap === 'object'
468
+ ? buildOptions.output.sourcemap.js
469
+ : buildOptions?.output?.sourcemap
388
470
  const builder = resolvedBuilderOptions.type === 'esbuild'
389
471
  ? import('rollup-plugin-esbuild').then(({ default: esbuild }) =>
390
472
  esbuild({
@@ -402,8 +484,8 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
402
484
  },
403
485
  ...noTypeResolvedBuilderOptions,
404
486
  supported: {
405
- 'import-attributes': features.keepImportAttributes !== false,
406
- ...resolvedBuilderOptions.supported
487
+ ...resolvedBuilderOptions.supported,
488
+ 'import-attributes': true
407
489
  }
408
490
  })
409
491
  )
@@ -431,7 +513,7 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
431
513
  },
432
514
  experimental: {
433
515
  ...resolvedBuilderOptions.jsc?.experimental,
434
- keepImportAttributes: features.keepImportAttributes !== false
516
+ keepImportAttributes: true
435
517
  }
436
518
  }
437
519
  })
@@ -444,36 +526,41 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
444
526
  input: inputObj,
445
527
  external,
446
528
  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)
529
+ ...withMinify(
530
+ {
531
+ dir: jsOutdir,
532
+ name,
533
+ interop: 'auto',
534
+ entryFileNames: (chunkInfo) => {
535
+ return Array.isArray(inputObj)
536
+ ? chunkInfo.facadeModuleId!
537
+ .replace(`${process.cwd()}/`, './')
538
+ .replace(globCommonDir, outputCommonDir)
539
+ .replace(/(\.[cm]?)ts$/, jsOutputSuffix)
540
+ .replace(`${jsOutdir}/`, '')
541
+ : output
542
+ .replace(`${jsOutdir}/`, '')
543
+ },
544
+ chunkFileNames: (chunkInfo) => {
545
+ return `.internal/.chunks/${chunkInfo.name}.[hash]${jsOutputSuffix}`
546
+ },
547
+ sourcemap,
548
+ format,
549
+ strict: typeof buildOptions?.output?.strict === 'object'
550
+ ? buildOptions.output.strict.js
551
+ : buildOptions?.output?.strict,
552
+ externalImportAttributes: features.keepImportAttributes === true
553
+ ? true
554
+ : features.keepImportAttributes,
555
+ importAttributesKey
556
+ },
557
+ disableMinify,
558
+ onlyOncePlugins
559
+ )
474
560
  ],
475
561
  plugins: [
476
562
  ...commonPlugins,
563
+ nodeResolvePluginInstance,
477
564
  import('rollup-plugin-postcss')
478
565
  .then(({ default: postcss }) =>
479
566
  postcss({
@@ -502,10 +589,10 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
502
589
  })
503
590
  }
504
591
 
505
- if (dtsOutput && !WITHOUT_DTS) {
506
- const sourcemap = typeof options?.output?.sourcemap === 'object'
507
- ? options.output.sourcemap.dts
508
- : options?.output?.sourcemap
592
+ if (!disableDTS && dtsOutput && !WITHOUT_DTS) {
593
+ const sourcemap = typeof buildOptions?.output?.sourcemap === 'object'
594
+ ? buildOptions.output.sourcemap.dts
595
+ : buildOptions?.output?.sourcemap
509
596
  rollupOptions.push({
510
597
  input: inputObj,
511
598
  external,
@@ -515,16 +602,20 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
515
602
  sourcemap,
516
603
  entryFileNames: (chunkInfo) => (
517
604
  Array.isArray(inputObj)
518
- ? chunkInfo.facadeModuleId!.replace(`${process.cwd()}/`, '')
519
- .replace(globCommonDir, pathCommonDir)
605
+ ? chunkInfo.facadeModuleId!
606
+ .replace(`${process.cwd()}/`, './')
607
+ .replace(globCommonDir, outputCommonDir)
520
608
  .replace(/(\.[cm]?)ts$/, tsOutputSuffix)
521
- : output
522
609
  .replace(`${jsOutdir}/`, '')
610
+ : output
523
611
  .replace(/(\.[cm]?)js$/, tsOutputSuffix)
612
+ .replace(`${jsOutdir}/`, '')
524
613
  ),
525
- strict: typeof options?.output?.strict === 'object'
526
- ? options.output.strict.dts
527
- : options?.output?.strict
614
+ strict: typeof buildOptions?.output?.strict === 'object'
615
+ ? buildOptions.output.strict.dts
616
+ : buildOptions?.output?.strict,
617
+ externalImportAttributes: features.keepImportAttributes !== false,
618
+ importAttributesKey
528
619
  }
529
620
  ],
530
621
  plugins: [
@@ -566,57 +657,162 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
566
657
  }
567
658
 
568
659
  export function template(packageJSON: PackageJSON): RollupOptions[] {
569
- const { name, type, exports: entrypoints } = packageJSON
660
+ const {
661
+ name,
662
+ type,
663
+ bin,
664
+ exports: entrypoints,
665
+ imports: internalEntrypoints
666
+ } = packageJSON
570
667
  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
668
  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
669
  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)
670
+ const inputTags = new Map<string, string>()
671
+ const inputExports = new Map<string, Record<string, unknown>>()
672
+ const configs: RollupOptions[] = []
673
+ const external = externalResolver(packageJSON)
674
+
675
+ let collectConfigTotal = 0
676
+ const collected = Promise.withResolvers<void>()
677
+ const internalModules = new Set<string>()
678
+ const internalModuleCollect = (id?: string) => {
679
+ if (!id) return
680
+ internalModules.add(id)
681
+ }
682
+ const collectPlugin: Plugin = {
683
+ name: 'jiek:collect',
684
+ buildStart() {
685
+ collectConfigTotal++
686
+ },
687
+ buildEnd() {
688
+ if (--collectConfigTotal === 0) {
689
+ collected.resolve()
690
+ }
593
691
  }
594
- return false
595
- })
692
+ }
693
+
694
+ if (entrypoints) {
695
+ const [filteredResolvedEntrypoints, exports] = resolveExports({
696
+ entrypoints,
697
+ pkgIsModule,
698
+ entries,
699
+ pkgName: JIEK_NAME!,
700
+ outdir: jsOutdir,
701
+ config
702
+ })
703
+ getAllLeafs(filteredResolvedEntrypoints as RecursiveRecord<string>, ({ keys, value }) => {
704
+ if (typeof value === 'string') {
705
+ const keysArr = leafMap.get(value) ?? []
706
+ leafMap.set(value, keysArr)
707
+ inputExports.set(value, exports)
708
+ keysArr.push(keys)
709
+ }
710
+ return false
711
+ })
712
+ }
713
+ if (bin) {
714
+ ;[...new Set(typeof bin === 'string' ? [bin] : Object.values(bin))]
715
+ .filter(binFile => binFile.startsWith('bin'))
716
+ .map(binFile => [
717
+ `./src/${binFile.replace(/(\.[cm]?)js$/, '$1ts')}`,
718
+ `./dist/${binFile}`
719
+ ])
720
+ .forEach(([input, output]) => {
721
+ configs.push(...generateConfigs({
722
+ path: output,
723
+ name,
724
+ input,
725
+ output,
726
+ external,
727
+ pkgIsModule,
728
+ conditionals: output.endsWith('.mjs')
729
+ ? ['import']
730
+ : output.endsWith('.cjs')
731
+ ? ['require']
732
+ : []
733
+ }, {
734
+ internalModuleCollect,
735
+ disableDTS: true,
736
+ disableMinify: true,
737
+ commonPlugins: [
738
+ collectPlugin
739
+ ]
740
+ }))
741
+ leafMap.set(input, [[output]])
742
+ inputTags.set(input, 'binary')
743
+ })
744
+ }
745
+ if (internalEntrypoints) {
746
+ const [filteredResolvedInternalEntrypoints, imports] = resolveExports({
747
+ entrypoints: internalEntrypoints,
748
+ pkgIsModule,
749
+ pkgName: JIEK_NAME!,
750
+ outdir: `${jsOutdir}/.internal`,
751
+ config
752
+ })
753
+ getAllLeafs(filteredResolvedInternalEntrypoints as RecursiveRecord<string>, ({ keys, value }) => {
754
+ if (typeof value === 'string') {
755
+ const keysArr = leafMap.get(value) ?? []
756
+ leafMap.set(value, keysArr)
757
+ inputExports.set(value, imports)
758
+ inputTags.set(value, 'internal')
759
+ keysArr.push(keys)
760
+ }
761
+ return false
762
+ })
763
+ }
764
+
765
+ leafMap.forEach((keysArr, input) => {
766
+ if (inputTags.get(input) === 'binary') return
767
+ const isInternal = inputTags.get(input) === 'internal'
768
+ const exports = inputExports.get(input)!
596
769
 
597
- const configs: RollupOptions[] = []
598
- leafMap.forEach((keysArr, input) =>
599
770
  keysArr.forEach((keys) => {
600
771
  const [path, ...conditionals] = keys
601
772
 
602
773
  const name = packageName + (path === '.' ? '' : pascalCase(path))
603
774
  const keyExports = reveal(exports, keys)
604
- const commonOptions = {
775
+ const commonContext = {
605
776
  path,
606
777
  name,
607
778
  input,
608
779
  external,
609
780
  pkgIsModule
610
781
  }
782
+ const commonOptions: GenerateConfigsOptions = isInternal
783
+ ? {
784
+ commonPlugins: [
785
+ {
786
+ name: 'jiek:loadInternalModules',
787
+ async options(inputOptions) {
788
+ await collected.promise
789
+ inputOptions.input = [...intersection(
790
+ Array.isArray(inputOptions.input)
791
+ ? inputOptions.input
792
+ : [inputOptions.input as string],
793
+ internalModules
794
+ )]
795
+ return inputOptions
796
+ }
797
+ }
798
+ ],
799
+ disableCollectInternalModule: true
800
+ }
801
+ : {
802
+ internalModuleCollect,
803
+ commonPlugins: [
804
+ collectPlugin
805
+ ]
806
+ }
611
807
 
612
808
  // eslint-disable-next-line ts/switch-exhaustiveness-check
613
809
  switch (typeof keyExports) {
614
810
  case 'string': {
615
811
  configs.push(...generateConfigs({
616
- ...commonOptions,
812
+ ...commonContext,
617
813
  output: keyExports,
618
814
  conditionals
619
- }, build))
815
+ }, commonOptions))
620
816
  break
621
817
  }
622
818
  case 'object': {
@@ -624,10 +820,10 @@ export function template(packageJSON: PackageJSON): RollupOptions[] {
624
820
  const allConditionals = [...new Set([...conditionals, ...nextKeys])]
625
821
  if (typeof value === 'string') {
626
822
  configs.push(...generateConfigs({
627
- ...commonOptions,
823
+ ...commonContext,
628
824
  output: value,
629
825
  conditionals: allConditionals
630
- }, build))
826
+ }, commonOptions))
631
827
  }
632
828
  return false
633
829
  })
@@ -635,7 +831,8 @@ export function template(packageJSON: PackageJSON): RollupOptions[] {
635
831
  }
636
832
  }
637
833
  })
638
- )
834
+ })
835
+
639
836
  void publish('init', { leafMap, targetsLength: configs.length })
640
837
  return configs.map(c => ({
641
838
  ...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<{}>