@yao-pkg/pkg 6.12.0 → 6.13.1

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/lib-es5/walker.js CHANGED
@@ -38,6 +38,7 @@ const colors_1 = require("./colors");
38
38
  const follow_1 = require("./follow");
39
39
  const log_1 = require("./log");
40
40
  const detector = __importStar(require("./detector"));
41
+ const esm_transformer_1 = require("./esm-transformer");
41
42
  const options_1 = __importDefault(require("./options"));
42
43
  // Note: as a developer, you can set the PKG_STRICT_VER variable.
43
44
  // this will turn on some assertion in the walker code below
@@ -63,9 +64,6 @@ function isBuiltin(moduleName) {
63
64
  : moduleName;
64
65
  return module_1.builtinModules.includes(moduleNameWithoutPrefix);
65
66
  }
66
- function unlikelyJavascript(file) {
67
- return ['.css', '.html', '.json', '.vue'].includes(path_1.default.extname(file));
68
- }
69
67
  function isPublic(config) {
70
68
  if (config.private) {
71
69
  return false;
@@ -232,7 +230,7 @@ function stepDetect(record, marker, derivatives) {
232
230
  return false;
233
231
  }
234
232
  return true; // can i go inside?
235
- });
233
+ }, record.file);
236
234
  }
237
235
  catch (error) {
238
236
  log_1.log.error(error.message, record.file);
@@ -265,6 +263,12 @@ async function findCommonJunctionPoint(file, realFile) {
265
263
  }
266
264
  }
