Skip to content

Commit 455ae71

Browse files
committedSep 6, 2024··
fix(jsx-explorer): support browser env
1 parent 4538374 commit 455ae71

File tree

6 files changed

+137
-679
lines changed

6 files changed

+137
-679
lines changed
 

‎packages/jsx-explorer/package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
"dependencies": {
1212
"@babel/core": "^7.25.2",
1313
"@vue/babel-plugin-jsx": "workspace:*",
14+
"assert": "^2.1.0",
1415
"monaco-editor": "^0.51.0",
16+
"pathe": "^1.1.2",
1517
"vue": "^3.5.3"
1618
},
1719
"devDependencies": {
1820
"@vitejs/plugin-vue-jsx": "^4.0.1",
19-
"vite": "~5.4.3",
20-
"vite-plugin-monaco-editor": "^1.1.0",
21-
"vite-plugin-node-polyfills": "^0.22.0"
21+
"terser": "^5.31.6",
22+
"unplugin-replace": "^0.3.3",
23+
"vite": "^5.4.3"
2224
}
2325
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
2+
import tsWorker from 'monaco-editor/esm/vs/language/typescript/ts.worker?worker';
3+
4+
// @ts-ignore
5+
self.MonacoEnvironment = {
6+
globalAPI: true,
7+
getWorker(_: any, label: string) {
8+
if (label === 'typescript' || label === 'javascript') {
9+
return new tsWorker();
10+
}
11+
return new editorWorker();
12+
},
13+
};

‎packages/jsx-explorer/src/env.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference types="vite/client" />

‎packages/jsx-explorer/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
compilerOptions,
1010
initOptions,
1111
} from './options';
12+
import './editor.worker';
1213
import './index.css';
1314

1415
main();
@@ -100,6 +101,7 @@ const App = defineComponent((props) => <div>Hello World</div>)`,
100101
console.log('AST', res.ast!);
101102
output.setValue(res.code!);
102103
} else {
104+
console.error(err);
103105
output.setValue(err.message!);
104106
}
105107
}

‎packages/jsx-explorer/vite.config.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import { defineConfig } from 'vite';
2-
import { nodePolyfills } from 'vite-plugin-node-polyfills';
32
import VueJSX from '@vitejs/plugin-vue-jsx';
4-
import MonacoEditorPlugin from 'vite-plugin-monaco-editor';
3+
import Replace from 'unplugin-replace/vite';
54

65
export default defineConfig({
6+
build: {
7+
minify: 'terser',
8+
},
79
resolve: {
810
alias: {
911
'@vue/babel-plugin-jsx': '@vue/babel-plugin-jsx/src/index.ts',
12+
path: 'pathe',
1013
},
1114
},
1215
plugins: [
1316
VueJSX(),
14-
// @ts-expect-error
15-
(MonacoEditorPlugin.default as typeof MonacoEditorPlugin)({
16-
languageWorkers: ['editorWorkerService', 'typescript'],
17-
}),
18-
nodePolyfills({
19-
globals: { process: true },
17+
Replace({
18+
values: {
19+
'process.env.BABEL_TYPES_8_BREAKING': 'false',
20+
'process.env.BABEL_ENV': '"development"',
21+
'process.env.NODE_NDEBUG': 'true',
22+
'process.env.NODE_DEBUG': 'false',
23+
'Buffer.isBuffer': 'function isBuffer() { return false; }',
24+
},
2025
}),
2126
],
2227
});

0 commit comments

Comments
 (0)
Please sign in to comment.