@sanity/pkg-utils 1.16.1 → 1.16.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.
package/dist/index.esm.js CHANGED
@@ -555,6 +555,81 @@ async function _buildTypes(options) {
555
555
  }
556
556
  }
557
557
 
558
+ async function _appendModuleBlocks(_ref2) {
559
+ let {
560
+ tsOutDir,
561
+ extractTypesOutFile
562
+ } = _ref2;
563
+ const moduleBlocks = await _extractModuleBlocksFromTypes(tsOutDir);
564
+
565
+ if (moduleBlocks.length) {
566
+ await fs.appendFile(extractTypesOutFile, "\n" + moduleBlocks.join("\n\n"));
567
+ }
568
+ }
569
+
570
+ async function _extractModuleBlocksFromTypes(dirname) {
571
+ const moduleBlocks = [];
572
+ const files = await _getFileList(dirname);
573
+
574
+ for (const fileName of files) {
575
+ const content = await fs.readFile(path.resolve(dirname, fileName), {
576
+ encoding: "utf-8"
577
+ });
578
+
579
+ if (content.includes("declare module")) {
580
+ moduleBlocks.push(..._extractModuleBlocks(content));
581
+ }
582
+ }
583
+
584
+ return moduleBlocks;
585
+ }
586
+
587
+ async function _getFileList(dirName) {
588
+ let files = [];
589
+ const filesInCurrentDir = await fs.readdir(dirName, {
590
+ withFileTypes: true
591
+ });
592
+
593
+ for (const fileInDir of filesInCurrentDir) {
594
+ if (fileInDir.isDirectory()) {
595
+ files = [...files, ...(await _getFileList(path.resolve(dirName, fileInDir.name)))];
596
+ } else {
597
+ files.push(path.resolve(dirName, fileInDir.name));
598
+ }
599
+ }
600
+
601
+ return files;
602
+ }
603
+
604
+ function _extractModuleBlocks(fileContent) {
605
+ const moduleBlocks = [];
606
+ let moduleIndentLevel = 0;
607
+ let moduleLines = [];
608
+ const lines = fileContent.split("\n");
609
+
610
+ for (const line of lines) {
611
+ if (!moduleLines.length) {
612
+ const moduleIndex = line.indexOf("declare module");
613
+ const isModuleStart = moduleIndex > -1;
614
+
615
+ if (isModuleStart) {
616
+ moduleIndentLevel = moduleIndex;
617
+ moduleLines = [line];
618
+ }
619
+ } else {
620
+ moduleLines.push(line);
621
+ const blockEndIndex = line.indexOf("}");
622
+
623
+ if (blockEndIndex === moduleIndentLevel) {
624
+ moduleBlocks.push(moduleLines.map(l => l.substring(moduleIndentLevel, l.length)).join("\n"));
625
+ moduleLines = [];
626
+ }
627
+ }
628
+ }
629
+
630
+ return moduleBlocks;
631
+ }
632
+
558
633
  class _DtsError extends Error {
559
634
  constructor(message, messages) {
560
635
  super(message);
@@ -813,6 +888,10 @@ async function _doExtract(ctx, task) {
813
888
  tsconfigPath: path.resolve(cwd, ts.configPath || "tsconfig.json"),
814
889
  distPath: outDir
815
890
  });
891
+ await _appendModuleBlocks({
892
+ tsOutDir: tmpPath,
893
+ extractTypesOutFile: path.resolve(outDir, targetPath)
894
+ });
816
895
  messages.push(...result.messages);
817
896
 
818
897
  if (result.messages.some(msg => msg.logLevel === "error")) {
@@ -895,7 +974,7 @@ const _dtsWatchTask = {
895
974
  };
896
975
 
897
976
  function _resolveRollupConfig(ctx, buildTask) {
898
- var _ref2, _ref5, _ref6, _ref7, _ref8, _ref9;
977
+ var _ref3, _ref6, _ref7, _ref8, _ref9, _ref10;
899
978
 
900
979
  var _a, _b, _c;
901
980
 
@@ -915,10 +994,10 @@ function _resolveRollupConfig(ctx, buildTask) {
915
994
  ts
916
995
  } = ctx;
917
996
  const outputExt = _MODULE_EXT[pkg.type][format];
918
- const minify = (_ref2 = config == null ? void 0 : config.minify) !== null && _ref2 !== void 0 ? _ref2 : true;
997
+ const minify = (_ref3 = config == null ? void 0 : config.minify) !== null && _ref3 !== void 0 ? _ref3 : true;
919
998
  const outDir = path.relative(cwd, distPath);
920
- const pathAliases = Object.fromEntries(Object.entries(((_a = ts.config) == null ? void 0 : _a.options.paths) || {}).map(_ref3 => {
921
- let [key, val] = _ref3;
999
+ const pathAliases = Object.fromEntries(Object.entries(((_a = ts.config) == null ? void 0 : _a.options.paths) || {}).map(_ref4 => {
1000
+ let [key, val] = _ref4;
922
1001
 
923
1002
  var _a2;
924
1003
 
@@ -934,8 +1013,8 @@ function _resolveRollupConfig(ctx, buildTask) {
934
1013
 
935
1014
  const sourcePaths = _exports && Object.values(_exports).map(e => path.resolve(cwd, e.source));
936
1015
 
937
- const replacements = Object.fromEntries(Object.entries((config == null ? void 0 : config.define) || {}).map(_ref4 => {
938
- let [key, val] = _ref4;
1016
+ const replacements = Object.fromEntries(Object.entries((config == null ? void 0 : config.define) || {}).map(_ref5 => {
1017
+ let [key, val] = _ref5;
939
1018
  return [key, JSON.stringify(val)];
940
1019
  }));
941
1020
  const defaultPlugins = [replace({
@@ -966,10 +1045,10 @@ function _resolveRollupConfig(ctx, buildTask) {
966
1045
  esmExternals: false,
967
1046
  extensions: [".js"]
968
1047
  }), json(), esbuild({
969
- jsx: (_ref5 = config == null ? void 0 : config.jsx) !== null && _ref5 !== void 0 ? _ref5 : "automatic",
970
- jsxFactory: (_ref6 = config == null ? void 0 : config.jsxFactory) !== null && _ref6 !== void 0 ? _ref6 : "createElement",
971
- jsxFragment: (_ref7 = config == null ? void 0 : config.jsxFragment) !== null && _ref7 !== void 0 ? _ref7 : "Fragment",
972
- jsxImportSource: (_ref8 = config == null ? void 0 : config.jsxImportSource) !== null && _ref8 !== void 0 ? _ref8 : "react",
1048
+ jsx: (_ref6 = config == null ? void 0 : config.jsx) !== null && _ref6 !== void 0 ? _ref6 : "automatic",
1049
+ jsxFactory: (_ref7 = config == null ? void 0 : config.jsxFactory) !== null && _ref7 !== void 0 ? _ref7 : "createElement",
1050
+ jsxFragment: (_ref8 = config == null ? void 0 : config.jsxFragment) !== null && _ref8 !== void 0 ? _ref8 : "Fragment",
1051
+ jsxImportSource: (_ref9 = config == null ? void 0 : config.jsxImportSource) !== null && _ref9 !== void 0 ? _ref9 : "react",
973
1052
  target,
974
1053
  tsconfig: ctx.ts.configPath || "tsconfig.json"
975
1054
  }), getBabelOutputPlugin({
@@ -1043,7 +1122,7 @@ function _resolveRollupConfig(ctx, buildTask) {
1043
1122
  entryFileNames: () => outputExt === ".esm.js" ? "[name].js" : "[name]".concat(outputExt),
1044
1123
  format,
1045
1124
  exports: "auto",
1046
- sourcemap: (_ref9 = config == null ? void 0 : config.sourcemap) !== null && _ref9 !== void 0 ? _ref9 : true
1125
+ sourcemap: (_ref10 = config == null ? void 0 : config.sourcemap) !== null && _ref10 !== void 0 ? _ref10 : true
1047
1126
  }
1048
1127
  };
1049
1128
  }
@@ -1332,7 +1411,7 @@ function _createLogger() {
1332
1411
  }
1333
1412
 
1334
1413
  async function _resolveBuildContext(options) {
1335
- var _ref10;
1414
+ var _ref11;
1336
1415
 
1337
1416
  const {
1338
1417
  config,
@@ -1426,7 +1505,7 @@ async function _resolveBuildContext(options) {
1426
1505
  files: [],
1427
1506
  logger,
1428
1507
  pkg,
1429
- runtime: (_ref10 = config == null ? void 0 : config.runtime) !== null && _ref10 !== void 0 ? _ref10 : "*",
1508
+ runtime: (_ref11 = config == null ? void 0 : config.runtime) !== null && _ref11 !== void 0 ? _ref11 : "*",
1430
1509
  strict,
1431
1510
  target,
1432
1511
  ts: {
@@ -1552,11 +1631,11 @@ function _printPackageTree(ctx) {
1552
1631
  return "".concat(file, " ").concat(chalk.gray(info.size));
1553
1632
  }
1554
1633
 
1555
- tree.exports = Object.fromEntries(Object.entries(exports).filter(_ref11 => {
1556
- let [, entry] = _ref11;
1634
+ tree.exports = Object.fromEntries(Object.entries(exports).filter(_ref12 => {
1635
+ let [, entry] = _ref12;
1557
1636
  return entry._exported;
1558
- }).map(_ref12 => {
1559
- let [exportPath, entry] = _ref12;
1637
+ }).map(_ref13 => {
1638
+ let [exportPath, entry] = _ref13;
1560
1639
  const exp = {
1561
1640
  source: entry.source,
1562
1641
  types: void 0,
@@ -1756,8 +1835,8 @@ function _resolveWatchTasks(ctx) {
1756
1835
  target
1757
1836
  } = ctx;
1758
1837
  const tasks = [];
1759
- const exports = Object.entries(ctx.exports || {}).map(_ref13 => {
1760
- let [_path, exp] = _ref13;
1838
+ const exports = Object.entries(ctx.exports || {}).map(_ref14 => {
1839
+ let [_path, exp] = _ref14;
1761
1840
  return _objectSpread({
1762
1841
  _path
1763
1842
  }, exp);