jiek 0.4.7-alpha.3 → 0.4.7-alpha.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,31 +1,35 @@
1
- import fs from 'node:fs';
2
- import path, { resolve, relative, dirname } from 'node:path';
3
- import { resolveEntrypoints, filterLeafs, DEFAULT_SKIP_VALUES, entrypoints2Exports, getAllLeafs } from '@jiek/pkger/entrypoints';
4
- import { dts } from '@jiek/rollup-plugin-dts';
5
- import { isWorkspaceDir, getWorkspaceDir } from '@jiek/utils/getWorkspaceDir';
6
- import json from '@rollup/plugin-json';
7
- import { nodeResolve } from '@rollup/plugin-node-resolve';
8
- import terser from '@rollup/plugin-terser';
9
- import { sendMessage } from 'execa';
10
- import { parse } from 'jsonc-parser';
11
- import { isMatch } from 'micromatch';
12
- import esbuild from 'rollup-plugin-esbuild';
13
- import ts from 'typescript';
14
- import { program } from 'commander';
15
- import { load } from 'js-yaml';
16
- import '@pnpm/filter-workspace-packages';
1
+ 'use strict';
2
+
3
+ var fs = require('node:fs');
4
+ var path = require('node:path');
5
+ var entrypoints = require('@jiek/pkger/entrypoints');
6
+ var rollupPluginDts = require('@jiek/rollup-plugin-dts');
7
+ var getWorkspaceDir = require('@jiek/utils/getWorkspaceDir');
8
+ var json = require('@rollup/plugin-json');
9
+ var pluginNodeResolve = require('@rollup/plugin-node-resolve');
10
+ var terser = require('@rollup/plugin-terser');
11
+ var execa = require('execa');
12
+ var jsoncParser = require('jsonc-parser');
13
+ var micromatch = require('micromatch');
14
+ var esbuild = require('rollup-plugin-esbuild');
15
+ var ts = require('typescript');
16
+ var commander = require('commander');
17
+ var jsYaml = require('js-yaml');
18
+ require('@pnpm/filter-workspace-packages');
17
19
 
18
20
  const intersection = (a, b) => new Set([...a].filter((i) => b.has(i)));
