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.
@@ -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`
@@ -124,6 +190,7 @@ const {
124
190
  JIEK_ONLY_MINIFY,
125
191
  JIEK_TSCONFIG,
126
192
  JIEK_DTSCONFIG,
193
+ JIEK_SKIP_JS,
127
194
  JIEK_FEATURES
128
195
  } = process__default.default.env;
129
196
  const resolveArrayString = (str) => {
@@ -147,6 +214,7 @@ const WITHOUT_DTS = JIEK_WITHOUT_DTS === "true";
147
214
  const WITHOUT_MINIFY = JIEK_WITHOUT_MINIFY === "true";
148
215
  const ONLY_MINIFY = JIEK_ONLY_MINIFY === "true";
149
216
  const CLEAN = JIEK_CLEAN === "true";
217
+ const SKIP_JS = JIEK_SKIP_JS === "false" ? false : JIEK_SKIP_JS === "true" ? true : void 0;
150
218
  const FEATURES = JSON.parse(JIEK_FEATURES ?? "{}");
151
219
  const MINIFY_DEFAULT_VALUE = WITHOUT_MINIFY ? false : ONLY_MINIFY ? "only-minify" : true;
152
220
  const BUILDER_OPTIONS = {
@@ -158,7 +226,11 @@ const MINIFY_OPTIONS = {
158
226
  const config = loadConfig.loadConfig({
159
227
  root: WORKSPACE_ROOT
160
228
  }) ?? {};
161
- const { experimental, build = {} } = config;
229
+ const {
230
+ experimental,
231
+ skipJS,
232
+ build = {}
233
+ } = config;
162
234
  const { js: jsOutdir, dts: dtsOutdir } = resolveExports.getOutDirs({
163
235
  config,
164
236
  pkgName: JIEK_NAME
@@ -354,7 +426,11 @@ const generateConfigs = (context, {
354
426
  const tsOutputSuffix = jsOutputSuffix.replace(/(\.[cm]?)js$/, ".d$1ts");
355
427
  const { js: jsOutput, dts: dtsOutput } = resolveOutputControls(context, build.output);
356
428
  const rollupOptions = [];
357
- const commonPlugins = [
429
+ const commonPlugins = ({ sourcemap }) => [
430
+ replace({
431
+ sourcemap: sourcemap === "hidden" ? false : !!sourcemap,
432
+ values: build.replacements
433
+ }),
358
434
  ...inputCommonPlugins,
359
435
  withExternal(),
360
436
  !disableCollectInternalModule && {
@@ -465,7 +541,7 @@ const generateConfigs = (context, {
465
541
  )
466
542
  ],
467
543
  plugins: [
468
- ...commonPlugins,
544
+ ...commonPlugins({ sourcemap }),
469
545
  nodeResolvePluginInstance,
470
546
  import('rollup-plugin-postcss').then(
471
547
  ({ default: postcss }) => postcss({
@@ -508,7 +584,7 @@ const generateConfigs = (context, {
508
584
  }
509
585
  ],
510
586
  plugins: [
511
- ...commonPlugins,
587
+ ...commonPlugins({ sourcemap }),
512
588
  skip({ patterns: [STYLE_REGEXP] }),
513
589
  rollupPluginDts.dts({
514
590
  respectExternal: true,
@@ -583,7 +659,8 @@ function template(packageJSON) {
583
659
  entries,
584
660
  pkgName: JIEK_NAME,
585
661
  outdir: jsOutdir,
586
- config
662
+ config,
663
+ skipJS: skipJS ?? SKIP_JS
587
664
  });
588
665
  entrypoints.getAllLeafs(filteredResolvedEntrypoints, ({ keys, value }) => {
589
666
  if (typeof value === "string") {
@@ -626,7 +703,8 @@ function template(packageJSON) {
626
703
  pkgIsModule,
627
704
  pkgName: JIEK_NAME,
628
705
  outdir: `${jsOutdir}/.internal`,
629
- config
706
+ config,
707
+ skipJS: SKIP_JS ?? skipJS
630
708
  });
631
709
  entrypoints.getAllLeafs(filteredResolvedInternalEntrypoints, ({ keys, value }) => {
632
710
  if (typeof value === "string") {
@@ -664,7 +742,29 @@ function template(packageJSON) {
664
742
  Array.isArray(inputOptions.input) ? inputOptions.input : [inputOptions.input],
665
743
  internalModules
666
744
  )];
745
+ if (inputOptions.input.length === 0) {
746
+ inputOptions.input = "__jiek_empty__";
747
+ const plugins = await inputOptions.plugins;
748
+ if (!Array.isArray(plugins)) {
749
+ throw new TypeError("plugins is not an array");
750
+ }
751
+ inputOptions.plugins = plugins.filter(
752
+ (p) => typeof p !== "object" ? true : p === null ? true : "name" in p && p.name === "jiek:loadInternalModules"
753
+ );
754
+ }
667
755
  return inputOptions;
756
+ },
757
+ resolveId: {
758
+ order: "post",
759
+ handler(id) {
760
+ if (id === "__jiek_empty__") return id;
761
+ }
762
+ },
763
+ load: {
764
+ order: "post",
765
+ handler(id) {
766
+ if (id === "__jiek_empty__") return "";
767
+ }
668
768
  }
669
769
  }
670
770
  ],
@@ -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`
@@ -113,6 +178,7 @@ const {
113
178
  JIEK_ONLY_MINIFY,
114
179
  JIEK_TSCONFIG,
115
180
  JIEK_DTSCONFIG,
181
+ JIEK_SKIP_JS,
116
182
  JIEK_FEATURES
117
183
  } = process.env;
