@sanity/pkg-utils 2.0.0-beta.1 → 2.0.0-beta.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.
Files changed (42) hide show
  1. package/dist/_chunks/{build-30149f1c.cjs → build-b879ad71.cjs} +10 -7
  2. package/dist/_chunks/build-b879ad71.cjs.map +1 -0
  3. package/dist/_chunks/{buildAction-7e75c8b7.cjs → buildAction-232e6073.cjs} +4 -4
  4. package/dist/_chunks/{buildAction-7e75c8b7.cjs.map → buildAction-232e6073.cjs.map} +1 -1
  5. package/dist/_chunks/{check-0f269395.cjs → check-617f9494.cjs} +23 -19
  6. package/dist/_chunks/check-617f9494.cjs.map +1 -0
  7. package/dist/_chunks/{checkAction-8b9632b6.cjs → checkAction-a2d388ad.cjs} +3 -3
  8. package/dist/_chunks/{checkAction-8b9632b6.cjs.map → checkAction-a2d388ad.cjs.map} +1 -1
  9. package/dist/_chunks/{index-db00ad50.cjs → index-b63b3deb.cjs} +73 -19
  10. package/dist/_chunks/index-b63b3deb.cjs.map +1 -0
  11. package/dist/_chunks/{resolveBuildContext-e938cee6.cjs → resolveBuildContext-bba19cd6.cjs} +43 -40
  12. package/dist/_chunks/resolveBuildContext-bba19cd6.cjs.map +1 -0
  13. package/dist/_chunks/{watch-73fd460f.cjs → watch-6fb4e453.cjs} +14 -11
  14. package/dist/_chunks/watch-6fb4e453.cjs.map +1 -0
  15. package/dist/_chunks/{watchAction-ffba41fd.cjs → watchAction-8135afe0.cjs} +4 -4
  16. package/dist/_chunks/{watchAction-ffba41fd.cjs.map → watchAction-8135afe0.cjs.map} +1 -1
  17. package/dist/cli.cjs +4 -4
  18. package/dist/index.cjs +17 -16
  19. package/dist/index.cjs.map +1 -1
  20. package/dist/index.d.ts +17 -8
  21. package/dist/index.js +363 -295
  22. package/dist/index.js.map +1 -1
  23. package/package.json +4 -4
  24. package/src/node/build.ts +13 -2
  25. package/src/node/check.ts +4 -3
  26. package/src/node/core/contexts/buildContext.ts +2 -7
  27. package/src/node/core/pkg/loadPkgWithReporting.ts +11 -20
  28. package/src/node/index.ts +3 -2
  29. package/src/node/logger.ts +33 -0
  30. package/src/node/printPackageTree.ts +11 -11
  31. package/src/node/resolveBuildContext.ts +11 -25
  32. package/src/node/spinner.ts +3 -3
  33. package/src/node/tasks/dts/dtsTask.ts +7 -4
  34. package/src/node/tasks/rollup/rollupTask.ts +66 -21
  35. package/src/node/tasks/rollup/rollupWatchTask.ts +0 -1
  36. package/src/node/watch.ts +6 -3
  37. package/src/node/watchConfigFiles.ts +9 -8
  38. package/dist/_chunks/build-30149f1c.cjs.map +0 -1
  39. package/dist/_chunks/check-0f269395.cjs.map +0 -1
  40. package/dist/_chunks/index-db00ad50.cjs.map +0 -1
  41. package/dist/_chunks/resolveBuildContext-e938cee6.cjs.map +0 -1
  42. package/dist/_chunks/watch-73fd460f.cjs.map +0 -1
package/dist/index.js CHANGED
@@ -14,6 +14,7 @@ import findConfig from 'find-config';
14
14
  import { z, ZodError } from 'zod';
15
15
  import chalk from 'chalk';
16
16
  import ts from 'typescript';
17
+ import browserslist from 'browserslist';
17
18
  import fs$1, { statSync } from 'fs';
18
19
  import { Observable, switchMap } from 'rxjs';
19
20
  import { promisify } from 'util';
@@ -34,7 +35,6 @@ import { nodeResolve } from '@rollup/plugin-node-resolve';
34
35
  import replace from '@rollup/plugin-replace';
35
36
  import terser from '@rollup/plugin-terser';
36
37
  import esbuild from 'rollup-plugin-esbuild';
37
- import browserslist from 'browserslist';
38
38
  import esbuild$1 from 'esbuild';
39
39
  import treeify from 'treeify';
40
40
  import prettyBytes from 'pretty-bytes';
@@ -168,21 +168,23 @@ async function loadPkg(options) {
168
168
  return validatePkg(JSON.parse(buf.toString()));
169
169
  }