19
21
  function getExports({
20
- entrypoints,
22
+ entrypoints: entrypoints$1,
21
23
  pkgIsModule,
22
24
  entries,
23
25
  config,
24
26
  dir,
25
- noFilter
27
+ noFilter,
28
+ withSource,
29
+ withSuffix
26
30
  }) {
27
- const dirResolve = (...paths) => resolve(dir ?? process.cwd(), ...paths);
28
- const dirRelative = (path) => relative(dir ?? process.cwd(), path);
31
+ const dirResolve = (...paths) => path.resolve(dir ?? process.cwd(), ...paths);
32
+ const dirRelative = (path$1) => path.relative(dir ?? process.cwd(), path$1);
29
33
  const { build = {} } = config ?? {};
30
34
  const {
31
35
  crossModuleConvertor = true
@@ -33,21 +37,21 @@ function getExports({
33
37
  const jsOutdir = `./${dirRelative(dirResolve(
34
38
  (typeof build?.output?.dir === "object" ? build.output.dir.js : build?.output?.dir) ?? "dist"
35
39
  ))}`;
36
- const [, resolvedEntrypoints] = resolveEntrypoints(entrypoints);
40
+ const [, resolvedEntrypoints] = entrypoints.resolveEntrypoints(entrypoints$1);
37
41
  if (entries) {
38
42
  Object.entries(resolvedEntrypoints).forEach(([key]) => {
39
- if (!entries.some((e) => isMatch(key, e, { matchBase: true }))) {
43
+ if (!entries.some((e) => micromatch.isMatch(key, e, { matchBase: true }))) {
40
44
  delete resolvedEntrypoints[key];
41
45
  }
42
46
  });
43
47
  }
44
- const filteredResolvedEntrypoints = noFilter ? resolvedEntrypoints : filterLeafs(
48
+ const filteredResolvedEntrypoints = noFilter ? resolvedEntrypoints : entrypoints.filterLeafs(
45
49
  resolvedEntrypoints,
46
50
  {
47
51
  skipValue: [
48
52
  // ignore values that filename starts with `.jk-noentry`
49
53
  /(^|\/)\.jk-noentry/,
50
- ...DEFAULT_SKIP_VALUES
54
+ ...entrypoints.DEFAULT_SKIP_VALUES
51
55
  ]
52
56
  }
53
57
  );
@@ -63,8 +67,10 @@ function getExports({
63
67
  } : {};
64
68
  return [
65
69
  filteredResolvedEntrypoints,
66
- entrypoints2Exports(filteredResolvedEntrypoints, {
70
+ entrypoints.entrypoints2Exports(filteredResolvedEntrypoints, {
67
71
  outdir: jsOutdir,
72
+ withSource,
73
+ withSuffix,
68
74
  withConditional: {
69
75
  ...crossModuleWithConditional
70
76
  }
@@ -76,7 +82,7 @@ let root;
76
82
  function getRoot() {
77
83
  if (root)
78
84
  return root;
79
- const rootOption = program.getOptionValue("root");
85
+ const rootOption = commander.program.getOptionValue("root");
80
86
  root = rootOption ? path.isAbsolute(rootOption) ? rootOption : path.resolve(process.cwd(), rootOption) : void 0;
81
87
  return root;
82
88
  }
@@ -88,7 +94,7 @@ try {
88
94
  } catch {
89
95
  }
90
96
  if (type !== "") {
91
- program.option("-f, --filter <filter>", "filter packages");
97
+ commander.program.option("-f, --filter <filter>", "filter packages");
92
98
  }
93
99
 
94
100
  let wd;
@@ -98,13 +104,13 @@ function getWD() {
98
104
  return { wd, notWorkspace };
99
105
  const root = getRoot();
100
106
  if (root !== void 0) {
101
- const isWorkspace = isWorkspaceDir(root, type);
107
+ const isWorkspace = getWorkspaceDir.isWorkspaceDir(root, type);
102
108
  notWorkspace = !isWorkspace;
103
109
  wd = root;
104
110
  return { wd, notWorkspace };
105
111
  }
106
112
  try {
107
- wd = getWorkspaceDir(type);
113
+ wd = getWorkspaceDir.getWorkspaceDir(type);
108
114
  } catch (e) {
109
115
  if ("message" in e && e.message === "workspace root not found") {
110
116
  wd = root;
@@ -171,7 +177,7 @@ function getConfigPath(root, dir) {
171
177
  }
172
178
  function loadConfig(dir) {
173
179
  const { wd: root } = getWD();
174
- let configPath = program.getOptionValue("configPath");
180
+ let configPath = commander.program.getOptionValue("configPath");
175
181
  if (!configPath) {
176
182
  configPath = getConfigPath(root, dir);
177
183
  } else {
@@ -191,7 +197,7 @@ function loadConfig(dir) {
191
197
  case ".json":
192
198
  return require(configPath);
193
199
  case ".yaml":
194
- return load(fs.readFileSync(configPath, "utf-8"));
200
+ return jsYaml.load(fs.readFileSync(configPath, "utf-8"));
195
201
  case ".ts":
196
202
  if (tsRegisterName) {
197
203
  require(tsRegisterName);
@@ -260,21 +266,21 @@ const {
260
266
  JIEK_ROOT,
261
267
  JIEK_ENTRIES
262
268
  } = process.env;
263
- const WORKSPACE_ROOT = JIEK_ROOT ?? getWorkspaceDir();
269
+ const WORKSPACE_ROOT = JIEK_ROOT ?? getWorkspaceDir.getWorkspaceDir();
264
270
  const COMMON_OPTIONS = {};
265
271
  const COMMON_PLUGINS = [
266
272
  json()
267
273
  ];
268
274
  const config = loadConfig() ?? {};
269
275
  const { build = {} } = config;
270
- const jsOutdir = `./${relative(
276
+ const jsOutdir = `./${path.relative(
271
277
  process.cwd(),
272
- resolve(
278
+ path.resolve(
273
279
  (typeof build?.output?.dir === "object" ? build.output.dir.js : build?.output?.dir) ?? "dist"
274
280
  )
275
281
  )}`;
276
282
  const STYLE_REGEXP = /\.(css|s[ac]ss|less|styl)$/;
277
- const resolveWorkspacePath = (p) => resolve(WORKSPACE_ROOT, p);
283
+ const resolveWorkspacePath = (p) => path.resolve(WORKSPACE_ROOT, p);
278
284
  const pascalCase = (str) => str.replace(/[@|/-](\w)/g, (_, $1) => $1.toUpperCase()).replace(/(?:^|-)(\w)/g, (_, $1) => $1.toUpperCase());
279
285
  const reveal = (obj, keys) => keys.reduce((acc, key) => {
280
286
  if (typeof acc === "string")
@@ -303,12 +309,12 @@ const withMinify = (output, minify = build?.output?.minify) => minify === false
303
309
  ]
304
310
  }
305
311
  ];
306
- const getTSConfig = (p) => !fs.existsSync(p) || !fs.statSync(p).isFile() ? {} : parse(fs.readFileSync(p, "utf-8"), [], { allowTrailingComma: true, allowEmptyContent: true });
312
+ const getTSConfig = (p) => !fs.existsSync(p) || !fs.statSync(p).isFile() ? {} : jsoncParser.parse(fs.readFileSync(p, "utf-8"), [], { allowTrailingComma: true, allowEmptyContent: true });
307
313
  const getExtendTSConfig = (tsconfigPath) => {
308
- tsconfigPath = resolve(tsconfigPath);
309
- const tsconfigPathDirname = dirname(tsconfigPath);
314
+ tsconfigPath = path.resolve(tsconfigPath);
315
+ const tsconfigPathDirname = path.dirname(tsconfigPath);
310
316
  const { extends: exts, ...tsconfig } = getTSConfig(tsconfigPath);
311
- const resolvePaths = (paths) => paths?.map((p) => resolve(tsconfigPathDirname, p)) ?? [];
317
+ const resolvePaths = (paths) => paths?.map((p) => path.resolve(tsconfigPathDirname, p)) ?? [];
312
318
  const extendsPaths = resolvePaths(
313
319
  exts ? Array.isArray(exts) ? exts : [exts] : []
314
320
  );
@@ -324,11 +330,11 @@ const getExtendTSConfig = (tsconfigPath) => {
324
330
  }), tsconfig);
325
331
  };
326
332
  const getCompilerOptionsByFilePath = (tsconfigPath, filePath) => {
327
- tsconfigPath = resolve(tsconfigPath);
328
- filePath = resolve(filePath);
329
- const tsconfigPathDirname = dirname(tsconfigPath);
333
+ tsconfigPath = path.resolve(tsconfigPath);
334
+ filePath = path.resolve(filePath);
335
+ const tsconfigPathDirname = path.dirname(tsconfigPath);
330
336
  const tsconfig = getExtendTSConfig(tsconfigPath);
331
- const resolvePaths = (paths) => paths?.map((p) => resolve(tsconfigPathDirname, p)) ?? [];
337
+ const resolvePaths = (paths) => paths?.map((p) => path.resolve(tsconfigPathDirname, p)) ?? [];
332
338
  const [
333
339
  references,
334
340
  files,
@@ -340,13 +346,13 @@ const getCompilerOptionsByFilePath = (tsconfigPath, filePath) => {
340
346
  tsconfig.include,
341
347
  tsconfig.exclude
342
348
  ].map(resolvePaths);
343
- if (exclude.length > 0 && exclude.some((i) => isMatch(filePath, i)))
349
+ if (exclude.length > 0 && exclude.some((i) => micromatch.isMatch(filePath, i)))
344
350
  return;
345
351
  if (tsconfig.files?.length === 0 && tsconfig.include?.length === 0)
346
352
  return;
347
353
  let isInclude = false;
348
354
  isInclude || (isInclude = files.length > 0 && files.includes(filePath));
349
- isInclude || (isInclude = include.length > 0 && include.some((i) => isMatch(filePath, i)));
355
+ isInclude || (isInclude = include.length > 0 && include.some((i) => micromatch.isMatch(filePath, i)));
350
356
  if (isInclude) {
351
357
  return tsconfig.compilerOptions ?? {};
352
358
  } else {
@@ -362,7 +368,7 @@ const getCompilerOptionsByFilePath = (tsconfigPath, filePath) => {
362
368
  return tsconfig.compilerOptions;
363
369
  };
364
370
  const generateConfigs = ({
365
- path,
371
+ path: path$1,
366
372
  name,
367
373
  input,
368
374
  output,
@@ -385,10 +391,10 @@ const generateConfigs = ({
385
391
  });
386
392
  let compilerOptions = {};
387
393
  if (dtsTSConfigPath) {
388
- const jsonCompilerOptions = getCompilerOptionsByFilePath(dtsTSConfigPath, resolve(input));
394
+ const jsonCompilerOptions = getCompilerOptionsByFilePath(dtsTSConfigPath, path.resolve(input));
389
395
  const { options: options2, errors } = ts.convertCompilerOptionsFromJson(
390
396
  jsonCompilerOptions,
391
- dirname(dtsTSConfigPath)
397
+ path.dirname(dtsTSConfigPath)
392
398
  );
393
399
  if (errors.length > 0) {
394
400
  throw new Error(errors.map((e) => e.messageText).join("\n"));
@@ -398,7 +404,7 @@ const generateConfigs = ({
398
404
  const exportConditions = [...conditionals, ...compilerOptions.customConditions ?? []];
399
405
  const throughEventProps = {
400
406
  type: "progress",
401
- data: { name, path, exportConditions, input }
407
+ data: { name, path: path$1, exportConditions, input }
402
408
  };
403
409
  const outdir = options?.output?.dir;
404
410
  return [
@@ -415,16 +421,16 @@ const generateConfigs = ({
415
421
  })
416
422
  ],
417
423
  plugins: [
418
- nodeResolve({ exportConditions }),
424
+ pluginNodeResolve.nodeResolve({ exportConditions }),
419
425
  import('rollup-plugin-postcss').then(
420
426
  ({ default: postcss }) => postcss({
421
- extract: resolve(output.replace(/\.[cm]?js$/, ".css")),
427
+ extract: path.resolve(output.replace(/\.[cm]?js$/, ".css")),
422
428
  minimize: true
423
429
  })
424
430
  ).catch(() => void 0),
425
431
  esbuild(),
426
432
  progress({
427
- onEvent: (event, message) => sendMessage(
433
+ onEvent: (event, message) => execa.sendMessage(
428
434
  {
429
435
  ...throughEventProps,
430
436
  data: { ...throughEventProps.data, event, message, tags: ["js"] }
@@ -438,21 +444,21 @@ const generateConfigs = ({
438
444
  external,
439
445
  output: [
440
446
  {
441
- dir: resolve((typeof outdir === "object" ? outdir.dts : outdir) ?? "dist"),
447
+ dir: path.resolve((typeof outdir === "object" ? outdir.dts : outdir) ?? "dist"),
442
448
  sourcemap: typeof options?.output?.sourcemap === "object" ? options.output.sourcemap.dts : options?.output?.sourcemap,
443
449
  entryFileNames: () => output.replace(`${jsOutdir}/`, "").replace(/(\.[cm]?)js$/, ".d$1ts"),
444
450
  strict: typeof options?.output?.strict === "object" ? options.output.strict.dts : options?.output?.strict
445
451
  }
446
452
  ],
447
453
  plugins: [
448
- nodeResolve({ exportConditions }),
454
+ pluginNodeResolve.nodeResolve({ exportConditions }),
449
455
  skip({ patterns: [STYLE_REGEXP] }),
450
- dts({
456
+ rollupPluginDts.dts({
451
457
  respectExternal: true,
452
458
  compilerOptions
453
459
  }),
454
460
  progress({
455
- onEvent: (event, message) => sendMessage(
461
+ onEvent: (event, message) => execa.sendMessage(
456
462
  {
457
463
  ...throughEventProps,
458
464
  data: { ...throughEventProps.data, event, message, tags: ["dts"] }
@@ -464,11 +470,11 @@ const generateConfigs = ({
464
470
  ];
465
471
  };
466
472
  function template(packageJSON) {
467
- const { name, type, exports: entrypoints } = packageJSON;
473
+ const { name, type, exports: entrypoints$1 } = packageJSON;
468
474
  const pkgIsModule = type === "module";
469
475
  if (!name)
470
476
  throw new Error("package.json name is required");
471
- if (!entrypoints)
477
+ if (!entrypoints$1)
472
478
  throw new Error("package.json exports is required");
473
479
  const entries = JIEK_ENTRIES?.split(",").map((e) => e.trim()).map((e) => ({
474
480
  "index": "."
@@ -476,13 +482,13 @@ function template(packageJSON) {
476
482
  const packageName = pascalCase(name);
477
483
  const external = externalResolver(packageJSON);
478
484
  const [filteredResolvedEntrypoints, exports] = getExports({
479
- entrypoints,
485
+ entrypoints: entrypoints$1,
480
486
  pkgIsModule,
481
487
  entries,
482
488
  config
483
489
  });
484
490
  const leafMap = /* @__PURE__ */ new Map();
485
- getAllLeafs(filteredResolvedEntrypoints, ({ keys, value }) => {
491
+ entrypoints.getAllLeafs(filteredResolvedEntrypoints, ({ keys, value }) => {
486
492
  if (typeof value === "string") {
487
493
  const keysArr = leafMap.get(value) ?? [];
488
494
  leafMap.set(value, keysArr);
@@ -513,7 +519,7 @@ function template(packageJSON) {
513
519
  break;
514
520
  }
515
521
  case "object": {
516
- getAllLeafs(keyExports, ({ keys: nextKeys, value }) => {
522
+ entrypoints.getAllLeafs(keyExports, ({ keys: nextKeys, value }) => {
517
523
  const allConditionals = [.../* @__PURE__ */ new Set([...conditionals, ...nextKeys])];
518
524
  if (typeof value === "string") {
519
525
  configs.push(...generateConfigs({
@@ -529,7 +535,7 @@ function template(packageJSON) {
529
535
  }
530
536
  })
531
537
  );
532
- sendMessage(
538
+ execa.sendMessage(
533
539
  {
534
540
  type: "init",
535
541
  data: {
@@ -548,4 +554,4 @@ function template(packageJSON) {
548
554
  }));
549
555
  }
550
556
 
551
- export { template };
557
+ exports.template = template;