@contrast/esm-hooks 2.0.3 → 2.1.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.
@@ -13,10 +13,11 @@
13
13
  * way not consistent with the End User License Agreement.
14
14
  */
15
15
  import fs from 'node:fs';
16
- import os from 'node:os';
17
16
  import path from 'path';
18
17
  import M from 'module';
19
18
 
19
+ import { findPackageJsonSync } from '@contrast/find-package-json';
20
+
20
21
  const isBuiltin = M.isBuiltin || function(pathname) {
21
22
  if (pathname.startsWith('node:')) {
22
23
  pathname = pathname.slice(5);
@@ -54,14 +55,17 @@ export function getFileType(filename) {
54
55
  return 'commonjs';
55
56
  }
56
57
 
57
- // Node assumes `commonjs ` if there's no `type` set in package.json
58
+ // Node assumes `commonjs` if `type` is not set in package.json
58
59
  let parentType = 'commonjs';
59
60
  try {
60
- let pkg = parent(pathname);
61
+ // should this have stopAt set at the application root? do we know what the
62
+ // root of the application is?
63
+ const pkg = findPackageJsonSync({ cwd: fixPath(path.dirname(pathname)) });
61
64
  if (pkg) {
62
- pkg = pkg.parse();
63
- if (pkg.type) {
64
- parentType = pkg.type;
65
+ let json = fs.readFileSync(pkg, 'utf8');
66
+ json = JSON.parse(json);
67
+ if (json.type) {
68
+ parentType = json.type;
65
69
  }
66
70
  }
67
71
  } catch (err) {
@@ -75,54 +79,3 @@ export function getFileType(filename) {
75
79
  // should this assume commonjs?
76
80
  return null;
77
81
  }
78
-
79
- function parent(startPath, ignore) {
80
- startPath = path.resolve(fixPath(startPath) || process.cwd());
81
- ignore = ignore || 0;
82
-
83
- let searchPath = path.dirname(startPath);
84
-
85
- let root = '/';
86
- if (os.platform() === 'win32') {
87
- root = path.parse(startPath).root;
88
- }
89
-
90
- let packageDotJsonContents;
91
-
92
- // put a limit on this though i think that for(;;) will work fine.
93
- for (let i = 100; i > 0; i -= 1) {
94
- try {
95
- const possible = path.join(searchPath, 'package.json');
96
- packageDotJsonContents = fs.readFileSync(possible, 'utf8');
97
- if (ignore > 0) {
98
- ignore--;
99
- } else {
100
- break;
101
- }
102
- } catch (err) {
103
- if (err.code !== 'ENOENT') {
104
- // log?
105
- }
106
- }
107
-
108
- // if root there is nothing more to check
109
- if (searchPath === root) {
110
- break;
111
- }
112
- searchPath = path.dirname(searchPath);
113
- }
114
-
115
- if (packageDotJsonContents) {
116
- return {
117
- read() {
118
- return packageDotJsonContents;
119
- },
120
- parse() {
121
- return JSON.parse(packageDotJsonContents);
122
- },
123
- path: searchPath,
124
- };
125
- }
126
-
127
- return null;
128
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/esm-hooks",
3
- "version": "2.0.3",
3
+ "version": "2.1.1",
4
4
  "type": "module",
5
5
  "description": "Support for loading and instrumenting ECMAScript modules",
6
6
  "license": "SEE LICENSE IN LICENSE",
@@ -19,6 +19,7 @@
19
19
  "test": "../scripts/test.sh"
20
20
  },
21
21
  "dependencies": {
22
- "@contrast/core": "1.27.2"
22
+ "@contrast/core": "1.28.0",
23
+ "@contrast/find-package-json": "^1.0.0"
23
24
  }
24
25
  }