jiek 1.0.8 → 1.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,3 @@
1
- import '../rollup/base'
2
-
3
1
  import fs from 'node:fs'
4
2
  import { createRequire } from 'node:module'
5
3
  import path from 'node:path'
@@ -9,11 +7,24 @@ import { program } from 'commander'
9
7
  import { execaCommand } from 'execa'
10
8
 
11
9
  import { actionDone, actionRestore } from '../inner'
12
- import type { RollupProgressEvent } from '../rollup/base'
10
+ import type { RollupProgressEvent, TemplateOptions } from '../rollup/base'
13
11
  import { getSelectedProjectsGraph } from '../utils/filterSupport'
14
12
  import { loadConfig } from '../utils/loadConfig'
15
13
  import { tsRegisterName } from '../utils/tsRegister'
16
14
 
15
+ declare module 'jiek' {
16
+ export interface Config {
17
+ build?: TemplateOptions & {
18
+ /**
19
+ * Whether to run in silent mode, only active when configured in the workspace root or cwd.
20
+ *
21
+ * @default false
22
+ */
23
+ silent?: boolean
24
+ }
25
+ }
26
+ }
27
+
17
28
  const FILE_TEMPLATE = (manifest: unknown) => (`
18
29
  module.exports = require('jiek/rollup').template(${JSON.stringify(manifest, null, 2)})
19
30
  `.trimStart())
@@ -78,16 +78,3 @@ export type RollupProgressEvent =
78
78
  message?: string
79
79
  }
80
80
  }
81
-
82
- declare module 'jiek' {
83
- export interface Config {
84
- build?: TemplateOptions & {
85
- /**
86
- * Whether to run in silent mode, only active when configured in the workspace root or cwd.
87
- *
88
- * @default false
89
- */
90
- silent?: boolean
91
- }
92
- }
93
- }
@@ -1,4 +1,4 @@
1
- import '../rollup/base'
1
+ import './base'
2
2
 
3
3
  import fs from 'node:fs'
4
4
  import { dirname, relative, resolve } from 'node:path'
@@ -16,6 +16,8 @@ import type { InputPluginOption, OutputOptions, OutputPlugin, RollupOptions } fr
16
16
  import esbuild from 'rollup-plugin-esbuild'
17
17
  import ts from 'typescript'
18
18
 
19
+ import { recusiveListFiles } from '#~/utils/recusiveListFiles.ts'
20
+
19
21
  import { getExports } from '../utils/getExports'
20
22
  import { loadConfig } from '../utils/loadConfig'
21
23
  import { getCompilerOptionsByFilePath } from '../utils/ts'