170
170
  async function loadPkgWithReporting(options) {
171
+ const {
172
+ cwd,
173
+ logger
174
+ } = options;
171
175
  try {
172
- const pkg = await loadPkg(options);
173
- if (pkg.type === "commonjs") {
174
- console.warn(["".concat(chalk.yellow("warn"), " "), 'Packages with `"type": "commonjs"` may not be compatible with popular bundlers like ', "webpack and rollup, and projects such as Astro and SvelteKit. Please consider using ", '`"type": "module"` instead.'].join(""));
175
- }
176
- return pkg;
176
+ return await loadPkg({
177
+ cwd
178
+ });
177
179
  } catch (err) {
178
180
  if (err instanceof ZodError) {
179
181
  for (const issue of err.issues) {
180
182
  if (issue.code === "invalid_type") {
181
- console.error(["".concat(chalk.red("fail"), " `").concat(formatPath(issue.path), "` "), "in `./package.json` must be of type ".concat(chalk.magenta(issue.expected), " "), "(received ".concat(chalk.magenta(issue.received), ")")].join(""));
183
+ logger.error(["`".concat(formatPath(issue.path), "` "), "in `./package.json` must be of type ".concat(chalk.magenta(issue.expected), " "), "(received ".concat(chalk.magenta(issue.received), ")")].join(""));
182
184
  }
183
185
  }
184
186
  } else {
185
- console.error(err);
187
+ logger.error(err);
186
188
  }
187
189
  process.exit(1);
188
190
  }
@@ -295,6 +297,222 @@ async function loadTSConfig(options) {
295
297
  const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
296
298
  return ts.parseJsonConfigFileContent(configFile.config, ts.sys, cwd);
297
299
  }
300
+ function createLogger() {
301
+ return {
302
+ log: function () {
303
+ console.log(...arguments);
304
+ },
305
+ info: function () {
306
+ for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
307
+ args[_key] = arguments[_key];
308
+ }
309
+ console.log(chalk.blue("[info]"), ...args);
310
+ },
311
+ warn: function () {
312
+ for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
313
+ args[_key2] = arguments[_key2];
314
+ }
315
+ console.log(chalk.yellow("[warning]"), ...args);
316
+ },
317
+ error: function () {
318
+ for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
319
+ args[_key3] = arguments[_key3];
320
+ }
321
+ console.log(chalk.red("[error]"), ...args);
322
+ },
323
+ success: function () {
324
+ for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
325
+ args[_key4] = arguments[_key4];
326
+ }
327
+ console.log(chalk.green("[success]"), ...args);
328
+ }
329
+ };
330
+ }
331
+ function pathContains(containerPath, itemPath) {
332
+ return !path.relative(containerPath, itemPath).startsWith("..");
333
+ }
334
+ function findCommonDirPath(filePaths) {
335
+ let ret = void 0;
336
+ for (const filePath of filePaths) {
337
+ let dirPath = path.dirname(filePath);
338
+ if (!ret) {
339
+ ret = dirPath;
340
+ continue;
341
+ }
342
+ while (dirPath !== ret) {
343
+ dirPath = path.dirname(dirPath);
344
+ if (dirPath === ret) {
345
+ break;
346
+ }
347
+ if (pathContains(dirPath, ret)) {
348
+ ret = dirPath;
349
+ break;
350
+ }
351
+ if (dirPath === ".") return void 0;
352
+ }
353
+ }
354
+ return ret;
355
+ }
356
+ function sortAsNumber(a, b) {
357
+ const aVersion = Number(a);
358
+ const bVersion = Number(b);
359
+ return aVersion - bVersion;
360
+ }
361
+ function resolveBrowserslistVersions(query) {
362
+ const result = browserslist(query);
363
+ const entries = result.map(key => {
364
+ const [name, version] = key.split(" ");
365
+ return {
366
+ name,
367
+ version: version.split("-")[0].split(".")[0]
368
+ };
369
+ });
370
+ const andChr = entries.filter(b => b.name === "and_chr");
371
+ const andFf = entries.filter(b => b.name === "and_ff");
372
+ const chrome = entries.filter(b => b.name === "chrome");
373
+ const edge = entries.filter(b => b.name === "edge");
374
+ const ff = entries.filter(b => b.name === "firefox");
375
+ const iosSaf = entries.filter(b => b.name === "ios_saf");
376
+ const saf = entries.filter(b => b.name === "safari");
377
+ const opera = entries.filter(b => b.name === "opera");
378
+ const node = entries.filter(b => b.name === "node");
379
+ const versions = {
380
+ chrome: andChr.concat(chrome).map(b => b.version),
381
+ ff: andFf.concat(ff).map(b => b.version),
382
+ edge: edge.map(b => b.version),
383
+ iosSaf: iosSaf.map(b => b.version),
384
+ saf: saf.map(b => b.version),
385
+ opera: opera.map(b => b.version),
386
+ node: node.map(b => b.version)
387
+ };
388
+ versions.chrome.sort(sortAsNumber);
389
+ versions.ff.sort(sortAsNumber);
390
+ versions.edge.sort(sortAsNumber);
391
+ versions.iosSaf.sort(sortAsNumber);
392
+ versions.saf.sort(sortAsNumber);
393
+ versions.opera.sort(sortAsNumber);
394
+ versions.node.sort(sortAsNumber);
395
+ return versions;
396
+ }
397
+ function resolveBrowserTarget(versions) {
398
+ const target = [];
399
+ if (versions.chrome.length) {
400
+ target.push("chrome".concat(versions.chrome[0]));
401
+ }
402
+ if (versions.ff.length) {
403
+ target.push("firefox".concat(versions.ff[0]));
404
+ }
405
+ if (versions.edge.length) {
406
+ target.push("edge".concat(versions.edge[0]));
407
+ }
408
+ if (versions.iosSaf.length) {
409
+ target.push("ios".concat(versions.iosSaf[0]));
410
+ }
411
+ if (versions.saf.length) {
412
+ target.push("safari".concat(versions.saf[0]));
413
+ }
414
+ if (versions.opera.length) {
415
+ target.push("opera".concat(versions.opera[0]));
416
+ }
417
+ if (target.length === 0) {
418
+ return void 0;
419
+ }
420
+ return target;
421
+ }
422
+ function resolveNodeTarget(versions) {
423
+ const target = [];
424
+ if (versions.node.length) {
425
+ target.push("node".concat(versions.node[0]));
426
+ }
427
+ if (target.length === 0) {
428
+ return void 0;
429
+ }
430
+ return target;
431
+ }
432
+ async function resolveBuildContext(options) {
433
+ var _ref;
434
+ const {
435
+ config,
436
+ cwd,
437
+ emitDeclarationOnly = false,
438
+ logger,
439
+ pkg,
440
+ strict,
441
+ tsconfig: tsconfigPath
442
+ } = options;
443
+ const tsconfig = await loadTSConfig({
444
+ cwd,
445
+ tsconfigPath
446
+ });
447
+ const targetVersions = resolveBrowserslistVersions(pkg.browserslist || DEFAULT_BROWSERSLIST_QUERY);
448
+ const nodeTarget = resolveNodeTarget(targetVersions);
449
+ const webTarget = resolveBrowserTarget(targetVersions);
450
+ if (!nodeTarget) {
451
+ throw new Error("no matching `node` target");
452
+ }
453
+ if (!webTarget) {
454
+ throw new Error("no matching `web` target");
455
+ }
456
+ const target = {
457
+ "*": webTarget.concat(nodeTarget),
458
+ browser: webTarget,
459
+ node: nodeTarget
460
+ };
461
+ const parsedExports = parseExports({
462
+ pkg,
463
+ strict
464
+ }).reduce((acc, x) => {
465
+ const {
466
+ _path: exportPath
467
+ } = x,
468
+ exportEntry = _objectWithoutProperties(x, _excluded);
469
+ return _objectSpread(_objectSpread({}, acc), {}, {
470
+ [exportPath]: exportEntry
471
+ });
472
+ }, {});
473
+ const exports = resolveConfigProperty(config == null ? void 0 : config.exports, parsedExports);
474
+ const parsedExternal = [...(pkg.dependencies ? Object.keys(pkg.dependencies) : []), ...(pkg.peerDependencies ? Object.keys(pkg.peerDependencies) : [])];
475
+ const external = config && Array.isArray(config.external) ? [...parsedExternal, ...config.external] : resolveConfigProperty(config == null ? void 0 : config.external, parsedExternal);
476
+ const outputPaths = Object.values(exports).flatMap(exportEntry => {
477
+ var _a, _b, _c, _d;
478
+ return [exportEntry.import, exportEntry.require, (_a = exportEntry.browser) == null ? void 0 : _a.import, (_b = exportEntry.browser) == null ? void 0 : _b.require, (_c = exportEntry.node) == null ? void 0 : _c.import, (_d = exportEntry.node) == null ? void 0 : _d.require].filter(Boolean);
479
+ }).map(p => path.resolve(cwd, p));
480
+ const commonDistPath = findCommonDirPath(outputPaths);
481
+ if (commonDistPath === cwd) {
482
+ throw new Error("all output files must share a common parent directory which is not the root package directory");
483
+ }
484
+ if (commonDistPath && !pathContains(cwd, commonDistPath)) {
485
+ throw new Error("all output files must be located within the package");
486
+ }
487
+ const configDistPath = (config == null ? void 0 : config.dist) ? path.resolve(cwd, config.dist) : void 0;
488
+ if (configDistPath && commonDistPath && configDistPath !== commonDistPath && !pathContains(configDistPath, commonDistPath)) {
489
+ logger.log("did you mean to configure `dist: './".concat(path.relative(cwd, commonDistPath), "'`?"));
490
+ throw new Error("all output files must be located with the configured `dist` path");
491
+ }
492
+ const distPath = configDistPath || commonDistPath;
493
+ if (!distPath) {
494
+ throw new Error("could not detect `dist` path");
495
+ }
496
+ const ctx = {
497
+ config,
498
+ cwd,
499
+ distPath,
500
+ emitDeclarationOnly,
501
+ exports,
502
+ external,
503
+ files: [],
504
+ logger,
505
+ pkg,
506
+ runtime: (_ref = config == null ? void 0 : config.runtime) !== null && _ref !== void 0 ? _ref : "*",
507
+ strict,
508
+ target,
509
+ ts: {
510
+ config: tsconfig,
511
+ configPath: tsconfigPath
512
+ }
513
+ };
514
+ return ctx;
515
+ }
298
516
  function resolveBuildTasks(ctx) {
299
517
  var _a, _b, _c, _d, _e;
300
518
  const {
@@ -305,8 +523,8 @@ function resolveBuildTasks(ctx) {
305
523
  } = ctx;
306
524
  const bundles = (config == null ? void 0 : config.bundles) || [];
307
525
  const tasks = [];
308
- const exports = Object.entries(ctx.exports || {}).map(_ref => {
309
- let [_path, exp] = _ref;
526
+ const exports = Object.entries(ctx.exports || {}).map(_ref2 => {
527
+ let [_path, exp] = _ref2;
310
528
  return _objectSpread({
311
529
  _path
312
530
  }, exp);
@@ -412,6 +630,18 @@ function resolveBuildTasks(ctx) {
412
630
  }
413
631
  return tasks;
414
632
  }
633
+ function createSpinner(msg) {
634
+ const startTime = Date.now();
635
+ console.log(msg);
636
+ return {
637
+ complete: () => {
638
+ console.log("".concat(chalk.green("[success]"), " ").concat(chalk.gray("".concat(Date.now() - startTime, "ms"))));
639
+ },
640
+ error: () => {
641
+ console.log("".concat(chalk.red("[error]"), " ").concat(chalk.gray("".concat(Date.now() - startTime, "ms"))));
642
+ }
643
+ };
644
+ }
415
645
  function printExtractMessages(ctx, messages) {
416
646
  const {
417
647
  cwd,
@@ -547,11 +777,11 @@ async function createTSDocConfig(opts) {
547
777
  supportForTags
548
778
  }));
549
779
  }
550
- async function extractModuleBlocksFromTypes(_ref2) {
780
+ async function extractModuleBlocksFromTypes(_ref3) {
551
781
  let {
552
782
  tsOutDir,
553
783
  extractResult
554
- } = _ref2;
784
+ } = _ref3;
555
785
  const program = extractResult.compilerState.program;
556
786
  const moduleBlocks = [];
557
787
  const allProgramFiles = [...program.getSourceFiles()];
@@ -766,7 +996,7 @@ async function doExtract(ctx, task) {
766
996
  };
767
997
  }
768
998
  const dtsTask = {
769
- name: (_ctx, task) => ["build type definitions", ...task.entries.map(entry => " ".concat(chalk.blue(entry.importId), ": ").concat(entry.sourcePath, " -> ").concat(entry.targetPath))].join("\n"),
999
+ name: (_ctx, task) => ["Build type definitions...", " entries:", ...task.entries.map(entry => [" - ".concat(chalk.cyan(entry.importId), ": "), "".concat(chalk.yellow(entry.sourcePath), " ").concat(chalk.gray("\u2192"), " ").concat(chalk.yellow(entry.targetPath))].join(""))].join("\n"),
770
1000
  exec: (ctx, task) => {
771
1001
  return new Observable(observer => {
772
1002
  doExtract(ctx, task).then(result => {
@@ -821,7 +1051,7 @@ const dtsWatchTask = {
821
1051
  }
822
1052
  };
823
1053
  function resolveRollupConfig(ctx, buildTask) {
824
- var _ref3, _ref6, _ref7, _ref8, _ref9, _ref10;
1054
+ var _ref4, _ref7, _ref8, _ref9, _ref10, _ref11;
825
1055
  var _a, _b, _c;
826
1056
  const {
827
1057
  format,
@@ -839,10 +1069,10 @@ function resolveRollupConfig(ctx, buildTask) {
839
1069
  ts
840
1070
  } = ctx;
841
1071
  const outputExt = MODULE_EXT[pkg.type][format];
842
- const minify = (_ref3 = config == null ? void 0 : config.minify) !== null && _ref3 !== void 0 ? _ref3 : true;
1072
+ const minify = (_ref4 = config == null ? void 0 : config.minify) !== null && _ref4 !== void 0 ? _ref4 : true;
843
1073
  const outDir = path.relative(cwd, distPath);
844
- const pathAliases = Object.fromEntries(Object.entries(((_a = ts.config) == null ? void 0 : _a.options.paths) || {}).map(_ref4 => {
845
- let [key, val] = _ref4;
1074
+ const pathAliases = Object.fromEntries(Object.entries(((_a = ts.config) == null ? void 0 : _a.options.paths) || {}).map(_ref5 => {
1075
+ let [key, val] = _ref5;
846
1076
  var _a2;
847
1077
  return [key, path.resolve(cwd, ((_a2 = ts.config) == null ? void 0 : _a2.options.baseUrl) || ".", val[0])];
848
1078
  }));
@@ -853,8 +1083,8 @@ function resolveRollupConfig(ctx, buildTask) {
853
1083
  }, {});
854
1084
  const exportIds = _exports && Object.keys(_exports).map(exportPath => path.join(pkg.name, exportPath));
855
1085
  const sourcePaths = _exports && Object.values(_exports).map(e => path.resolve(cwd, e.source));
856
- const replacements = Object.fromEntries(Object.entries((config == null ? void 0 : config.define) || {}).map(_ref5 => {
857
- let [key, val] = _ref5;
1086
+ const replacements = Object.fromEntries(Object.entries((config == null ? void 0 : config.define) || {}).map(_ref6 => {
1087
+ let [key, val] = _ref6;
858
1088
  return [key, JSON.stringify(val)];
859
1089
  }));
860
1090
  const defaultPlugins = [replace({
@@ -883,10 +1113,10 @@ function resolveRollupConfig(ctx, buildTask) {
883
1113
  esmExternals: false,
884
1114
  extensions: [".js"]
885
1115
  }), json(), esbuild({
886
- jsx: (_ref6 = config == null ? void 0 : config.jsx) !== null && _ref6 !== void 0 ? _ref6 : "automatic",
887
- jsxFactory: (_ref7 = config == null ? void 0 : config.jsxFactory) !== null && _ref7 !== void 0 ? _ref7 : "createElement",
888
- jsxFragment: (_ref8 = config == null ? void 0 : config.jsxFragment) !== null && _ref8 !== void 0 ? _ref8 : "Fragment",
889
- jsxImportSource: (_ref9 = config == null ? void 0 : config.jsxImportSource) !== null && _ref9 !== void 0 ? _ref9 : "react",
1116
+ jsx: (_ref7 = config == null ? void 0 : config.jsx) !== null && _ref7 !== void 0 ? _ref7 : "automatic",
1117
+ jsxFactory: (_ref8 = config == null ? void 0 : config.jsxFactory) !== null && _ref8 !== void 0 ? _ref8 : "createElement",
1118
+ jsxFragment: (_ref9 = config == null ? void 0 : config.jsxFragment) !== null && _ref9 !== void 0 ? _ref9 : "Fragment",
1119
+ jsxImportSource: (_ref10 = config == null ? void 0 : config.jsxImportSource) !== null && _ref10 !== void 0 ? _ref10 : "react",
890
1120
  target,
891
1121
  tsconfig: ctx.ts.configPath || "tsconfig.json"
892
1122
  }), getBabelOutputPlugin({
@@ -949,12 +1179,12 @@ function resolveRollupConfig(ctx, buildTask) {
949
1179
  format,
950
1180
  interop: "compat",
951
1181
  minifyInternalExports: minify,
952
- sourcemap: (_ref10 = config == null ? void 0 : config.sourcemap) !== null && _ref10 !== void 0 ? _ref10 : true
1182
+ sourcemap: (_ref11 = config == null ? void 0 : config.sourcemap) !== null && _ref11 !== void 0 ? _ref11 : true
953
1183
  }
954
1184
  };
955
1185
  }
956
1186
  const rollupTask = {
957
- name: (ctx, task) => "build javascript files (target ".concat(task.target.join(" + "), ", format ").concat(task.format, ")\n ").concat(task.entries.map(e => "".concat(chalk.blue(path.join(ctx.pkg.name, e.path)), ": ").concat(e.source, " -> ").concat(e.output)).join("\n ")),
1187
+ name: (ctx, task) => ["Build javascript files...", " format: ".concat(chalk.yellow(task.format)), " target:", ...task.target.map(t => " - ".concat(chalk.yellow(t))), " entries:", ...task.entries.map(e => [" - ", "".concat(chalk.cyan(path.join(ctx.pkg.name, e.path)), ": "), "".concat(chalk.yellow(e.source), " ").concat(chalk.gray("\u2192"), " ").concat(chalk.yellow(e.output))].join(""))].join("\n"),
958
1188
  exec: (ctx, task) => {
959
1189
  return new Observable(observer => {
960
1190
  execPromise(ctx, task).then(result => {
@@ -970,37 +1200,91 @@ const rollupTask = {
970
1200
  }
971
1201
  }
972
1202
  };
1203
+ function createSpyConsole() {
1204
+ const original = {
1205
+ log: console.log,
1206
+ warn: console.warn,
1207
+ error: console.error
1208
+ };
1209
+ const messages = [];
1210
+ console.log = function () {
1211
+ for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
1212
+ args[_key5] = arguments[_key5];
1213
+ }
1214
+ return messages.push({
1215
+ type: "log",
1216
+ args
1217
+ });
1218
+ };
1219
+ console.warn = function () {
1220
+ for (var _len6 = arguments.length, args = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
1221
+ args[_key6] = arguments[_key6];
1222
+ }
1223
+ return messages.push({
1224
+ type: "warn",
1225
+ args
1226
+ });
1227
+ };
1228
+ console.error = function () {
1229
+ for (var _len7 = arguments.length, args = new Array(_len7), _key7 = 0; _key7 < _len7; _key7++) {
1230
+ args[_key7] = arguments[_key7];
1231
+ }
1232
+ return messages.push({
1233
+ type: "error",
1234
+ args
1235
+ });
1236
+ };
1237
+ return {
1238
+ messages,
1239
+ restore: () => {
1240
+ console.log = original.log;
1241
+ console.warn = original.warn;
1242
+ console.error = original.error;
1243
+ }
1244
+ };
1245
+ }
973
1246
  async function execPromise(ctx, task) {
974
1247
  const {
1248
+ distPath,
975
1249
  files,
976
- distPath
1250
+ logger
977
1251
  } = ctx;
978
1252
  const outDir = path.relative(ctx.cwd, distPath);
979
- const _console = {
980
- log: console.log,
981
- warn: console.warn,
982
- error: console.error
983
- };
1253
+ const spyConsole = createSpyConsole();
984
1254
  const {
985
1255
  inputOptions,
986
1256
  outputOptions
987
1257
  } = resolveRollupConfig(ctx, task);
988
- console.log = () => void 0;
989
- console.warn = () => void 0;
990
- console.error = () => void 0;
991
1258
  const bundle = await rollup(_objectSpread(_objectSpread({}, inputOptions), {}, {
992
1259
  onwarn(warning) {
993
- if (!warning.code || !["CIRCULAR_DEPENDENCY"].includes(warning.code)) {
994
- _console.warn.bind(console)(chalk.yellow("warn "), warning.message);
995
- }
1260
+ spyConsole.messages.push({
1261
+ type: "warn",
1262
+ args: [warning.message]
1263
+ });
996
1264
  }
997
1265
  }));
998
1266
  const {
999
1267
  output
1000
1268
  } = await bundle.generate(outputOptions);
1001
- console.log = _console.log;
1002
- console.warn = _console.warn;
1003
- console.error = _console.error;
1269
+ spyConsole.restore();
1270
+ for (const msg of spyConsole.messages) {
1271
+ const text = String(msg.args[0]);
1272
+ if (text.startsWith("Dynamic import can only")) {
1273
+ continue;
1274
+ }
1275
+ if (text.startsWith("Sourcemap is likely to be incorrect")) {
1276
+ continue;
1277
+ }
1278
+ if (msg.type === "log") {
1279
+ logger.info(...msg.args);
1280
+ }
1281
+ if (msg.type === "warn") {
1282
+ logger.warn(...msg.args);
1283
+ }
1284
+ if (msg.type === "error") {
1285
+ logger.error(...msg.args);
1286
+ }
1287
+ }
1004
1288
  for (const chunkOrAsset of output) {
1005
1289
  if (chunkOrAsset.type === "asset") {
1006
1290
  files.push({
@@ -1077,234 +1361,6 @@ const watchTaskHandlers = {
1077
1361
  "watch:dts": dtsWatchTask,
1078
1362
  "watch:js": rollupWatchTask
1079
1363
  };
1080
- function pathContains(containerPath, itemPath) {
1081
- return !path.relative(containerPath, itemPath).startsWith("..");
1082
- }
1083
- function findCommonDirPath(filePaths) {
1084
- let ret = void 0;
1085
- for (const filePath of filePaths) {
1086
- let dirPath = path.dirname(filePath);
1087
- if (!ret) {
1088
- ret = dirPath;
1089
- continue;
1090
- }
1091
- while (dirPath !== ret) {
1092
- dirPath = path.dirname(dirPath);
1093
- if (dirPath === ret) {
1094
- break;
1095
- }
1096
- if (pathContains(dirPath, ret)) {
1097
- ret = dirPath;
1098
- break;
1099
- }
1100
- if (dirPath === ".") return void 0;
1101
- }
1102
- }
1103
- return ret;
1104
- }
1105
- function sortAsNumber(a, b) {
1106
- const aVersion = Number(a);
1107
- const bVersion = Number(b);
1108
- return aVersion - bVersion;
1109
- }
1110
- function resolveBrowserslistVersions(query) {
1111
- const result = browserslist(query);
1112
- const entries = result.map(key => {
1113
- const [name, version] = key.split(" ");
1114
- return {
1115
- name,
1116
- version: version.split("-")[0].split(".")[0]
1117
- };
1118
- });
1119
- const andChr = entries.filter(b => b.name === "and_chr");
1120
- const andFf = entries.filter(b => b.name === "and_ff");
1121
- const chrome = entries.filter(b => b.name === "chrome");
1122
- const edge = entries.filter(b => b.name === "edge");
1123
- const ff = entries.filter(b => b.name === "firefox");
1124
- const iosSaf = entries.filter(b => b.name === "ios_saf");
1125
- const saf = entries.filter(b => b.name === "safari");
1126
- const opera = entries.filter(b => b.name === "opera");
1127
- const node = entries.filter(b => b.name === "node");
1128
- const versions = {
1129
- chrome: andChr.concat(chrome).map(b => b.version),
1130
- ff: andFf.concat(ff).map(b => b.version),
1131
- edge: edge.map(b => b.version),
1132
- iosSaf: iosSaf.map(b => b.version),
1133
- saf: saf.map(b => b.version),
1134
- opera: opera.map(b => b.version),
1135
- node: node.map(b => b.version)
1136
- };
1137
- versions.chrome.sort(sortAsNumber);
1138
- versions.ff.sort(sortAsNumber);
1139
- versions.edge.sort(sortAsNumber);
1140
- versions.iosSaf.sort(sortAsNumber);
1141
- versions.saf.sort(sortAsNumber);
1142
- versions.opera.sort(sortAsNumber);
1143
- versions.node.sort(sortAsNumber);
1144
- return versions;
1145
- }
1146
- function resolveBrowserTarget(versions) {
1147
- const target = [];
1148
- if (versions.chrome.length) {
1149
- target.push("chrome".concat(versions.chrome[0]));
1150
- }
1151
- if (versions.ff.length) {
1152
- target.push("firefox".concat(versions.ff[0]));
1153
- }
1154
- if (versions.edge.length) {
1155
- target.push("edge".concat(versions.edge[0]));
1156
- }
1157
- if (versions.iosSaf.length) {
1158
- target.push("ios".concat(versions.iosSaf[0]));
1159
- }
1160
- if (versions.saf.length) {
1161
- target.push("safari".concat(versions.saf[0]));
1162
- }
1163
- if (versions.opera.length) {
1164
- target.push("opera".concat(versions.opera[0]));
1165
- }
1166
- if (target.length === 0) {
1167
- return void 0;
1168
- }
1169
- return target;
1170
- }
1171
- function resolveNodeTarget(versions) {
1172
- const target = [];
1173
- if (versions.node.length) {
1174
- target.push("node".concat(versions.node[0]));
1175
- }
1176
- if (target.length === 0) {
1177
- return void 0;
1178
- }
1179
- return target;
1180
- }
1181
- function createLogger() {
1182
- return {
1183
- log: function () {
1184
- console.log(...arguments);
1185
- },
1186
- info: function () {
1187
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
1188
- args[_key] = arguments[_key];
1189
- }
1190
- console.log(chalk.blue(" info"), ...args);
1191
- },
1192
- warn: function () {
1193
- for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
1194
- args[_key2] = arguments[_key2];
1195
- }
1196
- console.log(chalk.yellow(" warn"), ...args);
1197
- },
1198
- error: function () {
1199
- for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
1200
- args[_key3] = arguments[_key3];
1201
- }
1202
- console.log(chalk.red(" fail"), ...args);
1203
- },
1204
- success: function () {
1205
- for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
1206
- args[_key4] = arguments[_key4];
1207
- }
1208
- console.log(chalk.green(" ok "), ...args);
1209
- }
1210
- };
1211
- }
1212
- async function resolveBuildContext(options) {
1213
- var _ref11;
1214
- const {
1215
- config,
1216
- cwd,
1217
- emitDeclarationOnly = false,
1218
- pkg,
1219
- strict,
1220
- tsconfig: tsconfigPath
1221
- } = options;
1222
- const logger = createLogger();
1223
- const tsconfig = await loadTSConfig({
1224
- cwd,
1225
- tsconfigPath
1226
- });
1227
- const targetVersions = resolveBrowserslistVersions(pkg.browserslist || DEFAULT_BROWSERSLIST_QUERY);
1228
- const nodeTarget = resolveNodeTarget(targetVersions);
1229
- const webTarget = resolveBrowserTarget(targetVersions);
1230
- if (!nodeTarget) {
1231
- throw new Error("no matching `node` target");
1232
- }
1233
- if (!webTarget) {
1234
- throw new Error("no matching `web` target");
1235
- }
1236
- const target = {
1237
- "*": webTarget.concat(nodeTarget),
1238
- browser: webTarget,
1239
- node: nodeTarget
1240
- };
1241
- const parsedExports = parseExports({
1242
- pkg,
1243
- strict
1244
- }).reduce((acc, x) => {
1245
- const {
1246
- _path: exportPath
1247
- } = x,
1248
- exportEntry = _objectWithoutProperties(x, _excluded);
1249
- return _objectSpread(_objectSpread({}, acc), {}, {
1250
- [exportPath]: exportEntry
1251
- });
1252
- }, {});
1253
- const exports = resolveConfigProperty(config == null ? void 0 : config.exports, parsedExports);
1254
- const parsedExternal = [...(pkg.dependencies ? Object.keys(pkg.dependencies) : []), ...(pkg.peerDependencies ? Object.keys(pkg.peerDependencies) : [])];
1255
- const external = config && Array.isArray(config.external) ? [...parsedExternal, ...config.external] : resolveConfigProperty(config == null ? void 0 : config.external, parsedExternal);
1256
- const outputPaths = Object.values(exports).flatMap(exportEntry => {
1257
- var _a, _b, _c, _d;
1258
- return [exportEntry.import, exportEntry.require, (_a = exportEntry.browser) == null ? void 0 : _a.import, (_b = exportEntry.browser) == null ? void 0 : _b.require, (_c = exportEntry.node) == null ? void 0 : _c.import, (_d = exportEntry.node) == null ? void 0 : _d.require].filter(Boolean);
1259
- }).map(p => path.resolve(cwd, p));
1260
- const commonDistPath = findCommonDirPath(outputPaths);
1261
- if (commonDistPath === cwd) {
1262
- throw new Error("all output files must share a common parent directory which is not the root package directory");
1263
- }
1264
- if (commonDistPath && !pathContains(cwd, commonDistPath)) {
1265
- throw new Error("all output files must be located within the package");
1266
- }
1267
- const configDistPath = (config == null ? void 0 : config.dist) ? path.resolve(cwd, config.dist) : void 0;
1268
- if (configDistPath && commonDistPath && configDistPath !== commonDistPath && !pathContains(configDistPath, commonDistPath)) {
1269
- logger.log("did you mean to configure `dist: './".concat(path.relative(cwd, commonDistPath), "'`?"));
1270
- throw new Error("all output files must be located with the configured `dist` path");
1271
- }
1272
- const distPath = configDistPath || commonDistPath;
1273
- if (!distPath) {
1274
- throw new Error("could not detect `dist` path");
1275
- }
1276
- const ctx = {
1277
- config,
1278
- cwd,
1279
- distPath,
1280
- emitDeclarationOnly,
1281
- exports,
1282
- external,
1283
- files: [],
1284
- logger,
1285
- pkg,
1286
- runtime: (_ref11 = config == null ? void 0 : config.runtime) !== null && _ref11 !== void 0 ? _ref11 : "*",
1287
- strict,
1288
- target,
1289
- ts: {
1290
- config: tsconfig,
1291
- configPath: tsconfigPath
1292
- }
1293
- };
1294
- return ctx;
1295
- }
1296
- function createSpinner(msg) {
1297
- const startTime = Date.now();
1298
- console.log(" ".concat(msg));
1299
- return {
1300
- complete: () => {
1301
- console.log("".concat(chalk.green("ok "), " ").concat(chalk.gray("".concat(Date.now() - startTime, "ms"))));
1302
- },
1303
- error: () => {
1304
- console.log("".concat(chalk.red("failed"), " ").concat(chalk.gray("".concat(Date.now() - startTime, "ms"))));
1305
- }
1306
- };
1307
- }
1308
1364
  async function build(options) {
1309
1365
  const {
1310
1366
  cwd,
@@ -1312,8 +1368,10 @@ async function build(options) {
1312
1368
  strict = false,
1313
1369
  tsconfig: tsconfigOption
1314
1370
  } = options;
1371
+ const logger = createLogger();
1315
1372
  const pkg = await loadPkgWithReporting({
1316
- cwd
1373
+ cwd,
1374
+ logger
1317
1375
  });
1318
1376
  const config = await loadConfig({
1319
1377
  cwd
@@ -1323,6 +1381,7 @@ async function build(options) {
1323
1381
  config,
1324
1382
  cwd,
1325
1383
  emitDeclarationOnly,
1384
+ logger,
1326
1385
  pkg,
1327
1386
  strict,
1328
1387
  tsconfig
@@ -1379,30 +1438,35 @@ function printPackageTree(ctx) {
1379
1438
  } = ctx;
1380
1439
  if (!exports) return;
1381
1440
  logger.log("".concat(chalk.blue(pkg.name), "@").concat(chalk.green(pkg.version)));
1382
- const tree = {};
1441
+ const tree = {
1442
+ type: chalk.yellow(pkg.type)
1443
+ };
1383
1444
  if (pkg.bin) {
1384
- tree.bin = pkg.bin;
1445
+ tree.bin = Object.fromEntries(Object.entries(pkg.bin).map(_ref12 => {
1446
+ let [name, file] = _ref12;
1447
+ return [chalk.cyan(name), fileInfo(file)];
1448
+ }));
1385
1449
  }
1386
1450
  function fileInfo(file) {
1387
1451
  const info = getFileInfo(cwd, file);
1388
1452
  if (!info.size) {
1389
1453
  return "".concat(chalk.gray(file), " ").concat(chalk.red("does not exist"));
1390
1454
  }
1391
- return "".concat(file, " ").concat(chalk.gray(info.size));
1455
+ return "".concat(chalk.yellow(file), " ").concat(chalk.gray(info.size));
1392
1456
  }
1393
- tree.exports = Object.fromEntries(Object.entries(exports).filter(_ref12 => {
1394
- let [, entry] = _ref12;
1457
+ tree.exports = Object.fromEntries(Object.entries(exports).filter(_ref13 => {
1458
+ let [, entry] = _ref13;
1395
1459
  return entry._exported;
1396
- }).map(_ref13 => {
1397
- let [exportPath, entry] = _ref13;
1460
+ }).map(_ref14 => {
1461
+ let [exportPath, entry] = _ref14;
1398
1462
  const exp = {
1399
- source: entry.source,
1400
1463
  types: void 0,
1464
+ source: fileInfo(entry.source),
1401
1465
  browser: void 0,
1402
1466
  node: void 0,
1403
1467
  import: void 0,
1404
1468
  require: void 0,
1405
- default: entry.default
1469
+ default: fileInfo(entry.default)
1406
1470
  };
1407
1471
  if (entry.types) {
1408
1472
  exp.types = fileInfo(entry.types);
@@ -1411,7 +1475,7 @@ function printPackageTree(ctx) {
1411
1475
  }
1412
1476
  if (entry.browser) {
1413
1477
  exp.browser = {
1414
- source: entry.browser.source
1478
+ source: fileInfo(entry.browser.source)
1415
1479
  };
1416
1480
  if (entry.browser.import) exp.browser.import = fileInfo(entry.browser.import);
1417
1481
  if (entry.browser.require) exp.browser.require = fileInfo(entry.browser.require);
@@ -1420,7 +1484,7 @@ function printPackageTree(ctx) {
1420
1484
  }
1421
1485
  if (entry.node) {
1422
1486
  exp.node = {
1423
- source: entry.node.source
1487
+ source: fileInfo(entry.node.source)
1424
1488
  };
1425
1489
  if (entry.node.import) exp.node.import = fileInfo(entry.node.import);
1426
1490
  if (entry.node.require) exp.node.require = fileInfo(entry.node.require);
@@ -1437,8 +1501,7 @@ function printPackageTree(ctx) {
1437
1501
  } else {
1438
1502
  delete exp.require;
1439
1503
  }
1440
- exp.default = fileInfo(entry.default);
1441
- return [chalk.green(path.join(pkg.name, exportPath)), exp];
1504
+ return [chalk.cyan(path.join(pkg.name, exportPath)), exp];
1442
1505
  }));
1443
1506
  logger.log(treeify.asTree(tree, true, true));
1444
1507
  }
@@ -1448,8 +1511,10 @@ async function check(options) {
1448
1511
  strict = false,
1449
1512
  tsconfig: tsconfigOption
1450
1513
  } = options;
1514
+ const logger = createLogger();
1451
1515
  const pkg = await loadPkgWithReporting({
1452
- cwd
1516
+ cwd,
1517
+ logger
1453
1518
  });
1454
1519
  const config = await loadConfig({
1455
1520
  cwd
@@ -1458,13 +1523,11 @@ async function check(options) {
1458
1523
  const ctx = await resolveBuildContext({
1459
1524
  config,
1460
1525
  cwd,
1526
+ logger,
1461
1527
  pkg,
1462
1528
  strict,
1463
1529
  tsconfig
1464
1530
  });
1465
- const {
1466
- logger
1467
- } = ctx;
1468
1531
  printPackageTree(ctx);
1469
1532
  if (strict) {
1470
1533
  const missingFiles = [];
@@ -1565,8 +1628,8 @@ function resolveWatchTasks(ctx) {
1565
1628
  target
1566
1629
  } = ctx;
1567
1630
  const tasks = [];
1568
- const exports = Object.entries(ctx.exports || {}).map(_ref14 => {
1569
- let [_path, exp] = _ref14;
1631
+ const exports = Object.entries(ctx.exports || {}).map(_ref15 => {
1632
+ let [_path, exp] = _ref15;
1570
1633
  return _objectSpread({
1571
1634
  _path
1572
1635
  }, exp);
@@ -1677,7 +1740,8 @@ function watchFiles(patterns) {
1677
1740
  }
1678
1741
  async function watchConfigFiles(options) {
1679
1742
  const {
1680
- cwd
1743
+ cwd,
1744
+ logger
1681
1745
  } = options;
1682
1746
  const initialFiles = await globFiles([path.resolve(cwd, "package.json"), path.resolve(cwd, "package.config.cjs"), path.resolve(cwd, "package.config.js"), path.resolve(cwd, "package.config.ts")]);
1683
1747
  const fileEvent$ = watchFiles([path.resolve(cwd, "package.json"), path.resolve(cwd, "package.config.cjs"), path.resolve(cwd, "package.config.js"), path.resolve(cwd, "package.config.ts")]);
@@ -1689,9 +1753,9 @@ async function watchConfigFiles(options) {
1689
1753
  return files.filter(f => f !== fileEvent.file);
1690
1754
  }
1691
1755
  if (fileEvent.type === "change") {
1692
- console.log("--------------------------------------------------------------------------------");
1693
- console.log(chalk.blue("info "), path.relative(cwd, fileEvent.file), "changed");
1694
- console.log("");
1756
+ logger.log("--------------------------------------------------------------------------------");
1757
+ logger.info(path.relative(cwd, fileEvent.file), "changed");
1758
+ logger.log("");
1695
1759
  return files.slice(0);
1696
1760
  }
1697
1761
  return files;
@@ -1703,8 +1767,10 @@ async function watch(options) {
1703
1767
  strict = false,
1704
1768
  tsconfig: tsconfigOption
1705
1769
  } = options;
1770
+ const logger = createLogger();
1706
1771
  const configFiles$ = await watchConfigFiles({
1707
- cwd
1772
+ cwd,
1773
+ logger
1708
1774
  });
1709
1775
  const ctx$ = configFiles$.pipe(switchMap(async configFiles => {
1710
1776
  const files = configFiles.map(f => path.relative(cwd, f));
@@ -1713,7 +1779,8 @@ async function watch(options) {
1713
1779
  throw new Error("missing package.json");
1714
1780
  }
1715
1781
  const pkg = await loadPkgWithReporting({
1716
- cwd
1782
+ cwd,
1783
+ logger
1717
1784
  });
1718
1785
  const config = await loadConfig({
1719
1786
  cwd
@@ -1722,6 +1789,7 @@ async function watch(options) {
1722
1789
  return resolveBuildContext({
1723
1790
  config,
1724
1791
  cwd,
1792
+ logger,
1725
1793
  pkg,
1726
1794
  strict,
1727
1795
  tsconfig
@@ -1749,5 +1817,5 @@ async function watch(options) {
1749
1817
  }
1750
1818
  });
1751
1819
  }
1752
- export { DEFAULT_BROWSERSLIST_QUERY, MODULE_EXT, build, buildTaskHandlers, check, defineConfig, dtsTask, dtsWatchTask, getExtractMessagesConfig, isRecord, loadConfig, loadPkg, loadPkgWithReporting, loadTSConfig, parseExports, resolveBuildTasks, resolveConfigProperty, rollupTask, rollupWatchTask, watch, watchTaskHandlers };
1820
+ export { DEFAULT_BROWSERSLIST_QUERY, MODULE_EXT, build, buildTaskHandlers, check, createLogger, defineConfig, dtsTask, dtsWatchTask, getExtractMessagesConfig, isRecord, loadConfig, loadPkg, loadPkgWithReporting, loadTSConfig, parseExports, resolveBuildTasks, resolveConfigProperty, rollupTask, rollupWatchTask, watch, watchTaskHandlers };
1753
1821
  //# sourceMappingURL=index.js.map