online-editor 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +51 -2
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  ## ✨ 特性
16
16
 
17
- - 🎨 **多框架支持** - 支持 Vue 2/3、React、Svelte、Solid、HTML、JavaScript、TypeScript
17
+ - 🎨 **多框架支持** - 支持 Vue 2/3、React、Svelte、Solid、HTML、JavaScript、TypeScript
18
18
  - 🚀 **Monaco Editor** - 基于 VS Code 同款编辑器,提供专业的代码编辑体验
19
19
  - 📱 **响应式布局** - 同时支持桌面端和移动端
20
20
  - 🎯 **即时预览** - 实时编译运行,所见即所得
@@ -118,7 +118,7 @@ createApp(App).mount('#app');`,
118
118
  ### AppType 类型
119
119
 
120
120
  ```typescript
121
- type AppType = 'vue' | 'vue2' | 'vue3' | 'react' | 'svelte' | 'solid' | 'html' | 'javascript' | 'js' | 'typescript' | 'ts';
121
+ type AppType = 'vue' | 'vue2' | 'vue3' | 'react' | 'svelte' | 'solid' | 'html' | 'javascript' | 'js' | 'typescript' | 'ts';
122
122
  ```
123
123
 
124
124
  ### Control 类型
@@ -127,6 +127,55 @@ type AppType = 'vue' | 'vue2' | 'vue3' | 'react' | 'svelte' | 'solid' | 'html' |
127
127
  type Control = 'refresh' | 'copy' | 'share' | 'docs' | 'github' | 'format' | 'console';
128
128
  ```
129
129
 
130
+ ## 🏗️ 生产环境构建(Worker 兼容)
131
+
132
+ `online-editor` 依赖 Monaco 等能力,会用到 Web Worker。在某些静态部署场景(相对路径、非根路径、CDN/反向代理等)下,构建产物里的 Worker chunk 地址可能解析不符合预期,导致 Worker 404 或无法启动。
133
+
134
+ 一个通用做法是在构建完成后对产物做一次“构建后补丁”:把特定形式的 `new Worker(new URL(..., import.meta.url))` 改写为 `Blob + importScripts(...)`,让 Worker 使用 `location.href` 来解析并加载对应的 worker chunk。
135
+
136
+ ```js
137
+ const fs = require('fs');
138
+ const path = require('path');
139
+
140
+ const distDir = process.argv[2]
141
+ ? path.resolve(process.cwd(), process.argv[2])
142
+ : path.resolve(process.cwd(), 'dist');
143
+
144
+ function replaceWorkerUrls(filePath) {
145
+ const data = fs.readFileSync(filePath, 'utf8');
146
+ const workerPattern =
147
+ /new Worker\(""\+new URL\(""\+new URL\("([^"]+\.js)",import\.meta\.url\)\.href,self\.location\)\.href\)/g;
148
+
149
+ const newData = data.replace(workerPattern, (match, workerPath) => {
150
+ return `new Worker(URL.createObjectURL(new Blob([\`importScripts("\${new URL("${workerPath}",location.href).href}")\`],{type:"text/javascript"})))`;
151
+ });
152
+
153
+ if (newData !== data) fs.writeFileSync(filePath, newData, 'utf8');
154
+ }
155
+
156
+ function traverseDirectory(dir) {
157
+ const files = fs.readdirSync(dir);
158
+ for (const file of files) {
159
+ const filePath = path.join(dir, file);
160
+ const stat = fs.statSync(filePath);
161
+ if (stat.isDirectory()) traverseDirectory(filePath);
162
+ else if (file.endsWith('.js')) replaceWorkerUrls(filePath);
163
+ }
164
+ }
165
+
166
+ traverseDirectory(distDir);
167
+ ```
168
+
169
+ 构建命令示例:
170
+
171
+ ```json
172
+ {
173
+ "scripts": {
174
+ "build": "vite build && node ./scripts/patch-workers.cjs dist"
175
+ }
176
+ }
177
+ ```
178
+
130
179
  ## 📄 License
131
180
 
132
181
  [MIT](./LICENSE)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "online-editor",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "浏览器代码编辑器 & 可视化运行组件,支持 Vue/React 等框架的在线编辑与预览",
5
5
  "keywords": [
6
6
  "code-editor",