Compare commits
11 Commits
release/1.
...
254c5ab48f
| Author | SHA1 | Date | |
|---|---|---|---|
|
254c5ab48f
|
|||
|
89cf48e449
|
|||
|
d6d5cd927c
|
|||
|
72ab390756
|
|||
|
92115d3faa
|
|||
|
21eaee22f7
|
|||
|
44e32ce4f7
|
|||
|
6302ec6ab3
|
|||
|
1c45e7250b
|
|||
|
51ee15749e
|
|||
|
bba90c6783
|
10
package-lock.json
generated
10
package-lock.json
generated
@@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "oxygen-ui",
|
||||
"version": "1.0.0-SNAPSHOT",
|
||||
"version": "1.0.1-SNAPSHOT",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "oxygen-ui",
|
||||
"version": "1.0.0-SNAPSHOT",
|
||||
"version": "1.0.1-SNAPSHOT",
|
||||
"dependencies": {
|
||||
"@ant-design/icons": "^5.3.7",
|
||||
"@dnd-kit/core": "^6.1.0",
|
||||
@@ -2153,9 +2153,9 @@
|
||||
}
|
||||
},
|
||||
"node_modules/acorn": {
|
||||
"version": "8.11.3",
|
||||
"resolved": "https://registry.npmmirror.com/acorn/-/acorn-8.11.3.tgz",
|
||||
"integrity": "sha512-Y9rRfJG5jcKOE0CLisYbojUjIrIEE7AGMzA/Sm4BslANhbS+cDMpgBdcPT91oJ7OuJ9hYJBx59RjbhxVnrF8Xg==",
|
||||
"version": "8.12.1",
|
||||
"resolved": "https://registry.npmjs.org/acorn/-/acorn-8.12.1.tgz",
|
||||
"integrity": "sha512-tcpGyI9zbizT9JbV6oYE477V6mTlXvvi0T0G3SNIYE2apm/G5huBa1+K89VGeovbg+jycCrfhl3ADxErOuO6Jg==",
|
||||
"dev": true,
|
||||
"bin": {
|
||||
"acorn": "bin/acorn"
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
"name": "oxygen-ui",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"version": "1.0.0",
|
||||
"version": "1.0.1-SNAPSHOT",
|
||||
"description": "Oxygen Toolbox browser version",
|
||||
"author": {
|
||||
"author": {
|
||||
"name": "FatttSnake",
|
||||
"email": "fatttsnake@fatweb.top",
|
||||
"url": "https://fatweb.top"
|
||||
|
||||
@@ -32,7 +32,7 @@ const Preview = ({
|
||||
Compiler.compile(files, importMap, entryPoint)
|
||||
.then((result) => {
|
||||
setCompiledCode(
|
||||
`${preExpansionCode}\n${result.outputFiles[0].text}\n${postExpansionCode}`
|
||||
`(()=>{${preExpansionCode}})();\n(()=>{${result.outputFiles[0].text}})();\n(()=>{${postExpansionCode}})();`
|
||||
)
|
||||
setErrorMsg('')
|
||||
})
|
||||
|
||||
@@ -2,7 +2,7 @@ import MonacoEditor from '@monaco-editor/react'
|
||||
import { Loader } from 'esbuild-wasm'
|
||||
import '@/components/Playground/Output/Transform/transform.scss'
|
||||
import { IFile, ITheme } from '@/components/Playground/shared'
|
||||
import { cssToJs, jsonToJs, addReactImport } from '@/components/Playground/files'
|
||||
import { cssToJs, jsonToJs } from '@/components/Playground/files'
|
||||
import Compiler from '@/components/Playground/compiler'
|
||||
import { MonacoEditorConfig } from '@/components/Playground/CodeEditor/Editor/monacoConfig'
|
||||
|
||||
@@ -16,12 +16,7 @@ const Transform = ({ file, theme }: OutputProps) => {
|
||||
const [errorMsg, setErrorMsg] = useState('')
|
||||
|
||||
const compile = (code: string, loader: Loader) => {
|
||||
let _code = code
|
||||
if (['jsx', 'tsx'].includes(loader)) {
|
||||
_code = addReactImport(code)
|
||||
}
|
||||
|
||||
Compiler?.transform(_code, loader)
|
||||
Compiler?.transform(code, loader)
|
||||
.then((value) => {
|
||||
setCompiledCode(value.code)
|
||||
setErrorMsg('')
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import esbuild, { Loader, OnLoadArgs, Plugin, PluginBuild } from 'esbuild-wasm'
|
||||
import localforage from 'localforage'
|
||||
import axios from 'axios'
|
||||
import { IFiles, IImportMap } from '@/components/Playground/shared'
|
||||
import { cssToJs, jsonToJs, addReactImport } from '@/components/Playground/files'
|
||||
import { IFile, IFiles, IImportMap } from '@/components/Playground/shared'
|
||||
import { addReactImport, cssToJs, jsonToJs } from '@/components/Playground/files'
|
||||
|
||||
class Compiler {
|
||||
private init = false
|
||||
@@ -61,7 +61,7 @@ class Compiler {
|
||||
format: 'esm',
|
||||
metafile: true,
|
||||
write: false,
|
||||
plugins: [this.fileResolverPlugin(files, importMap, entryPoint)]
|
||||
plugins: [this.fileResolverPlugin(files, importMap)]
|
||||
})
|
||||
})
|
||||
|
||||
@@ -69,155 +69,128 @@ class Compiler {
|
||||
void esbuild.stop()
|
||||
}
|
||||
|
||||
private fileResolverPlugin = (
|
||||
files: IFiles,
|
||||
importMap: IImportMap,
|
||||
entryPoint: string
|
||||
): Plugin => {
|
||||
return {
|
||||
name: 'file-resolver-plugin',
|
||||
setup: (build: PluginBuild) => {
|
||||
build.onResolve({ filter: /.*/ }, (args: esbuild.OnResolveArgs) => {
|
||||
if (entryPoint === args.path) {
|
||||
return {
|
||||
namespace: 'OxygenToolbox',
|
||||
path: args.path
|
||||
}
|
||||
private fileResolverPlugin = (files: IFiles, importMap: IImportMap): Plugin => ({
|
||||
name: 'file-resolver-plugin',
|
||||
setup: (build: PluginBuild) => {
|
||||
build.onResolve({ filter: /.*/ }, (args: esbuild.OnResolveArgs) => {
|
||||
if (args.kind === 'entry-point') {
|
||||
return {
|
||||
namespace: 'oxygen',
|
||||
path: args.path
|
||||
}
|
||||
if (args.path.startsWith('./') && files[args.path.substring(2)]) {
|
||||
return {
|
||||
namespace: 'OxygenToolbox',
|
||||
path: args.path.substring(2)
|
||||
}
|
||||
}
|
||||
if (args.path.startsWith('./') && files[`${args.path.substring(2)}.tsx`]) {
|
||||
return {
|
||||
namespace: 'OxygenToolbox',
|
||||
path: `${args.path.substring(2)}.tsx`
|
||||
}
|
||||
}
|
||||
if (args.path.startsWith('./') && files[`${args.path.substring(2)}.jsx`]) {
|
||||
return {
|
||||
namespace: 'OxygenToolbox',
|
||||
path: `${args.path.substring(2)}.jsx`
|
||||
}
|
||||
}
|
||||
if (args.path.startsWith('./') && files[`${args.path.substring(2)}.ts`]) {
|
||||
return {
|
||||
namespace: 'OxygenToolbox',
|
||||
path: `${args.path.substring(2)}.ts`
|
||||
}
|
||||
}
|
||||
if (args.path.startsWith('./') && files[`${args.path.substring(2)}.js`]) {
|
||||
return {
|
||||
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 (/^https?:\/\/.*/.test(args.path)) {
|
||||
return {
|
||||
namespace: 'default',
|
||||
path
|
||||
path: args.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) {
|
||||
if (
|
||||
args.path.startsWith('./') &&
|
||||
(!args.resolveDir.length || args.resolveDir in files)
|
||||
) {
|
||||
const suffix = ['', '.tsx', '.jsx', '.ts', '.js'].find((suffix) => {
|
||||
return files[`${args.path.substring(2)}${suffix}`]
|
||||
})
|
||||
if (suffix !== undefined) {
|
||||
return {
|
||||
loader: 'tsx',
|
||||
contents: addReactImport(files[entryPoint].value)
|
||||
namespace: 'oxygen',
|
||||
path: `${args.path.substring(2)}${suffix}`
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
if (['./', '../', '/'].some((prefix) => args.path.startsWith(prefix))) {
|
||||
return {
|
||||
namespace: 'default',
|
||||
path: new URL(args.path, args.resolveDir.substring(1)).href
|
||||
}
|
||||
}
|
||||
|
||||
const cached = await this.fileCache.getItem<esbuild.OnLoadResult>(args.path)
|
||||
|
||||
if (cached) {
|
||||
return cached
|
||||
let path = importMap.imports[args.path]
|
||||
let tempPath = args.path
|
||||
while (!path && tempPath.includes('/')) {
|
||||
tempPath = tempPath.substring(0, tempPath.lastIndexOf('/'))
|
||||
if (importMap.imports[tempPath]) {
|
||||
const suffix = args.path.replace(tempPath, '')
|
||||
const importUrl = new URL(importMap.imports[tempPath])
|
||||
path = `${importUrl.origin}${importUrl.pathname}${suffix}${importUrl.search}`
|
||||
}
|
||||
|
||||
const axiosResponse = await axios.get<string>(args.path)
|
||||
const result: esbuild.OnLoadResult = {
|
||||
loader: 'jsx',
|
||||
contents: axiosResponse.data,
|
||||
resolveDir: (axiosResponse.request as XMLHttpRequest).responseURL
|
||||
}
|
||||
if (!path) {
|
||||
throw Error(`Import '${args.path}' not found in Import Map`)
|
||||
}
|
||||
const pathUrl = new URL(path)
|
||||
const externals = pathUrl.searchParams.get('external')?.split(',') ?? []
|
||||
Object.keys(importMap.imports).forEach((item) => {
|
||||
if (!(item in externals)) {
|
||||
externals.push(item)
|
||||
}
|
||||
|
||||
await this.fileCache.setItem(args.path, result)
|
||||
|
||||
return result
|
||||
})
|
||||
}
|
||||
pathUrl.searchParams.set('external', externals.join(','))
|
||||
return {
|
||||
namespace: 'default',
|
||||
path: pathUrl.href
|
||||
}
|
||||
})
|
||||
|
||||
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({ namespace: 'oxygen', filter: /.*/ }, (args: OnLoadArgs) => {
|
||||
let file: IFile | undefined
|
||||
|
||||
void ['', '.tsx', '.jsx', '.ts', '.js'].forEach((suffix) => {
|
||||
file = file || files[`${args.path}${suffix}`]
|
||||
})
|
||||
if (file) {
|
||||
return {
|
||||
loader: (() => {
|
||||
switch (file.language) {
|
||||
case 'javascript':
|
||||
return 'jsx'
|
||||
default:
|
||||
return 'tsx'
|
||||
}
|
||||
})(),
|
||||
contents: addReactImport(file.value)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
build.onLoad({ filter: /.*/ }, async (args: OnLoadArgs) => {
|
||||
const cached = await this.fileCache.getItem<esbuild.OnLoadResult>(args.path)
|
||||
|
||||
if (cached) {
|
||||
return cached
|
||||
}
|
||||
|
||||
const axiosResponse = await axios.get<string>(args.path)
|
||||
const result: esbuild.OnLoadResult = {
|
||||
loader: 'js',
|
||||
contents: axiosResponse.data,
|
||||
resolveDir: args.path
|
||||
}
|
||||
|
||||
await this.fileCache.setItem(args.path, result)
|
||||
|
||||
return result
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export default new Compiler()
|
||||
|
||||
@@ -106,7 +106,7 @@ export const cssToJs = (file: IFile) => {
|
||||
}
|
||||
|
||||
export const addReactImport = (code: string) => {
|
||||
if (!/import\s+React/g.test(code)) {
|
||||
if (!/^\s*import\s+React\s+/g.test(code)) {
|
||||
return `import React from 'react';\n${code}`
|
||||
}
|
||||
return code
|
||||
|
||||
@@ -27,7 +27,7 @@ const Execute = () => {
|
||||
const output = result.outputFiles[0].text
|
||||
setCompiledCode('')
|
||||
setTimeout(() => {
|
||||
setCompiledCode(`${output}\n${baseDist}`)
|
||||
setCompiledCode(`(() => {${output}})();\n(() => {${baseDist}})();`)
|
||||
}, 100)
|
||||
})
|
||||
.catch((reason) => {
|
||||
|
||||
@@ -145,7 +145,7 @@ const Create = () => {
|
||||
.compile(files, importMap, template.entryPoint)
|
||||
.then((result) => {
|
||||
const output = result.outputFiles[0].text
|
||||
setCompiledCode(`${output}\n${baseDist}`)
|
||||
setCompiledCode(`(() => {${output}})();\n(() => {${baseDist}})();`)
|
||||
})
|
||||
.catch((reason) => {
|
||||
void message.error(`编译失败:${reason}`)
|
||||
|
||||
@@ -34,7 +34,7 @@ const View = () => {
|
||||
const output = result.outputFiles[0].text
|
||||
setCompiledCode('')
|
||||
setTimeout(() => {
|
||||
setCompiledCode(`${output}\n${baseDist}`)
|
||||
setCompiledCode(`(() => {${output}})();\n(() => {${baseDist}})();`)
|
||||
}, 100)
|
||||
})
|
||||
.catch((reason) => {
|
||||
@@ -49,7 +49,7 @@ const View = () => {
|
||||
const dist = base64ToStr(toolVo.dist.data!)
|
||||
setCompiledCode('')
|
||||
setTimeout(() => {
|
||||
setCompiledCode(`${dist}\n${baseDist}`)
|
||||
setCompiledCode(`(() => {${dist}})();\n(() => {${baseDist}})();`)
|
||||
}, 100)
|
||||
} catch (e) {
|
||||
void message.error('载入工具失败')
|
||||
|
||||
Reference in New Issue
Block a user