fix(jsx-explorer): support browser env

This commit is contained in:
三咲智子 Kevin Deng 2024-09-07 04:56:24 +08:00
parent 45383745b4
commit 455ae7182d
No known key found for this signature in database
6 changed files with 137 additions and 679 deletions

View File

@ -11,13 +11,15 @@
"dependencies": { "dependencies": {
"@babel/core": "^7.25.2", "@babel/core": "^7.25.2",
"@vue/babel-plugin-jsx": "workspace:*", "@vue/babel-plugin-jsx": "workspace:*",
"assert": "^2.1.0",
"monaco-editor": "^0.51.0", "monaco-editor": "^0.51.0",
"pathe": "^1.1.2",
"vue": "^3.5.3" "vue": "^3.5.3"
}, },
"devDependencies": { "devDependencies": {
"@vitejs/plugin-vue-jsx": "^4.0.1", "@vitejs/plugin-vue-jsx": "^4.0.1",
"vite": "~5.4.3", "terser": "^5.31.6",
"vite-plugin-monaco-editor": "^1.1.0", "unplugin-replace": "^0.3.3",
"vite-plugin-node-polyfills": "^0.22.0" "vite": "^5.4.3"
} }
} }

View File

@ -0,0 +1,13 @@
import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
import tsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker';
// @ts-ignore
self.MonacoEnvironment = {
globalAPI: true,
getWorker(_: any, label: string) {
if (label === 'typescript' || label === 'javascript') {
return new tsWorker();
}
return new editorWorker();
},
};

1
packages/jsx-explorer/src/env.d.ts vendored Normal file
View File

@ -0,0 +1 @@
/// <reference types="vite/client" />

View File

@ -9,6 +9,7 @@ import {
compilerOptions, compilerOptions,
initOptions, initOptions,
} from './options'; } from './options';
import './editor.worker';
import './index.css'; import './index.css';
main(); main();
@ -100,6 +101,7 @@ const App = defineComponent((props) => <div>Hello World</div>)`,
console.log('AST', res.ast!); console.log('AST', res.ast!);
output.setValue(res.code!); output.setValue(res.code!);
} else { } else {
console.error(err);
output.setValue(err.message!); output.setValue(err.message!);
} }
} }

View File

@ -1,22 +1,27 @@
import { defineConfig } from 'vite'; import { defineConfig } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
import VueJSX from '@vitejs/plugin-vue-jsx'; import VueJSX from '@vitejs/plugin-vue-jsx';
import MonacoEditorPlugin from 'vite-plugin-monaco-editor'; import Replace from 'unplugin-replace/vite';
export default defineConfig({ export default defineConfig({
build: {
minify: 'terser',
},
resolve: { resolve: {
alias: { alias: {
'@vue/babel-plugin-jsx': '@vue/babel-plugin-jsx/src/index.ts', '@vue/babel-plugin-jsx': '@vue/babel-plugin-jsx/src/index.ts',
path: 'pathe',
}, },
}, },
plugins: [ plugins: [
VueJSX(), VueJSX(),
// @ts-expect-error Replace({
(MonacoEditorPlugin.default as typeof MonacoEditorPlugin)({ values: {
languageWorkers: ['editorWorkerService', 'typescript'], 'process.env.BABEL_TYPES_8_BREAKING': 'false',
}), 'process.env.BABEL_ENV': '"development"',
nodePolyfills({ 'process.env.NODE_NDEBUG': 'true',
globals: { process: true }, 'process.env.NODE_DEBUG': 'false',
'Buffer.isBuffer': 'function isBuffer() { return false; }',
},
}), }),
], ],
}); });

File diff suppressed because it is too large Load Diff