Skip to content

Commit

Permalink
chore(website): pass user defined compilerOptions to linter (#5080)
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 committed May 26, 2022
1 parent 0bfab6c commit 594116e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
9 changes: 2 additions & 7 deletions packages/website/src/components/editor/LoadedEditor.tsx
Expand Up @@ -8,6 +8,7 @@ import type { WebLinter } from '../linter/WebLinter';
import { debounce } from '../lib/debounce';
import { lintCode, LintCodeAction } from '../linter/lintCode';
import { createProvideCodeActions } from './createProvideCodeActions';
import { createCompilerOptions } from '@site/src/components/editor/config';
import { parseMarkers } from '../linter/utils';

export interface LoadedEditorProps extends CommonEditorProps {
Expand Down Expand Up @@ -39,13 +40,7 @@ export const LoadedEditor: React.FC<LoadedEditorProps> = ({
const fixes = useRef(new Map<string, LintCodeAction[]>()).current;

useEffect(() => {
const config = {
noResolve: true,
target: main.languages.typescript.ScriptTarget.ESNext,
module: main.languages.typescript.ModuleKind.ESNext,
...tsConfig,
jsx: jsx ? main.languages.typescript.JsxEmit.React : undefined,
};
const config = createCompilerOptions(jsx, tsConfig);

webLinter.updateOptions(config);
sandboxInstance.setCompilerSettings(config);
Expand Down
16 changes: 16 additions & 0 deletions packages/website/src/components/editor/config.ts
@@ -0,0 +1,16 @@
import type Monaco from 'monaco-editor';

export function createCompilerOptions(
jsx = false,
tsConfig: Record<string, unknown> = {},
): Monaco.languages.typescript.CompilerOptions {
return {
noResolve: true,
lib: ['es2021', 'esnext'],
// ts and monaco has different type as monaco types are not changing base on ts version
target: window.ts.ScriptTarget.ESNext as number,
module: window.ts.ModuleKind.ESNext as number,
...tsConfig,
jsx: jsx ? window.ts.JsxEmit.Preserve : window.ts.JsxEmit.None,
};
}
9 changes: 2 additions & 7 deletions packages/website/src/components/editor/useSandboxServices.ts
Expand Up @@ -12,6 +12,7 @@ import { WebLinter } from '../linter/WebLinter';
import { sandboxSingleton } from './loadSandbox';
import { editorEmbedId } from './EditorEmbed';
import { useColorMode } from '@docusaurus/theme-common';
import { createCompilerOptions } from '@site/src/components/editor/config';

export interface SandboxServicesProps {
readonly jsx?: boolean;
Expand Down Expand Up @@ -51,13 +52,7 @@ export const useSandboxServices = (

sandboxSingleton(props.ts)
.then(async ({ main, sandboxFactory, ts, lintUtils }) => {
const compilerOptions: Monaco.languages.typescript.CompilerOptions = {
noResolve: true,
target: main.languages.typescript.ScriptTarget.ESNext,
jsx: props.jsx ? main.languages.typescript.JsxEmit.React : undefined,
lib: ['es2021', 'esnext'],
module: main.languages.typescript.ModuleKind.ESNext,
};
const compilerOptions = createCompilerOptions(props.jsx);

const sandboxConfig: Partial<SandboxConfig> = {
text: '',
Expand Down

0 comments on commit 594116e

Please sign in to comment.