@modern-js/utils 2.65.2 → 2.65.4

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.
@@ -28,6 +28,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
  var project_exports = {};
30
30
  __export(project_exports, {
31
+ getReactVersion: () => getReactVersion,
31
32
  isApiOnly: () => isApiOnly,
32
33
  isBeyondReact17: () => isBeyondReact17,
33
34
  isDepExists: () => isDepExists,
@@ -82,7 +83,7 @@ const isWebOnly = async () => {
82
83
  const isVersionBeyond17 = (version) => {
83
84
  return import_compiled.semver.gte(import_compiled.semver.minVersion(version), "17.0.0");
84
85
  };
85
- const isBeyondReact17 = (cwd) => {
86
+ const getReactVersion = (cwd) => {
86
87
  const pkgPath = import_pkg_up.default.sync({
87
88
  cwd
88
89
  });
@@ -97,43 +98,44 @@ const isBeyondReact17 = (cwd) => {
97
98
  if (typeof deps.react !== "string") {
98
99
  return false;
99
100
  }
100
- return isVersionBeyond17(deps.react);
101
- };
102
- const isSupportAutomaticJsx = (cwd) => {
103
- const pkgPath = import_pkg_up.default.sync({
104
- cwd
105
- });
106
- if (!pkgPath) {
101
+ try {
102
+ const reactPath = require.resolve("react/package.json", {
103
+ paths: [
104
+ cwd
105
+ ]
106
+ });
107
+ const reactVersion = JSON.parse(import_compiled.fs.readFileSync(reactPath, "utf8")).version;
108
+ return reactVersion;
109
+ } catch (error) {
110
+ console.error("Failed to resolve React version:", error);
107
111
  return false;
108
112
  }
109
- const pkgInfo = JSON.parse(import_compiled.fs.readFileSync(pkgPath, "utf8"));
110
- const deps = {
111
- ...pkgInfo.devDependencies,
112
- ...pkgInfo.dependencies
113
- };
114
- if (typeof deps.react !== "string") {
113
+ };
114
+ const isBeyondReact17 = (cwd) => {
115
+ const reactVersion = getReactVersion(cwd);
116
+ if (!reactVersion) {
115
117
  return false;
116
118
  }
117
- return import_compiled.semver.satisfies(import_compiled.semver.minVersion(deps.react), ">=16.14.0");
119
+ return isVersionBeyond17(reactVersion);
118
120
  };
119
- const isReact18 = (cwd = process.cwd()) => {
120
- const pkgPath = import_path.default.join(cwd, "package.json");
121
- if (!import_compiled.fs.existsSync(pkgPath)) {
121
+ const isSupportAutomaticJsx = (cwd) => {
122
+ const reactVersion = getReactVersion(cwd);
123
+ if (!reactVersion) {
122
124
  return false;
123
125
  }
124
- const pkgInfo = JSON.parse(import_compiled.fs.readFileSync(pkgPath, "utf8"));
125
- const deps = {
126
- ...pkgInfo.devDependencies,
127
- ...pkgInfo.dependencies
128
- };
129
- if (typeof deps.react !== "string") {
126
+ return import_compiled.semver.satisfies(import_compiled.semver.minVersion(reactVersion), ">=16.14.0");
127
+ };
128
+ const isReact18 = (cwd = process.cwd()) => {
129
+ const reactVersion = getReactVersion(cwd);
130
+ if (!reactVersion) {
130
131
  return false;
131
132
  }
132
- return import_compiled.semver.gte(import_compiled.semver.minVersion(deps.react), "18.0.0");
133
+ return import_compiled.semver.gte(import_compiled.semver.minVersion(reactVersion), "18.0.0");
133
134
  };
134
135
  const isTypescript = (root) => import_compiled.fs.existsSync(import_path.default.resolve(root, "./tsconfig.json"));
135
136
  // Annotate the CommonJS export names for ESM import in node:
136
137
  0 && (module.exports = {
138
+ getReactVersion,
137
139
  isApiOnly,
138
140
  isBeyondReact17,
139
141
  isDepExists,
@@ -108,4 +108,4 @@
108
108
  "neo-async": "^2.6.1",
109
109
  "webpack-sources": "^3.2.0"
110
110
  }
111
- }
111
+ }
@@ -82,7 +82,7 @@ var isWebOnly = function() {
82
82
  var isVersionBeyond17 = function(version) {
83
83
  return semver.gte(semver.minVersion(version), "17.0.0");
84
84
  };
85
- var isBeyondReact17 = function(cwd) {
85
+ var getReactVersion = function(cwd) {
86
86
  var pkgPath = pkgUp.sync({
87
87
  cwd
88
88
  });
@@ -94,39 +94,46 @@ var isBeyondReact17 = function(cwd) {
94
94
  if (typeof deps.react !== "string") {
95
95
  return false;
96
96
  }
97
- return isVersionBeyond17(deps.react);
97
+ try {
98
+ var reactPath = require.resolve("react/package.json", {
99
+ paths: [
100
+ cwd
101
+ ]
102
+ });
103
+ var reactVersion = JSON.parse(fs.readFileSync(reactPath, "utf8")).version;
104
+ return reactVersion;
105
+ } catch (error) {
106
+ console.error("Failed to resolve React version:", error);
107
+ return false;
108
+ }
98
109
  };
99
- var isSupportAutomaticJsx = function(cwd) {
100
- var pkgPath = pkgUp.sync({
101
- cwd
102
- });
103
- if (!pkgPath) {
110
+ var isBeyondReact17 = function(cwd) {
111
+ var reactVersion = getReactVersion(cwd);
112
+ if (!reactVersion) {
104
113
  return false;
105
114
  }
106
- var pkgInfo = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
107
- var deps = _object_spread({}, pkgInfo.devDependencies, pkgInfo.dependencies);
108
- if (typeof deps.react !== "string") {
115
+ return isVersionBeyond17(reactVersion);
116
+ };
117
+ var isSupportAutomaticJsx = function(cwd) {
118
+ var reactVersion = getReactVersion(cwd);
119
+ if (!reactVersion) {
109
120
  return false;
110
121
  }
111
- return semver.satisfies(semver.minVersion(deps.react), ">=16.14.0");
122
+ return semver.satisfies(semver.minVersion(reactVersion), ">=16.14.0");
112
123
  };
113
124
  var isReact18 = function() {
114
125
  var cwd = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : process.cwd();
115
- var pkgPath = path.join(cwd, "package.json");
116
- if (!fs.existsSync(pkgPath)) {
117
- return false;
118
- }
119
- var pkgInfo = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
120
- var deps = _object_spread({}, pkgInfo.devDependencies, pkgInfo.dependencies);
121
- if (typeof deps.react !== "string") {
126
+ var reactVersion = getReactVersion(cwd);
127
+ if (!reactVersion) {
122
128
  return false;
123
129
  }
124
- return semver.gte(semver.minVersion(deps.react), "18.0.0");
130
+ return semver.gte(semver.minVersion(reactVersion), "18.0.0");
125
131
  };
126
132
  var isTypescript = function(root) {
127
133
  return fs.existsSync(path.resolve(root, "./tsconfig.json"));
128
134
  };
129
135
  export {
136
+ getReactVersion,
130
137
  isApiOnly,
131
138
  isBeyondReact17,
132
139
  isDepExists,
@@ -41,7 +41,7 @@ const isWebOnly = async () => {
41
41
  const isVersionBeyond17 = (version) => {
42
42
  return semver.gte(semver.minVersion(version), "17.0.0");
43
43
  };
44
- const isBeyondReact17 = (cwd) => {
44
+ const getReactVersion = (cwd) => {
45
45
  const pkgPath = pkgUp.sync({
46
46
  cwd
47
47
  });
@@ -56,42 +56,43 @@ const isBeyondReact17 = (cwd) => {
56
56
  if (typeof deps.react !== "string") {
57
57
  return false;
58
58
  }
59
- return isVersionBeyond17(deps.react);
60
- };
61
- const isSupportAutomaticJsx = (cwd) => {
62
- const pkgPath = pkgUp.sync({
63
- cwd
64
- });
65
- if (!pkgPath) {
59
+ try {
60
+ const reactPath = require.resolve("react/package.json", {
61
+ paths: [
62
+ cwd
63
+ ]
64
+ });
65
+ const reactVersion = JSON.parse(fs.readFileSync(reactPath, "utf8")).version;
66
+ return reactVersion;
67
+ } catch (error) {
68
+ console.error("Failed to resolve React version:", error);
66
69
  return false;
67
70
  }
68
- const pkgInfo = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
69
- const deps = {
70
- ...pkgInfo.devDependencies,
71
- ...pkgInfo.dependencies
72
- };
73
- if (typeof deps.react !== "string") {
71
+ };
72
+ const isBeyondReact17 = (cwd) => {
73
+ const reactVersion = getReactVersion(cwd);
74
+ if (!reactVersion) {
74
75
  return false;
75
76
  }
76
- return semver.satisfies(semver.minVersion(deps.react), ">=16.14.0");
77
+ return isVersionBeyond17(reactVersion);
77
78
  };
78
- const isReact18 = (cwd = process.cwd()) => {
79
- const pkgPath = path.join(cwd, "package.json");
80
- if (!fs.existsSync(pkgPath)) {
79
+ const isSupportAutomaticJsx = (cwd) => {
80
+ const reactVersion = getReactVersion(cwd);
81
+ if (!reactVersion) {
81
82
  return false;
82
83
  }
83
- const pkgInfo = JSON.parse(fs.readFileSync(pkgPath, "utf8"));
84
- const deps = {
85
- ...pkgInfo.devDependencies,
86
- ...pkgInfo.dependencies
87
- };
88
- if (typeof deps.react !== "string") {
84
+ return semver.satisfies(semver.minVersion(reactVersion), ">=16.14.0");
85
+ };
86
+ const isReact18 = (cwd = process.cwd()) => {
87
+ const reactVersion = getReactVersion(cwd);
88
+ if (!reactVersion) {
89
89
  return false;
90
90
  }
91
- return semver.gte(semver.minVersion(deps.react), "18.0.0");
91
+ return semver.gte(semver.minVersion(reactVersion), "18.0.0");
92
92
  };
93
93
  const isTypescript = (root) => fs.existsSync(path.resolve(root, "./tsconfig.json"));
94
94
  export {
95
+ getReactVersion,
95
96
  isApiOnly,
96
97
  isBeyondReact17,
97
98
  isDepExists,
@@ -23,6 +23,7 @@ export declare const isPackageInstalled: (name: string, resolvePaths: string | s
23
23
  export declare const isApiOnly: (appDirectory: string, entryDir?: string, apiDir?: string) => Promise<boolean>;
24
24
  export declare const isWebOnly: () => Promise<boolean>;
25
25
  export declare const isVersionBeyond17: (version: string) => boolean;
26
+ export declare const getReactVersion: (cwd: string) => string | false;
26
27
  /**
27
28
  * @deprecated Use {@link isSupportAutomaticJsx} to check if the project supports automatic JSX instead.
28
29
  */
package/package.json CHANGED
@@ -15,7 +15,7 @@
15
15
  "modern",
16
16
  "modern.js"
17
17
  ],
18
- "version": "2.65.2",
18
+ "version": "2.65.4",
19
19
  "jsnext:source": "./src/index.ts",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "main": "./dist/cjs/index.js",
@@ -191,9 +191,9 @@
191
191
  "jest": "^29",
192
192
  "typescript": "^5",
193
193
  "webpack": "^5.98.0",
194
- "@modern-js/types": "2.65.2",
195
- "@scripts/build": "2.65.2",
196
- "@scripts/jest-config": "2.65.2"
194
+ "@modern-js/types": "2.65.4",
195
+ "@scripts/build": "2.65.4",
196
+ "@scripts/jest-config": "2.65.4"
197
197
  },
198
198
  "sideEffects": false,
199
199
  "scripts": {