isolate-package 1.10.1 → 1.12.0

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.
@@ -7,7 +7,7 @@ import sourceMaps from "source-map-support";
7
7
  // src/isolate.ts
8
8
  import fs15 from "fs-extra";
9
9
  import assert6 from "node:assert";
10
- import path16 from "node:path";
10
+ import path17 from "node:path";
11
11
 
12
12
  // src/lib/config.ts
13
13
  import fs5 from "fs-extra";
@@ -88,12 +88,12 @@ function toErrorWithMessage(maybeError) {
88
88
  }
89
89
 
90
90
  // src/lib/utils/get-relative-path.ts
91
- function getRootRelativePath(path17, rootPath) {
92
- const strippedPath = path17.replace(rootPath, "");
91
+ function getRootRelativePath(path18, rootPath) {
92
+ const strippedPath = path18.replace(rootPath, "");
93
93
  return strippedPath.startsWith("/") ? `(root)${strippedPath}` : `(root)/${strippedPath}`;
94
94
  }
95
- function getIsolateRelativePath(path17, isolatePath) {
96
- const strippedPath = path17.replace(isolatePath, "");
95
+ function getIsolateRelativePath(path18, isolatePath) {
96
+ const strippedPath = path18.replace(isolatePath, "");
97
97
  return strippedPath.startsWith("/") ? `(isolate)${strippedPath}` : `(isolate)/${strippedPath}`;
98
98
  }
99
99
 
@@ -109,7 +109,9 @@ import stripJsonComments from "strip-json-comments";
109
109
  function readTypedJsonSync(filePath) {
110
110
  try {
111
111
  const rawContent = fs.readFileSync(filePath, "utf-8");
112
- const data = JSON.parse(stripJsonComments(rawContent));
112
+ const data = JSON.parse(
113
+ stripJsonComments(rawContent, { trailingCommas: true })
114
+ );
113
115
  return data;
114
116
  } catch (err) {
115
117
  throw new Error(
@@ -120,7 +122,9 @@ function readTypedJsonSync(filePath) {
120
122
  async function readTypedJson(filePath) {
121
123
  try {
122
124
  const rawContent = await fs.readFile(filePath, "utf-8");
123
- const data = JSON.parse(rawContent);
125
+ const data = JSON.parse(
126
+ stripJsonComments(rawContent, { trailingCommas: true })
127
+ );
124
128
  return data;
125
129
  } catch (err) {
126
130
  throw new Error(
@@ -263,9 +267,6 @@ function resolveConfig() {
263
267
  return config;
264
268
  }
265
269
 
266
- // src/lib/lockfile/process-lockfile.ts
267
- import { mapValues } from "remeda";
268
-
269
270
  // src/lib/package-manager/helpers/infer-from-files.ts
270
271
  import fs6 from "fs-extra";
271
272
  import { execSync } from "node:child_process";
@@ -394,15 +395,38 @@ import {
394
395
  readWantedLockfile,
395
396
  writeWantedLockfile
396
397
  } from "@pnpm/lockfile-file";
398
+ import { pruneLockfile } from "@pnpm/prune-lockfile";
397
399
  import assert3 from "node:assert";
398
400
  import path6 from "node:path";
399
401
  import { pick } from "remeda";
402
+
403
+ // src/lib/lockfile/helpers/pnpm-map-importer.ts
404
+ import { mapValues } from "remeda";
405
+ function pnpmMapImporter({ dependencies, devDependencies, ...rest }, {
406
+ includeDevDependencies,
407
+ directoryByPackageName
408
+ }) {
409
+ return {
410
+ dependencies: dependencies ? pnpmMapDependenciesLinks(dependencies, directoryByPackageName) : void 0,
411
+ devDependencies: includeDevDependencies && devDependencies ? pnpmMapDependenciesLinks(devDependencies, directoryByPackageName) : void 0,
412
+ ...rest
413
+ };
414
+ }
415
+ function pnpmMapDependenciesLinks(def, directoryByPackageName) {
416
+ return mapValues(
417
+ def,
418
+ (value, key) => value.startsWith("link:") ? `link:./${directoryByPackageName[key]}` : value
419
+ );
420
+ }
421
+
422
+ // src/lib/lockfile/helpers/generate-pnpm-lockfile.ts
400
423
  async function generatePnpmLockfile({
401
424
  workspaceRootDir,
402
425
  targetPackageDir,
403
426
  isolateDir,
404
427
  internalDepPackageNames,
405
- packagesRegistry
428
+ packagesRegistry,
429
+ targetPackageManifest
406
430
  }) {
407
431
  const { includeDevDependencies, includePatchedDependencies } = useConfig();
408
432
  const log = useLogger();
@@ -458,8 +482,14 @@ async function generatePnpmLockfile({
458
482
  }
459
483
  )
460
484
  );
485
+ log.debug("Pruning the lockfile");
486
+ const prunedLockfile = await pruneLockfile(
487
+ lockfile,
488
+ targetPackageManifest,
489
+ "."
490
+ );
461
491
  await writeWantedLockfile(isolateDir, {
462
- ...lockfile,
492
+ ...prunedLockfile,
463
493
  /**
464
494
  * Don't know how to map the patched dependencies yet, so we just include
465
495
  * them but I don't think it would work like this. The important thing for
@@ -501,28 +531,13 @@ async function generateYarnLockfile({
501
531
  }
502
532
 
503
533
  // src/lib/lockfile/process-lockfile.ts
504
- function pnpmMapImporter({ dependencies, devDependencies, ...rest }, {
505
- includeDevDependencies,
506
- directoryByPackageName
507
- }) {
508
- return {
509
- dependencies: dependencies ? pnpmMapDependenciesLinks(dependencies, directoryByPackageName) : void 0,
510
- devDependencies: includeDevDependencies && devDependencies ? pnpmMapDependenciesLinks(devDependencies, directoryByPackageName) : void 0,
511
- ...rest
512
- };
513
- }
514
- function pnpmMapDependenciesLinks(def, directoryByPackageName) {
515
- return mapValues(
516
- def,
517
- (value, key) => value.startsWith("link:") ? `link:./${directoryByPackageName[key]}` : value
518
- );
519
- }
520
534
  async function processLockfile({
521
535
  workspaceRootDir,
522
536
  packagesRegistry,
523
537
  isolateDir,
524
538
  internalDepPackageNames,
525
- targetPackageDir
539
+ targetPackageDir,
540
+ targetPackageManifest
526
541
  }) {
527
542
  const log = useLogger();
528
543
  const { forceNpm } = useConfig();
@@ -569,7 +584,8 @@ async function processLockfile({
569
584
  targetPackageDir,
570
585
  isolateDir,
571
586
  internalDepPackageNames,
572
- packagesRegistry
587
+ packagesRegistry,
588
+ targetPackageManifest
573
589
  });
574
590
  break;
575
591
  }
@@ -653,17 +669,17 @@ async function adaptInternalPackageManifests(internalPackageNames, packagesRegis
653
669
  await Promise.all(
654
670
  internalPackageNames.map(async (packageName) => {
655
671
  const { manifest, rootRelativeDir } = packagesRegistry[packageName];
656
- const inputManifest = omit(manifest, ["scripts", "devDependencies"]);
672
+ const strippedManifest = omit(manifest, ["scripts", "devDependencies"]);
657
673
  const outputManifest = packageManager2.name === "pnpm" && !forceNpm ? (
658
674
  /**
659
675
  * For PNPM the output itself is a workspace so we can preserve the specifiers
660
676
  * with "workspace:*" in the output manifest.
661
677
  */
662
- inputManifest
678
+ strippedManifest
663
679
  ) : (
664
680
  /** For other package managers we replace the links to internal dependencies */
665
681
  adaptManifestInternalDeps({
666
- manifest: inputManifest,
682
+ manifest: strippedManifest,
667
683
  packagesRegistry,
668
684
  parentRootRelativeDir: rootRelativeDir
669
685
  })
@@ -676,17 +692,40 @@ async function adaptInternalPackageManifests(internalPackageNames, packagesRegis
676
692
  );
677
693
  }
678
694
 
695
+ // src/lib/manifest/helpers/adopt-pnpm-fields-from-root.ts
696
+ import path11 from "path";
697
+ async function adoptPnpmFieldsFromRoot(targetPackageManifest, workspaceRootDir) {
698
+ const rootPackageManifest = await readTypedJson(
699
+ path11.join(workspaceRootDir, "package.json")
700
+ );
701
+ const overrides = rootPackageManifest.pnpm?.overrides;
702
+ if (!overrides) {
703
+ return targetPackageManifest;
704
+ }
705
+ return {
706
+ ...targetPackageManifest,
707
+ pnpm: {
708
+ overrides
709
+ }
710
+ };
711
+ }
712
+
679
713
  // src/lib/manifest/adapt-target-package-manifest.ts
680
- async function adaptTargetPackageManifest(manifest, packagesRegistry, isolateDir) {
714
+ async function adaptTargetPackageManifest({
715
+ manifest,
716
+ packagesRegistry,
717
+ workspaceRootDir
718
+ }) {
681
719
  const packageManager2 = usePackageManager();
682
720
  const { includeDevDependencies, forceNpm, pickFromScripts, omitFromScripts } = useConfig();
683
721
  const inputManifest = includeDevDependencies ? manifest : omit2(manifest, ["devDependencies"]);
684
722
  const adaptedManifest = packageManager2.name === "pnpm" && !forceNpm ? (
685
723
  /**
686
724
  * For PNPM the output itself is a workspace so we can preserve the specifiers
687
- * with "workspace:*" in the output manifest.
725
+ * with "workspace:*" in the output manifest, but we do want to adopt the
726
+ * pnpm.overrides field from the root package.json.
688
727
  */
689
- inputManifest
728
+ await adoptPnpmFieldsFromRoot(inputManifest, workspaceRootDir)
690
729
  ) : (
691
730
  /** For other package managers we replace the links to internal dependencies */
692
731
  adaptManifestInternalDeps({
@@ -694,7 +733,7 @@ async function adaptTargetPackageManifest(manifest, packagesRegistry, isolateDir
694
733
  packagesRegistry
695
734
  })
696
735
  );
697
- const outputManifest = {
736
+ return {
698
737
  ...adaptedManifest,
699
738
  /**
700
739
  * Scripts are removed by default if not explicitly picked or omitted via
@@ -702,27 +741,26 @@ async function adaptTargetPackageManifest(manifest, packagesRegistry, isolateDir
702
741
  */
703
742
  scripts: pickFromScripts ? pick2(manifest.scripts ?? {}, pickFromScripts) : omitFromScripts ? omit2(manifest.scripts ?? {}, omitFromScripts) : void 0
704
743
  };
705
- await writeManifest(isolateDir, outputManifest);
706
744
  }
707
745
 
708
746
  // src/lib/output/get-build-output-dir.ts
709
747
  import fs11 from "fs-extra";
710
- import path11 from "node:path";
748
+ import path12 from "node:path";
711
749
  import outdent from "outdent";
712
750
  async function getBuildOutputDir(targetPackageDir) {
713
751
  const config = useConfig();
714
752
  const log = useLogger();
715
753
  if (config.buildDirName) {
716
754
  log.debug("Using buildDirName from config:", config.buildDirName);
717
- return path11.join(targetPackageDir, config.buildDirName);
755
+ return path12.join(targetPackageDir, config.buildDirName);
718
756
  }
719
- const tsconfigPath = path11.join(targetPackageDir, config.tsconfigPath);
757
+ const tsconfigPath = path12.join(targetPackageDir, config.tsconfigPath);
720
758
  if (fs11.existsSync(tsconfigPath)) {
721
759
  log.debug("Found tsconfig at:", config.tsconfigPath);
722
760
  const tsconfig = await readTypedJson(tsconfigPath);
723
761
  const outDir = tsconfig.compilerOptions?.outDir;
724
762
  if (outDir) {
725
- return path11.join(targetPackageDir, outDir);
763
+ return path12.join(targetPackageDir, outDir);
726
764
  } else {
727
765
  throw new Error(outdent`
728
766
  Failed to find outDir in tsconfig. If you are executing isolate from the root of a monorepo you should specify the buildDirName in isolate.config.json.
@@ -778,7 +816,7 @@ async function packDependencies({
778
816
 
779
817
  // src/lib/output/process-build-output-files.ts
780
818
  import fs12 from "fs-extra";
781
- import path12 from "node:path";
819
+ import path13 from "node:path";
782
820
  var TIMEOUT_MS = 5e3;
783
821
  async function processBuildOutputFiles({
784
822
  targetPackageDir,
@@ -787,7 +825,7 @@ async function processBuildOutputFiles({
787
825
  }) {
788
826
  const log = useLogger();
789
827
  const packedFilePath = await pack(targetPackageDir, tmpDir);
790
- const unpackDir = path12.join(tmpDir, "target");
828
+ const unpackDir = path13.join(tmpDir, "target");
791
829
  const now = Date.now();
792
830
  let isWaitingYet = false;
793
831
  while (!fs12.existsSync(packedFilePath) && Date.now() - now < TIMEOUT_MS) {
@@ -798,19 +836,19 @@ async function processBuildOutputFiles({
798
836
  await new Promise((resolve) => setTimeout(resolve, 100));
799
837
  }
800
838
  await unpack(packedFilePath, unpackDir);
801
- await fs12.copy(path12.join(unpackDir, "package"), isolateDir);
839
+ await fs12.copy(path13.join(unpackDir, "package"), isolateDir);
802
840
  }
803
841
 
804
842
  // src/lib/output/unpack-dependencies.ts
805
843
  import fs13 from "fs-extra";
806
- import path13, { join } from "node:path";
844
+ import path14, { join } from "node:path";
807
845
  async function unpackDependencies(packedFilesByName, packagesRegistry, tmpDir, isolateDir) {
808
846
  const log = useLogger();
809
847
  await Promise.all(
810
848
  Object.entries(packedFilesByName).map(async ([packageName, filePath]) => {
811
849
  const dir = packagesRegistry[packageName].rootRelativeDir;
812
850
  const unpackDir = join(tmpDir, dir);
813
- log.debug("Unpacking", `(temp)/${path13.basename(filePath)}`);
851
+ log.debug("Unpacking", `(temp)/${path14.basename(filePath)}`);
814
852
  await unpack(filePath, unpackDir);
815
853
  const destinationDir = join(isolateDir, dir);
816
854
  await fs13.ensureDir(destinationDir);
@@ -830,25 +868,25 @@ async function unpackDependencies(packedFilesByName, packagesRegistry, tmpDir, i
830
868
  // src/lib/registry/create-packages-registry.ts
831
869
  import fs14 from "fs-extra";
832
870
  import { globSync } from "glob";
833
- import path15 from "node:path";
871
+ import path16 from "node:path";
834
872
 
835
873
  // src/lib/registry/helpers/find-packages-globs.ts
836
874
  import assert5 from "node:assert";
837
- import path14 from "node:path";
875
+ import path15 from "node:path";
838
876
  function findPackagesGlobs(workspaceRootDir) {
839
877
  const log = useLogger();
840
878
  const packageManager2 = usePackageManager();
841
879
  switch (packageManager2.name) {
842
880
  case "pnpm": {
843
881
  const { packages: globs } = readTypedYamlSync(
844
- path14.join(workspaceRootDir, "pnpm-workspace.yaml")
882
+ path15.join(workspaceRootDir, "pnpm-workspace.yaml")
845
883
  );
846
884
  log.debug("Detected pnpm packages globs:", inspectValue(globs));
847
885
  return globs;
848
886
  }
849
887
  case "yarn":
850
888
  case "npm": {
851
- const workspaceRootManifestPath = path14.join(
889
+ const workspaceRootManifestPath = path15.join(
852
890
  workspaceRootDir,
853
891
  "package.json"
854
892
  );
@@ -888,7 +926,7 @@ async function createPackagesRegistry(workspaceRootDir, workspacePackagesOverrid
888
926
  const allPackages = packagesGlobs.flatMap((glob) => globSync(glob)).filter((dir) => fs14.lstatSync(dir).isDirectory());
889
927
  const registry = (await Promise.all(
890
928
  allPackages.map(async (rootRelativeDir) => {
891
- const manifestPath = path15.join(rootRelativeDir, "package.json");
929
+ const manifestPath = path16.join(rootRelativeDir, "package.json");
892
930
  if (!fs14.existsSync(manifestPath)) {
893
931
  log.warn(
894
932
  `Ignoring directory ./${rootRelativeDir} because it does not contain a package.json file`
@@ -897,12 +935,12 @@ async function createPackagesRegistry(workspaceRootDir, workspacePackagesOverrid
897
935
  } else {
898
936
  log.debug(`Registering package ./${rootRelativeDir}`);
899
937
  const manifest = await readTypedJson(
900
- path15.join(rootRelativeDir, "package.json")
938
+ path16.join(rootRelativeDir, "package.json")
901
939
  );
902
940
  return {
903
941
  manifest,
904
942
  rootRelativeDir,
905
- absoluteDir: path15.join(workspaceRootDir, rootRelativeDir)
943
+ absoluteDir: path16.join(workspaceRootDir, rootRelativeDir)
906
944
  };
907
945
  }
908
946
  })
@@ -947,11 +985,11 @@ async function isolate(options = {}) {
947
985
  setLogLevel(config.logLevel);
948
986
  const log = useLogger();
949
987
  const thisPackageManifest = await readTypedJson(
950
- path16.join(path16.join(__dirname, "..", "package.json"))
988
+ path17.join(path17.join(__dirname, "..", "package.json"))
951
989
  );
952
990
  log.debug("Using isolate-package version", thisPackageManifest.version);
953
- const targetPackageDir = config.targetPackagePath ? path16.join(process.cwd(), config.targetPackagePath) : process.cwd();
954
- const workspaceRootDir = config.targetPackagePath ? process.cwd() : path16.join(targetPackageDir, config.workspaceRoot);
991
+ const targetPackageDir = config.targetPackagePath ? path17.join(process.cwd(), config.targetPackagePath) : process.cwd();
992
+ const workspaceRootDir = config.targetPackagePath ? process.cwd() : path17.join(targetPackageDir, config.workspaceRoot);
955
993
  const buildOutputDir = await getBuildOutputDir(targetPackageDir);
956
994
  assert6(
957
995
  fs15.existsSync(buildOutputDir),
@@ -962,7 +1000,7 @@ async function isolate(options = {}) {
962
1000
  "Isolate target package",
963
1001
  getRootRelativePath(targetPackageDir, workspaceRootDir)
964
1002
  );
965
- const isolateDir = path16.join(targetPackageDir, config.isolateDirName);
1003
+ const isolateDir = path17.join(targetPackageDir, config.isolateDirName);
966
1004
  log.debug(
967
1005
  "Isolate output directory",
968
1006
  getRootRelativePath(isolateDir, workspaceRootDir)
@@ -972,10 +1010,10 @@ async function isolate(options = {}) {
972
1010
  log.debug("Cleaned the existing isolate output directory");
973
1011
  }
974
1012
  await fs15.ensureDir(isolateDir);
975
- const tmpDir = path16.join(isolateDir, "__tmp");
1013
+ const tmpDir = path17.join(isolateDir, "__tmp");
976
1014
  await fs15.ensureDir(tmpDir);
977
1015
  const targetPackageManifest = await readTypedJson(
978
- path16.join(targetPackageDir, "package.json")
1016
+ path17.join(targetPackageDir, "package.json")
979
1017
  );
980
1018
  const packageManager2 = detectPackageManager(workspaceRootDir);
981
1019
  log.debug(
@@ -1015,18 +1053,20 @@ async function isolate(options = {}) {
1015
1053
  tmpDir,
1016
1054
  isolateDir
1017
1055
  });
1018
- await adaptTargetPackageManifest(
1019
- targetPackageManifest,
1056
+ const outputManifest = await adaptTargetPackageManifest({
1057
+ manifest: targetPackageManifest,
1020
1058
  packagesRegistry,
1021
- isolateDir
1022
- );
1059
+ workspaceRootDir
1060
+ });
1061
+ await writeManifest(isolateDir, outputManifest);
1023
1062
  const usedFallbackToNpm = await processLockfile({
1024
1063
  workspaceRootDir,
1025
1064
  isolateDir,
1026
1065
  packagesRegistry,
1027
1066
  internalDepPackageNames: internalPackageNames,
1028
1067
  targetPackageDir,
1029
- targetPackageName: targetPackageManifest.name
1068
+ targetPackageName: targetPackageManifest.name,
1069
+ targetPackageManifest: outputManifest
1030
1070
  });
1031
1071
  if (usedFallbackToNpm) {
1032
1072
  const manifest = await readManifest(isolateDir);
@@ -1036,13 +1076,13 @@ async function isolate(options = {}) {
1036
1076
  }
1037
1077
  if (packageManager2.name === "pnpm" && !config.forceNpm) {
1038
1078
  fs15.copyFileSync(
1039
- path16.join(workspaceRootDir, "pnpm-workspace.yaml"),
1040
- path16.join(isolateDir, "pnpm-workspace.yaml")
1079
+ path17.join(workspaceRootDir, "pnpm-workspace.yaml"),
1080
+ path17.join(isolateDir, "pnpm-workspace.yaml")
1041
1081
  );
1042
1082
  }
1043
- const npmrcPath = path16.join(workspaceRootDir, ".npmrc");
1083
+ const npmrcPath = path17.join(workspaceRootDir, ".npmrc");
1044
1084
  if (fs15.existsSync(npmrcPath)) {
1045
- fs15.copyFileSync(npmrcPath, path16.join(isolateDir, ".npmrc"));
1085
+ fs15.copyFileSync(npmrcPath, path17.join(isolateDir, ".npmrc"));
1046
1086
  log.debug("Copied .npmrc file to the isolate output");
1047
1087
  }
1048
1088
  log.debug(