Skip to content

Commit

Permalink
fix(jsx-explorer): support browser env
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Sep 6, 2024
1 parent 4538374 commit 455ae71
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 679 deletions.
8 changes: 5 additions & 3 deletions packages/jsx-explorer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@
"dependencies": {
"@babel/core": "^7.25.2",
"@vue/babel-plugin-jsx": "workspace:*",
"assert": "^2.1.0",
"monaco-editor": "^0.51.0",
"pathe": "^1.1.2",
"vue": "^3.5.3"
},
"devDependencies": {
"@vitejs/plugin-vue-jsx": "^4.0.1",
"vite": "~5.4.3",
"vite-plugin-monaco-editor": "^1.1.0",
"vite-plugin-node-polyfills": "^0.22.0"
"terser": "^5.31.6",
"unplugin-replace": "^0.3.3",
"vite": "^5.4.3"
}
}
13 changes: 13 additions & 0 deletions packages/jsx-explorer/src/editor.worker.ts
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions packages/jsx-explorer/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
2 changes: 2 additions & 0 deletions packages/jsx-explorer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
compilerOptions,
initOptions,
} from './options';
import './editor.worker';
import './index.css';

main();
Expand Down Expand Up @@ -100,6 +101,7 @@ const App = defineComponent((props) => <div>Hello World</div>)`,
console.log('AST', res.ast!);
output.setValue(res.code!);
} else {
console.error(err);
output.setValue(err.message!);
}
}
Expand Down
21 changes: 13 additions & 8 deletions packages/jsx-explorer/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import { defineConfig } from 'vite';
import { nodePolyfills } from 'vite-plugin-node-polyfills';
import VueJSX from '@vitejs/plugin-vue-jsx';
import MonacoEditorPlugin from 'vite-plugin-monaco-editor';
import Replace from 'unplugin-replace/vite';

export default defineConfig({
build: {
minify: 'terser',
},
resolve: {
alias: {
'@vue/babel-plugin-jsx': '@vue/babel-plugin-jsx/src/index.ts',
path: 'pathe',
},
},
plugins: [
VueJSX(),
// @ts-expect-error
(MonacoEditorPlugin.default as typeof MonacoEditorPlugin)({
languageWorkers: ['editorWorkerService', 'typescript'],
}),
nodePolyfills({
globals: { process: true },
Replace({
values: {
'process.env.BABEL_TYPES_8_BREAKING': 'false',
'process.env.BABEL_ENV': '"development"',
'process.env.NODE_NDEBUG': 'true',
'process.env.NODE_DEBUG': 'false',
'Buffer.isBuffer': 'function isBuffer() { return false; }',
},
}),
],
});
Loading

0 comments on commit 455ae71

Please sign in to comment.