@@ -58,9 +60,6 @@ const jsOutdir = `./${
58
60
 
59
61
  const STYLE_REGEXP = /\.(css|s[ac]ss|less|styl)$/
60
62
 
61
- // eslint-disable-next-line unused-imports/no-unused-vars
62
- const debug = (...args: unknown[]) => sendMessage({ type: 'debug', data: args } satisfies RollupProgressEvent)
63
-
64
63
  const resolveBuildPlugins = (context: ConfigGenerateContext, plugins: TemplateOptions['plugins']): {
65
64
  js: InputPluginOption
66
65
  dts: InputPluginOption
@@ -107,7 +106,7 @@ const withMinify = (
107
106
  plugins?: OutputPlugin[]
108
107
  },
109
108
  minify = build?.output?.minify
110
- ) =>
109
+ ): OutputOptions[] =>
111
110
  minify === false
112
111
  ? [output]
113
112
  : minify === 'only-minify'
@@ -115,7 +114,12 @@ const withMinify = (
115
114
  ...output,
116
115
  // TODO replace suffix when pubish to npm and the `build.output.minify` is 'only-minify'
117
116
  // TODO resolve dts output file name
118
- file: output.file?.replace(/(\.[cm]?js)$/, '.min$1'),
117
+ entryFileNames: chunkInfo =>
118
+ typeof output.entryFileNames === 'function'
119
+ ? output.entryFileNames(chunkInfo).replace(/(\.[cm]?js)$/, '.min$1')
120
+ : (() => {
121
+ throw new Error('entryFileNames must be a function')
122
+ })(),
119
123
  plugins: [
120
124
  ...(output.plugins ?? []),
121
125
  terser()
@@ -125,6 +129,12 @@ const withMinify = (
125
129
  output,
126
130
  {
127
131
  ...output,
132
+ entryFileNames: chunkInfo =>
133
+ typeof output.entryFileNames === 'function'
134
+ ? output.entryFileNames(chunkInfo).replace(/(\.[cm]?js)$/, '.min$1')
135
+ : (() => {
136
+ throw new Error('entryFileNames must be a function')
137
+ })(),
128
138
  file: output.file?.replace(/(\.[cm]?js)$/, '.min$1'),
129
139
  plugins: [
130
140
  ...(output.plugins ?? []),
@@ -176,15 +186,34 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
176
186
  }
177
187
  const outdir = options?.output?.dir
178
188
  const { js: jsPlugins, dts: dtsPlugins } = resolveBuildPlugins(context, build.plugins)
189
+ if (input.includes('**')) {
190
+ throw new Error(
191
+ 'input should not include "**", please read the [documentation](https://nodejs.org/api/packages.html#subpath-patterns).'
192
+ )
193
+ }
194
+ const inputObj = !input.includes('*')
195
+ ? input
196
+ : recusiveListFiles(process.cwd())
197
+ .filter(p => /(?<!\.d)\.[cm]?tsx?$/.test(p))
198
+ const globCommonDir = input.includes('*')
199
+ ? input.split('*')[0]
200
+ : ''
179
201
  return [
180
202
  {
181
- input,
203
+ input: inputObj,
182
204
  external,
183
205
  output: [
184
206
  ...withMinify({
185
- file: output,
207
+ dir: jsOutdir,
186
208
  name,
187
209
  interop: 'auto',
210
+ entryFileNames: (chunkInfo) => (
211
+ Array.isArray(inputObj)
212
+ ? chunkInfo.facadeModuleId!.replace(`${process.cwd()}/`, './')
213
+ .replace(globCommonDir, '')
214
+ .replace(/(\.[cm]?)ts$/, '$1js')
215
+ : output.replace(`${jsOutdir}/`, '')
216
+ ),
188
217
  sourcemap: typeof options?.output?.sourcemap === 'object'
189
218
  ? options.output.sourcemap.js
190
219
  : options?.output?.sourcemap,
@@ -227,7 +256,7 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
227
256
  ]
228
257
  },
229
258
  {
230
- input,
259
+ input: inputObj,
231
260
  external,
232
261
  output: [
233
262
  {
@@ -235,10 +264,15 @@ const generateConfigs = (context: ConfigGenerateContext, options: TemplateOption
235
264
  sourcemap: typeof options?.output?.sourcemap === 'object'
236
265
  ? options.output.sourcemap.dts
237
266
  : options?.output?.sourcemap,
238
- entryFileNames: () =>
239
- output
240
- .replace(`${jsOutdir}/`, '')
241
- .replace(/(\.[cm]?)js$/, '.d$1ts'),
267
+ entryFileNames: (chunkInfo) => (
268
+ Array.isArray(inputObj)
269
+ ? chunkInfo.facadeModuleId!.replace(`${process.cwd()}/`, './')
270
+ .replace(globCommonDir, '')
271
+ .replace(/(\.[cm]?)ts$/, '.d$1ts')
272
+ : output
273
+ .replace(`${jsOutdir}/`, '')
274
+ .replace(/(\.[cm]?)js$/, '.d$1ts')
275
+ ),
242
276
  strict: typeof options?.output?.strict === 'object'
243
277
  ? options.output.strict.dts
244
278
  : options?.output?.strict
@@ -0,0 +1,13 @@
1
+ import fs from 'node:fs'
2
+ import { resolve } from 'node:path'
3
+
4
+ export const recusiveListFiles = (dir: string): string[] =>
5
+ fs.readdirSync(dir).reduce((acc, file) => {
6
+ const filePath = resolve(dir, file)
7
+ if (fs.statSync(filePath).isDirectory()) {
8
+ if (filePath.endsWith('/node_modules')) return acc
9
+
10
+ return [...acc, ...recusiveListFiles(filePath)]
11
+ }
12
+ return [...acc, filePath]
13
+ }, [] as string[])
@@ -1,75 +0,0 @@
1
- import path from 'node:path'
2
-
3
- import { type Options, pkger } from '@jiek/pkger'
4
- import { commondir } from '@jiek/utils/commondir'
5
- import type { Manifest } from '@pnpm/workspace.pkgs-graph'
6
-
7
- export function mergePackageJson(manifest: Manifest & {
8
- jiek?: Options
9
- exports?: unknown | unknown[]
10
- }, cwd: string, options: {
11
- excludeDistInExports?: boolean
12
- } = {}) {
13
- const {
14
- excludeDistInExports = false
15
- } = options
16
- const {
17
- jiek: { cwd: _, ...jiek } = {}
18
- } = manifest
19
- const { outdir = 'dist' } = jiek
20
- let { exports } = manifest
21
- let includeIndex = false
22
- if (typeof exports === 'string') {
23
- includeIndex = true
24
- exports = { '.': exports }
25
- }
26
- if (exports === undefined) {
27
- exports = { '.': './src/index.ts' }
28
- }
29
- if (typeof exports === 'object') {
30
- if (Array.isArray(exports) && exports.length > 0) {
31
- includeIndex = true
32
- } else {
33
- includeIndex = !!(<Record<string, unknown>>exports)['.']
34
- }
35
- }
36
- let inputs = Array.isArray(exports)
37
- ? exports as string[]
38
- : Object
39
- .entries(<Record<string, unknown>>exports)
40
- .reduce((acc, [key, value]) => {
41
- if (typeof value === 'string') return key === '.'
42
- ? [value, ...acc]
43
- : acc.concat(value)
44
- if (Array.isArray(value)) return acc.concat(value)
45
-
46
- throw new TypeError(`Unexpected value type for key "${key}" in exports, expected string, got ${typeof value}`)
47
- }, [] as string[])
48
- if (excludeDistInExports) {
49
- inputs = inputs.filter(input => !input.startsWith(`./${outdir}`) && !input.startsWith(outdir))
50
- }
51
- if (inputs.length === 0)
52
- throw new Error('No inputs found')
53
-
54
- const absoluteInputs = inputs.map(input => path.isAbsolute(input)
55
- ? input
56
- : path.resolve(cwd, input)
57
- )
58
- let cDir = path.dirname(absoluteInputs[0])
59
- if (absoluteInputs.length > 1) {
60
- cDir = commondir(absoluteInputs, cwd)
61
- }
62
- const resolvedInputs = absoluteInputs.map(input => {
63
- return path.relative(cDir, input)
64
- })
65
- return {
66
- ...manifest,
67
- ...pkger({
68
- cwd,
69
- noIndex: !includeIndex,
70
- source: path.relative(cwd, cDir),
71
- inputs: resolvedInputs,
72
- ...jiek
73
- })
74
- }
75
- }