jiek 2.2.7 → 2.3.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 (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} +229 -445
  13. package/dist/.internal/bin/common.d.cts +22 -0
  14. package/dist/.internal/bin/common.d.ts +22 -0
  15. package/dist/{cli-only-build.js → .internal/bin/common.js} +224 -439
  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 +124 -0
  53. package/dist/.internal/utils/resolveExports.d.cts +27 -0
  54. package/dist/.internal/utils/resolveExports.d.ts +27 -0
  55. package/dist/.internal/utils/resolveExports.js +117 -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 +522 -0
  62. package/dist/index.d.cts +16 -114
  63. package/dist/index.d.ts +16 -114
  64. package/dist/rollup/index.cjs +310 -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 +298 -4558
  68. package/package.json +16 -24
  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 +244 -213
  78. package/src/commands/meta.ts +1 -1
  79. package/src/commands/publish.ts +190 -78
  80. package/src/index.ts +6 -1
  81. package/src/rollup/base.ts +2 -2
  82. package/src/rollup/index.ts +361 -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} +28 -19
  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,9 @@ const {
48
45
  JIEK_CLEAN,
49
46
  JIEK_ONLY_MINIFY,
50
47
  JIEK_TSCONFIG,
51
- JIEK_DTSCONFIG
48
+ JIEK_DTSCONFIG,
49
+ JIEK_SKIP_JS,
50
+ JIEK_FEATURES
52
51
  } = process.env
53
52
 