118
184
  const resolveArrayString = (str) => {
@@ -136,6 +202,7 @@ const WITHOUT_DTS = JIEK_WITHOUT_DTS === "true";
136
202
  const WITHOUT_MINIFY = JIEK_WITHOUT_MINIFY === "true";
137
203
  const ONLY_MINIFY = JIEK_ONLY_MINIFY === "true";
138
204
  const CLEAN = JIEK_CLEAN === "true";
205
+ const SKIP_JS = JIEK_SKIP_JS === "false" ? false : JIEK_SKIP_JS === "true" ? true : void 0;
139
206
  const FEATURES = JSON.parse(JIEK_FEATURES ?? "{}");
140
207
  const MINIFY_DEFAULT_VALUE = WITHOUT_MINIFY ? false : ONLY_MINIFY ? "only-minify" : true;
141
208
  const BUILDER_OPTIONS = {
@@ -147,7 +214,11 @@ const MINIFY_OPTIONS = {
147
214
  const config = loadConfig({
148
215
  root: WORKSPACE_ROOT
149
216
  }) ?? {};
150
- const { experimental, build = {} } = config;
217
+ const {
218
+ experimental,
219
+ skipJS,
220
+ build = {}
221
+ } = config;
151
222
  const { js: jsOutdir, dts: dtsOutdir } = getOutDirs({
152
223
  config,
153
224
  pkgName: JIEK_NAME
@@ -343,7 +414,11 @@ const generateConfigs = (context, {
343
414
  const tsOutputSuffix = jsOutputSuffix.replace(/(\.[cm]?)js$/, ".d$1ts");
344
415
  const { js: jsOutput, dts: dtsOutput } = resolveOutputControls(context, build.output);
345
416
  const rollupOptions = [];
346
- const commonPlugins = [
417
+ const commonPlugins = ({ sourcemap }) => [
418
+ replace({
419
+ sourcemap: sourcemap === "hidden" ? false : !!sourcemap,
420
+ values: build.replacements
421
+ }),
347
422
  ...inputCommonPlugins,
348
423
  withExternal(),
349
424
  !disableCollectInternalModule && {
@@ -454,7 +529,7 @@ const generateConfigs = (context, {
454
529
  )
455
530
  ],
456
531
  plugins: [
457
- ...commonPlugins,
532
+ ...commonPlugins({ sourcemap }),
458
533
  nodeResolvePluginInstance,
459
534
  import('rollup-plugin-postcss').then(
460
535
  ({ default: postcss }) => postcss({
@@ -497,7 +572,7 @@ const generateConfigs = (context, {
497
572
  }
498
573
  ],
499
574
  plugins: [
500
- ...commonPlugins,
575
+ ...commonPlugins({ sourcemap }),
501
576
  skip({ patterns: [STYLE_REGEXP] }),
502
577
  dts({
503
578
  respectExternal: true,
@@ -572,7 +647,8 @@ function template(packageJSON) {
572
647
  entries,
573
648
  pkgName: JIEK_NAME,
574
649
  outdir: jsOutdir,
575
- config
650
+ config,
651
+ skipJS: skipJS ?? SKIP_JS
576
652
  });
577
653
  getAllLeafs(filteredResolvedEntrypoints, ({ keys, value }) => {
578
654
  if (typeof value === "string") {
@@ -615,7 +691,8 @@ function template(packageJSON) {
615
691
  pkgIsModule,
616
692
  pkgName: JIEK_NAME,
617
693
  outdir: `${jsOutdir}/.internal`,
618
- config
694
+ config,
695
+ skipJS: SKIP_JS ?? skipJS
619
696
  });
620
697
  getAllLeafs(filteredResolvedInternalEntrypoints, ({ keys, value }) => {
621
698
  if (typeof value === "string") {
@@ -653,7 +730,29 @@ function template(packageJSON) {
653
730
  Array.isArray(inputOptions.input) ? inputOptions.input : [inputOptions.input],
654
731
  internalModules
655
732
  )];
733
+ if (inputOptions.input.length === 0) {
734
+ inputOptions.input = "__jiek_empty__";
735
+ const plugins = await inputOptions.plugins;
736
+ if (!Array.isArray(plugins)) {
737
+ throw new TypeError("plugins is not an array");
738
+ }
739
+ inputOptions.plugins = plugins.filter(
740
+ (p) => typeof p !== "object" ? true : p === null ? true : "name" in p && p.name === "jiek:loadInternalModules"
741
+ );
742
+ }
656
743
  return inputOptions;
744
+ },
745
+ resolveId: {
746
+ order: "post",
747
+ handler(id) {
748
+ if (id === "__jiek_empty__") return id;
749
+ }
750
+ },
751
+ load: {
752
+ order: "post",
753
+ handler(id) {
754
+ if (id === "__jiek_empty__") return "";
755
+ }
657
756
  }
658
757
  }
659
758
  ],