Optimize transform speed
This commit is contained in:
@@ -6,6 +6,7 @@ import { IFile, ITheme } from '@/components/Playground/shared'
|
|||||||
import Compiler from '@/components/Playground/compiler'
|
import Compiler from '@/components/Playground/compiler'
|
||||||
import { cssToJs, jsonToJs } from '@/components/Playground/files'
|
import { cssToJs, jsonToJs } from '@/components/Playground/files'
|
||||||
import { MonacoEditorConfig } from '@/components/Playground/CodeEditor/Editor/monacoConfig'
|
import { MonacoEditorConfig } from '@/components/Playground/CodeEditor/Editor/monacoConfig'
|
||||||
|
import { addReactImport } from '@/components/Playground/utils.ts'
|
||||||
|
|
||||||
interface OutputProps {
|
interface OutputProps {
|
||||||
file: IFile
|
file: IFile
|
||||||
@@ -23,8 +24,13 @@ const Preview: React.FC<OutputProps> = ({ file, theme }) => {
|
|||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const compile = (code: string, loader: Loader) => {
|
const compile = (code: string, loader: Loader) => {
|
||||||
|
let _code = code
|
||||||
|
if (['jsx', 'tsx'].includes(loader)) {
|
||||||
|
_code = addReactImport(code)
|
||||||
|
}
|
||||||
|
|
||||||
compiler.current
|
compiler.current
|
||||||
?.transform(code, loader)
|
?.transform(_code, loader)
|
||||||
.then((value) => {
|
.then((value) => {
|
||||||
setCompileCode(value.code)
|
setCompileCode(value.code)
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -9,11 +9,17 @@ class Compiler {
|
|||||||
void esbuild.initialize({ worker: true, wasmURL: wasm }).then(() => {
|
void esbuild.initialize({ worker: true, wasmURL: wasm }).then(() => {
|
||||||
this.init = true
|
this.init = true
|
||||||
})
|
})
|
||||||
} catch (e) {}
|
} catch (e) {
|
||||||
|
/* empty */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
transform = (code: string, loader: Loader) =>
|
transform = (code: string, loader: Loader) =>
|
||||||
new Promise<boolean>((resolve) => {
|
new Promise<boolean>((resolve) => {
|
||||||
|
if (this.init) {
|
||||||
|
resolve(true)
|
||||||
|
return
|
||||||
|
}
|
||||||
const timer = setInterval(() => {
|
const timer = setInterval(() => {
|
||||||
if (this.init) {
|
if (this.init) {
|
||||||
clearInterval(timer)
|
clearInterval(timer)
|
||||||
|
|||||||
@@ -13,3 +13,10 @@ export const getPlaygroundTheme = (): ITheme => {
|
|||||||
const isDarkTheme = JSON.parse(localStorage.getItem(STORAGE_DARK_THEME) || 'false') as ITheme
|
const isDarkTheme = JSON.parse(localStorage.getItem(STORAGE_DARK_THEME) || 'false') as ITheme
|
||||||
return isDarkTheme ? 'vs-dark' : 'light'
|
return isDarkTheme ? 'vs-dark' : 'light'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const addReactImport = (code: string) => {
|
||||||
|
if (!/import\s+React/g.test(code)) {
|
||||||
|
return `import React from 'react';\n${code}`
|
||||||
|
}
|
||||||
|
return code
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user