54
53
  const resolveArrayString = (str: string | undefined) => {
@@ -74,6 +73,8 @@ const COMMON_PLUGINS = [
74
73
  json()
75
74
  ]
76
75
 
76
+ const INTERNAL_MODULE_NAME = getInternalModuleName(JIEK_NAME!)
77
+
77
78
  const WITHOUT_JS = JIEK_WITHOUT_JS === 'true'
78
79
  const WITHOUT_DTS = JIEK_WITHOUT_DTS === 'true'
79
80
  const WITHOUT_MINIFY = JIEK_WITHOUT_MINIFY === 'true'
@@ -82,6 +83,14 @@ const ONLY_MINIFY = JIEK_ONLY_MINIFY === 'true'
82
83
 
83
84
  const CLEAN = JIEK_CLEAN === 'true'
84
85
 
86
+ const SKIP_JS = JIEK_SKIP_JS === 'false'
87
+ ? false
88
+ : JIEK_SKIP_JS === 'true'
89
+ ? true
90
+ : undefined
91
+
92
+ const FEATURES = JSON.parse(JIEK_FEATURES ?? '{}') as Record<string, unknown>
93
+
85
94
  const MINIFY_DEFAULT_VALUE = WITHOUT_MINIFY
86
95
  ? false
87
96
  : ONLY_MINIFY
@@ -103,7 +112,11 @@ const MINIFY_OPTIONS = {
103
112
  const config = loadConfig({
104
113
  root: WORKSPACE_ROOT
105
114
  }) ?? {}
106
- const { build = {} } = config
115
+ const {
116
+ experimental,
117
+ skipJS,
118
+ build = {}
119
+ } = config
107
120
  const { js: jsOutdir, dts: dtsOutdir } = getOutDirs({
108
121
  config,
109
122
  pkgName: JIEK_NAME
@@ -116,6 +129,10 @@ if (CLEAN) {
116
129
 
117
130
  const STYLE_REGEXP = /\.(css|s[ac]ss|less|styl)$/
118
131
 
132
+ const CWD_FILES = recursiveListFiles(process.cwd())
133
+ .filter(p => /(?<!\.d)\.[cm]?tsx?$/.test(p))
134
+ .map(p => `./${relative(process.cwd(), p)}`)
135
+
119
136
  const resolveBuildPlugins = (context: ConfigGenerateContext, plugins: TemplateOptions['plugins']): {
120
137
  js: InputPluginOption
121
138
  dts: InputPluginOption
@@ -197,9 +214,12 @@ const withMinify = (
197
214
  output: OutputOptions & {
198
215
  plugins?: OutputPlugin[]
199
216
  },
217
+ disableMinify?: boolean,
200
218
  onlyOncePlugins: OutputPluginOption[] = []
201
219
  ): OutputOptions[] => {
202
- const minify = build?.output?.minify ?? MINIFY_DEFAULT_VALUE
220
+ const minify = disableMinify !== undefined
221
+ ? !disableMinify
222
+ : build?.output?.minify ?? MINIFY_DEFAULT_VALUE
203
223
 
204
224
  output.plugins = output.plugins ?? []
205
225
  const notOnlyOncePlugins = [...output.plugins]
@@ -226,7 +246,7 @@ const withMinify = (
226
246
  typeof output.entryFileNames === 'function'
227
247
  ? output.entryFileNames(chunkInfo)
228
248
  : (() => {
229
- throw new Error('entryFileNames must be a function')
249
+ throw new TypeError('entryFileNames must be a function')
230
250
  })(),
231
251
  plugins: [
232
252
  ...output.plugins,
@@ -244,6 +264,12 @@ const withMinify = (
244
264
  : (() => {
245
265
  throw new Error('entryFileNames must be a function')
246
266
  })(),
267
+ chunkFileNames: chunkInfo =>
268
+ typeof output.chunkFileNames === 'function'
269
+ ? output.chunkFileNames(chunkInfo).replace(/(\.[cm]?js)$/, '.min$1')
270
+ : (() => {
271
+ throw new Error('chunkFileNames must be a function')
272
+ })(),
247
273
  file: output.file?.replace(/(\.[cm]?js)$/, '.min$1'),
248
274
  plugins: [
249
275
  ...notOnlyOncePlugins,
@@ -253,7 +279,25 @@ const withMinify = (
253
279
  ]
254
280
  }
255
281
 
256
- const generateConfigs = (context: ConfigGenerateContext, options: TemplateOptions = {}): RollupOptions[] => {
282
+ interface GenerateConfigsOptions {
283
+ internalModuleCollect?: (id: string) => void
284
+ commonPlugins?: InputPluginOption[]
285
+ disableDTS?: boolean
286
+ disableMinify?: boolean
287
+ disableCollectInternalModule?: boolean
288
+ }
289
+
290
+ const generateConfigs = (
291
+ context: ConfigGenerateContext,
292
+ {
293
+ internalModuleCollect,
294
+ commonPlugins: inputCommonPlugins = [],
295
+ disableDTS = false,
296
+ disableMinify,
297
+ disableCollectInternalModule
298
+ }: GenerateConfigsOptions = {}
299
+ ): RollupOptions[] => {
300
+ const buildOptions: TemplateOptions = build
257
301
  const {
258
302
  path,
259
303
  name,
@@ -263,7 +307,7 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
263
307
  pkgIsModule,
264
308
  conditionals
265
309
  } = context
266
- const external = [...inputExternal, ...(options.external ?? []), ...(commandExternal ?? [])]
310
+ const external = [...inputExternal, ...(buildOptions.external ?? []), ...(commandExternal ?? [])]
267
311
  const isModule = conditionals.includes('import')
268
312
  const isCommonJS = conditionals.includes('require')
269
313
  const isBrowser = conditionals.includes('browser')
@@ -314,6 +358,23 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
314
358
  delete compilerOptions.composite
315
359
  }
316
360
  const exportConditions = [...conditionals, ...(compilerOptions.customConditions ?? [])]
361
+ const nodeResolvePluginInstance = nodeResolve({
362
+ exportConditions,
363
+ extensions: [
364
+ '.js',
365
+ '.cjs',
366
+ '.mjs',
367
+ '.jsx',
368
+ '.cjsx',
369
+ '.mjsx',
370
+ '.ts',
371
+ '.cts',
372
+ '.mts',
373
+ '.tsx',
374
+ '.ctsx',
375
+ '.mtsx'
376
+ ]
377
+ })
317
378
  const publishInEntry = <K extends keyof RollupBuildEventMap>(
318
379
  type: K,
319
380
  data: Omit<RollupBuildEventMap[K], keyof RollupBuildEntryCtx>
@@ -336,21 +397,24 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
336
397
  'input should not include "**", please read the [documentation](https://nodejs.org/api/packages.html#subpath-patterns).'
337
398
  )
338
399
  }
400
+ const reg = new RegExp(`^\./${
401
+ input
402
+ .slice(2)
403
+ .replace(/\./g, '\\.')
404
+ .replace(/\*/g, '.*')
405
+ }$`)
339
406
  const inputObj = !input.includes('*')
340
407
  ? 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)))
408
+ : CWD_FILES.filter(p => reg.test(p))
345
409
  const globCommonDir = input.includes('*')
346
- ? input.split('*')[0].replace('./', '')
410
+ ? input.split('*')[0]
347
411
  : ''
348
- const pathCommonDir = path.includes('*')
349
- ? path.split('*')[0].replace('./', '')
412
+ const outputCommonDir = output.includes('*')
413
+ ? output.split('*')[0]
350
414
  : ''
351
415
  if (
352
- (globCommonDir.length > 0 && pathCommonDir.length === 0)
353
- || (globCommonDir.length === 0 && pathCommonDir.length > 0)
416
+ (globCommonDir.length > 0 && outputCommonDir.length === 0)
417
+ || (globCommonDir.length === 0 && outputCommonDir.length > 0)
354
418
  ) {
355
419
  throw new Error('input and path should both include "*" or not include "*"')
356
420
  }
@@ -359,32 +423,61 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
359
423
  const { js: jsOutput, dts: dtsOutput } = resolveOutputControls(context, build.output)
360
424
  const rollupOptions: RollupOptions[] = []
361
425
 
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
- })
426
+ const commonPlugins: InputPluginOption[] = [
427
+ ...inputCommonPlugins,
428
+ withExternal(),
429
+ !disableCollectInternalModule && {
430
+ name: 'jiek:collect-internal-module',
431
+ resolveId: {
432
+ order: 'pre',
433
+ async handler(source, importer, options) {
434
+ if (!source.startsWith('#')) return
435
+
436
+ if (!nodeResolvePluginInstance.resolveId || !('handler' in nodeResolvePluginInstance.resolveId)) {
437
+ throw new Error('nodeResolvePluginInstance.resolveId is not a plugin instance')
438
+ }
439
+ let resolved = await nodeResolvePluginInstance
440
+ .resolveId
441
+ .handler
442
+ .call(this, source, importer, options)
443
+ if (typeof resolved === 'string') {
444
+ resolved = { id: resolved }
445
+ }
446
+ if (!resolved || !('id' in resolved)) {
447
+ throw new Error('nodeResolvePluginInstance.resolveId.handler did not return a resolved object')
448
+ }
449
+ internalModuleCollect?.(`./${relative(process.cwd(), resolved.id)}`)
450
+ return {
451
+ id: experimental?.importsDowngrade
452
+ ? source
453
+ .replaceAll('#', `${INTERNAL_MODULE_NAME}/`)
454
+ .replaceAll('~', '+')
455
+ : source,
456
+ external: true
457
+ }
458
+ }
459
+ }
460
+ }
380
461
  ]
462
+ const features = Object.assign(
463
+ {
464
+ keepImportAttributes: false
465
+ },
466
+ build.features,
467
+ FEATURES
468
+ )
469
+ const importAttributesKey = (
470
+ features.keepImportAttributes === false
471
+ || features.keepImportAttributes === undefined
472
+ )
473
+ ? undefined
474
+ : features.keepImportAttributes === true
475
+ ? 'with'
476
+ : features.keepImportAttributes
381
477
  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)
478
+ const sourcemap = typeof buildOptions?.output?.sourcemap === 'object'
479
+ ? buildOptions.output.sourcemap.js
480
+ : buildOptions?.output?.sourcemap
388
481
  const builder = resolvedBuilderOptions.type === 'esbuild'
389
482
  ? import('rollup-plugin-esbuild').then(({ default: esbuild }) =>
390
483
  esbuild({
@@ -402,8 +495,8 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
402
495
  },
403
496
  ...noTypeResolvedBuilderOptions,
404
497
  supported: {
405
- 'import-attributes': features.keepImportAttributes !== false,
406
- ...resolvedBuilderOptions.supported
498
+ ...resolvedBuilderOptions.supported,
499
+ 'import-attributes': true
407
500
  }
408
501
  })
409
502
  )
@@ -431,7 +524,7 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
431
524
  },
432
525
  experimental: {
433
526
  ...resolvedBuilderOptions.jsc?.experimental,
434
- keepImportAttributes: features.keepImportAttributes !== false
527
+ keepImportAttributes: true
435
528
  }
436
529
  }
437
530
  })
