@blocklet/pages-kit-block-studio 0.4.32 → 0.4.34
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.
- package/lib/cjs/components/create-resource.js +2 -2
- package/lib/cjs/constants/new-block-template/index.js +2 -14
- package/lib/cjs/middlewares/init-block-studio-router.js +15 -21
- package/lib/cjs/middlewares/init-resource-router.js +34 -42
- package/lib/cjs/plugins/vite-plugin-block-studio.js +107 -86
- package/lib/cjs/plugins/vite-plugin-html-transform.js +13 -24
- package/lib/cjs/plugins/vite-plugin-remote-script-localizer.js +87 -103
- package/lib/cjs/tsconfig.tsbuildinfo +1 -1
- package/lib/cjs/utils/build-lib.js +62 -74
- package/lib/cjs/utils/generate-wrapper-code.js +366 -120
- package/lib/cjs/utils/helper.js +9 -19
- package/lib/esm/components/create-resource.js +2 -2
- package/lib/esm/constants/new-block-template/index.js +2 -14
- package/lib/esm/middlewares/init-block-studio-router.js +15 -21
- package/lib/esm/middlewares/init-resource-router.js +34 -42
- package/lib/esm/plugins/vite-plugin-block-studio.js +107 -86
- package/lib/esm/plugins/vite-plugin-html-transform.js +13 -24
- package/lib/esm/plugins/vite-plugin-remote-script-localizer.js +87 -103
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/build-lib.js +62 -74
- package/lib/esm/utils/generate-wrapper-code.js +366 -120
- package/lib/esm/utils/helper.js +9 -19
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
|
@@ -32,15 +32,6 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
32
32
|
return result;
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
45
36
|
exports.initRemoteScriptLocalizerPlugin = initRemoteScriptLocalizerPlugin;
|
|
46
37
|
/* eslint-disable no-console */
|
|
@@ -51,22 +42,22 @@ function initRemoteScriptLocalizerPlugin(options = {}) {
|
|
|
51
42
|
const { tempDir = 'temp/remote-scripts', maxConcurrent = 5, timeout = 30000 } = options;
|
|
52
43
|
let initialized = false;
|
|
53
44
|
const downloadQueue = [];
|
|
54
|
-
const initTempDir = () =>
|
|
45
|
+
const initTempDir = async () => {
|
|
55
46
|
try {
|
|
56
|
-
|
|
57
|
-
|
|
47
|
+
await (0, promises_1.rm)(tempDir, { recursive: true, force: true });
|
|
48
|
+
await (0, promises_1.mkdir)(tempDir, { recursive: true });
|
|
58
49
|
initialized = true;
|
|
59
50
|
}
|
|
60
51
|
catch (error) {
|
|
61
52
|
console.error('Failed to initialize temp directory:', error);
|
|
62
53
|
throw error;
|
|
63
54
|
}
|
|
64
|
-
}
|
|
65
|
-
const downloadWithTimeout = (url, ms) =>
|
|
55
|
+
};
|
|
56
|
+
const downloadWithTimeout = async (url, ms) => {
|
|
66
57
|
const controller = new AbortController();
|
|
67
58
|
const timeoutId = setTimeout(() => controller.abort(), ms);
|
|
68
59
|
try {
|
|
69
|
-
const response =
|
|
60
|
+
const response = await fetch(url, { signal: controller.signal });
|
|
70
61
|
clearTimeout(timeoutId);
|
|
71
62
|
return response;
|
|
72
63
|
}
|
|
@@ -74,48 +65,48 @@ function initRemoteScriptLocalizerPlugin(options = {}) {
|
|
|
74
65
|
clearTimeout(timeoutId);
|
|
75
66
|
throw error;
|
|
76
67
|
}
|
|
77
|
-
}
|
|
78
|
-
const downloadScript = (url) =>
|
|
79
|
-
var _a;
|
|
68
|
+
};
|
|
69
|
+
const downloadScript = async (url) => {
|
|
80
70
|
try {
|
|
81
|
-
const response =
|
|
71
|
+
const response = await downloadWithTimeout(url, timeout);
|
|
82
72
|
if (!response.ok) {
|
|
83
73
|
throw new Error(`Failed to fetch ${url}: ${response.statusText}`);
|
|
84
74
|
}
|
|
85
|
-
const content =
|
|
75
|
+
const content = await response.text();
|
|
86
76
|
const hash = (0, crypto_1.createHash)('md5').update(url).digest('hex').slice(0, 8);
|
|
87
|
-
const filename = `${hash}-${
|
|
77
|
+
const filename = `${hash}-${url
|
|
88
78
|
.split('/')
|
|
89
|
-
.pop()
|
|
79
|
+
.pop()
|
|
80
|
+
?.replace(/[^a-zA-Z0-9.-]/g, '_') || 'script.js'}`;
|
|
90
81
|
const localPath = path.join(tempDir, filename);
|
|
91
|
-
|
|
82
|
+
await (0, promises_1.writeFile)(localPath, content, 'utf-8');
|
|
92
83
|
return localPath;
|
|
93
84
|
}
|
|
94
85
|
catch (error) {
|
|
95
86
|
console.error(`Error downloading script from ${url}:`, error);
|
|
96
87
|
throw error;
|
|
97
88
|
}
|
|
98
|
-
}
|
|
99
|
-
const processDownloads = (urls) =>
|
|
89
|
+
};
|
|
90
|
+
const processDownloads = async (urls) => {
|
|
100
91
|
const results = new Map();
|
|
101
92
|
// Process downloads in chunks to limit concurrency
|
|
102
93
|
for (let i = 0; i < urls.length; i += maxConcurrent) {
|
|
103
94
|
const chunk = urls.slice(i, i + maxConcurrent);
|
|
104
|
-
const promises = chunk.map((url) =>
|
|
95
|
+
const promises = chunk.map(async (url) => {
|
|
105
96
|
try {
|
|
106
|
-
const localPath =
|
|
97
|
+
const localPath = await downloadScript(url);
|
|
107
98
|
results.set(url, localPath);
|
|
108
99
|
}
|
|
109
100
|
catch (error) {
|
|
110
101
|
console.warn(`Failed to download ${url}:`, error);
|
|
111
102
|
}
|
|
112
|
-
})
|
|
103
|
+
});
|
|
113
104
|
downloadQueue.push(...promises);
|
|
114
105
|
// eslint-disable-next-line no-await-in-loop
|
|
115
|
-
|
|
106
|
+
await Promise.all(promises);
|
|
116
107
|
}
|
|
117
108
|
return results;
|
|
118
|
-
}
|
|
109
|
+
};
|
|
119
110
|
return {
|
|
120
111
|
name: 'remote-script-localizer',
|
|
121
112
|
enforce: 'pre',
|
|
@@ -129,83 +120,76 @@ function initRemoteScriptLocalizerPlugin(options = {}) {
|
|
|
129
120
|
console.log('[remote-script-localizer] load:', id);
|
|
130
121
|
return null; // 让其他插件继续处理
|
|
131
122
|
},
|
|
132
|
-
transform(code, id) {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
codeLength: code.length,
|
|
139
|
-
});
|
|
140
|
-
// 检查文件类型
|
|
141
|
-
const isJS = /\.[jt]sx?$/.test(id);
|
|
142
|
-
const isHTML = /\.html$/.test(id);
|
|
143
|
-
if (!isJS && !isHTML)
|
|
144
|
-
return null;
|
|
145
|
-
// 匹配多种远程脚本模式,包括模板字符串中的内容
|
|
146
|
-
const patterns = [
|
|
147
|
-
// 动态导入
|
|
148
|
-
/import\s*\(\s*['"]https?:\/\/[^'"]+['"]\s*\)/g,
|
|
149
|
-
// HTML script 标签 (包括转义的版本)
|
|
150
|
-
/<script[^>]*src=["'](https?:\/\/[^"']+)["'][^>]*>(?:<\\\/script>)?/g,
|
|
151
|
-
// 模板字符串中的 script 标签
|
|
152
|
-
/`[^`]*<script[^>]*src=["'](https?:\/\/[^"']+)["'][^>]*>(?:<\\\/script>)?[^`]*`/g,
|
|
153
|
-
// HTML link 标签
|
|
154
|
-
/<link[^>]*href=["'](https?:\/\/[^"']+)["'][^>]*>/g,
|
|
155
|
-
];
|
|
156
|
-
let hasRemoteUrls = false;
|
|
157
|
-
let newCode = code;
|
|
158
|
-
const urls = new Set();
|
|
159
|
-
// 收集所有远程 URL
|
|
160
|
-
patterns.forEach((pattern) => {
|
|
161
|
-
var _a, _b;
|
|
162
|
-
const matches = code.matchAll(pattern);
|
|
163
|
-
for (const match of matches) {
|
|
164
|
-
hasRemoteUrls = true;
|
|
165
|
-
// 提取 URL(处理不同的匹配组)
|
|
166
|
-
const url = match[1] || ((_b = (_a = match[0].match(/['"]https?:\/\/[^'"]+['"]/)) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.slice(1, -1));
|
|
167
|
-
if (url)
|
|
168
|
-
urls.add(url);
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
if (hasRemoteUrls) {
|
|
172
|
-
console.log(`[remote-script-localizer] Found remote URLs in ${id}:`, [...urls]);
|
|
173
|
-
const downloadResults = yield processDownloads([...urls]);
|
|
174
|
-
downloadResults.forEach((localPath, url) => {
|
|
175
|
-
// 替换模板字符串中的 script 标签
|
|
176
|
-
newCode = newCode.replace(new RegExp(`<script([^>]*)src=["']${url}["']([^>]*)>(?:<\\\\/script>)?`, 'g'), `<script$1src="/${localPath}"$2></script>`);
|
|
177
|
-
// 替换其他情况
|
|
178
|
-
newCode = newCode.replace(new RegExp(`(['"\`])${url}\\1`, 'g'), `$1/${localPath}$1`);
|
|
179
|
-
console.log(`[remote-script-localizer] Localized: ${url} -> ${localPath}`);
|
|
180
|
-
});
|
|
181
|
-
return {
|
|
182
|
-
code: newCode,
|
|
183
|
-
map: null,
|
|
184
|
-
};
|
|
185
|
-
}
|
|
186
|
-
return null;
|
|
123
|
+
async transform(code, id) {
|
|
124
|
+
// 添加调试日志
|
|
125
|
+
console.log('[remote-script-localizer] transform:', {
|
|
126
|
+
id,
|
|
127
|
+
isVirtual: id.includes('\0'),
|
|
128
|
+
codeLength: code.length,
|
|
187
129
|
});
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
130
|
+
// 检查文件类型
|
|
131
|
+
const isJS = /\.[jt]sx?$/.test(id);
|
|
132
|
+
const isHTML = /\.html$/.test(id);
|
|
133
|
+
if (!isJS && !isHTML)
|
|
134
|
+
return null;
|
|
135
|
+
// 匹配多种远程脚本模式,包括模板字符串中的内容
|
|
136
|
+
const patterns = [
|
|
137
|
+
// 动态导入
|
|
138
|
+
/import\s*\(\s*['"]https?:\/\/[^'"]+['"]\s*\)/g,
|
|
139
|
+
// HTML script 标签 (包括转义的版本)
|
|
140
|
+
/<script[^>]*src=["'](https?:\/\/[^"']+)["'][^>]*>(?:<\\\/script>)?/g,
|
|
141
|
+
// 模板字符串中的 script 标签
|
|
142
|
+
/`[^`]*<script[^>]*src=["'](https?:\/\/[^"']+)["'][^>]*>(?:<\\\/script>)?[^`]*`/g,
|
|
143
|
+
// HTML link 标签
|
|
144
|
+
/<link[^>]*href=["'](https?:\/\/[^"']+)["'][^>]*>/g,
|
|
145
|
+
];
|
|
146
|
+
let hasRemoteUrls = false;
|
|
147
|
+
let newCode = code;
|
|
148
|
+
const urls = new Set();
|
|
149
|
+
// 收集所有远程 URL
|
|
150
|
+
patterns.forEach((pattern) => {
|
|
151
|
+
const matches = code.matchAll(pattern);
|
|
152
|
+
for (const match of matches) {
|
|
153
|
+
hasRemoteUrls = true;
|
|
154
|
+
// 提取 URL(处理不同的匹配组)
|
|
155
|
+
const url = match[1] || match[0].match(/['"]https?:\/\/[^'"]+['"]/)?.[0]?.slice(1, -1);
|
|
156
|
+
if (url)
|
|
157
|
+
urls.add(url);
|
|
193
158
|
}
|
|
194
159
|
});
|
|
160
|
+
if (hasRemoteUrls) {
|
|
161
|
+
console.log(`[remote-script-localizer] Found remote URLs in ${id}:`, [...urls]);
|
|
162
|
+
const downloadResults = await processDownloads([...urls]);
|
|
163
|
+
downloadResults.forEach((localPath, url) => {
|
|
164
|
+
// 替换模板字符串中的 script 标签
|
|
165
|
+
newCode = newCode.replace(new RegExp(`<script([^>]*)src=["']${url}["']([^>]*)>(?:<\\\\/script>)?`, 'g'), `<script$1src="/${localPath}"$2></script>`);
|
|
166
|
+
// 替换其他情况
|
|
167
|
+
newCode = newCode.replace(new RegExp(`(['"\`])${url}\\1`, 'g'), `$1/${localPath}$1`);
|
|
168
|
+
console.log(`[remote-script-localizer] Localized: ${url} -> ${localPath}`);
|
|
169
|
+
});
|
|
170
|
+
return {
|
|
171
|
+
code: newCode,
|
|
172
|
+
map: null,
|
|
173
|
+
};
|
|
174
|
+
}
|
|
175
|
+
return null;
|
|
195
176
|
},
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
}
|
|
177
|
+
async buildStart() {
|
|
178
|
+
if (!initialized) {
|
|
179
|
+
await initTempDir();
|
|
180
|
+
}
|
|
181
|
+
},
|
|
182
|
+
async buildEnd() {
|
|
183
|
+
// Wait for any remaining downloads to complete
|
|
184
|
+
await Promise.all(downloadQueue);
|
|
185
|
+
// Clean up temp directory
|
|
186
|
+
try {
|
|
187
|
+
// await rm(tempDir, { recursive: true, force: true });
|
|
188
|
+
// console.log('Cleaned up temporary remote scripts directory');
|
|
189
|
+
}
|
|
190
|
+
catch (error) {
|
|
191
|
+
console.error('Failed to clean up temp directory:', error);
|
|
192
|
+
}
|
|
209
193
|
},
|
|
210
194
|
};
|
|
211
195
|
}
|