jiek 2.0.1 → 2.0.2-alpha.10
Sign up to get free protection for your applications and to get access to all the features.
- package/{dist/cli-only-build.cjs → cli-only-build.cjs} +19 -87
- package/{dist/cli-only-build.js → cli-only-build.js} +19 -87
- package/{dist/cli.cjs → cli.cjs} +385 -531
- package/cli.d.cts +14 -0
- package/cli.d.ts +14 -0
- package/{dist/cli.js → cli.js} +385 -530
- package/{dist/index.d.cts → index.d.cts} +0 -53
- package/{dist/index.d.ts → index.d.ts} +0 -53
- package/package.json +29 -36
- package/{dist/rollup → rollup}/index.cjs +2 -2
- package/{dist/rollup → rollup}/index.js +2 -2
- package/README.md +0 -78
- package/bin/jiek-build.js +0 -16
- package/bin/jiek.js +0 -13
- package/dist/cli.d.cts +0 -67
- package/dist/cli.d.ts +0 -67
- package/src/cli-only-build.ts +0 -7
- package/src/cli.ts +0 -3
- package/src/commands/base.ts +0 -18
- package/src/commands/build.ts +0 -462
- package/src/commands/descriptions.ts +0 -17
- package/src/commands/init.ts +0 -373
- package/src/commands/meta.ts +0 -5
- package/src/commands/publish.ts +0 -204
- package/src/index.ts +0 -8
- package/src/inner.ts +0 -11
- package/src/rollup/base.ts +0 -137
- package/src/rollup/index.ts +0 -565
- package/src/rollup/plugins/progress.ts +0 -26
- package/src/rollup/plugins/skip.ts +0 -21
- package/src/rollup/utils/commonOptions.ts +0 -9
- package/src/rollup/utils/externalResolver.ts +0 -35
- package/src/rollup/utils/globalResolver.ts +0 -13
- package/src/rollup/utils/withMinify.ts +0 -18
- package/src/utils/filterSupport.ts +0 -91
- package/src/utils/getExports.ts +0 -140
- package/src/utils/getRoot.ts +0 -16
- package/src/utils/getWD.ts +0 -31
- package/src/utils/loadConfig.ts +0 -111
- package/src/utils/recusiveListFiles.ts +0 -13
- package/src/utils/ts.ts +0 -94
- package/src/utils/tsRegister.ts +0 -26
- /package/{dist/cli-only-build.d.cts → cli-only-build.d.cts} +0 -0
- /package/{dist/cli-only-build.d.ts → cli-only-build.d.ts} +0 -0
- /package/{dist/index.cjs → index.cjs} +0 -0
- /package/{dist/index.js → index.js} +0 -0
- /package/{dist/rollup → rollup}/index.d.cts +0 -0
- /package/{dist/rollup → rollup}/index.d.ts +0 -0
package/src/rollup/base.ts
DELETED
@@ -1,137 +0,0 @@
|
|
1
|
-
import type { InputPluginOption, OutputOptions } from 'rollup'
|
2
|
-
|
3
|
-
export type Mapping2ROO<K extends keyof OutputOptions> = OutputOptions[K] | {
|
4
|
-
js?: OutputOptions[K]
|
5
|
-
dts?: OutputOptions[K]
|
6
|
-
}
|
7
|
-
|
8
|
-
export interface ConfigGenerateContext {
|
9
|
-
path: string
|
10
|
-
name: string
|
11
|
-
input: string
|
12
|
-
output: string
|
13
|
-
external: (string | RegExp)[]
|
14
|
-
pkgIsModule: boolean
|
15
|
-
conditionals: string[]
|
16
|
-
}
|
17
|
-
|
18
|
-
export type OutputControl = boolean | ((context: ConfigGenerateContext) => boolean)
|
19
|
-
|
20
|
-
export const BUILDER_TYPES = ['esbuild', 'swc'] as const
|
21
|
-
|
22
|
-
export const BUILDER_TYPE_PACKAGE_NAME_MAP = {
|
23
|
-
esbuild: 'rollup-plugin-esbuild',
|
24
|
-
swc: 'rollup-plugin-swc3'
|
25
|
-
}
|
26
|
-
|
27
|
-
export interface TemplateOptions {
|
28
|
-
/**
|
29
|
-
* When the user configures type: module, the generated output from entry points that don't
|
30
|
-
* have cts as a suffix will automatically include the CJS version.
|
31
|
-
* if it is not configured, and the generated output from entry points that do not have mts
|
32
|
-
* as a suffix will automatically include the ESM version.
|
33
|
-
*
|
34
|
-
* @default true
|
35
|
-
*/
|
36
|
-
crossModuleConvertor?: boolean
|
37
|
-
/**
|
38
|
-
* Auto-detect the builder from the installed dependencies.
|
39
|
-
* If the builder is not installed, it will prompt the user to install it.
|
40
|
-
* If exists multiple builders, it will fall back to the 'esbuild'.
|
41
|
-
*
|
42
|
-
* @default 'esbuild'
|
43
|
-
*/
|
44
|
-
builder?:
|
45
|
-
| typeof BUILDER_TYPES[number]
|
46
|
-
| ({
|
47
|
-
type: 'esbuild'
|
48
|
-
} & import('rollup-plugin-esbuild').Options)
|
49
|
-
| ({
|
50
|
-
type: 'swc'
|
51
|
-
} & import('rollup-plugin-swc3').PluginOptions)
|
52
|
-
output?: {
|
53
|
-
/**
|
54
|
-
* @default true
|
55
|
-
*
|
56
|
-
* When minify is set to true, the output will with minified files.
|
57
|
-
* When minify is set to 'only-minify', the output will direct output minified files.
|
58
|
-
*/
|
59
|
-
minify?: boolean | 'only-minify'
|
60
|
-
minifyOptions?:
|
61
|
-
| typeof BUILDER_TYPES[number]
|
62
|
-
| 'terser'
|
63
|
-
| (
|
64
|
-
{
|
65
|
-
type: 'terser'
|
66
|
-
} & import('@rollup/plugin-terser').Options
|
67
|
-
)
|
68
|
-
| (
|
69
|
-
{
|
70
|
-
type: 'esbuild'
|
71
|
-
} & Parameters<typeof import('rollup-plugin-esbuild').minify>[0]
|
72
|
-
)
|
73
|
-
| (
|
74
|
-
{
|
75
|
-
type: 'swc'
|
76
|
-
} & Parameters<typeof import('rollup-plugin-swc3').minify>[0]
|
77
|
-
)
|
78
|
-
/**
|
79
|
-
* @default 'dist'
|
80
|
-
*/
|
81
|
-
dir?: Mapping2ROO<'dir'>
|
82
|
-
sourcemap?: Mapping2ROO<'sourcemap'>
|
83
|
-
strict?: Mapping2ROO<'strict'>
|
84
|
-
js?: OutputControl
|
85
|
-
dts?: OutputControl
|
86
|
-
}
|
87
|
-
/**
|
88
|
-
* Set the external dependencies of the package.
|
89
|
-
*/
|
90
|
-
external?: (string | RegExp)[]
|
91
|
-
plugins?:
|
92
|
-
| InputPluginOption
|
93
|
-
| ((type: 'js' | 'dts', context: ConfigGenerateContext) => InputPluginOption)
|
94
|
-
| {
|
95
|
-
js: InputPluginOption
|
96
|
-
dts?: InputPluginOption
|
97
|
-
}
|
98
|
-
| {
|
99
|
-
js?: InputPluginOption
|
100
|
-
dts: InputPluginOption
|
101
|
-
}
|
102
|
-
}
|
103
|
-
|
104
|
-
export type RollupProgressEvent =
|
105
|
-
| {
|
106
|
-
type: 'init'
|
107
|
-
data: {
|
108
|
-
leafMap: Map<string, string[][]>
|
109
|
-
targetsLength: number
|
110
|
-
}
|
111
|
-
}
|
112
|
-
| {
|
113
|
-
type: 'watchChange'
|
114
|
-
data: {
|
115
|
-
id: string
|
116
|
-
name: string
|
117
|
-
path: string
|
118
|
-
input: string
|
119
|
-
}
|
120
|
-
}
|
121
|
-
| {
|
122
|
-
type: 'debug'
|
123
|
-
data: unknown
|
124
|
-
}
|
125
|
-
| {
|
126
|
-
type: 'progress'
|
127
|
-
data: {
|
128
|
-
// name, path, exportConditions, input
|
129
|
-
name: string
|
130
|
-
path: string
|
131
|
-
exportConditions: string[]
|
132
|
-
input: string
|
133
|
-
tags?: string[]
|
134
|
-
event?: string
|
135
|
-
message?: string
|
136
|
-
}
|
137
|
-
}
|
package/src/rollup/index.ts
DELETED
@@ -1,565 +0,0 @@
|
|
1
|
-
import fs from 'node:fs'
|
2
|
-
import { dirname, extname, relative, resolve } from 'node:path'
|
3
|
-
|
4
|
-
import type { RecursiveRecord } from '@jiek/pkger/entrypoints'
|
5
|
-
import { getAllLeafs } from '@jiek/pkger/entrypoints'
|
6
|
-
import { dts } from '@jiek/rollup-plugin-dts'
|
7
|
-
import { getWorkspaceDir } from '@jiek/utils/getWorkspaceDir'
|
8
|
-
import commonjs from '@rollup/plugin-commonjs'
|
9
|
-
import json from '@rollup/plugin-json'
|
10
|
-
import { nodeResolve } from '@rollup/plugin-node-resolve'
|
11
|
-
import { sendMessage } from 'execa'
|
12
|
-
import { isMatch } from 'micromatch'
|
13
|
-
import type { InputPluginOption, OutputOptions, OutputPlugin, Plugin, RollupOptions } from 'rollup'
|
14
|
-
import ts from 'typescript'
|
15
|
-
|
16
|
-
import { recusiveListFiles } from '#~/utils/recusiveListFiles.ts'
|
17
|
-
|
18
|
-
import { getExports, getOutDirs } from '../utils/getExports'
|
19
|
-
import { loadConfig } from '../utils/loadConfig'
|
20
|
-
import { getCompilerOptionsByFilePath } from '../utils/ts'
|
21
|
-
import type { ConfigGenerateContext, RollupProgressEvent, TemplateOptions } from './base'
|
22
|
-
import progress from './plugins/progress'
|
23
|
-
import skip from './plugins/skip'
|
24
|
-
import externalResolver from './utils/externalResolver'
|
25
|
-
|
26
|
-
interface PackageJSON {
|
27
|
-
name?: string
|
28
|
-
type?: string
|
29
|
-
exports?: Record<string, unknown> | string | string[]
|
30
|
-
}
|
31
|
-
|
32
|
-
const {
|
33
|
-
JIEK_ROOT,
|
34
|
-
JIEK_NAME,
|
35
|
-
JIEK_BUILDER,
|
36
|
-
JIEK_ENTRIES,
|
37
|
-
JIEK_EXTERNAL,
|
38
|
-
JIEK_WITHOUT_JS,
|
39
|
-
JIEK_WITHOUT_DTS,
|
40
|
-
JIEK_WITHOUT_MINIFY,
|
41
|
-
JIEK_MINIFY_TYPE,
|
42
|
-
JIEK_NO_CLEAN,
|
43
|
-
JIEK_ONLY_MINIFY,
|
44
|
-
JIEK_TSCONFIG,
|
45
|
-
JIEK_DTSCONFIG
|
46
|
-
} = process.env
|
47
|
-
|
48
|
-
const resolveArrayString = (str: string | undefined) => {
|
49
|
-
const arr = [
|
50
|
-
...new Set(
|
51
|
-
str
|
52
|
-
?.split(',')
|
53
|
-
.map(e => e.trim())
|
54
|
-
.filter(e => e.length > 0)
|
55
|
-
?? []
|
56
|
-
)
|
57
|
-
]
|
58
|
-
return arr?.length ? arr : undefined
|
59
|
-
}
|
60
|
-
|
61
|
-
const entries = resolveArrayString(JIEK_ENTRIES)?.map(e => ({ 'index': '.' }[e] ?? e))
|
62
|
-
|
63
|
-
const commandExternal = resolveArrayString(JIEK_EXTERNAL)?.map(e => new RegExp(`^${e}$`))
|
64
|
-
|
65
|
-
const WORKSPACE_ROOT = JIEK_ROOT ?? getWorkspaceDir()
|
66
|
-
const COMMON_OPTIONS = {} satisfies RollupOptions
|
67
|
-
const COMMON_PLUGINS = [
|
68
|
-
json()
|
69
|
-
]
|
70
|
-
|
71
|
-
const WITHOUT_JS = JIEK_WITHOUT_JS === 'true'
|
72
|
-
const WITHOUT_DTS = JIEK_WITHOUT_DTS === 'true'
|
73
|
-
const WITHOUT_MINIFY = JIEK_WITHOUT_MINIFY === 'true'
|
74
|
-
|
75
|
-
const ONLY_MINIFY = JIEK_ONLY_MINIFY === 'true'
|
76
|
-
|
77
|
-
const CLEAN = JIEK_NO_CLEAN !== 'true'
|
78
|
-
|
79
|
-
const MINIFY_DEFAULT_VALUE = WITHOUT_MINIFY
|
80
|
-
? false
|
81
|
-
: ONLY_MINIFY
|
82
|
-
? 'only-minify'
|
83
|
-
: true
|
84
|
-
|
85
|
-
type BuilderOptions = NonNullable<TemplateOptions['builder']>
|
86
|
-
|
87
|
-
const BUILDER_OPTIONS = {
|
88
|
-
type: JIEK_BUILDER ?? 'esbuild'
|
89
|
-
} as NonNullable<Exclude<BuilderOptions, string>>
|
90
|
-
|
91
|
-
type MinifyOptions = NonNullable<TemplateOptions['output']>['minifyOptions']
|
92
|
-
|
93
|
-
const MINIFY_OPTIONS = {
|
94
|
-
type: JIEK_MINIFY_TYPE ?? 'esbuild'
|
95
|
-
} as NonNullable<Exclude<MinifyOptions, string>>
|
96
|
-
|
97
|
-
const config = loadConfig({
|
98
|
-
root: WORKSPACE_ROOT
|
99
|
-
}) ?? {}
|
100
|
-
const { build = {} } = config
|
101
|
-
const { js: jsOutdir, dts: dtsOutdir } = getOutDirs({
|
102
|
-
config,
|
103
|
-
pkgName: JIEK_NAME
|
104
|
-
})
|
105
|
-
|
106
|
-
if (CLEAN) {
|
107
|
-
fs.existsSync(jsOutdir) && fs.rmdirSync(jsOutdir, { recursive: true })
|
108
|
-
fs.existsSync(dtsOutdir) && fs.rmdirSync(dtsOutdir, { recursive: true })
|
109
|
-
}
|
110
|
-
|
111
|
-
const STYLE_REGEXP = /\.(css|s[ac]ss|less|styl)$/
|
112
|
-
|
113
|
-
const resolveBuildPlugins = (context: ConfigGenerateContext, plugins: TemplateOptions['plugins']): {
|
114
|
-
js: InputPluginOption
|
115
|
-
dts: InputPluginOption
|
116
|
-
} => {
|
117
|
-
if (plugins === false || plugins === undefined || plugins === null) {
|
118
|
-
return { js: [], dts: [] }
|
119
|
-
}
|
120
|
-
let js: InputPluginOption = []
|
121
|
-
let dts: InputPluginOption = []
|
122
|
-
switch (typeof plugins) {
|
123
|
-
case 'function':
|
124
|
-
js = plugins('js', context)
|
125
|
-
dts = plugins('dts', context)
|
126
|
-
break
|
127
|
-
case 'object':
|
128
|
-
if ('js' in plugins || 'dts' in plugins) {
|
129
|
-
js = plugins.js ?? []
|
130
|
-
dts = plugins.dts ?? []
|
131
|
-
} else {
|
132
|
-
js = plugins
|
133
|
-
dts = plugins
|
134
|
-
}
|
135
|
-
break
|
136
|
-
}
|
137
|
-
return { js, dts }
|
138
|
-
}
|
139
|
-
|
140
|
-
const resolveOutputControls = (
|
141
|
-
context: ConfigGenerateContext,
|
142
|
-
output: TemplateOptions['output']
|
143
|
-
): { js: boolean; dts: boolean } => ({
|
144
|
-
js: typeof output?.js === 'boolean'
|
145
|
-
? output.js
|
146
|
-
: typeof output?.js === 'function'
|
147
|
-
? output.js(context)
|
148
|
-
: true,
|
149
|
-
dts: typeof output?.dts === 'boolean'
|
150
|
-
? output.dts
|
151
|
-
: typeof output?.dts === 'function'
|
152
|
-
? output.dts(context)
|
153
|
-
: true
|
154
|
-
})
|
155
|
-
|
156
|
-
const resolveWorkspacePath = (p: string) => resolve(WORKSPACE_ROOT, p)
|
157
|
-
|
158
|
-
const pascalCase = (str: string) =>
|
159
|
-
str
|
160
|
-
.replace(/[@|/-](\w)/g, (_, $1) => $1.toUpperCase())
|
161
|
-
.replace(/(?:^|-)(\w)/g, (_, $1) => $1.toUpperCase())
|
162
|
-
|
163
|
-
const reveal = (obj: string | Record<string, unknown>, keys: string[]) =>
|
164
|
-
keys.reduce((acc, key) => {
|
165
|
-
if (typeof acc === 'string') throw new Error('key not found in exports')
|
166
|
-
if (!(key in acc)) throw new Error(`key ${key} not found in exports`)
|
167
|
-
return acc[key] as string | Record<string, unknown>
|
168
|
-
}, obj)
|
169
|
-
|
170
|
-
const resolveMinifyOptions = (minifyOptions: MinifyOptions): typeof MINIFY_OPTIONS =>
|
171
|
-
typeof minifyOptions === 'string'
|
172
|
-
? { type: minifyOptions }
|
173
|
-
: minifyOptions ?? { type: 'esbuild' }
|
174
|
-
|
175
|
-
const resolveBuilderOptions = (
|
176
|
-
builder: TemplateOptions['builder']
|
177
|
-
): Exclude<TemplateOptions['builder'], string | undefined> =>
|
178
|
-
typeof builder === 'string'
|
179
|
-
? { type: builder }
|
180
|
-
: builder ?? { type: 'esbuild' }
|
181
|
-
|
182
|
-
const resolvedMinifyOptions = resolveMinifyOptions(build.output?.minifyOptions ?? MINIFY_OPTIONS)
|
183
|
-
const { type: _resolvedMinifyOptionsType, ...noTypeResolvedMinifyOptions } = resolvedMinifyOptions
|
184
|
-
const resolvedBuilderOptions = resolveBuilderOptions(build.builder ?? BUILDER_OPTIONS)
|
185
|
-
const { type: _resolvedBuilderOptionsType, ...noTypeResolvedBuilderOptions } = resolvedBuilderOptions
|
186
|
-
|
187
|
-
const withMinify = (
|
188
|
-
output: OutputOptions & {
|
189
|
-
plugins?: OutputPlugin[]
|
190
|
-
},
|
191
|
-
minify = build?.output?.minify ?? MINIFY_DEFAULT_VALUE
|
192
|
-
): OutputOptions[] => {
|
193
|
-
if (minify === false) return [output]
|
194
|
-
|
195
|
-
const minifyPlugin = resolvedMinifyOptions.type === 'esbuild'
|
196
|
-
? import('rollup-plugin-esbuild').then(({ minify }) => minify(noTypeResolvedMinifyOptions as any))
|
197
|
-
: resolvedMinifyOptions.type === 'swc'
|
198
|
-
? import('rollup-plugin-swc3').then(({ minify }) => minify(noTypeResolvedMinifyOptions as any))
|
199
|
-
: import('@rollup/plugin-terser').then(({ default: minify }) => minify(noTypeResolvedMinifyOptions as any))
|
200
|
-
return minify === 'only-minify'
|
201
|
-
? [{
|
202
|
-
...output,
|
203
|
-
// TODO replace suffix when pubish to npm and the `build.output.minify` is 'only-minify'
|
204
|
-
// TODO resolve dts output file name
|
205
|
-
entryFileNames: chunkInfo =>
|
206
|
-
typeof output.entryFileNames === 'function'
|
207
|
-
? output.entryFileNames(chunkInfo)
|
208
|
-
: (() => {
|
209
|
-
throw new Error('entryFileNames must be a function')
|
210
|
-
})(),
|
211
|
-
plugins: [
|
212
|
-
...(output.plugins ?? []),
|
213
|
-
minifyPlugin
|
214
|
-
]
|
215
|
-
}]
|
216
|
-
: [
|
217
|
-
output,
|
218
|
-
{
|
219
|
-
...output,
|
220
|
-
entryFileNames: chunkInfo =>
|
221
|
-
typeof output.entryFileNames === 'function'
|
222
|
-
? output.entryFileNames(chunkInfo).replace(/(\.[cm]?js)$/, '.min$1')
|
223
|
-
: (() => {
|
224
|
-
throw new Error('entryFileNames must be a function')
|
225
|
-
})(),
|
226
|
-
file: output.file?.replace(/(\.[cm]?js)$/, '.min$1'),
|
227
|
-
plugins: [
|
228
|
-
...(output.plugins ?? []),
|
229
|
-
minifyPlugin
|
230
|
-
]
|
231
|
-
}
|
232
|
-
]
|
233
|
-
}
|
234
|
-
|
235
|
-
const generateConfigs = (context: ConfigGenerateContext, options: TemplateOptions = {}): RollupOptions[] => {
|
236
|
-
const {
|
237
|
-
path,
|
238
|
-
name,
|
239
|
-
input,
|
240
|
-
output,
|
241
|
-
external: inputExternal,
|
242
|
-
pkgIsModule,
|
243
|
-
conditionals
|
244
|
-
} = context
|
245
|
-
const external = [...inputExternal, ...(options.external ?? []), ...(commandExternal ?? [])]
|
246
|
-
const isModule = conditionals.includes('import')
|
247
|
-
const isCommonJS = conditionals.includes('require')
|
248
|
-
const isBrowser = conditionals.includes('browser')
|
249
|
-
const dtsTSConfigPaths = [
|
250
|
-
resolveWorkspacePath('tsconfig.json'),
|
251
|
-
resolveWorkspacePath('tsconfig.dts.json')
|
252
|
-
]
|
253
|
-
JIEK_TSCONFIG && dtsTSConfigPaths.push(resolveWorkspacePath(JIEK_TSCONFIG))
|
254
|
-
JIEK_DTSCONFIG && dtsTSConfigPaths.push(resolveWorkspacePath(JIEK_DTSCONFIG))
|
255
|
-
const buildTSConfigPaths = [
|
256
|
-
...dtsTSConfigPaths,
|
257
|
-
resolveWorkspacePath('tsconfig.build.json')
|
258
|
-
]
|
259
|
-
// 这里重复写了俩次 JIEK_TSCONFIG 到 tsconfig 的加载列表中
|
260
|
-
// 目的是保证在 build 的时候,JIEK_TSCONFIG 的优先级高于 JIEK_DTSCONFIG
|
261
|
-
JIEK_TSCONFIG && buildTSConfigPaths.push(resolveWorkspacePath(JIEK_TSCONFIG))
|
262
|
-
let dtsTSConfigPath: string | undefined
|
263
|
-
dtsTSConfigPaths.forEach(p => {
|
264
|
-
if (fs.existsSync(p) && fs.statSync(p).isFile()) {
|
265
|
-
dtsTSConfigPath = p
|
266
|
-
}
|
267
|
-
})
|
268
|
-
let buildTSConfigPath: string | undefined
|
269
|
-
buildTSConfigPaths.forEach(p => {
|
270
|
-
if (fs.existsSync(p) && fs.statSync(p).isFile()) {
|
271
|
-
buildTSConfigPath = p
|
272
|
-
}
|
273
|
-
})
|
274
|
-
let compilerOptions: ts.CompilerOptions = {}
|
275
|
-
if (dtsTSConfigPath) {
|
276
|
-
const jsonCompilerOptions = getCompilerOptionsByFilePath(dtsTSConfigPath, resolve(input))
|
277
|
-
const { options, errors } = ts.convertCompilerOptionsFromJson(
|
278
|
-
jsonCompilerOptions,
|
279
|
-
dirname(dtsTSConfigPath)
|
280
|
-
)
|
281
|
-
if (errors.length > 0) {
|
282
|
-
throw new Error(errors.map(e => e.messageText).join('\n'))
|
283
|
-
}
|
284
|
-
compilerOptions = options
|
285
|
-
delete compilerOptions.composite
|
286
|
-
}
|
287
|
-
const exportConditions = [...conditionals, ...(compilerOptions.customConditions ?? [])]
|
288
|
-
const throughEventProps: RollupProgressEvent & { type: 'progress' } = {
|
289
|
-
type: 'progress',
|
290
|
-
data: { name, path, exportConditions, input }
|
291
|
-
}
|
292
|
-
const { js: jsPlugins, dts: dtsPlugins } = resolveBuildPlugins(context, build.plugins)
|
293
|
-
if (input.includes('**')) {
|
294
|
-
throw new Error(
|
295
|
-
'input should not include "**", please read the [documentation](https://nodejs.org/api/packages.html#subpath-patterns).'
|
296
|
-
)
|
297
|
-
}
|
298
|
-
const inputObj = !input.includes('*')
|
299
|
-
? input
|
300
|
-
: recusiveListFiles(process.cwd())
|
301
|
-
.filter(p => /(?<!\.d)\.[cm]?tsx?$/.test(p))
|
302
|
-
.map(p => relative(process.cwd(), p))
|
303
|
-
.filter(p => isMatch(p, input.slice(2)))
|
304
|
-
const globCommonDir = input.includes('*')
|
305
|
-
? input.split('*')[0].replace('./', '')
|
306
|
-
: ''
|
307
|
-
const pathCommonDir = path.includes('*')
|
308
|
-
? path.split('*')[0].replace('./', '')
|
309
|
-
: ''
|
310
|
-
if (
|
311
|
-
(globCommonDir.length > 0 && pathCommonDir.length === 0)
|
312
|
-
|| (globCommonDir.length === 0 && pathCommonDir.length > 0)
|
313
|
-
) {
|
314
|
-
throw new Error('input and path should both include "*" or not include "*"')
|
315
|
-
}
|
316
|
-
const jsOutputSuffix = extname(output)
|
317
|
-
const tsOutputSuffix = jsOutputSuffix.replace(/(\.[cm]?)js$/, '.d$1ts')
|
318
|
-
const { js: jsOutput, dts: dtsOutput } = resolveOutputControls(context, build.output)
|
319
|
-
const rollupOptions: RollupOptions[] = []
|
320
|
-
|
321
|
-
const commonPlugins: Plugin[] = []
|
322
|
-
if (jsOutput && !WITHOUT_JS) {
|
323
|
-
const sourcemap = typeof options?.output?.sourcemap === 'object'
|
324
|
-
? options.output.sourcemap.js
|
325
|
-
: options?.output?.sourcemap
|
326
|
-
const builder = resolvedBuilderOptions.type === 'esbuild'
|
327
|
-
? import('rollup-plugin-esbuild').then(({ default: esbuild }) =>
|
328
|
-
esbuild({
|
329
|
-
sourceMap: sourcemap === 'hidden' ? false : !!sourcemap,
|
330
|
-
tsconfig: buildTSConfigPath,
|
331
|
-
...noTypeResolvedBuilderOptions
|
332
|
-
})
|
333
|
-
)
|
334
|
-
: import('rollup-plugin-swc3').then(({ default: swc }) =>
|
335
|
-
swc({
|
336
|
-
sourceMaps: typeof sourcemap === 'boolean'
|
337
|
-
? sourcemap
|
338
|
-
: typeof sourcemap === 'undefined'
|
339
|
-
? undefined
|
340
|
-
: ({
|
341
|
-
hidden: false,
|
342
|
-
inline: 'inline'
|
343
|
-
} as const)[sourcemap] ?? undefined,
|
344
|
-
tsconfig: buildTSConfigPath,
|
345
|
-
...noTypeResolvedBuilderOptions
|
346
|
-
})
|
347
|
-
)
|
348
|
-
rollupOptions.push({
|
349
|
-
input: inputObj,
|
350
|
-
external,
|
351
|
-
output: [
|
352
|
-
...withMinify({
|
353
|
-
dir: jsOutdir,
|
354
|
-
name,
|
355
|
-
interop: 'auto',
|
356
|
-
entryFileNames: (chunkInfo) => (
|
357
|
-
Array.isArray(inputObj)
|
358
|
-
? chunkInfo.facadeModuleId!.replace(`${process.cwd()}/`, '')
|
359
|
-
.replace(globCommonDir, pathCommonDir)
|
360
|
-
.replace(/(\.[cm]?)ts$/, jsOutputSuffix)
|
361
|
-
: output.replace(`${jsOutdir}/`, '')
|
362
|
-
),
|
363
|
-
sourcemap,
|
364
|
-
format: isModule ? 'esm' : (
|
365
|
-
isCommonJS ? 'cjs' : (
|
366
|
-
isBrowser ? 'umd' : (
|
367
|
-
pkgIsModule ? 'esm' : 'cjs'
|
368
|
-
)
|
369
|
-
)
|
370
|
-
),
|
371
|
-
strict: typeof options?.output?.strict === 'object'
|
372
|
-
? options.output.strict.js
|
373
|
-
: options?.output?.strict
|
374
|
-
})
|
375
|
-
],
|
376
|
-
plugins: [
|
377
|
-
...commonPlugins,
|
378
|
-
nodeResolve({ exportConditions }),
|
379
|
-
import('rollup-plugin-postcss')
|
380
|
-
.then(({ default: postcss }) =>
|
381
|
-
postcss({
|
382
|
-
extract: resolve(output.replace(/\.[cm]?js$/, '.css')),
|
383
|
-
minimize: true
|
384
|
-
})
|
385
|
-
)
|
386
|
-
.catch(() => void 0),
|
387
|
-
builder,
|
388
|
-
commonjs(),
|
389
|
-
progress({
|
390
|
-
onEvent: (event, message) =>
|
391
|
-
sendMessage(
|
392
|
-
{
|
393
|
-
...throughEventProps,
|
394
|
-
data: { ...throughEventProps.data, event, message, tags: ['js'] }
|
395
|
-
} satisfies RollupProgressEvent
|
396
|
-
)
|
397
|
-
}),
|
398
|
-
jsPlugins
|
399
|
-
]
|
400
|
-
})
|
401
|
-
}
|
402
|
-
|
403
|
-
if (dtsOutput && !WITHOUT_DTS) {
|
404
|
-
rollupOptions.push({
|
405
|
-
input: inputObj,
|
406
|
-
external,
|
407
|
-
output: [
|
408
|
-
{
|
409
|
-
dir: dtsOutdir,
|
410
|
-
sourcemap: typeof options?.output?.sourcemap === 'object'
|
411
|
-
? options.output.sourcemap.dts
|
412
|
-
: options?.output?.sourcemap,
|
413
|
-
entryFileNames: (chunkInfo) => (
|
414
|
-
Array.isArray(inputObj)
|
415
|
-
? chunkInfo.facadeModuleId!.replace(`${process.cwd()}/`, '')
|
416
|
-
.replace(globCommonDir, pathCommonDir)
|
417
|
-
.replace(/(\.[cm]?)ts$/, tsOutputSuffix)
|
418
|
-
: output
|
419
|
-
.replace(`${jsOutdir}/`, '')
|
420
|
-
.replace(/(\.[cm]?)js$/, tsOutputSuffix)
|
421
|
-
),
|
422
|
-
strict: typeof options?.output?.strict === 'object'
|
423
|
-
? options.output.strict.dts
|
424
|
-
: options?.output?.strict
|
425
|
-
}
|
426
|
-
],
|
427
|
-
plugins: [
|
428
|
-
...commonPlugins,
|
429
|
-
nodeResolve({ exportConditions }),
|
430
|
-
skip({ patterns: [STYLE_REGEXP] }),
|
431
|
-
dts({
|
432
|
-
respectExternal: true,
|
433
|
-
compilerOptions: {
|
434
|
-
// temp directory, it not affect the output
|
435
|
-
// but if the user not set it and `declaration`, inputs can't generate any dts files when the input relative imports of `package.json`
|
436
|
-
outDir: 'dist',
|
437
|
-
declaration: true,
|
438
|
-
// https://github.com/Swatinem/rollup-plugin-dts/issues/143
|
439
|
-
preserveSymlinks: false,
|
440
|
-
// Expected '{', got 'type' (Note that you need plugins to import files that are not JavaScript)
|
441
|
-
// https://github.com/Swatinem/rollup-plugin-dts/issues/96
|
442
|
-
noEmit: false
|
443
|
-
},
|
444
|
-
tsconfig: dtsTSConfigPath
|
445
|
-
}),
|
446
|
-
progress({
|
447
|
-
onEvent: (event, message) =>
|
448
|
-
sendMessage(
|
449
|
-
{
|
450
|
-
...throughEventProps,
|
451
|
-
data: { ...throughEventProps.data, event, message, tags: ['dts'] }
|
452
|
-
} satisfies RollupProgressEvent
|
453
|
-
)
|
454
|
-
}),
|
455
|
-
dtsPlugins
|
456
|
-
]
|
457
|
-
})
|
458
|
-
}
|
459
|
-
if (rollupOptions.length > 0) {
|
460
|
-
// only push the first one a watcher plugin
|
461
|
-
rollupOptions[0].plugins = [
|
462
|
-
{
|
463
|
-
name: 'jiek-plugin-watcher',
|
464
|
-
watchChange: (id) =>
|
465
|
-
sendMessage(
|
466
|
-
{
|
467
|
-
type: 'watchChange',
|
468
|
-
data: { id, name: JIEK_NAME!, path, input }
|
469
|
-
} satisfies RollupProgressEvent
|
470
|
-
)
|
471
|
-
},
|
472
|
-
...(rollupOptions[0].plugins as any)
|
473
|
-
]
|
474
|
-
}
|
475
|
-
return rollupOptions
|
476
|
-
}
|
477
|
-
|
478
|
-
export function template(packageJSON: PackageJSON): RollupOptions[] {
|
479
|
-
const { name, type, exports: entrypoints } = packageJSON
|
480
|
-
const pkgIsModule = type === 'module'
|
481
|
-
if (!name) throw new Error('package.json name is required')
|
482
|
-
if (!entrypoints) throw new Error('package.json exports is required')
|
483
|
-
|
484
|
-
const packageName = pascalCase(name)
|
485
|
-
|
486
|
-
const external = externalResolver(packageJSON as Record<string, unknown>)
|
487
|
-
|
488
|
-
const [filteredResolvedEntrypoints, exports] = getExports({
|
489
|
-
entrypoints,
|
490
|
-
pkgIsModule,
|
491
|
-
entries,
|
492
|
-
pkgName: JIEK_NAME!,
|
493
|
-
outdir: jsOutdir,
|
494
|
-
config
|
495
|
-
})
|
496
|
-
|
497
|
-
const leafMap = new Map<string, string[][]>()
|
498
|
-
getAllLeafs(filteredResolvedEntrypoints as RecursiveRecord<string>, ({ keys, value }) => {
|
499
|
-
if (typeof value === 'string') {
|
500
|
-
const keysArr = leafMap.get(value) ?? []
|
501
|
-
leafMap.set(value, keysArr)
|
502
|
-
keysArr.push(keys)
|
503
|
-
}
|
504
|
-
return false
|
505
|
-
})
|
506
|
-
|
507
|
-
const configs: RollupOptions[] = []
|
508
|
-
leafMap.forEach((keysArr, input) =>
|
509
|
-
keysArr.forEach((keys) => {
|
510
|
-
const [path, ...conditionals] = keys
|
511
|
-
|
512
|
-
const name = packageName + (path === '.' ? '' : pascalCase(path))
|
513
|
-
const keyExports = reveal(exports, keys)
|
514
|
-
const commonOptions = {
|
515
|
-
path,
|
516
|
-
name,
|
517
|
-
input,
|
518
|
-
external,
|
519
|
-
pkgIsModule
|
520
|
-
}
|
521
|
-
|
522
|
-
switch (typeof keyExports) {
|
523
|
-
case 'string': {
|
524
|
-
configs.push(...generateConfigs({
|
525
|
-
...commonOptions,
|
526
|
-
output: keyExports,
|
527
|
-
conditionals
|
528
|
-
}, build))
|
529
|
-
break
|
530
|
-
}
|
531
|
-
case 'object': {
|
532
|
-
getAllLeafs(keyExports as RecursiveRecord<string>, ({ keys: nextKeys, value }) => {
|
533
|
-
const allConditionals = [...new Set([...conditionals, ...nextKeys])]
|
534
|
-
if (typeof value === 'string') {
|
535
|
-
configs.push(...generateConfigs({
|
536
|
-
...commonOptions,
|
537
|
-
output: value,
|
538
|
-
conditionals: allConditionals
|
539
|
-
}, build))
|
540
|
-
}
|
541
|
-
return false
|
542
|
-
})
|
543
|
-
break
|
544
|
-
}
|
545
|
-
}
|
546
|
-
})
|
547
|
-
)
|
548
|
-
sendMessage(
|
549
|
-
{
|
550
|
-
type: 'init',
|
551
|
-
data: {
|
552
|
-
leafMap,
|
553
|
-
targetsLength: configs.length
|
554
|
-
}
|
555
|
-
} satisfies RollupProgressEvent
|
556
|
-
)
|
557
|
-
return configs.map(c => ({
|
558
|
-
...COMMON_OPTIONS,
|
559
|
-
...c,
|
560
|
-
plugins: [
|
561
|
-
...COMMON_PLUGINS,
|
562
|
-
c.plugins
|
563
|
-
]
|
564
|
-
}))
|
565
|
-
}
|