@@ -444,36 +537,41 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
444
537
  input: inputObj,
445
538
  external,
446
539
  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)
540
+ ...withMinify(
541
+ {
542
+ dir: jsOutdir,
543
+ name,
544
+ interop: 'auto',
545
+ entryFileNames: (chunkInfo) => {
546
+ return Array.isArray(inputObj)
547
+ ? chunkInfo.facadeModuleId!
548
+ .replace(`${process.cwd()}/`, './')
549
+ .replace(globCommonDir, outputCommonDir)
550
+ .replace(/(\.[cm]?)ts$/, jsOutputSuffix)
551
+ .replace(`${jsOutdir}/`, '')
552
+ : output
553
+ .replace(`${jsOutdir}/`, '')
554
+ },
555
+ chunkFileNames: (chunkInfo) => {
556
+ return `.internal/.chunks/${chunkInfo.name}.[hash]${jsOutputSuffix}`
557
+ },
558
+ sourcemap,
559
+ format,
560
+ strict: typeof buildOptions?.output?.strict === 'object'
561
+ ? buildOptions.output.strict.js
562
+ : buildOptions?.output?.strict,
563
+ externalImportAttributes: features.keepImportAttributes === true
564
+ ? true
565
+ : features.keepImportAttributes,
566
+ importAttributesKey
567
+ },
568
+ disableMinify,
569
+ onlyOncePlugins
570
+ )
474
571
  ],
