jiek 2.1.11 → 2.1.12

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.
@@ -2,6 +2,7 @@
2
2
 
3
3
  var fs = require('node:fs');
4
4
  var path = require('node:path');
5
+ var process$1 = require('node:process');
5
6
  var entrypoints = require('@jiek/pkger/entrypoints');
6
7
  var rollupPluginDts = require('@jiek/rollup-plugin-dts');
7
8
  var getWorkspaceDir = require('@jiek/utils/getWorkspaceDir');
@@ -16,17 +17,22 @@ var node_module = require('node:module');
16
17
  var commander = require('commander');
17
18
  var jsYaml = require('js-yaml');
18
19
  var jsoncParser = require('jsonc-parser');
20
+ var inject = require('@rollup/plugin-inject');
21
+ var replace = require('@rollup/plugin-replace');
19
22
 
20
23
  var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
21
24
  function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
22
25
 
23
26
  var fs__default = /*#__PURE__*/_interopDefault(fs);
24
27
  var path__default = /*#__PURE__*/_interopDefault(path);
28
+ var process__default = /*#__PURE__*/_interopDefault(process$1);
25
29
  var commonjs__default = /*#__PURE__*/_interopDefault(commonjs);
26
30
  var json__default = /*#__PURE__*/_interopDefault(json);
27
31
  var require$$0__default = /*#__PURE__*/_interopDefault(require$$0);
28
32
  var require$$0__default$1 = /*#__PURE__*/_interopDefault(require$$0$1);
29
33
  var ts__default = /*#__PURE__*/_interopDefault(ts);
34
+ var inject__default = /*#__PURE__*/_interopDefault(inject);
35
+ var replace__default = /*#__PURE__*/_interopDefault(replace);
30
36
 
31
37
  var utils$1 = {};
32
38
 
@@ -4134,16 +4140,6 @@ function requireMicromatch () {
4134
4140
 
4135
4141
  var micromatchExports = requireMicromatch();
4136
4142
 
4137
- const recusiveListFiles = (dir) => fs__default.default.readdirSync(dir).reduce((acc, file) => {
4138
- const filePath = path.resolve(dir, file);
4139
- if (fs__default.default.statSync(filePath).isDirectory()) {
4140
- if (filePath.endsWith("/node_modules"))
4141
- return acc;
4142
- return [...acc, ...recusiveListFiles(filePath)];
4143
- }
4144
- return [...acc, filePath];
4145
- }, []);
4146
-
4147
4143
  const intersection = (a, b) => new Set([...a].filter((i) => b.has(i)));
4148
4144
  const {
4149
4145
  JIEK_OUT_DIR
@@ -4246,7 +4242,7 @@ function getRoot() {
4246
4242
 
4247
4243
  let type = "";
4248
4244
  try {
4249
- const require$1 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('rollup/index.cjs', document.baseURI).href)));
4245
+ const require$1 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('rollup/index.cjs', document.baseURI).href)));
4250
4246
  require$1.resolve("@pnpm/filter-workspace-packages");
4251
4247
  type = "pnpm";
4252
4248
  } catch {
@@ -4277,7 +4273,7 @@ function getWD() {
4277
4273
  return { wd, notWorkspace };
4278
4274
  }
4279
4275
 
4280
- const require$2 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('rollup/index.cjs', document.baseURI).href)));
4276
+ const require$2 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('rollup/index.cjs', document.baseURI).href)));
4281
4277
  function packageIsExist(name) {
4282
4278
  try {
4283
4279
  require$2.resolve(name);
@@ -4300,7 +4296,7 @@ for (const register of registers) {
4300
4296
  }
4301
4297
  }
4302
4298
 
4303
- const require$1 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.src || new URL('rollup/index.cjs', document.baseURI).href)));
4299
+ const require$1 = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('rollup/index.cjs', document.baseURI).href)));
4304
4300
  let configName = "jiek.config";
