jiek 2.3.0 → 2.3.2
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.
- package/dist/.internal/bin/common.cjs +9 -8
- package/dist/.internal/bin/common.d.cts +4 -0
- package/dist/.internal/bin/common.d.ts +4 -0
- package/dist/.internal/bin/common.js +9 -8
- package/dist/.internal/rollup/base.d.cts +23 -0
- package/dist/.internal/rollup/base.d.ts +23 -0
- package/dist/.internal/utils/resolveExports.cjs +10 -4
- package/dist/.internal/utils/resolveExports.d.cts +2 -1
- package/dist/.internal/utils/resolveExports.d.ts +2 -1
- package/dist/.internal/utils/resolveExports.js +11 -5
- package/dist/bin/index.cjs +45 -24
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/rollup/index.cjs +106 -6
- package/dist/rollup/index.js +105 -6
- package/dist/rollup-plugin-utils.cjs +4135 -0
- package/dist/rollup-plugin-utils.d.cts +10 -0
- package/dist/rollup-plugin-utils.d.ts +10 -0
- package/dist/rollup-plugin-utils.js +4127 -0
- package/package.json +8 -5
- package/rollup-plugin-utils/package.json +1 -0
- package/src/commands/build.ts +18 -2
- package/src/commands/publish.ts +77 -46
- package/src/rollup/base.ts +14 -0
- package/src/rollup/index.ts +53 -6
- package/src/rollup/plugins/replace.ts +96 -0
- package/src/rollup-plugin-utils.ts +32 -0
- package/src/utils/resolveExports.ts +12 -5
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "jiek",
|
3
3
|
"type": "module",
|
4
|
-
"version": "2.3.
|
4
|
+
"version": "2.3.2",
|
5
5
|
"description": "A lightweight toolkit for compiling and managing libraries based on `package.json` metadata and suitable for `Monorepo`.",
|
6
6
|
"author": "YiJie <yijie4188@gmail.com>",
|
7
7
|
"homepage": "https://github.com/NWYLZW/jiek/tree/master/packages/jiek#readme",
|
@@ -35,6 +35,11 @@
|
|
35
35
|
"jiek/__source__": "./src/rollup/index.ts",
|
36
36
|
"require": "./dist/rollup/index.cjs",
|
37
37
|
"default": "./dist/rollup/index.js"
|
38
|
+
},
|
39
|
+
"./rollup-plugin-utils": {
|
40
|
+
"jiek/__source__": "./src/rollup-plugin-utils.ts",
|
41
|
+
"require": "./dist/rollup-plugin-utils.cjs",
|
42
|
+
"default": "./dist/rollup-plugin-utils.js"
|
38
43
|
}
|
39
44
|
},
|
40
45
|
"imports": {
|
@@ -74,13 +79,11 @@
|
|
74
79
|
"js-yaml": "^4.1.0",
|
75
80
|
"jsonc-parser": "^3.2.1",
|
76
81
|
"koa": "^2.15.3",
|
82
|
+
"magic-string": "^0.30.17",
|
77
83
|
"rollup": "^4.0.0",
|
78
|
-
"@jiek/pkger": "^0.2.
|
84
|
+
"@jiek/pkger": "^0.2.2",
|
79
85
|
"@jiek/utils": "^0.2.3"
|
80
86
|
},
|
81
|
-
"publishConfig": {
|
82
|
-
"directory": "./dist/.internal"
|
83
|
-
},
|
84
87
|
"peerDependenciesMeta": {
|
85
88
|
"@pnpm/filter-workspace-packages": {
|
86
89
|
"optional": true
|
@@ -0,0 +1 @@
|
|
1
|
+
{"type":"module","module":"../dist/rollup-plugin-utils.js","main":"../dist/rollup-plugin-utils.cjs"}
|
package/src/commands/build.ts
CHANGED
@@ -5,6 +5,7 @@ import process from 'node:process'
|
|
5
5
|
import { MultiBar, Presets } from 'cli-progress'
|
6
6
|
import { program } from 'commander'
|
7
7
|
import { execaCommand } from 'execa'
|
8
|
+
import type { Config } from 'jiek'
|
8
9
|
|
9
10
|
import type { RollupBuildEvent } from '#~/bridge'
|
10
11
|
import type { AnalyzerBuildOptions } from '#~/commands/build/analyzer'
|
@@ -24,6 +25,10 @@ import { tsRegisterName } from '#~/utils/tsRegister'
|
|
24
25
|
|
25
26
|
declare module 'jiek' {
|
26
27
|
interface Config {
|
28
|
+
/**
|
29
|
+
* Skip entries which end with '.js'.
|
30
|
+
*/
|
31
|
+
skipJS?: boolean
|
27
32
|
build?: TemplateOptions & {
|
28
33
|
/**
|
29
34
|
* Whether to run in silent mode, only active when configured in the workspace root or cwd.
|
@@ -51,7 +56,13 @@ If you want to through the options to the \`rollup\` command, you can pass the o
|
|
51
56
|
${isDefault ? 'This command is the default command.' : ''}
|
52
57
|
`.trim()
|
53
58
|
|
54
|
-
interface BuildOptions extends
|
59
|
+
interface BuildOptions extends
|
60
|
+
AnalyzerBuildOptions,
|
61
|
+
Pick<
|
62
|
+
Config,
|
63
|
+
'skipJS'
|
64
|
+
>
|
65
|
+
{
|
55
66
|
/**
|
56
67
|
* Auto-detect the builder from the installed dependencies.
|
57
68
|
* If the builder is not installed, it will prompt the user to install it.
|
@@ -179,6 +190,9 @@ command = command
|
|
179
190
|
parseBoolean
|
180
191
|
)
|
181
192
|
|
193
|
+
command = command
|
194
|
+
.option('--skipJS', 'Skip entries which end with ".js".', parseBoolean)
|
195
|
+
|
182
196
|
command = command
|
183
197
|
.option('--features.keepImportAttributes', 'Keep the import attributes in the output.')
|
184
198
|
|
@@ -214,7 +228,8 @@ command
|
|
214
228
|
noClean,
|
215
229
|
onlyMin,
|
216
230
|
tsconfig,
|
217
|
-
dtsconfig
|
231
|
+
dtsconfig,
|
232
|
+
skipJS
|
218
233
|
} = options
|
219
234
|
const resolvedType = type ?? DEFAULT_BUILDER_TYPE
|
220
235
|
if (!withoutJs) {
|
@@ -286,6 +301,7 @@ command
|
|
286
301
|
JIEK_MINIFY_TYPE: minifyType,
|
287
302
|
JIEK_TSCONFIG: tsconfig,
|
288
303
|
JIEK_DTSCONFIG: dtsconfig,
|
304
|
+
JIEK_SKIP_JS: String(skipJS),
|
289
305
|
JIEK_FEATURES: JSON.stringify({
|
290
306
|
keepImportAttributes: options['features.keepImportAttributes']
|
291
307
|
}),
|
package/src/commands/publish.ts
CHANGED
@@ -5,7 +5,7 @@ import path from 'node:path'
|
|
5
5
|
import process from 'node:process'
|
6
6
|
|
7
7
|
import { type BumperType, TAGS, bump } from '@jiek/utils/bumper'
|
8
|
-
import { program } from 'commander'
|
8
|
+
import { type Command, program } from 'commander'
|
9
9
|
import detectIndent from 'detect-indent'
|
10
10
|
import type { Config } from 'jiek'
|
11
11
|
import type { JSONPath } from 'jsonc-parser'
|
@@ -60,17 +60,33 @@ async function forEachSelectedProjectsGraphEntries(
|
|
60
60
|
}
|
61
61
|
}
|
62
62
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
63
|
+
interface PublishOptions {
|
64
|
+
outdir?: string
|
65
|
+
bumper: false | BumperType
|
66
|
+
/**
|
67
|
+
* Skip entries which end with '.js'.
|
68
|
+
*/
|
69
|
+
skipJS: boolean
|
70
|
+
}
|
71
|
+
|
72
|
+
const attachPublishOptions = (command: Command) =>
|
73
|
+
command
|
74
|
+
.option('-b, --bumper <bumper>', 'bump version', 'patch')
|
75
|
+
.option('-no-b, --no-bumper', 'no bump version')
|
76
|
+
.option('-o, --outdir <OUTDIR>', outdirDescription, String, 'dist')
|
77
|
+
.option('--skipJS', 'skip entries which end with ".js"')
|
78
|
+
|
79
|
+
attachPublishOptions(
|
80
|
+
program
|
81
|
+
.command('publish')
|
82
|
+
.description(description)
|
83
|
+
.aliases(['pub', 'p'])
|
84
|
+
)
|
85
|
+
.action(async ({
|
86
|
+
outdir,
|
87
|
+
bumper,
|
88
|
+
skipJS
|
89
|
+
}: PublishOptions) => {
|
74
90
|
let shouldPassThrough = false
|
75
91
|
|
76
92
|
const passThroughOptions = process.argv
|
@@ -99,18 +115,21 @@ program
|
|
99
115
|
env: {
|
100
116
|
...process.env,
|
101
117
|
JIEK_PUBLISH_OUTDIR: JSON.stringify(outdir),
|
102
|
-
JIEK_PUBLISH_BUMPER: JSON.stringify(bumper)
|
118
|
+
JIEK_PUBLISH_BUMPER: JSON.stringify(bumper),
|
119
|
+
JIEK_PUBLISH_SKIP_JS: JSON.stringify(skipJS)
|
103
120
|
}
|
104
121
|
})
|
105
122
|
})
|
106
123
|
})
|
107
124
|
|
108
|
-
async function prepublish({
|
109
|
-
bumper
|
110
|
-
|
125
|
+
async function prepublish({
|
126
|
+
bumper,
|
127
|
+
skipJS
|
128
|
+
}: PublishOptions) {
|
111
129
|
const {
|
112
130
|
JIEK_PUBLISH_OUTDIR: outdirEnv,
|
113
|
-
JIEK_PUBLISH_BUMPER: bumperEnv
|
131
|
+
JIEK_PUBLISH_BUMPER: bumperEnv,
|
132
|
+
JIEK_PUBLISH_SKIP_JS: skipJSEnv
|
114
133
|
} = process.env
|
115
134
|
const outdir = outdirEnv
|
116
135
|
? JSON.parse(outdirEnv) as string
|
@@ -118,6 +137,9 @@ async function prepublish({ bumper }: {
|
|
118
137
|
bumper = bumper ?? (
|
119
138
|
bumperEnv ? JSON.parse(bumperEnv) as string | boolean : false
|
120
139
|
)
|
140
|
+
skipJS = skipJS ?? (
|
141
|
+
skipJSEnv ? JSON.parse(skipJSEnv) as boolean : undefined
|
142
|
+
)
|
121
143
|
|
122
144
|
const generateNewManifest = (
|
123
145
|
dir: string,
|
@@ -142,7 +164,8 @@ async function prepublish({ bumper }: {
|
|
142
164
|
config,
|
143
165
|
dir,
|
144
166
|
noFilter: true,
|
145
|
-
isPublish: true
|
167
|
+
isPublish: true,
|
168
|
+
skipJS: skipJS ?? config.skipJS
|
146
169
|
} satisfies Partial<ResolveExportsOptions>
|
147
170
|
let resolvedOutdir = outdir
|
148
171
|
if (entrypoints) {
|
@@ -346,7 +369,7 @@ async function prepublish({ bumper }: {
|
|
346
369
|
}
|
347
370
|
|
348
371
|
const newVersion = bumper
|
349
|
-
? bump(oldJSON.version, bumper
|
372
|
+
? bump(oldJSON.version, bumper)
|
350
373
|
: oldJSON.version
|
351
374
|
const modifyVersionPackageJSON = applyEdits(
|
352
375
|
oldJSONString,
|
@@ -522,7 +545,8 @@ async function prepublish({ bumper }: {
|
|
522
545
|
})
|
523
546
|
}
|
524
547
|
|
525
|
-
|
548
|
+
// eslint-disable-next-line no-empty-pattern
|
549
|
+
async function postpublish({}: PublishOptions) {
|
526
550
|
await forEachSelectedProjectsGraphEntries(dir => {
|
527
551
|
const jiekTempDir = path.resolve(dir, 'node_modules/.jiek/.tmp')
|
528
552
|
const packageJSONPath = path.resolve(dir, 'package.json')
|
@@ -548,24 +572,6 @@ async function postpublish() {
|
|
548
572
|
})
|
549
573
|
}
|
550
574
|
|
551
|
-
program
|
552
|
-
.action(async () => {
|
553
|
-
const {
|
554
|
-
npm_lifecycle_event: NPM_LIFECYCLE_EVENT
|
555
|
-
} = process.env
|
556
|
-
// eslint-disable-next-line ts/switch-exhaustiveness-check
|
557
|
-
switch (NPM_LIFECYCLE_EVENT) {
|
558
|
-
case 'prepublish':
|
559
|
-
await prepublish()
|
560
|
-
break
|
561
|
-
case 'postpublish':
|
562
|
-
await postpublish()
|
563
|
-
break
|
564
|
-
default:
|
565
|
-
program.help()
|
566
|
-
}
|
567
|
-
})
|
568
|
-
|
569
575
|
const prepublishDescription = `
|
570
576
|
Prepare package.json for publish, you can add \`jk\` to the \`prepublish\` script in package.json, the command will automatically run \`jk prepublish\`.
|
571
577
|
.e.g
|
@@ -575,11 +581,11 @@ Prepare package.json for publish, you can add \`jk\` to the \`prepublish\` scrip
|
|
575
581
|
}
|
576
582
|
}
|
577
583
|
`.trim()
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
584
|
+
attachPublishOptions(
|
585
|
+
program
|
586
|
+
.command('prepublish')
|
587
|
+
.description(prepublishDescription)
|
588
|
+
)
|
583
589
|
.action(prepublish)
|
584
590
|
|
585
591
|
const postpublishDescription = `
|
@@ -591,7 +597,32 @@ Restore package.json after publish, you can add \`jk\` to the \`postpublish\` sc
|
|
591
597
|
}
|
592
598
|
}
|
593
599
|
`.trim()
|
594
|
-
|
595
|
-
|
596
|
-
|
600
|
+
attachPublishOptions(
|
601
|
+
program
|
602
|
+
.command('postpublish')
|
603
|
+
.description(postpublishDescription)
|
604
|
+
)
|
597
605
|
.action(postpublish)
|
606
|
+
|
607
|
+
const {
|
608
|
+
npm_lifecycle_event: NPM_LIFECYCLE_EVENT
|
609
|
+
} = process.env
|
610
|
+
|
611
|
+
if (
|
612
|
+
NPM_LIFECYCLE_EVENT && [
|
613
|
+
'prepublish',
|
614
|
+
'postpublish'
|
615
|
+
].includes(NPM_LIFECYCLE_EVENT)
|
616
|
+
) {
|
617
|
+
attachPublishOptions(program)
|
618
|
+
.action(async (options: PublishOptions) => {
|
619
|
+
switch (NPM_LIFECYCLE_EVENT) {
|
620
|
+
case 'prepublish':
|
621
|
+
await prepublish(options)
|
622
|
+
break
|
623
|
+
case 'postpublish':
|
624
|
+
await postpublish(options)
|
625
|
+
break
|
626
|
+
}
|
627
|
+
})
|
628
|
+
}
|
package/src/rollup/base.ts
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
import type { InputPluginOption, OutputOptions } from 'rollup'
|
2
|
+
import { Replacements } from './plugins/replace'
|
2
3
|
|
3
4
|
export type Mapping2ROO<K extends keyof OutputOptions> = OutputOptions[K] | {
|
4
5
|
js?: OutputOptions[K]
|
@@ -134,4 +135,17 @@ export interface TemplateOptions {
|
|
134
135
|
* ```
|
135
136
|
*/
|
136
137
|
injects?: Record<string, string | [string, string]>
|
138
|
+
/**
|
139
|
+
* Replace the specified content in the code.
|
140
|
+
*
|
141
|
+
* @example
|
142
|
+
* ```js
|
143
|
+
* {
|
144
|
+
* 'process.env.DEBUG': 'false',
|
145
|
+
* 'process.env.NODE_ENV': JSON.stringify('production'),
|
146
|
+
* 'process.env.BUILD_PATH': ctx => JSON.stringify(ctx.id)
|
147
|
+
* }
|
148
|
+
* ```
|
149
|
+
*/
|
150
|
+
replacements?: Replacements
|
137
151
|
}
|
package/src/rollup/index.ts
CHANGED
@@ -27,6 +27,7 @@ import { getCompilerOptionsByFilePath } from '#~/utils/ts'
|
|
27
27
|
import type { ConfigGenerateContext, TemplateOptions } from './base'
|
28
28
|
import createRequire, { CREATE_REQUIRE_VIRTUAL_MODULE_NAME } from './plugins/create-require'
|
29
29
|
import progress from './plugins/progress'
|
30
|
+
import replace from './plugins/replace'
|
30
31
|
import skip from './plugins/skip'
|
31
32
|
import withExternal from './plugins/with-external.ts'
|
32
33
|
import type { PackageJSON } from './utils/externalResolver'
|
@@ -46,6 +47,7 @@ const {
|
|
46
47
|
JIEK_ONLY_MINIFY,
|
47
48
|
JIEK_TSCONFIG,
|
48
49
|
JIEK_DTSCONFIG,
|
50
|
+
JIEK_SKIP_JS,
|
49
51
|
JIEK_FEATURES
|
50
52
|
} = process.env
|
51
53
|
|
@@ -82,6 +84,12 @@ const ONLY_MINIFY = JIEK_ONLY_MINIFY === 'true'
|
|
82
84
|
|
83
85
|
const CLEAN = JIEK_CLEAN === 'true'
|
84
86
|
|
87
|
+
const SKIP_JS = JIEK_SKIP_JS === 'false'
|
88
|
+
? false
|
89
|
+
: JIEK_SKIP_JS === 'true'
|
90
|
+
? true
|
91
|
+
: undefined
|
92
|
+
|
85
93
|
const FEATURES = JSON.parse(JIEK_FEATURES ?? '{}') as Record<string, unknown>
|
86
94
|
|
87
95
|
const MINIFY_DEFAULT_VALUE = WITHOUT_MINIFY
|
@@ -105,7 +113,11 @@ const MINIFY_OPTIONS = {
|
|
105
113
|
const config = loadConfig({
|
106
114
|
root: WORKSPACE_ROOT
|
107
115
|
}) ?? {}
|
108
|
-
const {
|
116
|
+
const {
|
117
|
+
experimental,
|
118
|
+
skipJS,
|
119
|
+
build = {}
|
120
|
+
} = config
|
109
121
|
const { js: jsOutdir, dts: dtsOutdir } = getOutDirs({
|
110
122
|
config,
|
111
123
|
pkgName: JIEK_NAME
|
@@ -412,7 +424,13 @@ const generateConfigs = (
|
|
412
424
|
const { js: jsOutput, dts: dtsOutput } = resolveOutputControls(context, build.output)
|
413
425
|
const rollupOptions: RollupOptions[] = []
|
414
426
|
|
415
|
-
const commonPlugins
|
427
|
+
const commonPlugins = (
|
428
|
+
{ sourcemap }: { sourcemap?: string | boolean }
|
429
|
+
): InputPluginOption[] => [
|
430
|
+
replace({
|
431
|
+
sourcemap: sourcemap === 'hidden' ? false : !!sourcemap,
|
432
|
+
values: build.replacements
|
433
|
+
}),
|
416
434
|
...inputCommonPlugins,
|
417
435
|
withExternal(),
|
418
436
|
!disableCollectInternalModule && {
|
@@ -559,7 +577,7 @@ const generateConfigs = (
|
|
559
577
|
)
|
560
578
|
],
|
561
579
|
plugins: [
|
562
|
-
...commonPlugins,
|
580
|
+
...commonPlugins({ sourcemap }),
|
563
581
|
nodeResolvePluginInstance,
|
564
582
|
import('rollup-plugin-postcss')
|
565
583
|
.then(({ default: postcss }) =>
|
@@ -619,7 +637,7 @@ const generateConfigs = (
|
|
619
637
|
}
|
620
638
|
],
|
621
639
|
plugins: [
|
622
|
-
...commonPlugins,
|
640
|
+
...commonPlugins({ sourcemap }),
|
623
641
|
skip({ patterns: [STYLE_REGEXP] }),
|
624
642
|
dts({
|
625
643
|
respectExternal: true,
|
@@ -698,7 +716,8 @@ export function template(packageJSON: PackageJSON): RollupOptions[] {
|
|
698
716
|
entries,
|
699
717
|
pkgName: JIEK_NAME!,
|
700
718
|
outdir: jsOutdir,
|
701
|
-
config
|
719
|
+
config,
|
720
|
+
skipJS: skipJS ?? SKIP_JS
|
702
721
|
})
|
703
722
|
getAllLeafs(filteredResolvedEntrypoints as RecursiveRecord<string>, ({ keys, value }) => {
|
704
723
|
if (typeof value === 'string') {
|
@@ -748,7 +767,8 @@ export function template(packageJSON: PackageJSON): RollupOptions[] {
|
|
748
767
|
pkgIsModule,
|
749
768
|
pkgName: JIEK_NAME!,
|
750
769
|
outdir: `${jsOutdir}/.internal`,
|
751
|
-
config
|
770
|
+
config,
|
771
|
+
skipJS: SKIP_JS ?? skipJS
|
752
772
|
})
|
753
773
|
getAllLeafs(filteredResolvedInternalEntrypoints as RecursiveRecord<string>, ({ keys, value }) => {
|
754
774
|
if (typeof value === 'string') {
|
@@ -792,7 +812,34 @@ export function template(packageJSON: PackageJSON): RollupOptions[] {
|
|
792
812
|
: [inputOptions.input as string],
|
793
813
|
internalModules
|
794
814
|
)]
|
815
|
+
if (inputOptions.input.length === 0) {
|
816
|
+
inputOptions.input = '__jiek_empty__'
|
817
|
+
const plugins = await inputOptions.plugins
|
818
|
+
if (!Array.isArray(plugins)) {
|
819
|
+
throw new TypeError('plugins is not an array')
|
820
|
+
}
|
821
|
+
inputOptions.plugins = plugins.filter(
|
822
|
+
p =>
|
823
|
+
typeof p !== 'object'
|
824
|
+
? true
|
825
|
+
: p === null
|
826
|
+
? true
|
827
|
+
: 'name' in p && p.name === 'jiek:loadInternalModules'
|
828
|
+
)
|
829
|
+
}
|
795
830
|
return inputOptions
|
831
|
+
},
|
832
|
+
resolveId: {
|
833
|
+
order: 'post',
|
834
|
+
handler(id) {
|
835
|
+
if (id === '__jiek_empty__') return id
|
836
|
+
}
|
837
|
+
},
|
838
|
+
load: {
|
839
|
+
order: 'post',
|
840
|
+
handler(id) {
|
841
|
+
if (id === '__jiek_empty__') return ''
|
842
|
+
}
|
796
843
|
}
|
797
844
|
}
|
798
845
|
],
|
@@ -0,0 +1,96 @@
|
|
1
|
+
import type { FilterOptions } from 'jiek/rollup-plugin-utils'
|
2
|
+
import { createFilter, definePlugin } from 'jiek/rollup-plugin-utils'
|
3
|
+
import MagicString from 'magic-string'
|
4
|
+
|
5
|
+
export interface ReplacementFuncCtx {
|
6
|
+
type: 'transform' | 'renderChunk'
|
7
|
+
id: string
|
8
|
+
code: string
|
9
|
+
start: number
|
10
|
+
end: number
|
11
|
+
}
|
12
|
+
|
13
|
+
export type ReplacementFunc = (ctx: ReplacementFuncCtx) => string
|
14
|
+
|
15
|
+
export type Replacements = Record<
|
16
|
+
string,
|
17
|
+
string | ReplacementFunc
|
18
|
+
>
|
19
|
+
|
20
|
+
export type Options =
|
21
|
+
& FilterOptions
|
22
|
+
& {
|
23
|
+
sourcemap?: boolean
|
24
|
+
values?: Replacements
|
25
|
+
}
|
26
|
+
|
27
|
+
export default definePlugin((options: Options = {}) => {
|
28
|
+
const {
|
29
|
+
include = [/\.[cm]?[tj]sx?$/],
|
30
|
+
exclude = [/node_modules/],
|
31
|
+
values = {},
|
32
|
+
sourcemap
|
33
|
+
} = options
|
34
|
+
const allValues = { ...values }
|
35
|
+
const allKeys = Object.keys(allValues)
|
36
|
+
const filter = createFilter({ include, exclude })
|
37
|
+
|
38
|
+
const replaceAll = (ctx: Pick<ReplacementFuncCtx, 'type' | 'id'>, code: string) => {
|
39
|
+
const ms = new MagicString(code)
|
40
|
+
allKeys.forEach(key => {
|
41
|
+
const reg = new RegExp(key, 'g')
|
42
|
+
let match
|
43
|
+
// eslint-disable-next-line no-cond-assign
|
44
|
+
while ((match = reg.exec(code))) {
|
45
|
+
const start = match.index
|
46
|
+
const end = start + key.length
|
47
|
+
ms.overwrite(
|
48
|
+
match.index,
|
49
|
+
match.index + key.length,
|
50
|
+
typeof allValues[key] === 'function'
|
51
|
+
? allValues[key]({
|
52
|
+
...ctx,
|
53
|
+
code,
|
54
|
+
start,
|
55
|
+
end
|
56
|
+
})
|
57
|
+
: allValues[key]
|
58
|
+
)
|
59
|
+
}
|
60
|
+
})
|
61
|
+
return ms
|
62
|
+
}
|
63
|
+
|
64
|
+
return {
|
65
|
+
name: 'jiek:replace',
|
66
|
+
transform: {
|
67
|
+
order: 'pre',
|
68
|
+
handler(code, id) {
|
69
|
+
if (allKeys.length === 0) return
|
70
|
+
if (filter(id)) return
|
71
|
+
const ms = replaceAll({ type: 'transform', id }, code)
|
72
|
+
if (ms == null) return
|
73
|
+
|
74
|
+
return {
|
75
|
+
code: ms.toString(),
|
76
|
+
map: sourcemap ? ms.generateMap({ hires: true }) : null
|
77
|
+
}
|
78
|
+
}
|
79
|
+
},
|
80
|
+
renderChunk: {
|
81
|
+
order: 'post',
|
82
|
+
handler(code, { fileName: id }) {
|
83
|
+
if (allKeys.length === 0) return
|
84
|
+
if (filter(id)) return
|
85
|
+
|
86
|
+
const ms = replaceAll({ type: 'renderChunk', id }, code)
|
87
|
+
if (ms == null) return
|
88
|
+
|
89
|
+
return {
|
90
|
+
code: ms.toString(),
|
91
|
+
map: sourcemap ? ms.generateMap({ hires: true }) : null
|
92
|
+
}
|
93
|
+
}
|
94
|
+
}
|
95
|
+
}
|
96
|
+
})
|
@@ -0,0 +1,32 @@
|
|
1
|
+
import { isMatch } from 'micromatch'
|
2
|
+
import type { PluginImpl } from 'rollup'
|
3
|
+
|
4
|
+
export const definePlugin = <O extends object>(plugin: PluginImpl<O>) => plugin
|
5
|
+
|
6
|
+
export interface FilterOptions {
|
7
|
+
include?: string | RegExp | (string | RegExp)[]
|
8
|
+
exclude?: string | RegExp | (string | RegExp)[]
|
9
|
+
}
|
10
|
+
|
11
|
+
export function createFilter(options: FilterOptions) {
|
12
|
+
const { include = [], exclude = [] } = options
|
13
|
+
|
14
|
+
const resolvedInclude = Array.isArray(include) ? include : [include]
|
15
|
+
const resolvedExclude = Array.isArray(exclude) ? exclude : [exclude]
|
16
|
+
|
17
|
+
return (id: string) => {
|
18
|
+
if (typeof id !== 'string') return false
|
19
|
+
const isInclude = resolvedInclude.length === 0 || resolvedInclude.some(filter => {
|
20
|
+
return filter instanceof RegExp
|
21
|
+
? filter.test(id)
|
22
|
+
: isMatch(id, filter)
|
23
|
+
})
|
24
|
+
const isExclude = resolvedExclude.length > 0 && resolvedExclude.some(filter => {
|
25
|
+
return filter instanceof RegExp
|
26
|
+
? filter.test(id)
|
27
|
+
: isMatch(id, filter)
|
28
|
+
})
|
29
|
+
|
30
|
+
return !isInclude || isExclude
|
31
|
+
}
|
32
|
+
}
|
@@ -4,9 +4,9 @@ import process from 'node:process'
|
|
4
4
|
import {
|
5
5
|
type Entrypoints2ExportsOptions,
|
6
6
|
type RecursiveRecord,
|
7
|
-
DEFAULT_SKIP_VALUES,
|
8
7
|
entrypoints2Exports,
|
9
8
|
filterLeafs,
|
9
|
+
getDefaultSkipValues,
|
10
10
|
resolveEntrypoints
|
11
11
|
} from '@jiek/pkger/entrypoints'
|
12
12
|
import type { Config } from 'jiek'
|
@@ -69,6 +69,7 @@ export interface ResolveExportsOptions {
|
|
69
69
|
defaultOutdir?: string
|
70
70
|
noFilter?: boolean
|
71
71
|
isPublish?: boolean
|
72
|
+
skipJS?: boolean
|
72
73
|
}
|
73
74
|
|
74
75
|
export function resolveExports({
|
@@ -82,7 +83,8 @@ export function resolveExports({
|
|
82
83
|
// FIXME dts support
|
83
84
|
outdir = getOutDirs({ pkgName, defaultOutdir, config, cwd: dir }).js,
|
84
85
|
noFilter,
|
85
|
-
isPublish
|
86
|
+
isPublish,
|
87
|
+
skipJS
|
86
88
|
}: ResolveExportsOptions) {
|
87
89
|
const {
|
88
90
|
build = {},
|
@@ -94,7 +96,9 @@ export function resolveExports({
|
|
94
96
|
const {
|
95
97
|
crossModuleConvertor = crossModuleConvertorDefault
|
96
98
|
} = build
|
97
|
-
const [, resolvedEntrypoints] = resolveEntrypoints(entrypoints
|
99
|
+
const [, resolvedEntrypoints] = resolveEntrypoints(entrypoints, {
|
100
|
+
allowJS: !skipJS
|
101
|
+
})
|
98
102
|
if (entries) {
|
99
103
|
Object
|
100
104
|
.entries(resolvedEntrypoints)
|
@@ -110,7 +114,9 @@ export function resolveExports({
|
|
110
114
|
skipValue: [
|
111
115
|
// ignore values that filename starts with `.jk-noentry`
|
112
116
|
/(^|\/)\.jk-noentry/,
|
113
|
-
...
|
117
|
+
...getDefaultSkipValues({
|
118
|
+
allowJS: !skipJS
|
119
|
+
})
|
114
120
|
]
|
115
121
|
}
|
116
122
|
)
|
@@ -151,7 +157,8 @@ export function resolveExports({
|
|
151
157
|
withSource: isPublish ? withSource : undefined,
|
152
158
|
withConditional: {
|
153
159
|
...crossModuleWithConditional
|
154
|
-
}
|
160
|
+
},
|
161
|
+
allowJS: !skipJS
|
155
162
|
}),
|
156
163
|
outdir
|
157
164
|
] as const
|