jiek 2.3.1 → 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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "jiek",
3
3
  "type": "module",
4
- "version": "2.3.1",
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
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"}
@@ -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
  }
@@ -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'
@@ -423,7 +424,13 @@ const generateConfigs = (
423
424
  const { js: jsOutput, dts: dtsOutput } = resolveOutputControls(context, build.output)
424
425
  const rollupOptions: RollupOptions[] = []
425
426
 
426
- const commonPlugins: InputPluginOption[] = [
427
+ const commonPlugins = (
428
+ { sourcemap }: { sourcemap?: string | boolean }
429
+ ): InputPluginOption[] => [
430
+ replace({
431
+ sourcemap: sourcemap === 'hidden' ? false : !!sourcemap,
432
+ values: build.replacements
433
+ }),
427
434
  ...inputCommonPlugins,
428
435
  withExternal(),
429
436
  !disableCollectInternalModule && {
@@ -570,7 +577,7 @@ const generateConfigs = (
570
577
  )
571
578
  ],
572
579
  plugins: [
573
- ...commonPlugins,
580
+ ...commonPlugins({ sourcemap }),
574
581
  nodeResolvePluginInstance,
575
582
  import('rollup-plugin-postcss')
576
583
  .then(({ default: postcss }) =>
@@ -630,7 +637,7 @@ const generateConfigs = (
630
637
  }
631
638
  ],
632
639
  plugins: [
633
- ...commonPlugins,
640
+ ...commonPlugins({ sourcemap }),
634
641
  skip({ patterns: [STYLE_REGEXP] }),
635
642
  dts({
636
643
  respectExternal: true,
@@ -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
+ }