4305
4301
  function getConfigPath(root, dir) {
4306
4302
  const isSupportTsLoader = !!tsRegisterName;
@@ -4383,6 +4379,16 @@ function loadConfig(dirOrOptions) {
4383
4379
  return module.default ?? module;
4384
4380
  }
4385
4381
 
4382
+ const recusiveListFiles = (dir) => fs__default.default.readdirSync(dir).reduce((acc, file) => {
4383
+ const filePath = path.resolve(dir, file);
4384
+ if (fs__default.default.statSync(filePath).isDirectory()) {
4385
+ if (filePath.endsWith("/node_modules"))
4386
+ return acc;
4387
+ return [...acc, ...recusiveListFiles(filePath)];
4388
+ }
4389
+ return [...acc, filePath];
4390
+ }, []);
4391
+
4386
4392
  const getTSConfig = (p) => !fs__default.default.existsSync(p) || !fs__default.default.statSync(p).isFile() ? {} : jsoncParser.parse(fs__default.default.readFileSync(p, "utf-8"), [], { allowTrailingComma: true, allowEmptyContent: true });
4387
4393
  const getExtendTSConfig = (tsconfigPath) => {
4388
4394
  tsconfigPath = path.resolve(tsconfigPath);
@@ -4442,6 +4448,44 @@ const getCompilerOptionsByFilePath = (tsconfigPath, filePath) => {
4442
4448
  return tsconfig.compilerOptions;
4443
4449
  };
4444
4450
 
4451
+ const virtualModuleName = "jiek:create-require";
4452
+ const isEsmVariableName = `IS_ESM${Math.random().toString(36).slice(2)}`;
4453
+ const INSERT_STR = `
4454
+ import { createRequire } from 'node:module'
4455
+
4456
+ export default (
4457
+ ${isEsmVariableName}
4458
+ ? /* @__PURE__ */ createRequire(import.meta.url)
4459
+ : require
4460
+ )
4461
+ `.trim();
4462
+ var createRequire = () => ({
4463
+ ...inject__default.default({
4464
+ require: virtualModuleName
4465
+ }),
4466
+ name: "create-require",
4467
+ resolveId: (source) => source === virtualModuleName ? source : null,
4468
+ load: (id) => {
4469
+ if (id !== virtualModuleName) {
4470
+ return null;
4471
+ }
4472
+ return INSERT_STR;
4473
+ }
4474
+ });
4475
+ const isFormatEsm = (isEsm) => {
4476
+ const handler = replace__default.default({
4477
+ [isEsmVariableName]: isEsm
4478
+ }).renderChunk;
4479
+ return {
4480
+ name: "create-require-insert-format",
4481
+ // Pick out renderChunk because it's used as an output plugin
4482
+ renderChunk: {
4483
+ order: "pre",
4484
+ handler: typeof handler === "function" ? handler : handler.handler
4485
+ }
4486
+ };
4487
+ };
4488
+
4445
4489
  var progress = (options = {}) => {
4446
4490
  const { onEvent } = options;
4447
4491
  return {
@@ -4494,6 +4538,7 @@ function externalResolver(jsonOrPath = process.cwd()) {
4494
4538
  }
4495
4539
 
4496
4540
  const {
4541
+ JIEK_ANALYZER,
4497
4542
  JIEK_ROOT,
4498
4543
  JIEK_NAME,
4499
4544
  JIEK_BUILDER,
@@ -4507,7 +4552,7 @@ const {
4507
4552
  JIEK_ONLY_MINIFY,
4508
4553
  JIEK_TSCONFIG,
4509
4554
  JIEK_DTSCONFIG
4510
- } = process.env;
4555
+ } = process__default.default.env;
4511
4556
  const resolveArrayString = (str) => {
4512
4557
  const arr = [
4513
4558
  ...new Set(
@@ -4516,6 +4561,7 @@ const resolveArrayString = (str) => {
4516
4561
  ];
4517
4562
  return arr?.length ? arr : void 0;
4518
4563
  };
4564
+ const ANALYZER = JIEK_ANALYZER && JSON.parse(JIEK_ANALYZER);
4519
4565
  const entries = resolveArrayString(JIEK_ENTRIES)?.map((e) => ({ "index": "." })[e] ?? e);
4520
4566
  const commandExternal = resolveArrayString(JIEK_EXTERNAL)?.map((e) => new RegExp(`^${e}$`));
4521
4567
  const WORKSPACE_ROOT = JIEK_ROOT ?? getWorkspaceDir.getWorkspaceDir();
@@ -4591,9 +4637,20 @@ const { type: _resolvedMinifyOptionsType, ...noTypeResolvedMinifyOptions } = res
4591
4637
  const resolvedBuilderOptions = resolveBuilderOptions(build.builder ?? BUILDER_OPTIONS);
4592
4638
  const { type: _resolvedBuilderOptionsType, ...noTypeResolvedBuilderOptions } = resolvedBuilderOptions;
4593
4639
  const withMinify = (output, minify = build?.output?.minify ?? MINIFY_DEFAULT_VALUE) => {
4594
- if (minify === false)
4640
+ output.plugins = output.plugins ?? [];
4641
+ const onlyOncePlugins = [
4642
+ // adapter(analyzer({
4643
+ // analyzerMode: 'server',
4644
+ // analyzerPort: 8888,
4645
+ // reportTitle: 'Bundle Analysis'
4646
+ // }))
4647
+ ];
4648
+ if (minify === false) {
4649
+ output.plugins.push(...onlyOncePlugins);
4595
4650
  return [output];
4651
+ }
4596
4652
  const minifyPlugin = resolvedMinifyOptions.type === "esbuild" ? import('rollup-plugin-esbuild').then(({ minify: minify2 }) => minify2(noTypeResolvedMinifyOptions)) : resolvedMinifyOptions.type === "swc" ? import('rollup-plugin-swc3').then(({ minify: minify2 }) => minify2(noTypeResolvedMinifyOptions)) : import('@rollup/plugin-terser').then(({ default: minify2 }) => minify2(noTypeResolvedMinifyOptions));
4653
+ const notOnlyOncePlugins = output.plugins;
4597
4654
  return minify === "only-minify" ? [{
4598
4655
  ...output,
4599
4656
  // TODO replace suffix when pubish to npm and the `build.output.minify` is 'only-minify'
@@ -4602,7 +4659,7 @@ const withMinify = (output, minify = build?.output?.minify ?? MINIFY_DEFAULT_VAL
4602
4659
  throw new Error("entryFileNames must be a function");
4603
4660
  })(),
4604
4661
  plugins: [
4605
- ...output.plugins ?? [],
4662
+ ...notOnlyOncePlugins,
4606
4663
  minifyPlugin
4607
4664
  ]
4608
4665
  }] : [
@@ -4614,7 +4671,7 @@ const withMinify = (output, minify = build?.output?.minify ?? MINIFY_DEFAULT_VAL
4614
4671
  })(),
4615
4672
  file: output.file?.replace(/(\.[cm]?js)$/, ".min$1"),
4616
4673
  plugins: [
4617
- ...output.plugins ?? [],
4674
+ ...notOnlyOncePlugins,
4618
4675
  minifyPlugin
4619
4676
  ]
4620
4677
  }
@@ -4681,7 +4738,7 @@ const generateConfigs = (context, options = {}) => {
4681
4738
  'input should not include "**", please read the [documentation](https://nodejs.org/api/packages.html#subpath-patterns).'
4682
4739
  );
4683
4740
  }
4684
- const inputObj = !input.includes("*") ? input : recusiveListFiles(process.cwd()).filter((p) => /(?<!\.d)\.[cm]?tsx?$/.test(p)).map((p) => path.relative(process.cwd(), p)).filter((p) => micromatchExports.isMatch(p, input.slice(2)));
4741
+ const inputObj = !input.includes("*") ? input : recusiveListFiles(process__default.default.cwd()).filter((p) => /(?<!\.d)\.[cm]?tsx?$/.test(p)).map((p) => path.relative(process__default.default.cwd(), p)).filter((p) => micromatchExports.isMatch(p, input.slice(2)));
4685
4742
  const globCommonDir = input.includes("*") ? input.split("*")[0].replace("./", "") : "";
4686
4743
  const pathCommonDir = path$1.includes("*") ? path$1.split("*")[0].replace("./", "") : "";
4687
4744
  if (globCommonDir.length > 0 && pathCommonDir.length === 0 || globCommonDir.length === 0 && pathCommonDir.length > 0) {
@@ -4712,6 +4769,55 @@ const generateConfigs = (context, options = {}) => {
4712
4769
  ...noTypeResolvedBuilderOptions
4713
4770
  })
4714
4771
  );
4772
+ const ana = ANALYZER ? import('vite-bundle-analyzer').then(({ adapter, analyzer }) => {
4773
+ const defaultSizes = {
4774
+ parsed: "parsed",
4775
+ stat: "stat",
4776
+ gzip: "gzip"
4777
+ }[ANALYZER.size ?? "stat"] ?? "parsed";
4778
+ const title = `${path.join(context.name, context.path)} ${context.conditionals.join(",")}`;
4779
+ const filename = title.replace("/", "_").replace(" ", "_");
4780
+ switch (ANALYZER.mode ?? "server") {
4781
+ case "server":
4782
+ return adapter(analyzer({
4783
+ defaultSizes,
4784
+ analyzerMode: "server",
4785
+ analyzerPort: ANALYZER.port ?? "auto",
4786
+ openAnalyzer: ANALYZER.open ?? false,
4787
+ reportTitle: `Bundle Analysis ${title}`
4788
+ }));
4789
+ case "json":
4790
+ return adapter(analyzer({
4791
+ defaultSizes,
4792
+ analyzerMode: "json",
4793
+ fileName: ANALYZER.dir ? path.join(ANALYZER.dir, filename) : filename
4794
+ }));
4795
+ case "static":
4796
+ return adapter(analyzer({
4797
+ defaultSizes,
4798
+ analyzerMode: "static",
4799
+ analyzerPort: ANALYZER.port ?? "auto",
4800
+ openAnalyzer: ANALYZER.open ?? false,
4801
+ reportTitle: `Bundle Analysis ${title}`,
4802
+ fileName: ANALYZER.dir ? path.join(ANALYZER.dir, filename) : filename
4803
+ }));
4804
+ case void 0: {
4805
+ throw new Error("Not implemented yet: undefined case");
4806
+ }
4807
+ default:
4808
+ void execa.sendMessage(
4809
+ {
4810
+ ...throughEventProps,
4811
+ data: {
4812
+ ...throughEventProps.data,
4813
+ event: "error",
4814
+ message: "ANALYZER.mode not supported",
4815
+ tags: ["js"]
4816
+ }
4817
+ }
4818
+ );
4819
+ }
4820
+ }) : void 0;
4715
4821
  rollupOptions.push({
4716
4822
  input: inputObj,
4717
4823
  external,
@@ -4720,10 +4826,13 @@ const generateConfigs = (context, options = {}) => {
4720
4826
  dir: jsOutdir,
4721
4827
  name,
4722
4828
  interop: "auto",
4723
- entryFileNames: (chunkInfo) => Array.isArray(inputObj) ? chunkInfo.facadeModuleId.replace(`${process.cwd()}/`, "").replace(globCommonDir, pathCommonDir).replace(/(\.[cm]?)ts$/, jsOutputSuffix) : output.replace(`${jsOutdir}/`, ""),
4829
+ entryFileNames: (chunkInfo) => Array.isArray(inputObj) ? chunkInfo.facadeModuleId.replace(`${process__default.default.cwd()}/`, "").replace(globCommonDir, pathCommonDir).replace(/(\.[cm]?)ts$/, jsOutputSuffix) : output.replace(`${jsOutdir}/`, ""),
4724
4830
  sourcemap,
4725
4831
  format: isModule ? "esm" : isCommonJS ? "cjs" : isBrowser ? "umd" : pkgIsModule ? "esm" : "cjs",
4726
- strict: typeof options?.output?.strict === "object" ? options.output.strict.js : options?.output?.strict
4832
+ strict: typeof options?.output?.strict === "object" ? options.output.strict.js : options?.output?.strict,
4833
+ plugins: [
4834
+ isFormatEsm(isModule)
4835
+ ]
4727
4836
  })
4728
4837
  ],
4729
4838
  plugins: [
@@ -4734,10 +4843,12 @@ const generateConfigs = (context, options = {}) => {
4734
4843
  minimize: true
4735
4844
  })
4736
4845
  ).catch(() => void 0),
4737
- builder,
4738
4846
  commonjs__default.default(),
4847
+ createRequire(),
4848
+ builder,
4849
+ ana,
4739
4850
  progress({
4740
- onEvent: (event, message) => execa.sendMessage(
4851
+ onEvent: (event, message) => void execa.sendMessage(
4741
4852
  {
4742
4853
  ...throughEventProps,
4743
4854
  data: { ...throughEventProps.data, event, message, tags: ["js"] }
@@ -4756,7 +4867,7 @@ const generateConfigs = (context, options = {}) => {
4756
4867
  {
4757
4868
  dir: dtsOutdir,
4758
4869
  sourcemap: typeof options?.output?.sourcemap === "object" ? options.output.sourcemap.dts : options?.output?.sourcemap,
4759
- entryFileNames: (chunkInfo) => Array.isArray(inputObj) ? chunkInfo.facadeModuleId.replace(`${process.cwd()}/`, "").replace(globCommonDir, pathCommonDir).replace(/(\.[cm]?)ts$/, tsOutputSuffix) : output.replace(`${jsOutdir}/`, "").replace(/(\.[cm]?)js$/, tsOutputSuffix),
4870
+ entryFileNames: (chunkInfo) => Array.isArray(inputObj) ? chunkInfo.facadeModuleId.replace(`${process__default.default.cwd()}/`, "").replace(globCommonDir, pathCommonDir).replace(/(\.[cm]?)ts$/, tsOutputSuffix) : output.replace(`${jsOutdir}/`, "").replace(/(\.[cm]?)js$/, tsOutputSuffix),
4760
4871
  strict: typeof options?.output?.strict === "object" ? options.output.strict.dts : options?.output?.strict
4761
4872
  }
4762
4873
  ],
@@ -4779,7 +4890,7 @@ const generateConfigs = (context, options = {}) => {
4779
4890
  tsconfig: dtsTSConfigPath
4780
4891
  }),
4781
4892
  progress({
4782
- onEvent: (event, message) => execa.sendMessage(
4893
+ onEvent: (event, message) => void execa.sendMessage(
4783
4894
  {
4784
4895
  ...throughEventProps,
4785
4896
  data: { ...throughEventProps.data, event, message, tags: ["dts"] }
@@ -4794,7 +4905,7 @@ const generateConfigs = (context, options = {}) => {
4794
4905
  rollupOptions[0].plugins = [
4795
4906
  {
4796
4907
  name: "jiek-plugin-watcher",
4797
- watchChange: (id) => execa.sendMessage(
4908
+ watchChange: (id) => void execa.sendMessage(
4798
4909
  {
4799
4910
  type: "watchChange",
4800
4911
  data: { id, name: JIEK_NAME, path: path$1, input }
@@ -4871,7 +4982,7 @@ function template(packageJSON) {
4871
4982
  }
4872
4983
  })
4873
4984
  );
4874
- execa.sendMessage(
4985
+ void execa.sendMessage(
4875
4986
  {
4876
4987
  type: "init",
4877
4988
  data: {
@@ -1,5 +1,6 @@
1
1
  import fs from 'node:fs';
2
- import path, { resolve, isAbsolute, relative, dirname, extname } from 'node:path';
2
+ import path, { isAbsolute, relative, resolve, dirname, extname, join } from 'node:path';
3
+ import process$1 from 'node:process';
3
4
  import { resolveEntrypoints, filterLeafs, DEFAULT_SKIP_VALUES, entrypoints2Exports, getAllLeafs } from '@jiek/pkger/entrypoints';
4
5
  import { dts } from '@jiek/rollup-plugin-dts';
5
6
  import { isWorkspaceDir, getWorkspaceDir } from '@jiek/utils/getWorkspaceDir';
@@ -10,10 +11,12 @@ import { sendMessage } from 'execa';
10
11
  import require$$0 from 'util';
11
12
  import require$$0$1 from 'path';
12
13
  import ts from 'typescript';
13
- import { createRequire, builtinModules } from 'node:module';
14
+ import { createRequire as createRequire$1, builtinModules } from 'node:module';
14
15
  import { program } from 'commander';
15
16
  import { load } from 'js-yaml';
16
17
  import { parse } from 'jsonc-parser';
18
+ import inject from '@rollup/plugin-inject';
19
+ import replace from '@rollup/plugin-replace';
17
20
 
18
21
  var utils$1 = {};
19
22
 
@@ -4121,16 +4124,6 @@ function requireMicromatch () {
4121
4124
 
4122
4125
  var micromatchExports = requireMicromatch();
4123
4126
 
4124
- const recusiveListFiles = (dir) => fs.readdirSync(dir).reduce((acc, file) => {
4125
- const filePath = resolve(dir, file);
4126
- if (fs.statSync(filePath).isDirectory()) {
4127
- if (filePath.endsWith("/node_modules"))
4128
- return acc;
4129
- return [...acc, ...recusiveListFiles(filePath)];
4130
- }
4131
- return [...acc, filePath];
4132
- }, []);
4133
-
4134
4127
  const intersection = (a, b) => new Set([...a].filter((i) => b.has(i)));
4135
4128
  const {
4136
4129
  JIEK_OUT_DIR
@@ -4233,7 +4226,7 @@ function getRoot() {
4233
4226
 
4234
4227
  let type = "";
4235
4228
  try {
4236
- const require = createRequire(import.meta.url);
4229
+ const require = createRequire$1(import.meta.url);
4237
4230
  require.resolve("@pnpm/filter-workspace-packages");
4238
4231
  type = "pnpm";
4239
4232
  } catch {
@@ -4264,7 +4257,7 @@ function getWD() {
4264
4257
  return { wd, notWorkspace };
4265
4258
  }
4266
4259
 
4267
- const require$1 = createRequire(import.meta.url);
4260
+ const require$1 = createRequire$1(import.meta.url);
4268
4261
  function packageIsExist(name) {
4269
4262
  try {
4270
4263
  require$1.resolve(name);
@@ -4287,7 +4280,7 @@ for (const register of registers) {
4287
4280
  }
4288
4281
  }
4289
4282
 
4290
- const require = createRequire(import.meta.url);
4283
+ const require = createRequire$1(import.meta.url);
4291
4284
  let configName = "jiek.config";
4292
4285
  function getConfigPath(root, dir) {
4293
4286
  const isSupportTsLoader = !!tsRegisterName;
@@ -4370,6 +4363,16 @@ function loadConfig(dirOrOptions) {
4370
4363
  return module.default ?? module;
4371
4364
  }
4372
4365
 
4366
+ const recusiveListFiles = (dir) => fs.readdirSync(dir).reduce((acc, file) => {
4367
+ const filePath = resolve(dir, file);
4368
+ if (fs.statSync(filePath).isDirectory()) {
4369
+ if (filePath.endsWith("/node_modules"))
4370
+ return acc;
4371
+ return [...acc, ...recusiveListFiles(filePath)];
4372
+ }
4373
+ return [...acc, filePath];
4374
+ }, []);
4375
+
4373
4376
  const getTSConfig = (p) => !fs.existsSync(p) || !fs.statSync(p).isFile() ? {} : parse(fs.readFileSync(p, "utf-8"), [], { allowTrailingComma: true, allowEmptyContent: true });
4374
4377
  const getExtendTSConfig = (tsconfigPath) => {
4375
4378
  tsconfigPath = resolve(tsconfigPath);
@@ -4429,6 +4432,44 @@ const getCompilerOptionsByFilePath = (tsconfigPath, filePath) => {
4429
4432
  return tsconfig.compilerOptions;
4430
4433
  };
4431
4434
 
4435
+ const virtualModuleName = "jiek:create-require";
4436
+ const isEsmVariableName = `IS_ESM${Math.random().toString(36).slice(2)}`;
4437
+ const INSERT_STR = `
4438
+ import { createRequire } from 'node:module'
4439
+
4440
+ export default (
4441
+ ${isEsmVariableName}
4442
+ ? /* @__PURE__ */ createRequire(import.meta.url)
4443
+ : require
4444
+ )
4445
+ `.trim();
4446
+ var createRequire = () => ({
4447
+ ...inject({
4448
+ require: virtualModuleName
4449
+ }),
4450
+ name: "create-require",
4451
+ resolveId: (source) => source === virtualModuleName ? source : null,
4452
+ load: (id) => {
4453
+ if (id !== virtualModuleName) {
4454
+ return null;
4455
+ }
4456
+ return INSERT_STR;
4457
+ }
4458
+ });
4459
+ const isFormatEsm = (isEsm) => {
4460
+ const handler = replace({
4461
+ [isEsmVariableName]: isEsm
4462
+ }).renderChunk;
4463
+ return {
4464
+ name: "create-require-insert-format",
4465
+ // Pick out renderChunk because it's used as an output plugin
4466
+ renderChunk: {
4467
+ order: "pre",
4468
+ handler: typeof handler === "function" ? handler : handler.handler
4469
+ }
4470
+ };
4471
+ };
4472
+
4432
4473
  var progress = (options = {}) => {
4433
4474
  const { onEvent } = options;
4434
4475
  return {
@@ -4481,6 +4522,7 @@ function externalResolver(jsonOrPath = process.cwd()) {
4481
4522
  }
4482
4523
 
4483
4524
  const {
4525
+ JIEK_ANALYZER,
4484
4526
  JIEK_ROOT,
4485
4527
  JIEK_NAME,
4486
4528
  JIEK_BUILDER,
@@ -4494,7 +4536,7 @@ const {
4494
4536
  JIEK_ONLY_MINIFY,
4495
4537
  JIEK_TSCONFIG,
4496
4538
  JIEK_DTSCONFIG
4497
- } = process.env;
4539
+ } = process$1.env;
4498
4540
  const resolveArrayString = (str) => {
4499
4541
  const arr = [
4500
4542
  ...new Set(
@@ -4503,6 +4545,7 @@ const resolveArrayString = (str) => {
4503
4545
  ];
4504
4546
  return arr?.length ? arr : void 0;
4505
4547
  };
4548
+ const ANALYZER = JIEK_ANALYZER && JSON.parse(JIEK_ANALYZER);
4506
4549
  const entries = resolveArrayString(JIEK_ENTRIES)?.map((e) => ({ "index": "." })[e] ?? e);
4507
4550
  const commandExternal = resolveArrayString(JIEK_EXTERNAL)?.map((e) => new RegExp(`^${e}$`));
4508
4551
  const WORKSPACE_ROOT = JIEK_ROOT ?? getWorkspaceDir();
@@ -4578,9 +4621,20 @@ const { type: _resolvedMinifyOptionsType, ...noTypeResolvedMinifyOptions } = res
4578
4621
  const resolvedBuilderOptions = resolveBuilderOptions(build.builder ?? BUILDER_OPTIONS);
4579
4622
  const { type: _resolvedBuilderOptionsType, ...noTypeResolvedBuilderOptions } = resolvedBuilderOptions;
4580
4623
  const withMinify = (output, minify = build?.output?.minify ?? MINIFY_DEFAULT_VALUE) => {
4581
- if (minify === false)
4624
+ output.plugins = output.plugins ?? [];
4625
+ const onlyOncePlugins = [
4626
+ // adapter(analyzer({
4627
+ // analyzerMode: 'server',
4628
+ // analyzerPort: 8888,
4629
+ // reportTitle: 'Bundle Analysis'
4630
+ // }))
4631
+ ];
4632
+ if (minify === false) {
4633
+ output.plugins.push(...onlyOncePlugins);
4582
4634
  return [output];
4635
+ }
4583
4636
  const minifyPlugin = resolvedMinifyOptions.type === "esbuild" ? import('rollup-plugin-esbuild').then(({ minify: minify2 }) => minify2(noTypeResolvedMinifyOptions)) : resolvedMinifyOptions.type === "swc" ? import('rollup-plugin-swc3').then(({ minify: minify2 }) => minify2(noTypeResolvedMinifyOptions)) : import('@rollup/plugin-terser').then(({ default: minify2 }) => minify2(noTypeResolvedMinifyOptions));
4637
+ const notOnlyOncePlugins = output.plugins;
4584
4638
  return minify === "only-minify" ? [{
4585
4639
  ...output,
4586
4640
  // TODO replace suffix when pubish to npm and the `build.output.minify` is 'only-minify'
@@ -4589,7 +4643,7 @@ const withMinify = (output, minify = build?.output?.minify ?? MINIFY_DEFAULT_VAL
4589
4643
  throw new Error("entryFileNames must be a function");
4590
4644
  })(),
4591
4645
  plugins: [
4592
- ...output.plugins ?? [],
4646
+ ...notOnlyOncePlugins,
4593
4647
  minifyPlugin
4594
4648
  ]
4595
4649
  }] : [
@@ -4601,7 +4655,7 @@ const withMinify = (output, minify = build?.output?.minify ?? MINIFY_DEFAULT_VAL
4601
4655
  })(),
4602
4656
  file: output.file?.replace(/(\.[cm]?js)$/, ".min$1"),
4603
4657
  plugins: [
4604
- ...output.plugins ?? [],
4658
+ ...notOnlyOncePlugins,
4605
4659
  minifyPlugin
4606
4660
  ]
4607
4661
  }
@@ -4668,7 +4722,7 @@ const generateConfigs = (context, options = {}) => {
4668
4722
  'input should not include "**", please read the [documentation](https://nodejs.org/api/packages.html#subpath-patterns).'
4669
4723
  );
4670
4724
  }
4671
- const inputObj = !input.includes("*") ? input : recusiveListFiles(process.cwd()).filter((p) => /(?<!\.d)\.[cm]?tsx?$/.test(p)).map((p) => relative(process.cwd(), p)).filter((p) => micromatchExports.isMatch(p, input.slice(2)));
4725
+ const inputObj = !input.includes("*") ? input : recusiveListFiles(process$1.cwd()).filter((p) => /(?<!\.d)\.[cm]?tsx?$/.test(p)).map((p) => relative(process$1.cwd(), p)).filter((p) => micromatchExports.isMatch(p, input.slice(2)));
4672
4726
  const globCommonDir = input.includes("*") ? input.split("*")[0].replace("./", "") : "";
4673
4727
  const pathCommonDir = path.includes("*") ? path.split("*")[0].replace("./", "") : "";
4674
4728
  if (globCommonDir.length > 0 && pathCommonDir.length === 0 || globCommonDir.length === 0 && pathCommonDir.length > 0) {
@@ -4699,6 +4753,55 @@ const generateConfigs = (context, options = {}) => {
4699
4753
  ...noTypeResolvedBuilderOptions
4700
4754
  })
4701
4755
  );
4756
+ const ana = ANALYZER ? import('vite-bundle-analyzer').then(({ adapter, analyzer }) => {
4757
+ const defaultSizes = {
4758
+ parsed: "parsed",
4759
+ stat: "stat",
4760
+ gzip: "gzip"
4761
+ }[ANALYZER.size ?? "stat"] ?? "parsed";
4762
+ const title = `${join(context.name, context.path)} ${context.conditionals.join(",")}`;
4763
+ const filename = title.replace("/", "_").replace(" ", "_");
4764
+ switch (ANALYZER.mode ?? "server") {
4765
+ case "server":
4766
+ return adapter(analyzer({
4767
+ defaultSizes,
4768
+ analyzerMode: "server",
4769
+ analyzerPort: ANALYZER.port ?? "auto",
4770
+ openAnalyzer: ANALYZER.open ?? false,
4771
+ reportTitle: `Bundle Analysis ${title}`
4772
+ }));
4773
+ case "json":
4774
+ return adapter(analyzer({
4775
+ defaultSizes,
4776
+ analyzerMode: "json",
4777
+ fileName: ANALYZER.dir ? join(ANALYZER.dir, filename) : filename
4778
+ }));
4779
+ case "static":
4780
+ return adapter(analyzer({
4781
+ defaultSizes,
4782
+ analyzerMode: "static",
4783
+ analyzerPort: ANALYZER.port ?? "auto",
4784
+ openAnalyzer: ANALYZER.open ?? false,
4785
+ reportTitle: `Bundle Analysis ${title}`,
4786
+ fileName: ANALYZER.dir ? join(ANALYZER.dir, filename) : filename
4787
+ }));
4788
+ case void 0: {
4789
+ throw new Error("Not implemented yet: undefined case");
4790
+ }
4791
+ default:
4792
+ void sendMessage(
4793
+ {
4794
+ ...throughEventProps,
4795
+ data: {
4796
+ ...throughEventProps.data,
4797
+ event: "error",
4798
+ message: "ANALYZER.mode not supported",
4799
+ tags: ["js"]
4800
+ }
4801
+ }
4802
+ );
4803
+ }
4804
+ }) : void 0;
4702
4805
  rollupOptions.push({
4703
4806
  input: inputObj,
4704
4807
  external,
@@ -4707,10 +4810,13 @@ const generateConfigs = (context, options = {}) => {
4707
4810
  dir: jsOutdir,
4708
4811
  name,
4709
4812
  interop: "auto",
4710
- entryFileNames: (chunkInfo) => Array.isArray(inputObj) ? chunkInfo.facadeModuleId.replace(`${process.cwd()}/`, "").replace(globCommonDir, pathCommonDir).replace(/(\.[cm]?)ts$/, jsOutputSuffix) : output.replace(`${jsOutdir}/`, ""),
4813
+ entryFileNames: (chunkInfo) => Array.isArray(inputObj) ? chunkInfo.facadeModuleId.replace(`${process$1.cwd()}/`, "").replace(globCommonDir, pathCommonDir).replace(/(\.[cm]?)ts$/, jsOutputSuffix) : output.replace(`${jsOutdir}/`, ""),
4711
4814
  sourcemap,
4712
4815
  format: isModule ? "esm" : isCommonJS ? "cjs" : isBrowser ? "umd" : pkgIsModule ? "esm" : "cjs",
4713
- strict: typeof options?.output?.strict === "object" ? options.output.strict.js : options?.output?.strict
4816
+ strict: typeof options?.output?.strict === "object" ? options.output.strict.js : options?.output?.strict,
4817
+ plugins: [
4818
+ isFormatEsm(isModule)
4819
+ ]
4714
4820
  })
4715
4821
  ],
4716
4822
  plugins: [
@@ -4721,10 +4827,12 @@ const generateConfigs = (context, options = {}) => {
4721
4827
  minimize: true
4722
4828
  })
4723
4829
  ).catch(() => void 0),
4724
- builder,
4725
4830
  commonjs(),
4831
+ createRequire(),
4832
+ builder,
4833
+ ana,
4726
4834
  progress({
4727
- onEvent: (event, message) => sendMessage(
4835
+ onEvent: (event, message) => void sendMessage(
4728
4836
  {
4729
4837
  ...throughEventProps,
4730
4838
  data: { ...throughEventProps.data, event, message, tags: ["js"] }
@@ -4743,7 +4851,7 @@ const generateConfigs = (context, options = {}) => {
4743
4851
  {
4744
4852
  dir: dtsOutdir,
4745
4853
  sourcemap: typeof options?.output?.sourcemap === "object" ? options.output.sourcemap.dts : options?.output?.sourcemap,
4746
- entryFileNames: (chunkInfo) => Array.isArray(inputObj) ? chunkInfo.facadeModuleId.replace(`${process.cwd()}/`, "").replace(globCommonDir, pathCommonDir).replace(/(\.[cm]?)ts$/, tsOutputSuffix) : output.replace(`${jsOutdir}/`, "").replace(/(\.[cm]?)js$/, tsOutputSuffix),
4854
+ entryFileNames: (chunkInfo) => Array.isArray(inputObj) ? chunkInfo.facadeModuleId.replace(`${process$1.cwd()}/`, "").replace(globCommonDir, pathCommonDir).replace(/(\.[cm]?)ts$/, tsOutputSuffix) : output.replace(`${jsOutdir}/`, "").replace(/(\.[cm]?)js$/, tsOutputSuffix),
4747
4855
  strict: typeof options?.output?.strict === "object" ? options.output.strict.dts : options?.output?.strict
4748
4856
  }
4749
4857
  ],
@@ -4766,7 +4874,7 @@ const generateConfigs = (context, options = {}) => {
4766
4874
  tsconfig: dtsTSConfigPath
4767
4875
  }),
4768
4876
  progress({
4769
- onEvent: (event, message) => sendMessage(
4877
+ onEvent: (event, message) => void sendMessage(
4770
4878
  {
4771
4879
  ...throughEventProps,
4772
4880
  data: { ...throughEventProps.data, event, message, tags: ["dts"] }
@@ -4781,7 +4889,7 @@ const generateConfigs = (context, options = {}) => {
4781
4889
  rollupOptions[0].plugins = [
4782
4890
  {
4783
4891
  name: "jiek-plugin-watcher",
4784
- watchChange: (id) => sendMessage(
4892
+ watchChange: (id) => void sendMessage(
4785
4893
  {
4786
4894
  type: "watchChange",
4787
4895
  data: { id, name: JIEK_NAME, path, input }
@@ -4858,7 +4966,7 @@ function template(packageJSON) {
4858
4966
  }
4859
4967
  })
4860
4968
  );
4861
- sendMessage(
4969
+ void sendMessage(
4862
4970
  {
4863
4971
  type: "init",
4864
4972
  data: {