475
572
  plugins: [
476
573
  ...commonPlugins,
574
+ nodeResolvePluginInstance,
477
575
  import('rollup-plugin-postcss')
478
576
  .then(({ default: postcss }) =>
479
577
  postcss({
@@ -502,10 +600,10 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
502
600
  })
503
601
  }
504
602
 
505
- if (dtsOutput && !WITHOUT_DTS) {
506
- const sourcemap = typeof options?.output?.sourcemap === 'object'
507
- ? options.output.sourcemap.dts
508
- : options?.output?.sourcemap
603
+ if (!disableDTS && dtsOutput && !WITHOUT_DTS) {
604
+ const sourcemap = typeof buildOptions?.output?.sourcemap === 'object'
605
+ ? buildOptions.output.sourcemap.dts
606
+ : buildOptions?.output?.sourcemap
509
607
  rollupOptions.push({
510
608
  input: inputObj,
511
609
  external,
@@ -515,16 +613,20 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
515
613
  sourcemap,
516
614
  entryFileNames: (chunkInfo) => (
517
615
  Array.isArray(inputObj)
518
- ? chunkInfo.facadeModuleId!.replace(`${process.cwd()}/`, '')
519
- .replace(globCommonDir, pathCommonDir)
616
+ ? chunkInfo.facadeModuleId!
617
+ .replace(`${process.cwd()}/`, './')
618
+ .replace(globCommonDir, outputCommonDir)
520
619
  .replace(/(\.[cm]?)ts$/, tsOutputSuffix)
521
- : output
522
620
  .replace(`${jsOutdir}/`, '')
621
+ : output
523
622
  .replace(/(\.[cm]?)js$/, tsOutputSuffix)
623
+ .replace(`${jsOutdir}/`, '')
524
624
  ),
525
- strict: typeof options?.output?.strict === 'object'
526
- ? options.output.strict.dts
527
- : options?.output?.strict
625
+ strict: typeof buildOptions?.output?.strict === 'object'
626
+ ? buildOptions.output.strict.dts
627
+ : buildOptions?.output?.strict,
628
+ externalImportAttributes: features.keepImportAttributes !== false,
629
+ importAttributesKey
528
630
  }
529
631
  ],
530
632
  plugins: [
@@ -566,57 +668,191 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
566
668
  }
567
669
 
568
670
  export function template(packageJSON: PackageJSON): RollupOptions[] {
569
- const { name, type, exports: entrypoints } = packageJSON
671
+ const {
672
+ name,
673
+ type,
674
+ bin,
675
+ exports: entrypoints,
676
+ imports: internalEntrypoints
677
+ } = packageJSON
570
678
  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
679
  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
680
  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)
681
+ const inputTags = new Map<string, string>()
682
+ const inputExports = new Map<string, Record<string, unknown>>()
683
+ const configs: RollupOptions[] = []
684
+ const external = externalResolver(packageJSON)
685
+
686
+ let collectConfigTotal = 0
687
+ const collected = Promise.withResolvers<void>()
688
+ const internalModules = new Set<string>()
689
+ const internalModuleCollect = (id?: string) => {
690
+ if (!id) return
691
+ internalModules.add(id)
692
+ }
693
+ const collectPlugin: Plugin = {
694
+ name: 'jiek:collect',
695
+ buildStart() {
696
+ collectConfigTotal++
697
+ },
698
+ buildEnd() {
699
+ if (--collectConfigTotal === 0) {
700
+ collected.resolve()
701
+ }
593
702
  }
594
- return false
595
- })
703
+ }
704
+
705
+ if (entrypoints) {
706
+ const [filteredResolvedEntrypoints, exports] = resolveExports({
707
+ entrypoints,
708
+ pkgIsModule,
709
+ entries,
710
+ pkgName: JIEK_NAME!,
711
+ outdir: jsOutdir,
712
+ config,
713
+ skipJS: skipJS ?? SKIP_JS
714
+ })
715
+ getAllLeafs(filteredResolvedEntrypoints as RecursiveRecord<string>, ({ keys, value }) => {
716
+ if (typeof value === 'string') {
717
+ const keysArr = leafMap.get(value) ?? []
718
+ leafMap.set(value, keysArr)
719
+ inputExports.set(value, exports)
720
+ keysArr.push(keys)
721
+ }
722
+ return false
723
+ })
724
+ }
725
+ if (bin) {
726
+ ;[...new Set(typeof bin === 'string' ? [bin] : Object.values(bin))]
727
+ .filter(binFile => binFile.startsWith('bin'))
728
+ .map(binFile => [
729
+ `./src/${binFile.replace(/(\.[cm]?)js$/, '$1ts')}`,
730
+ `./dist/${binFile}`
731
+ ])
732
+ .forEach(([input, output]) => {
733
+ configs.push(...generateConfigs({
734
+ path: output,
735
+ name,
736
+ input,
737
+ output,
738
+ external,
739
+ pkgIsModule,
740
+ conditionals: output.endsWith('.mjs')
741
+ ? ['import']
742
+ : output.endsWith('.cjs')
743
+ ? ['require']
744
+ : []
745
+ }, {
746
+ internalModuleCollect,
747
+ disableDTS: true,
748
+ disableMinify: true,
749
+ commonPlugins: [
750
+ collectPlugin
751
+ ]
752
+ }))
753
+ leafMap.set(input, [[output]])
754
+ inputTags.set(input, 'binary')
755
+ })
756
+ }
757
+ if (internalEntrypoints) {
758
+ const [filteredResolvedInternalEntrypoints, imports] = resolveExports({
759
+ entrypoints: internalEntrypoints,
760
+ pkgIsModule,
761
+ pkgName: JIEK_NAME!,
762
+ outdir: `${jsOutdir}/.internal`,
763
+ config,
764
+ skipJS: SKIP_JS ?? skipJS
765
+ })
766
+ getAllLeafs(filteredResolvedInternalEntrypoints as RecursiveRecord<string>, ({ keys, value }) => {
767
+ if (typeof value === 'string') {
768
+ const keysArr = leafMap.get(value) ?? []
769
+ leafMap.set(value, keysArr)
770
+ inputExports.set(value, imports)
771
+ inputTags.set(value, 'internal')
772
+ keysArr.push(keys)
773
+ }
774
+ return false
775
+ })
776
+ }
777
+
778
+ leafMap.forEach((keysArr, input) => {
779
+ if (inputTags.get(input) === 'binary') return
780
+ const isInternal = inputTags.get(input) === 'internal'
781
+ const exports = inputExports.get(input)!
596
782
 
597
- const configs: RollupOptions[] = []
598
- leafMap.forEach((keysArr, input) =>
599
783
  keysArr.forEach((keys) => {
600
784
  const [path, ...conditionals] = keys
601
785
 
602
786
  const name = packageName + (path === '.' ? '' : pascalCase(path))
603
787
  const keyExports = reveal(exports, keys)
604
- const commonOptions = {
788
+ const commonContext = {
605
789
  path,
606
790
  name,
607
791
  input,
608
792
  external,
609
793
  pkgIsModule
610
794
  }
795
+ const commonOptions: GenerateConfigsOptions = isInternal
796
+ ? {
797
+ commonPlugins: [
798
+ {
799
+ name: 'jiek:loadInternalModules',
800
+ async options(inputOptions) {
801
+ await collected.promise
802
+ inputOptions.input = [...intersection(
803
+ Array.isArray(inputOptions.input)
804
+ ? inputOptions.input
805
+ : [inputOptions.input as string],
806
+ internalModules
807
+ )]
808
+ if (inputOptions.input.length === 0) {
809
+ inputOptions.input = '__jiek_empty__'
810
+ const plugins = await inputOptions.plugins
811
+ if (!Array.isArray(plugins)) {
812
+ throw new TypeError('plugins is not an array')
813
+ }
814
+ inputOptions.plugins = plugins.filter(
815
+ p =>
816
+ typeof p !== 'object'
817
+ ? true
818
+ : p === null
819
+ ? true
820
+ : 'name' in p && p.name === 'jiek:loadInternalModules'
821
+ )
822
+ }
823
+ return inputOptions
824
+ },
825
+ resolveId: {
826
+ order: 'post',
827
+ handler(id) {
828
+ if (id === '__jiek_empty__') return id
829
+ }
830
+ },
831
+ load: {
832
+ order: 'post',
833
+ handler(id) {
834
+ if (id === '__jiek_empty__') return ''
835
+ }
836
+ }
837
+ }
838
+ ],
839
+ disableCollectInternalModule: true
840
+ }
841
+ : {
842
+ internalModuleCollect,
843
+ commonPlugins: [
844
+ collectPlugin
845
+ ]
846
+ }
611
847
 
612
848
  // eslint-disable-next-line ts/switch-exhaustiveness-check
613
849
  switch (typeof keyExports) {
614
850
  case 'string': {
615
851
  configs.push(...generateConfigs({
616
- ...commonOptions,
852
+ ...commonContext,
617
853
  output: keyExports,
618
854
  conditionals
619
- }, build))
855
+ }, commonOptions))
620
856
  break
621
857
  }
622
858
  case 'object': {
@@ -624,10 +860,10 @@ export function template(packageJSON: PackageJSON): RollupOptions[] {
624
860
  const allConditionals = [...new Set([...conditionals, ...nextKeys])]
625
861
  if (typeof value === 'string') {
626
862
  configs.push(...generateConfigs({
627
- ...commonOptions,
863
+ ...commonContext,
628
864
  output: value,
629
865
  conditionals: allConditionals
630
- }, build))
866
+ }, commonOptions))
631
867
  }
632
868
  return false
633
869
  })
@@ -635,7 +871,8 @@ export function template(packageJSON: PackageJSON): RollupOptions[] {
635
871
  }
636
872
  }
637
873
  })
638
- )
874
+ })
875
+
639
876
  void publish('init', { leafMap, targetsLength: configs.length })
640
877
  return configs.map(c => ({
641
878
  ...COMMON_OPTIONS,