Fix(Preview): Fixed code pollution bug
This commit is contained in:
@@ -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('')
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import localforage from 'localforage'
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
import esbuildWasmUrl from 'esbuild-wasm/esbuild.wasm?url'
|
import esbuildWasmUrl from 'esbuild-wasm/esbuild.wasm?url'
|
||||||
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
|
||||||
@@ -74,151 +74,149 @@ 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) => {
|
if (entryPoint === args.path) {
|
||||||
if (entryPoint === args.path) {
|
return {
|
||||||
return {
|
namespace: 'OxygenToolbox',
|
||||||
namespace: 'OxygenToolbox',
|
path: args.path
|
||||||
path: args.path
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (args.path.startsWith('./') && files[args.path.substring(2)]) {
|
}
|
||||||
return {
|
if (args.path.startsWith('./') && files[args.path.substring(2)]) {
|
||||||
namespace: 'OxygenToolbox',
|
return {
|
||||||
path: args.path.substring(2)
|
namespace: 'OxygenToolbox',
|
||||||
}
|
path: args.path.substring(2)
|
||||||
}
|
}
|
||||||
if (args.path.startsWith('./') && files[`${args.path.substring(2)}.tsx`]) {
|
}
|
||||||
return {
|
if (args.path.startsWith('./') && files[`${args.path.substring(2)}.tsx`]) {
|
||||||
namespace: 'OxygenToolbox',
|
return {
|
||||||
path: `${args.path.substring(2)}.tsx`
|
namespace: 'OxygenToolbox',
|
||||||
}
|
path: `${args.path.substring(2)}.tsx`
|
||||||
}
|
}
|
||||||
if (args.path.startsWith('./') && files[`${args.path.substring(2)}.jsx`]) {
|
}
|
||||||
return {
|
if (args.path.startsWith('./') && files[`${args.path.substring(2)}.jsx`]) {
|
||||||
namespace: 'OxygenToolbox',
|
return {
|
||||||
path: `${args.path.substring(2)}.jsx`
|
namespace: 'OxygenToolbox',
|
||||||
}
|
path: `${args.path.substring(2)}.jsx`
|
||||||
}
|
}
|
||||||
if (args.path.startsWith('./') && files[`${args.path.substring(2)}.ts`]) {
|
}
|
||||||
return {
|
if (args.path.startsWith('./') && files[`${args.path.substring(2)}.ts`]) {
|
||||||
namespace: 'OxygenToolbox',
|
return {
|
||||||
path: `${args.path.substring(2)}.ts`
|
namespace: 'OxygenToolbox',
|
||||||
}
|
path: `${args.path.substring(2)}.ts`
|
||||||
}
|
}
|
||||||
if (args.path.startsWith('./') && files[`${args.path.substring(2)}.js`]) {
|
}
|
||||||
return {
|
if (args.path.startsWith('./') && files[`${args.path.substring(2)}.js`]) {
|
||||||
namespace: 'OxygenToolbox',
|
return {
|
||||||
path: `${args.path.substring(2)}.js`
|
namespace: 'OxygenToolbox',
|
||||||
}
|
path: `${args.path.substring(2)}.js`
|
||||||
}
|
|
||||||
if (/\.\/.*\.css/.test(args.path) && !args.resolveDir) {
|
|
||||||
throw Error(`Css '${args.path}' not found`)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (/^https?:\/\/.*/.test(args.path)) {
|
|
||||||
return {
|
|
||||||
namespace: 'default',
|
|
||||||
path: args.path
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
args.path.includes('./') ||
|
|
||||||
args.path.includes('../') ||
|
|
||||||
args.path.startsWith('/')
|
|
||||||
) {
|
|
||||||
return {
|
|
||||||
namespace: 'default',
|
|
||||||
path: new URL(args.path, args.resolveDir.substring(1)).href
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const path = importMap.imports[args.path]
|
|
||||||
|
|
||||||
if (!path) {
|
|
||||||
throw Error(`Import '${args.path}' not found in Import Map`)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
if (/\.\/.*\.css/.test(args.path) && !args.resolveDir) {
|
||||||
|
throw Error(`Css '${args.path}' not found`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (/^https?:\/\/.*/.test(args.path)) {
|
||||||
return {
|
return {
|
||||||
namespace: 'default',
|
namespace: 'default',
|
||||||
path
|
path: args.path
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
build.onLoad({ filter: /.*\.css$/ }, (args: OnLoadArgs) => {
|
if (
|
||||||
const contents = cssToJs(files[args.path])
|
args.path.includes('./') ||
|
||||||
|
args.path.includes('../') ||
|
||||||
|
args.path.startsWith('/')
|
||||||
|
) {
|
||||||
return {
|
return {
|
||||||
loader: 'js',
|
namespace: 'default',
|
||||||
contents
|
path: new URL(args.path, args.resolveDir.substring(1)).href
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
build.onLoad({ filter: /.*\.json$/ }, (args: OnLoadArgs) => {
|
const path = importMap.imports[args.path]
|
||||||
const contents = jsonToJs(files[args.path])
|
|
||||||
|
if (!path) {
|
||||||
|
throw Error(`Import '${args.path}' not found in Import Map`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
namespace: 'default',
|
||||||
|
path
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
build.onLoad({ filter: /.*\.css$/ }, (args: OnLoadArgs) => {
|
||||||
|
const contents = cssToJs(files[args.path])
|
||||||
|
return {
|
||||||
|
loader: 'js',
|
||||||
|
contents
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
build.onLoad({ filter: /.*\.json$/ }, (args: OnLoadArgs) => {
|
||||||
|
const contents = jsonToJs(files[args.path])
|
||||||
|
return {
|
||||||
|
loader: 'js',
|
||||||
|
contents
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
build.onLoad({ filter: /.*/ }, async (args: OnLoadArgs) => {
|
||||||
|
if (entryPoint === args.path) {
|
||||||
return {
|
return {
|
||||||
loader: 'js',
|
loader: 'tsx',
|
||||||
contents
|
contents: addReactImport(files[entryPoint].value)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
build.onLoad({ filter: /.*/ }, async (args: OnLoadArgs) => {
|
if (files[args.path]) {
|
||||||
if (entryPoint === args.path) {
|
const contents = addReactImport(files[args.path].value)
|
||||||
|
if (args.path.endsWith('.jsx')) {
|
||||||
return {
|
return {
|
||||||
loader: 'tsx',
|
loader: 'jsx',
|
||||||
contents: addReactImport(files[entryPoint].value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (files[args.path]) {
|
|
||||||
const contents = addReactImport(files[args.path].value)
|
|
||||||
if (args.path.endsWith('.jsx')) {
|
|
||||||
return {
|
|
||||||
loader: 'jsx',
|
|
||||||
contents
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (args.path.endsWith('.ts')) {
|
|
||||||
return {
|
|
||||||
loader: 'ts',
|
|
||||||
contents
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (args.path.endsWith('.js')) {
|
|
||||||
return {
|
|
||||||
loader: 'js',
|
|
||||||
contents
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return {
|
|
||||||
loader: 'tsx',
|
|
||||||
contents
|
contents
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (args.path.endsWith('.ts')) {
|
||||||
const cached = await this.fileCache.getItem<esbuild.OnLoadResult>(args.path)
|
return {
|
||||||
|
loader: 'ts',
|
||||||
if (cached) {
|
contents
|
||||||
return cached
|
}
|
||||||
}
|
}
|
||||||
|
if (args.path.endsWith('.js')) {
|
||||||
const axiosResponse = await axios.get<string>(args.path)
|
return {
|
||||||
const result: esbuild.OnLoadResult = {
|
loader: 'js',
|
||||||
loader: 'jsx',
|
contents
|
||||||
contents: axiosResponse.data,
|
}
|
||||||
resolveDir: (axiosResponse.request as XMLHttpRequest).responseURL
|
|
||||||
}
|
}
|
||||||
|
return {
|
||||||
|
loader: 'tsx',
|
||||||
|
contents
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
await this.fileCache.setItem(args.path, result)
|
const cached = await this.fileCache.getItem<esbuild.OnLoadResult>(args.path)
|
||||||
|
|
||||||
return result
|
if (cached) {
|
||||||
})
|
return cached
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const axiosResponse = await axios.get<string>(args.path)
|
||||||
|
const result: esbuild.OnLoadResult = {
|
||||||
|
loader: 'jsx',
|
||||||
|
contents: axiosResponse.data,
|
||||||
|
resolveDir: (axiosResponse.request as XMLHttpRequest).responseURL
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.fileCache.setItem(args.path, result)
|
||||||
|
|
||||||
|
return result
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
export default new Compiler()
|
export default new Compiler()
|
||||||
|
|||||||
@@ -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) => {
|
||||||
|
|||||||
@@ -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}`)
|
||||||
|
|||||||
@@ -39,7 +39,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) => {
|
||||||
@@ -54,7 +54,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('载入工具失败')
|
||||||
|
|||||||
Reference in New Issue
Block a user