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.
@@ -37,7 +37,7 @@ var Koa__default = /*#__PURE__*/_interopDefault(Koa);
37
37
 
38
38
  var name = "jiek";
39
39
  var type = "module";
40
- var version = "2.3.0";
40
+ var version = "2.3.1";
41
41
  var description$1 = "A lightweight toolkit for compiling and managing libraries based on `package.json` metadata and suitable for `Monorepo`.";
42
42
  var author = "YiJie <yijie4188@gmail.com>";
43
43
  var homepage = "https://github.com/NWYLZW/jiek/tree/master/packages/jiek#readme";
@@ -63,7 +63,8 @@ var exports$1 = {
63
63
  require: "./bin-helper/index.cjs",
64
64
  "default": "./bin-helper/index.js"
65
65
  },
66
- "./rollup": "./src/rollup/index.ts"
66
+ "./rollup": "./src/rollup/index.ts",
67
+ "./rollup-plugin-utils": "./src/rollup-plugin-utils.ts"
67
68
  };
68
69
  var imports = {
69
70
  "#~/*": "./src/*.ts"
@@ -113,6 +114,7 @@ var dependencies = {
113
114
  "js-yaml": "^4.1.0",
114
115
  "jsonc-parser": "^3.2.1",
115
116
  koa: "^2.15.3",
117
+ "magic-string": "^0.30.17",
116
118
  rollup: "^4.0.0"
117
119
  };
118
120
  var devDependencies = {
@@ -136,9 +138,6 @@ var devDependencies = {
136
138
  "rollup-plugin-swc3": "^0.12.1",
137
139
  "vite-bundle-analyzer": "0.16.0-beta.3"
138
140
  };
139
- var publishConfig = {
140
- directory: "./dist/.internal"
141
- };
142
141
  var pkg = {
143
142
  name: name,
144
143
  type: type,
@@ -156,8 +155,7 @@ var pkg = {
156
155
  scripts: scripts,
157
156
  peerDependencies: peerDependencies,
158
157
  dependencies: dependencies,
159
- devDependencies: devDependencies,
160
- publishConfig: publishConfig
158
+ devDependencies: devDependencies
161
159
  };
162
160
 
163
161
  const entriesDescription = `
@@ -30,7 +30,7 @@ import 'node:stream/promises';
30
30
 
31
31
  var name = "jiek";
32
32
  var type = "module";
33
- var version = "2.3.0";
33
+ var version = "2.3.1";
34
34
  var description$1 = "A lightweight toolkit for compiling and managing libraries based on `package.json` metadata and suitable for `Monorepo`.";
35
35
  var author = "YiJie <yijie4188@gmail.com>";
36
36
  var homepage = "https://github.com/NWYLZW/jiek/tree/master/packages/jiek#readme";
@@ -56,7 +56,8 @@ var exports = {
56
56
  require: "./bin-helper/index.cjs",
57
57
  "default": "./bin-helper/index.js"
58
58
  },
59
- "./rollup": "./src/rollup/index.ts"
59
+ "./rollup": "./src/rollup/index.ts",
60
+ "./rollup-plugin-utils": "./src/rollup-plugin-utils.ts"
60
61
  };
61
62
  var imports = {
62
63
  "#~/*": "./src/*.ts"
@@ -106,6 +107,7 @@ var dependencies = {
106
107
  "js-yaml": "^4.1.0",
107
108
  "jsonc-parser": "^3.2.1",
108
109
  koa: "^2.15.3",
110
+ "magic-string": "^0.30.17",
109
111
  rollup: "^4.0.0"
110
112
  };
111
113
  var devDependencies = {
@@ -129,9 +131,6 @@ var devDependencies = {
129
131
  "rollup-plugin-swc3": "^0.12.1",
130
132
  "vite-bundle-analyzer": "0.16.0-beta.3"
131
133
  };
132
- var publishConfig = {
133
- directory: "./dist/.internal"
134
- };
135
134
  var pkg = {
136
135
  name: name,
137
136
  type: type,
@@ -149,8 +148,7 @@ var pkg = {
149
148
  scripts: scripts,
150
149
  peerDependencies: peerDependencies,
151
150
  dependencies: dependencies,
152
- devDependencies: devDependencies,
153
- publishConfig: publishConfig
151
+ devDependencies: devDependencies
154
152
  };
155
153
 
156
154
  const entriesDescription = `
@@ -3,6 +3,16 @@ import * as rollup_plugin_swc3 from 'rollup-plugin-swc3';
3
3
  import * as rollup_plugin_esbuild from 'rollup-plugin-esbuild';
4
4
  import { OutputOptions, InputPluginOption } from 'rollup';
5
5
 
6
+ interface ReplacementFuncCtx {
7
+ type: 'transform' | 'renderChunk';
8
+ id: string;
9
+ code: string;
10
+ start: number;
11
+ end: number;
12
+ }
13
+ type ReplacementFunc = (ctx: ReplacementFuncCtx) => string;
14
+ type Replacements = Record<string, string | ReplacementFunc>;
15
+
6
16
  type Mapping2ROO<K extends keyof OutputOptions> = OutputOptions[K] | {
7
17
  js?: OutputOptions[K];
8
18
  dts?: OutputOptions[K];
@@ -114,6 +124,19 @@ interface TemplateOptions {
114
124
  * ```
115
125
  */
116
126
  injects?: Record<string, string | [string, string]>;
127
+ /**
128
+ * Replace the specified content in the code.
129
+ *
130
+ * @example
131
+ * ```js
132
+ * {
133
+ * 'process.env.DEBUG': 'false',
134
+ * 'process.env.NODE_ENV': JSON.stringify('production'),
135
+ * 'process.env.BUILD_PATH': ctx => JSON.stringify(ctx.id)
136
+ * }
137
+ * ```
138
+ */
139
+ replacements?: Replacements;
117
140
  }
118
141
 
119
142
  export { BUILDER_TYPES, BUILDER_TYPE_PACKAGE_NAME_MAP, type ConfigGenerateContext, type Mapping2ROO, type OutputControl, type TemplateOptions };
@@ -3,6 +3,16 @@ import * as rollup_plugin_swc3 from 'rollup-plugin-swc3';
3
3
  import * as rollup_plugin_esbuild from 'rollup-plugin-esbuild';
4
4
  import { OutputOptions, InputPluginOption } from 'rollup';
5
5
 
6
+ interface ReplacementFuncCtx {
7
+ type: 'transform' | 'renderChunk';
8
+ id: string;
9
+ code: string;
10
+ start: number;
11
+ end: number;
12
+ }
13
+ type ReplacementFunc = (ctx: ReplacementFuncCtx) => string;
14
+ type Replacements = Record<string, string | ReplacementFunc>;
15
+
6
16
  type Mapping2ROO<K extends keyof OutputOptions> = OutputOptions[K] | {
7
17
  js?: OutputOptions[K];
8
18
  dts?: OutputOptions[K];
@@ -114,6 +124,19 @@ interface TemplateOptions {
114
124
  * ```
115
125
  */
116
126
  injects?: Record<string, string | [string, string]>;
127
+ /**
128
+ * Replace the specified content in the code.
129
+ *
130
+ * @example
131
+ * ```js
132
+ * {
133
+ * 'process.env.DEBUG': 'false',
134
+ * 'process.env.NODE_ENV': JSON.stringify('production'),
135
+ * 'process.env.BUILD_PATH': ctx => JSON.stringify(ctx.id)
136
+ * }
137
+ * ```
138
+ */
139
+ replacements?: Replacements;
117
140
  }
118
141
 
119
142
  export { BUILDER_TYPES, BUILDER_TYPE_PACKAGE_NAME_MAP, type ConfigGenerateContext, type Mapping2ROO, type OutputControl, type TemplateOptions };
@@ -19,6 +19,8 @@ var loadConfig = require('#~/utils/loadConfig');
19
19
  var recursiveListFiles = require('#~/utils/recursiveListFiles');
20
20
  var resolveExports = require('#~/utils/resolveExports');
21
21
  var ts = require('#~/utils/ts');
22
+ var rollupPluginUtils = require('jiek/rollup-plugin-utils');
23
+ var MagicString = require('magic-string');
22
24
  var node_module = require('node:module');
23
25
 
24
26
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
@@ -29,6 +31,7 @@ var commonjs__default = /*#__PURE__*/_interopDefault(commonjs);
29
31
  var inject__default = /*#__PURE__*/_interopDefault(inject);
30
32
  var json__default = /*#__PURE__*/_interopDefault(json);
31
33
  var ts__default = /*#__PURE__*/_interopDefault(ts$1);
34
+ var MagicString__default = /*#__PURE__*/_interopDefault(MagicString);
32
35
 
33
36
  const CREATE_REQUIRE_VIRTUAL_MODULE_NAME = "jiek:create-require";
34
37
  const INSERT_STR = (isESM) => `
@@ -62,6 +65,69 @@ var progress = (options = {}) => {
62
65
  };
63
66
  };
64
67
 
68
+ var replace = rollupPluginUtils.definePlugin((options = {}) => {
69
+ const {
70
+ include = [/\.[cm]?[tj]sx?$/],
71
+ exclude = [/node_modules/],
72
+ values = {},
73
+ sourcemap
74
+ } = options;
75
+ const allValues = { ...values };
76
+ const allKeys = Object.keys(allValues);
77
+ const filter = rollupPluginUtils.createFilter({ include, exclude });
78
+ const replaceAll = (ctx, code) => {
79
+ const ms = new MagicString__default.default(code);
80
+ allKeys.forEach((key) => {
81
+ const reg = new RegExp(key, "g");
82
+ let match;
83
+ while (match = reg.exec(code)) {
84
+ const start = match.index;
85
+ const end = start + key.length;
86
+ ms.overwrite(
87
+ match.index,
88
+ match.index + key.length,
89
+ typeof allValues[key] === "function" ? allValues[key]({
90
+ ...ctx,
91
+ code,
92
+ start,
93
+ end
94
+ }) : allValues[key]
95
+ );
96
+ }
97
+ });
98
+ return ms;
99
+ };
100
+ return {
101
+ name: "jiek:replace",
102
+ transform: {
103
+ order: "pre",
104
+ handler(code, id) {
105
+ if (allKeys.length === 0) return;
106
+ if (filter(id)) return;
107
+ const ms = replaceAll({ type: "transform", id }, code);
108
+ if (ms == null) return;
109
+ return {
110
+ code: ms.toString(),
111
+ map: sourcemap ? ms.generateMap({ hires: true }) : null
112
+ };
113
+ }
114
+ },
115
+ renderChunk: {
116
+ order: "post",
117
+ handler(code, { fileName: id }) {
118
+ if (allKeys.length === 0) return;
119
+ if (filter(id)) return;
120
+ const ms = replaceAll({ type: "renderChunk", id }, code);
121
+ if (ms == null) return;
122
+ return {
123
+ code: ms.toString(),
124
+ map: sourcemap ? ms.generateMap({ hires: true }) : null
125
+ };
126
+ }
127
+ }
128
+ };
129
+ });
130
+
65
131
  var skip = (options = {}) => ({
66
132
  name: "skip",
67
133
  // skip the specified files by `options.patterns`
@@ -360,7 +426,11 @@ const generateConfigs = (context, {
360
426
  const tsOutputSuffix = jsOutputSuffix.replace(/(\.[cm]?)js$/, ".d$1ts");
361
427
  const { js: jsOutput, dts: dtsOutput } = resolveOutputControls(context, build.output);
362
428
  const rollupOptions = [];
363
- const commonPlugins = [
429
+ const commonPlugins = ({ sourcemap }) => [
430
+ replace({
431
+ sourcemap: sourcemap === "hidden" ? false : !!sourcemap,
432
+ values: build.replacements
433
+ }),
364
434
  ...inputCommonPlugins,
365
435
  withExternal(),
366
436
  !disableCollectInternalModule && {
@@ -471,7 +541,7 @@ const generateConfigs = (context, {
471
541
  )
472
542
  ],
473
543
  plugins: [
474
- ...commonPlugins,
544
+ ...commonPlugins({ sourcemap }),
475
545
  nodeResolvePluginInstance,
476
546
  import('rollup-plugin-postcss').then(
477
547
  ({ default: postcss }) => postcss({
@@ -514,7 +584,7 @@ const generateConfigs = (context, {
514
584
  }
515
585
  ],
516
586
  plugins: [
517
- ...commonPlugins,
587
+ ...commonPlugins({ sourcemap }),
518
588
  skip({ patterns: [STYLE_REGEXP] }),
519
589
  rollupPluginDts.dts({
520
590
  respectExternal: true,
@@ -17,6 +17,8 @@ import { loadConfig } from '#~/utils/loadConfig';
17
17
  import { recursiveListFiles } from '#~/utils/recursiveListFiles';
18
18
  import { getOutDirs, resolveExports } from '#~/utils/resolveExports';
19
19
  import { getCompilerOptionsByFilePath } from '#~/utils/ts';
20
+ import { definePlugin, createFilter } from 'jiek/rollup-plugin-utils';
21
+ import MagicString from 'magic-string';
20
22
  import { builtinModules } from 'node:module';
21
23
 
22
24
  const CREATE_REQUIRE_VIRTUAL_MODULE_NAME = "jiek:create-require";
@@ -51,6 +53,69 @@ var progress = (options = {}) => {
51
53
  };
52
54
  };
53
55
 
56
+ var replace = definePlugin((options = {}) => {
57
+ const {
58
+ include = [/\.[cm]?[tj]sx?$/],
59
+ exclude = [/node_modules/],
60
+ values = {},
61
+ sourcemap
62
+ } = options;
63
+ const allValues = { ...values };
64
+ const allKeys = Object.keys(allValues);
65
+ const filter = createFilter({ include, exclude });
66
+ const replaceAll = (ctx, code) => {
67
+ const ms = new MagicString(code);
68
+ allKeys.forEach((key) => {
69
+ const reg = new RegExp(key, "g");
70
+ let match;
71
+ while (match = reg.exec(code)) {
72
+ const start = match.index;
73
+ const end = start + key.length;
74
+ ms.overwrite(
75
+ match.index,
76
+ match.index + key.length,
77
+ typeof allValues[key] === "function" ? allValues[key]({
78
+ ...ctx,
79
+ code,
80
+ start,
81
+ end
82
+ }) : allValues[key]
83
+ );
84
+ }
85
+ });
86
+ return ms;
87
+ };
88
+ return {
89
+ name: "jiek:replace",
90
+ transform: {
91
+ order: "pre",
92
+ handler(code, id) {
93
+ if (allKeys.length === 0) return;
94
+ if (filter(id)) return;
95
+ const ms = replaceAll({ type: "transform", id }, code);
96
+ if (ms == null) return;
97
+ return {
98
+ code: ms.toString(),
99
+ map: sourcemap ? ms.generateMap({ hires: true }) : null
100
+ };
101
+ }
102
+ },
103
+ renderChunk: {
104
+ order: "post",
105
+ handler(code, { fileName: id }) {
106
+ if (allKeys.length === 0) return;
107
+ if (filter(id)) return;
108
+ const ms = replaceAll({ type: "renderChunk", id }, code);
109
+ if (ms == null) return;
110
+ return {
111
+ code: ms.toString(),
112
+ map: sourcemap ? ms.generateMap({ hires: true }) : null
113
+ };
114
+ }
115
+ }
116
+ };
117
+ });
118
+
54
119
  var skip = (options = {}) => ({
55
120
  name: "skip",
56
121
  // skip the specified files by `options.patterns`
@@ -349,7 +414,11 @@ const generateConfigs = (context, {
349
414
  const tsOutputSuffix = jsOutputSuffix.replace(/(\.[cm]?)js$/, ".d$1ts");
350
415
  const { js: jsOutput, dts: dtsOutput } = resolveOutputControls(context, build.output);
351
416
  const rollupOptions = [];
352
- const commonPlugins = [
417
+ const commonPlugins = ({ sourcemap }) => [
418
+ replace({
419
+ sourcemap: sourcemap === "hidden" ? false : !!sourcemap,
420
+ values: build.replacements
421
+ }),
353
422
  ...inputCommonPlugins,
354
423
  withExternal(),
355
424
  !disableCollectInternalModule && {
@@ -460,7 +529,7 @@ const generateConfigs = (context, {
460
529
  )
461
530
  ],
462
531
  plugins: [
463
- ...commonPlugins,
532
+ ...commonPlugins({ sourcemap }),
464
533
  nodeResolvePluginInstance,
465
534
  import('rollup-plugin-postcss').then(
466
535
  ({ default: postcss }) => postcss({
@@ -503,7 +572,7 @@ const generateConfigs = (context, {
503
572
  }
504
573
  ],
505
574
  plugins: [
506
- ...commonPlugins,
575
+ ...commonPlugins({ sourcemap }),
507
576
  skip({ patterns: [STYLE_REGEXP] }),
508
577
  dts({
509
578
  respectExternal: true,