Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
fix(website): update hash value if problem occurs due to ts version error#11292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
94b1b7c75c44e795ba0c010d5b0534bedfaFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -7,7 +7,7 @@ import semverSatisfies from 'semver/functions/satisfies'; | ||
| import type { createTypeScriptSandbox } from '../../vendor/sandbox'; | ||
| import type { CreateLinter } from '../linter/createLinter'; | ||
| import type { PlaygroundSystem } from '../linter/types'; | ||
| import type {ConfigModel,RuleDetails } from '../types'; | ||
| import type { CommonEditorProps } from './types'; | ||
| import rootPackageJson from '../../../../../package.json'; | ||
| @@ -23,6 +23,7 @@ export interface SandboxServicesProps { | ||
| ruleDetails: RuleDetails[], | ||
| tsVersions: readonly string[], | ||
| ) => void; | ||
| readonly setState: (value: Partial<ConfigModel>) => void; | ||
| readonly ts: string; | ||
| } | ||
| @@ -34,6 +35,23 @@ export interface SandboxServices { | ||
| webLinter: CreateLinter; | ||
| } | ||
| const checkUseSupportedTypescriptVersion = async (tsVersion: string) => { | ||
| const supportedVersionsResponse = await fetch( | ||
| 'https://typescript.azureedge.net/indexes/releases.json', | ||
| ); | ||
| if (supportedVersionsResponse.ok) { | ||
| const supportedVersions = (await supportedVersionsResponse.json()) as { | ||
| versions: string[]; | ||
| }; | ||
| const filteredVersions = supportedVersions.versions.filter(item => | ||
| semverSatisfies(item, rootPackageJson.devDependencies.typescript), | ||
| ); | ||
| return filteredVersions.includes(tsVersion); | ||
| } | ||
| return false; | ||
| }; | ||
| export const useSandboxServices = ( | ||
| props: CommonEditorProps & SandboxServicesProps, | ||
| ): Error | SandboxServices | undefined => { | ||
| @@ -44,107 +62,122 @@ export const useSandboxServices = ( | ||
| useEffect(() => { | ||
| let sandboxInstance: SandboxInstance | undefined; | ||
| checkUseSupportedTypescriptVersion(props.ts) | ||
| .then(res => { | ||
| if (!res) { | ||
| props.setState({ ts: process.env.TS_VERSION }); | ||
| } | ||
| }) | ||
| .then(() => { | ||
| sandboxSingleton(props.ts) | ||
| .then(async ({ lintUtils, main, sandboxFactory }) => { | ||
Comment on lines +71 to +73 Member There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. [Refactor] The code already has an | ||
| const compilerOptions = createCompilerOptions(); | ||
| sandboxInstance = sandboxFactory.createTypeScriptSandbox( | ||
| { | ||
| acquireTypes: true, | ||
| compilerOptions: | ||
| compilerOptions as Monaco.languages.typescript.CompilerOptions, | ||
| domID: editorEmbedId, | ||
| monacoSettings: { | ||
| autoIndent: 'full', | ||
| fontSize: 13, | ||
| formatOnPaste: true, | ||
| formatOnType: true, | ||
| hover: { above: false }, | ||
| minimap: { enabled: false }, | ||
| scrollBeyondLastLine: false, | ||
| smoothScrolling: true, | ||
| wordWrap: 'off', | ||
| wrappingIndent: 'same', | ||
| }, | ||
| text: props.code, | ||
| }, | ||
| main, | ||
| window.ts, | ||
| ); | ||
| sandboxInstance.monaco.editor.setTheme( | ||
| colorMode === 'dark' ? 'vs-dark' : 'vs-light', | ||
| ); | ||
| sandboxInstance.monaco.languages.registerInlayHintsProvider( | ||
| sandboxInstance.language, | ||
| createTwoslashInlayProvider(sandboxInstance), | ||
| ); | ||
| const system = createFileSystem(props, sandboxInstance.tsvfs); | ||
| // Write files in vfs when a model is created in the editor (this is used only for ATA types) | ||
| sandboxInstance.monaco.editor.onDidCreateModel(model => { | ||
| if (!model.uri.path.includes('node_modules')) { | ||
| return; | ||
| } | ||
| const path = model.uri.path.replace('/file:///', '/'); | ||
| system.writeFile(path, model.getValue()); | ||
| }); | ||
| // Delete files in vfs when a model is disposed in the editor (this is used only for ATA types) | ||
| sandboxInstance.monaco.editor.onWillDisposeModel(model => { | ||
| if (!model.uri.path.includes('node_modules')) { | ||
| return; | ||
| } | ||
| const path = model.uri.path.replace('/file:///', '/'); | ||
| system.deleteFile(path); | ||
| }); | ||
| // Load the lib files from typescript to vfs (eg. es2020.d.ts) | ||
| const worker = await sandboxInstance.getWorkerProcess(); | ||
| if (worker.getLibFiles) { | ||
| const libs = await worker.getLibFiles(); | ||
| for (const [key, value] of Object.entries(libs)) { | ||
| system.writeFile(`/${key}`, value); | ||
| } | ||
| } | ||
| window.system = system; | ||
| window.esquery = lintUtils.esquery; | ||
| window.visitorKeys = lintUtils.visitorKeys; | ||
| const webLinter = createLinter( | ||
| system, | ||
| lintUtils, | ||
| sandboxInstance.tsvfs, | ||
| ); | ||
| onLoaded( | ||
| [...webLinter.rules.values()], | ||
| [ | ||
| ...new Set([ | ||
| window.ts.version, | ||
| ...sandboxInstance.supportedVersions, | ||
| ]), | ||
| ] | ||
| .filter(item => | ||
| semverSatisfies( | ||
| item, | ||
| rootPackageJson.devDependencies.typescript, | ||
| ), | ||
| ) | ||
| .sort((a, b) => b.localeCompare(a)), | ||
| ); | ||
| setServices({ | ||
| sandboxInstance, | ||
| system, | ||
| webLinter, | ||
| }); | ||
| }) | ||
| .catch((err: unknown) => { | ||
| if (err instanceof Error) { | ||
| setServices(err); | ||
| } else { | ||
| setServices(new Error(String(err))); | ||
| } | ||
| }); | ||
| }) | ||
| .catch((err: unknown) => { | ||
| console.error(err); | ||
| }); | ||
| return (): void => { | ||
| if (!sandboxInstance) { | ||
| return; | ||
Uh oh!
There was an error while loading.Please reload this page.