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