@reactunity/scripts 0.21.0 → 0.23.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.
@@ -1,263 +0,0 @@
1
- // @remove-file-on-eject
2
- /**
3
- * Copyright (c) 2015-present, Facebook, Inc.
4
- *
5
- * This source code is licensed under the MIT license found in the
6
- * LICENSE file in the root directory of this source tree.
7
- */
8
-
9
- const chalk = require('react-dev-utils/chalk');
10
- const fs = require('node:fs');
11
- const resolve = require('resolve');
12
- const path = require('node:path');
13
- const paths = require('../../config/paths');
14
- const os = require('node:os');
15
- const semver = require('semver');
16
- const immer = require('react-dev-utils/immer').produce;
17
- const globby = require('react-dev-utils/globby').sync;
18
-
19
- const hasJsxRuntime = (() => {
20
- if (process.env.DISABLE_NEW_JSX_TRANSFORM === 'true') {
21
- return false;
22
- }
23
-
24
- try {
25
- require.resolve('react/jsx-runtime', { paths: [paths.appPath] });
26
- return true;
27
- } catch (e) {
28
- return false;
29
- }
30
- })();
31
-
32
- function writeJson(fileName, object) {
33
- fs.writeFileSync(fileName, JSON.stringify(object, null, 2).replace(/\n/g, os.EOL) + os.EOL);
34
- }
35
-
36
- function verifyNoTypeScript() {
37
- const typescriptFiles = globby(['**/*.(ts|tsx)', '!**/node_modules', '!**/*.d.ts'], { cwd: paths.appSrc });
38
- if (typescriptFiles.length > 0) {
39
- console.warn(
40
- chalk.yellow(
41
- `We detected TypeScript in your project (${chalk.bold(
42
- `src${path.sep}${typescriptFiles[0]}`,
43
- )}) and created a ${chalk.bold('tsconfig.json')} file for you.`,
44
- ),
45
- );
46
- console.warn();
47
- return false;
48
- }
49
- return true;
50
- }
51
-
52
- function verifyTypeScriptSetup() {
53
- let firstTimeSetup = false;
54
-
55
- if (!fs.existsSync(paths.appTsConfig)) {
56
- if (verifyNoTypeScript()) {
57
- return;
58
- }
59
- writeJson(paths.appTsConfig, {});
60
- firstTimeSetup = true;
61
- }
62
-
63
- const isYarn = fs.existsSync(paths.yarnLockFile);
64
-
65
- // Ensure typescript is installed
66
- let ts;
67
- try {
68
- // TODO: Remove this hack once `globalThis` issue is resolved
69
- // https://github.com/jsdom/jsdom/issues/2961
70
- const globalThisWasDefined = !!global.globalThis;
71
-
72
- ts = require(
73
- resolve.sync('typescript', {
74
- basedir: paths.appNodeModules,
75
- }),
76
- );
77
-
78
- if (!globalThisWasDefined && !!global.globalThis) {
79
- // biome-ignore lint/performance/noDelete: <explanation>
80
- delete global.globalThis;
81
- }
82
- } catch (_) {
83
- console.error(chalk.bold.red(`It looks like you're trying to use TypeScript but do not have ${chalk.bold('typescript')} installed.`));
84
- console.error(
85
- chalk.bold(
86
- 'Please install',
87
- chalk.cyan.bold('typescript'),
88
- 'by running',
89
- `${chalk.cyan.bold(isYarn ? 'yarn add typescript' : 'npm install typescript')}.`,
90
- ),
91
- );
92
- console.error(
93
- chalk.bold(
94
- `If you are not trying to use TypeScript, please remove the ${chalk.cyan('tsconfig.json')} file from your package root (and any TypeScript files).`,
95
- ),
96
- );
97
- console.error();
98
- process.exit(1);
99
- }
100
-
101
- const compilerOptions = {
102
- // These are suggested values and will be set when not present in the
103
- // tsconfig.json
104
- // 'parsedValue' matches the output value from ts.parseJsonConfigFileContent()
105
- target: {
106
- parsedValue: ts.ScriptTarget.ES5,
107
- suggested: 'es5',
108
- },
109
- lib: { suggested: ['dom', 'dom.iterable', 'esnext'] },
110
- allowJs: { suggested: true },
111
- skipLibCheck: { suggested: true },
112
- esModuleInterop: { suggested: true },
113
- allowSyntheticDefaultImports: { suggested: true },
114
- strict: { suggested: true },
115
- forceConsistentCasingInFileNames: { suggested: true },
116
- noFallthroughCasesInSwitch: { suggested: true },
117
-
118
- // These values are required and cannot be changed by the user
119
- // Keep this in sync with the webpack config
120
- module: {
121
- parsedValue: ts.ModuleKind.ESNext,
122
- value: 'esnext',
123
- reason: 'for import() and import/export',
124
- },
125
- moduleResolution: {
126
- parsedValue: ts.ModuleResolutionKind.NodeJs,
127
- value: 'node',
128
- reason: 'to match webpack resolution',
129
- },
130
- resolveJsonModule: { value: true, reason: 'to match webpack loader' },
131
- isolatedModules: { value: true, reason: 'implementation limitation' },
132
- noEmit: { value: true },
133
- jsx: {
134
- parsedValue: hasJsxRuntime && semver.gte(ts.version, '4.1.0-beta') ? ts.JsxEmit.ReactJSX : ts.JsxEmit.React,
135
- value: hasJsxRuntime && semver.gte(ts.version, '4.1.0-beta') ? 'react-jsx' : 'react',
136
- reason: 'to support the new JSX transform in React 17',
137
- },
138
- paths: { value: undefined, reason: 'aliased imports are not supported' },
139
- };
140
-
141
- const formatDiagnosticHost = {
142
- getCanonicalFileName: (fileName) => fileName,
143
- getCurrentDirectory: ts.sys.getCurrentDirectory,
144
- getNewLine: () => os.EOL,
145
- };
146
-
147
- const messages = [];
148
- let appTsConfig;
149
- let parsedTsConfig;
150
- let parsedCompilerOptions;
151
- try {
152
- const { config: readTsConfig, error } = ts.readConfigFile(paths.appTsConfig, ts.sys.readFile);
153
-
154
- if (error) {
155
- throw new Error(ts.formatDiagnostic(error, formatDiagnosticHost));
156
- }
157
-
158
- appTsConfig = readTsConfig;
159
-
160
- // Get TS to parse and resolve any "extends"
161
- // Calling this function also mutates the tsconfig above,
162
- // adding in "include" and "exclude", but the compilerOptions remain untouched
163
- let result;
164
- parsedTsConfig = immer(readTsConfig, (config) => {
165
- result = ts.parseJsonConfigFileContent(config, ts.sys, path.dirname(paths.appTsConfig));
166
- });
167
-
168
- if (result.errors?.length) {
169
- throw new Error(ts.formatDiagnostic(result.errors[0], formatDiagnosticHost));
170
- }
171
-
172
- parsedCompilerOptions = result.options;
173
- } catch (e) {
174
- if (e && e.name === 'SyntaxError') {
175
- console.error(
176
- chalk.red.bold('Could not parse', `${chalk.cyan('tsconfig.json')}.`, 'Please make sure it contains syntactically correct JSON.'),
177
- );
178
- }
179
-
180
- console.log(e?.message ? `${e.message}` : '');
181
- process.exit(1);
182
- }
183
-
184
- if (appTsConfig.compilerOptions == null) {
185
- appTsConfig.compilerOptions = {};
186
- firstTimeSetup = true;
187
- }
188
-
189
- for (const option of Object.keys(compilerOptions)) {
190
- const { parsedValue, value, suggested, reason } = compilerOptions[option];
191
-
192
- const valueToCheck = parsedValue === undefined ? value : parsedValue;
193
- const coloredOption = chalk.cyan(`compilerOptions.${option}`);
194
-
195
- if (suggested != null) {
196
- if (parsedCompilerOptions[option] === undefined) {
197
- appTsConfig = immer(appTsConfig, (config) => {
198
- config.compilerOptions[option] = suggested;
199
- });
200
- messages.push(`${coloredOption} to be ${chalk.bold('suggested')} value: ${chalk.cyan.bold(suggested)} (this can be changed)`);
201
- }
202
- } else if (parsedCompilerOptions[option] !== valueToCheck) {
203
- appTsConfig = immer(appTsConfig, (config) => {
204
- config.compilerOptions[option] = value;
205
- });
206
- messages.push(
207
- `${coloredOption} ${chalk.bold(
208
- valueToCheck == null ? 'must not' : 'must',
209
- )} be ${valueToCheck == null ? 'set' : chalk.cyan.bold(value)}${reason != null ? ` (${reason})` : ''}`,
210
- );
211
- }
212
- }
213
-
214
- // tsconfig will have the merged "include" and "exclude" by this point
215
- if (parsedTsConfig.include == null) {
216
- appTsConfig = immer(appTsConfig, (config) => {
217
- config.include = ['src'];
218
- });
219
- messages.push(`${chalk.cyan('include')} should be ${chalk.cyan.bold('src')}`);
220
- }
221
-
222
- const types = appTsConfig.compilerOptions.types || (appTsConfig.compilerOptions.types = []);
223
- const importSource = appTsConfig.compilerOptions.jsxImportSource;
224
-
225
- if (!types.includes('@reactunity/scripts/main')) {
226
- types.push('@reactunity/scripts/main');
227
- messages.push(`${chalk.cyan('types')} should include ${chalk.cyan.bold('@reactunity/scripts/main')}`);
228
- }
229
-
230
- if (
231
- !['@reactunity/scripts/ugui', '@reactunity/scripts/uitoolkit', '@reactunity/scripts/editor', '@reactunity/scripts/tests'].includes(
232
- importSource,
233
- )
234
- ) {
235
- appTsConfig.compilerOptions.jsxImportSource = '@reactunity/scripts/ugui';
236
- messages.push(
237
- `${chalk.cyan('jsxImportSource')} should include a valid platform target. Assuming ${chalk.cyan.bold('@reactunity/scripts/ugui')}`,
238
- );
239
- }
240
-
241
- // Deprecate `react-unity-scripts` types
242
- if (fs.existsSync(paths.appTypeDeclarations)) {
243
- messages.push(
244
- `References to ${chalk.cyan('@reactunity/scripts')} does not have to be in ${chalk.cyan.bold('react-unity.d.ts')} anymore. It can be removed.`,
245
- );
246
- }
247
-
248
- if (messages.length > 0) {
249
- if (firstTimeSetup) {
250
- console.log(chalk.bold('Your', chalk.cyan('tsconfig.json'), 'has been populated with default values.'));
251
- console.log();
252
- } else {
253
- console.warn(chalk.bold('The following changes are being made to your', chalk.cyan('tsconfig.json'), 'file:'));
254
- messages.forEach((message) => {
255
- console.warn(` - ${message}`);
256
- });
257
- console.warn();
258
- }
259
- writeJson(paths.appTsConfig, appTsConfig);
260
- }
261
- }
262
-
263
- module.exports = verifyTypeScriptSetup;