267
265
  class Walker {
266
+ params;
267
+ symLinks;
268
+ patches;
269
+ tasks;
270
+ records;
271
+ dictionary;
268
272
  constructor() {
269
273
  this.tasks = [];
270
274
  this.records = {};
@@ -348,7 +352,7 @@ class Walker {
348
352
  this.append(task);
349
353
  return;
350
354
  }
351
- this.append(Object.assign(Object.assign({}, task), { file: realFile }));
355
+ this.append({ ...task, file: realFile });
352
356
  this.appendStat({
353
357
  file: task.file,
354
358
  store: common_1.STORE_STAT,
@@ -380,7 +384,7 @@ class Walker {
380
384
  this.append(task);
381
385
  return;
382
386
  }
383
- this.append(Object.assign(Object.assign({}, task), { file: realFile }));
387
+ this.append({ ...task, file: realFile });
384
388
  await this.appendSymlink(task.file, realFile);
385
389
  this.appendStat({
386
390
  file: task.file,
@@ -389,7 +393,7 @@ class Walker {
389
393
  }
390
394
  async appendFilesFromConfig(marker) {
391
395
  const { config, configPath, base } = marker;
392
- const pkgConfig = config === null || config === void 0 ? void 0 : config.pkg;
396
+ const pkgConfig = config?.pkg;
393
397
  if (pkgConfig) {
394
398
  let { scripts } = pkgConfig;
395
399
  if (scripts) {
@@ -606,7 +610,6 @@ class Walker {
606
610
  }
607
611
  }
608
612
  async stepDerivatives_ALIAS_AS_RESOLVABLE(record, marker, derivative) {
609
- var _a, _b;
610
613
  const newPackages = [];
611
614
  const catchReadFile = (file) => {
612
615
  (0, assert_1.default)((0, common_1.isPackageJson)(file), `walker: ${file} must be package.json`);
@@ -640,7 +643,7 @@ class Walker {
640
643
  }
641
644
  if (failure) {
642
645
  const { toplevel } = marker;
643
- const mainNotFound = newPackages.length > 0 && !((_b = (_a = newPackages[0].marker) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.main);
646
+ const mainNotFound = newPackages.length > 0 && !newPackages[0].marker?.config?.main;
644
647
  const debug = !toplevel ||
645
648
  derivative.mayExclude ||
646
649
  (mainNotFound && derivative.fromDependencies);
@@ -678,17 +681,33 @@ class Walker {
678
681
  break;
679
682
  }
680
683
  }
684
+ // Add all discovered package.json files, not just the one determined by the double-resolution logic
685
+ // This is necessary because ESM resolution may bypass the standard packageFilter mechanism
686
+ // However, only include package.json files that are either:
687
+ // 1. Inside node_modules (dependencies)
688
+ // 2. Inside the base directory of the current marker (application being packaged)
689
+ // This prevents including pkg's own package.json when used from source
690
+ for (const newPackage of newPackages) {
691
+ if (newPackage.marker) {
692
+ const file = newPackage.packageJson;
693
+ const isInNodeModules = file.includes(`${path_1.default.sep}node_modules${path_1.default.sep}`);
694
+ const isInMarkerBase = marker.base && file.startsWith(marker.base);
695
+ if (isInNodeModules || isInMarkerBase) {
696
+ await this.appendBlobOrContent({
697
+ file,
698
+ marker: newPackage.marker,
699
+ store: common_1.STORE_CONTENT,
700
+ reason: record.file,
701
+ });
702
+ }
703
+ }
704
+ }
705
+ // Keep the original logic for determining the marker for the resolved file
681
706
  if (newPackageForNewRecords) {
682
707
  if (strictVerify) {
683
708
  (0, assert_1.default)(newPackageForNewRecords.packageJson ===
684
709
  (0, common_1.normalizePath)(newPackageForNewRecords.packageJson));
685
710
  }
686
- await this.appendBlobOrContent({
687
- file: newPackageForNewRecords.packageJson,
688
- marker: newPackageForNewRecords.marker,
689
- store: common_1.STORE_CONTENT,
690
- reason: record.file,
691
- });
692
711
  }
693
712
  await this.appendBlobOrContent({
694
713
  file: newFile,
@@ -729,7 +748,7 @@ class Walker {
729
748
  await this.stepActivate(marker, derivatives1);
730
749
  await this.stepDerivatives(record, marker, derivatives1);
731
750
  if (store === common_1.STORE_BLOB) {
732
- if (unlikelyJavascript(record.file) || (0, common_1.isDotNODE)(record.file)) {
751
+ if ((0, common_1.unlikelyJavascript)(record.file) || (0, common_1.isDotNODE)(record.file)) {
733
752
  await this.appendBlobOrContent({
734
753
  file: record.file,
735
754
  marker,
@@ -745,7 +764,9 @@ class Walker {
745
764
  });
746
765
  }
747
766
  }
748
- if (store === common_1.STORE_BLOB || this.hasPatch(record)) {
767
+ if (store === common_1.STORE_BLOB ||
768
+ (store === common_1.STORE_CONTENT && (0, common_1.isPackageJson)(record.file)) ||
769
+ this.hasPatch(record)) {
749
770
  if (!record.body) {
750
771
  await stepRead(record);
751
772
  this.stepPatch(record);
@@ -753,6 +774,96 @@ class Walker {
753
774
  stepStrip(record);
754
775
  }
755
776
  }
777
+ // Patch package.json files to add synthetic main field if needed
778
+ if (store === common_1.STORE_CONTENT &&
779
+ (0, common_1.isPackageJson)(record.file) &&
780
+ record.body) {
781
+ try {
782
+ const pkgContent = JSON.parse(record.body.toString('utf8'));
783
+ let modified = false;
784
+ // If package has exports but no main, add a synthetic main field
785
+ if (pkgContent.exports && !pkgContent.main) {
786
+ // Try to get main from marker.config first (set by catchPackageFilter in follow.ts)
787
+ if (marker.config?.main) {
788
+ pkgContent.main = marker.config.main;
789
+ modified = true;
790
+ }
791
+ else {
792
+ // Fallback: try to infer main from exports field
793
+ const { exports } = pkgContent;
794
+ if (typeof exports === 'string') {
795
+ pkgContent.main = exports;
796
+ modified = true;
797
+ }
798
+ else if (exports && typeof exports === 'object') {
799
+ // Handle conditional exports
800
+ if (exports['.']) {
801
+ if (typeof exports['.'] === 'string') {
802
+ pkgContent.main = exports['.'];
803
+ modified = true;
804
+ }
805
+ else if (typeof exports['.'] === 'object') {
806
+ // Try to get the best entry point for CJS
807
+ // Prefer: require > node > default
808
+ let mainEntry;
809
+ if (typeof exports['.'].require === 'string' &&
810
+ exports['.'].require) {
811
+ mainEntry = exports['.'].require;
812
+ }
813
+ else if (typeof exports['.'].node === 'string' &&
814
+ exports['.'].node) {
815
+ mainEntry = exports['.'].node;
816
+ }
817
+ else if (typeof exports['.'].default === 'string' &&
818
+ exports['.'].default) {
819
+ mainEntry = exports['.'].default;
820
+ }
821
+ if (mainEntry) {
822
+ pkgContent.main = mainEntry;
823
+ modified = true;
824
+ }
825
+ }
826
+ }
827
+ }
828
+ }
829
+ }
830
+ // If package has "type": "module", we need to change it to "commonjs"
831
+ // because we transform all ESM files to CJS before bytecode compilation
832
+ if (pkgContent.type === 'module') {
833
+ pkgContent.type = 'commonjs';
834
+ modified = true;
835
+ }
836
+ // Only rewrite if we made changes
837
+ if (modified) {
838
+ record.body = Buffer.from(JSON.stringify(pkgContent, null, 2), 'utf8');
839
+ }
840
+ }
841
+ catch (error) {
842
+ // Ignore JSON parsing errors
843
+ }
844
+ }
845
+ // Transform ESM to CJS before bytecode compilation
846
+ // Check all JS-like files (.js, .mjs, .cjs) but only transform ESM ones
847
+ if (store === common_1.STORE_BLOB &&
848
+ record.body &&
849
+ ((0, common_1.isDotJS)(record.file) || record.file.endsWith('.mjs'))) {
850
+ if ((0, common_1.isESMFile)(record.file)) {
851
+ try {
852
+ const result = (0, esm_transformer_1.transformESMtoCJS)(record.body.toString('utf8'), record.file);
853
+ if (result.isTransformed) {
854
+ record.body = Buffer.from(result.code, 'utf8');
855
+ // Mark .mjs files as transformed so packer can rename them to .js
856
+ if (record.file.endsWith('.mjs')) {
857
+ record.wasTransformed = true;
858
+ }
859
+ }
860
+ }
861
+ catch (error) {
862
+ const message = error instanceof Error ? error.message : String(error);
863
+ throw new Error(`Failed to transform ESM module to CJS for file "${record.file}": ${message}`);
864
+ }
865
+ }
866
+ }
756
867
  if (store === common_1.STORE_BLOB) {
757
868
  const derivatives2 = [];
758
869
  stepDetect(record, marker, derivatives2);
@@ -834,8 +945,7 @@ class Walker {
834
945
  }
835
946
  }
836
947
  async readDictionary(marker) {
837
- var _a, _b, _c;
838
- if (((_a = this.params.noDictionary) === null || _a === void 0 ? void 0 : _a[0]) === '*') {
948
+ if (this.params.noDictionary?.[0] === '*') {
839
949
  return;
840
950
  }
841
951
  const dd = path_1.default.join(__dirname, '../dictionary');
@@ -843,7 +953,7 @@ class Walker {
843
953
  for (const file of files) {
844
954
  if (/\.js$/.test(file)) {
845
955
  const name = file.slice(0, -3);
846
- if ((_b = this.params.noDictionary) === null || _b === void 0 ? void 0 : _b.includes(file)) {
956
+ if (this.params.noDictionary?.includes(file)) {
847
957
  continue;
848
958
  }
849
959
  // eslint-disable-next-line import/no-dynamic-require, global-require, @typescript-eslint/no-var-requires
@@ -851,7 +961,7 @@ class Walker {
851
961
  this.dictionary[name] = config;
852
962
  }
853
963
  }
854
- const pkgConfig = (_c = marker.config) === null || _c === void 0 ? void 0 : _c.pkg;
964
+ const pkgConfig = marker.config?.pkg;
855
965
  if (pkgConfig) {
856
966
  const { dictionary } = pkgConfig;
857
967
  if (dictionary) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yao-pkg/pkg",
3
- "version": "6.12.0",
3
+ "version": "6.13.1",
4
4
  "description": "Package your Node.js project into an executable",
5
5
  "main": "lib-es5/index.js",
6
6
  "license": "MIT",
@@ -24,7 +24,9 @@
24
24
  "dependencies": {
25
25
  "@babel/generator": "^7.23.0",
26
26
  "@babel/parser": "^7.23.0",
27
+ "@babel/traverse": "^7.23.0",
27
28
  "@babel/types": "^7.23.0",
29
+ "esbuild": "^0.24.0",
28
30
  "@yao-pkg/pkg-fetch": "3.5.32",
29
31
  "into-stream": "^6.0.0",
30
32
  "minimist": "^1.2.6",
@@ -33,15 +35,16 @@
33
35
  "picomatch": "^4.0.2",
34
36
  "prebuild-install": "^7.1.1",
35
37
  "resolve": "^1.22.10",
38
+ "resolve.exports": "^2.0.3",
36
39
  "stream-meter": "^1.0.4",
37
- "tar": "^7.4.3",
40
+ "tar": "^7.5.6",
38
41
  "tinyglobby": "^0.2.11",
39
42
  "unzipper": "^0.12.3"
40
43
  },
41
44
  "devDependencies": {
42
- "@babel/core": "^7.23.0",
43
45
  "@release-it/conventional-changelog": "^7.0.2",
44
46
  "@types/babel__generator": "^7.6.5",
47
+ "@types/babel__traverse": "^7.20.3",
45
48
  "@types/minimist": "^1.2.2",
46
49
  "@types/multistream": "^4.1.0",
47
50
  "@types/node": "^16.18.113",
@@ -52,7 +55,6 @@
52
55
  "@types/unzipper": "^0.10.10",
53
56
  "@typescript-eslint/eslint-plugin": "^6.7.4",
54
57
  "@typescript-eslint/parser": "^6.7.4",
55
- "esbuild": "^0.24.0",
56
58
  "esbuild-register": "^3.6.0",
57
59
  "eslint": "^8.50.0",
58
60
  "eslint-config-airbnb-base": "^15.0.0",