Fix(Preview): Fixed code pollution bug

Methods and variables in base and main code are isolated to solve the problem of code pollution.
This commit is contained in:
2024-09-09 16:24:22 +08:00
parent bba90c6783
commit f111c83817
7 changed files with 129 additions and 131 deletions

10
package-lock.json generated
View File

@@ -1,12 +1,12 @@
{ {
"name": "oxygen-ui", "name": "oxygen-ui",
"version": "1.0.0-SNAPSHOT", "version": "1.0.1-SNAPSHOT",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "oxygen-ui", "name": "oxygen-ui",
"version": "1.0.0-SNAPSHOT", "version": "1.0.1-SNAPSHOT",
"dependencies": { "dependencies": {
"@ant-design/icons": "^5.3.7", "@ant-design/icons": "^5.3.7",
"@dnd-kit/core": "^6.1.0", "@dnd-kit/core": "^6.1.0",
@@ -2153,9 +2153,9 @@
} }
}, },
"node_modules/acorn": { "node_modules/acorn": {
"version": "8.11.3", "version": "8.12.1",
"resolved": "https://registry.npmmirror.com/acorn/-/acorn-8.11.3.tgz", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.12.1.tgz",
"integrity": "sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==", "integrity": "sha512-tcpGyI9zbizT9JbV6oYE477V6mTlXvvi0T0G3SNIYE2apm/G5huBa1+K89VGeovbg+jycCrfhl3ADxErOuO6Jg==",
"dev": true, "dev": true,
"bin": { "bin": {
"acorn": "bin/acorn" "acorn": "bin/acorn"

View File

@@ -32,7 +32,7 @@ const Preview = ({
Compiler.compile(files, importMap, entryPoint) Compiler.compile(files, importMap, entryPoint)
.then((result) => { .then((result) => {
setCompiledCode( setCompiledCode(
`${preExpansionCode}\n${result.outputFiles[0].text}\n${postExpansionCode}` `(()=>{${preExpansionCode}})();\n(()=>{${result.outputFiles[0].text}})();\n(()=>{${postExpansionCode}})();`
) )
setErrorMsg('') setErrorMsg('')
}) })

View File

@@ -2,7 +2,7 @@ import esbuild, { Loader, OnLoadArgs, Plugin, PluginBuild } from 'esbuild-wasm'
import localforage from 'localforage' import localforage from 'localforage'
import axios from 'axios' import axios from 'axios'
import { IFiles, IImportMap } from '@/components/Playground/shared' import { IFiles, IImportMap } from '@/components/Playground/shared'
import { cssToJs, jsonToJs, addReactImport } from '@/components/Playground/files' import { addReactImport, cssToJs, jsonToJs } from '@/components/Playground/files'
class Compiler { class Compiler {
private init = false private init = false
@@ -73,8 +73,7 @@ class Compiler {
files: IFiles, files: IFiles,
importMap: IImportMap, importMap: IImportMap,
entryPoint: string entryPoint: string
): Plugin => { ): Plugin => ({
return {
name: 'file-resolver-plugin', name: 'file-resolver-plugin',
setup: (build: PluginBuild) => { setup: (build: PluginBuild) => {
build.onResolve({ filter: /.*/ }, (args: esbuild.OnResolveArgs) => { build.onResolve({ filter: /.*/ }, (args: esbuild.OnResolveArgs) => {
@@ -216,8 +215,7 @@ class Compiler {
return result return result
}) })
} }
} })
}
} }
export default new Compiler() export default new Compiler()

View File

@@ -27,7 +27,7 @@ const Execute = () => {
const output = result.outputFiles[0].text const output = result.outputFiles[0].text
setCompiledCode('') setCompiledCode('')
setTimeout(() => { setTimeout(() => {
setCompiledCode(`${output}\n${baseDist}`) setCompiledCode(`(() => {${output}})();\n(() => {${baseDist}})();`)
}, 100) }, 100)
}) })
.catch((reason) => { .catch((reason) => {

View File

@@ -145,7 +145,7 @@ const Create = () => {
.compile(files, importMap, template.entryPoint) .compile(files, importMap, template.entryPoint)
.then((result) => { .then((result) => {
const output = result.outputFiles[0].text const output = result.outputFiles[0].text
setCompiledCode(`${output}\n${baseDist}`) setCompiledCode(`(() => {${output}})();\n(() => {${baseDist}})();`)
}) })
.catch((reason) => { .catch((reason) => {
void message.error(`编译失败:${reason}`) void message.error(`编译失败:${reason}`)

View File

@@ -34,7 +34,7 @@ const View = () => {
const output = result.outputFiles[0].text const output = result.outputFiles[0].text
setCompiledCode('') setCompiledCode('')
setTimeout(() => { setTimeout(() => {
setCompiledCode(`${output}\n${baseDist}`) setCompiledCode(`(() => {${output}})();\n(() => {${baseDist}})();`)
}, 100) }, 100)
}) })
.catch((reason) => { .catch((reason) => {
@@ -49,7 +49,7 @@ const View = () => {
const dist = base64ToStr(toolVo.dist.data!) const dist = base64ToStr(toolVo.dist.data!)
setCompiledCode('') setCompiledCode('')
setTimeout(() => { setTimeout(() => {
setCompiledCode(`${dist}\n${baseDist}`) setCompiledCode(`(() => {${dist}})();\n(() => {${baseDist}})();`)
}, 100) }, 100)
} catch (e) { } catch (e) {
void message.error('载入工具失败') void message.error('载入工具失败')