@blocklet/pages-kit-block-studio 0.0.16 → 0.0.18
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/constants/index.js +9 -0
- package/lib/cjs/middlewares/init-block-studio-router.js +61 -13
- package/lib/cjs/middlewares/init-resource-router.js +49 -2
- package/lib/cjs/middlewares/init-uploader-router.js +7 -6
- package/lib/cjs/plugins/vite-plugin-block-studio.js +4 -0
- package/lib/cjs/plugins/vite-plugin-html-transform.js +5 -0
- package/lib/cjs/tsconfig.tsbuildinfo +1 -1
- package/lib/cjs/utils/build-lib.js +1 -1
- package/lib/cjs/utils/generate-wrapper-code.js +145 -7
- package/lib/cjs/utils/helper.js +172 -6
- package/lib/esm/constants/index.js +6 -0
- package/lib/esm/middlewares/init-block-studio-router.js +62 -14
- package/lib/esm/middlewares/init-resource-router.js +50 -3
- package/lib/esm/middlewares/init-uploader-router.js +7 -6
- package/lib/esm/plugins/vite-plugin-block-studio.js +5 -1
- package/lib/esm/plugins/vite-plugin-html-transform.js +5 -2
- package/lib/esm/tsconfig.tsbuildinfo +1 -1
- package/lib/esm/utils/build-lib.js +1 -1
- package/lib/esm/utils/generate-wrapper-code.js +145 -7
- package/lib/esm/utils/helper.js +131 -4
- package/lib/types/constants/index.d.ts +6 -0
- package/lib/types/plugins/vite-plugin-html-transform.d.ts +14 -0
- package/lib/types/tsconfig.tsbuildinfo +1 -1
- package/lib/types/utils/helper.d.ts +18 -3
- package/package.json +3 -2
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MEDIA_KIT_RESOURCE_TYPE = exports.MEDIA_KIT_DID = exports.PAGES_KIT_BLOCK_STUDIO_RESOURCE_TYPE = exports.PAGES_KIT_BLOCK_STUDIO_DID = exports.PAGES_KIT_RESOURCE_TYPE = exports.PAGES_KIT_DID = void 0;
|
|
4
|
+
exports.PAGES_KIT_DID = 'z8iZiDFg3vkkrPwsiba1TLXy3H9XHzFERsP8o';
|
|
5
|
+
exports.PAGES_KIT_RESOURCE_TYPE = 'page';
|
|
6
|
+
exports.PAGES_KIT_BLOCK_STUDIO_DID = 'z2qa7rr3eUyVnWp2PCxEVARuUfLFh6cE5V2xV';
|
|
7
|
+
exports.PAGES_KIT_BLOCK_STUDIO_RESOURCE_TYPE = 'page';
|
|
8
|
+
exports.MEDIA_KIT_DID = 'z8ia1mAXo8ZE7ytGF36L5uBf9kD2kenhqFGp9';
|
|
9
|
+
exports.MEDIA_KIT_RESOURCE_TYPE = 'imgpack';
|
|
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
exports.initBlockStudioRouter = void 0;
|
|
16
16
|
const express_1 = require("express");
|
|
17
17
|
const fs_1 = __importDefault(require("fs"));
|
|
18
|
+
const lodash_1 = require("lodash");
|
|
18
19
|
const path_1 = __importDefault(require("path"));
|
|
19
20
|
const helper_1 = require("../utils/helper");
|
|
20
21
|
exports.initBlockStudioRouter = (0, express_1.Router)();
|
|
@@ -31,25 +32,23 @@ exports.initBlockStudioRouter.get('/', (req, res) => __awaiter(void 0, void 0, v
|
|
|
31
32
|
return res.status(403).json({ error: 'Invalid path' });
|
|
32
33
|
}
|
|
33
34
|
try {
|
|
34
|
-
if (!fs_1.default.existsSync(filePath)) {
|
|
35
|
-
return res.json(null);
|
|
36
|
-
}
|
|
37
35
|
const ext = path_1.default.extname(filePath).toLowerCase();
|
|
38
36
|
if (BINARY_EXTENSIONS.includes(ext)) {
|
|
37
|
+
if (!fs_1.default.existsSync(filePath)) {
|
|
38
|
+
return res.json(null);
|
|
39
|
+
}
|
|
39
40
|
// For images, stream the file directly
|
|
40
41
|
const mimeType = `image/${ext.slice(1)}`;
|
|
41
42
|
res.setHeader('Content-Type', mimeType);
|
|
42
43
|
return fs_1.default.createReadStream(filePath).pipe(res);
|
|
43
44
|
}
|
|
44
|
-
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
catch (_a) {
|
|
50
|
-
// If JSON parsing fails, return as plain text
|
|
51
|
-
return res.json(content);
|
|
45
|
+
const metadata = (0, helper_1.initializeMetadata)(filePath);
|
|
46
|
+
const code = (0, helper_1.getBlockCode)(filePath);
|
|
47
|
+
if (code) {
|
|
48
|
+
(0, lodash_1.set)(metadata, 'renderer.script', code);
|
|
49
|
+
(0, lodash_1.set)(metadata, 'renderer.type', 'react-component');
|
|
52
50
|
}
|
|
51
|
+
return res.json(metadata);
|
|
53
52
|
}
|
|
54
53
|
catch (error) {
|
|
55
54
|
return res.status(500).json({ error: 'Failed to read file' });
|
|
@@ -71,11 +70,60 @@ exports.initBlockStudioRouter.post('/', (req, res) => __awaiter(void 0, void 0,
|
|
|
71
70
|
if (!fs_1.default.existsSync(dir)) {
|
|
72
71
|
fs_1.default.mkdirSync(dir, { recursive: true });
|
|
73
72
|
}
|
|
74
|
-
|
|
75
|
-
|
|
73
|
+
const currentMetadata = (0, helper_1.initializeMetadata)(filePath);
|
|
74
|
+
const mergedContent = Object.assign(Object.assign({}, currentMetadata), content);
|
|
75
|
+
if ((0, lodash_1.isEqual)(currentMetadata, mergedContent)) {
|
|
76
|
+
return res.json({ success: true, content: mergedContent, message: 'No changes' });
|
|
77
|
+
}
|
|
78
|
+
mergedContent.updatedAt = new Date().toISOString();
|
|
79
|
+
// Check if this is a metadata file and has previewImage update
|
|
80
|
+
if ((0, helper_1.isMetadataFile)(filePath) && content.previewImage && currentMetadata.previewImage !== content.previewImage) {
|
|
81
|
+
const previewImagePath = path_1.default.join(dir, (0, helper_1.getPreviewImageRelativePath)(content.previewImage));
|
|
82
|
+
if (!fs_1.default.existsSync(previewImagePath)) {
|
|
83
|
+
yield (0, helper_1.downloadAsset)({
|
|
84
|
+
asset: content.previewImage,
|
|
85
|
+
savePath: previewImagePath,
|
|
86
|
+
componentDid: process.env.BLOCKLET_COMPONENT_DID || '',
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
res.json({ success: true, content: mergedContent, message: 'Updated' });
|
|
91
|
+
// save metadata without renderer after response
|
|
92
|
+
delete mergedContent.renderer;
|
|
93
|
+
fs_1.default.writeFileSync(filePath, JSON.stringify(mergedContent, null, 2));
|
|
94
|
+
return null;
|
|
76
95
|
}
|
|
77
96
|
catch (error) {
|
|
78
97
|
return res.status(500).json({ error: 'Failed to write file' });
|
|
79
98
|
}
|
|
80
99
|
}));
|
|
100
|
+
exports.initBlockStudioRouter.get('/all', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
|
|
101
|
+
const { withBlockletData = true } = req.query;
|
|
102
|
+
const allBlocks = yield (0, helper_1.findComponentFiles)();
|
|
103
|
+
// get code to metadata
|
|
104
|
+
const allBlocksWithCode = allBlocks.map((block) => {
|
|
105
|
+
const code = (0, helper_1.getBlockCode)(block.fullPath);
|
|
106
|
+
if (code) {
|
|
107
|
+
(0, lodash_1.set)(block.metadata, 'renderer.script', code);
|
|
108
|
+
(0, lodash_1.set)(block.metadata, 'renderer.type', 'react-component');
|
|
109
|
+
}
|
|
110
|
+
return block;
|
|
111
|
+
});
|
|
112
|
+
if (withBlockletData) {
|
|
113
|
+
const allBlocksWithBlockletData = allBlocksWithCode.map((block) => {
|
|
114
|
+
const blockletInfo = (0, helper_1.getBlockStudioInfo)() || {
|
|
115
|
+
title: '',
|
|
116
|
+
did: '',
|
|
117
|
+
};
|
|
118
|
+
const item = {
|
|
119
|
+
blockletTitle: blockletInfo.title,
|
|
120
|
+
blockletId: blockletInfo.did,
|
|
121
|
+
data: block.metadata,
|
|
122
|
+
};
|
|
123
|
+
return item;
|
|
124
|
+
});
|
|
125
|
+
return res.json((0, lodash_1.keyBy)(allBlocksWithBlockletData, 'data.id'));
|
|
126
|
+
}
|
|
127
|
+
return res.json((0, lodash_1.keyBy)(allBlocksWithCode, 'id'));
|
|
128
|
+
}));
|
|
81
129
|
exports.default = exports.initBlockStudioRouter;
|
|
@@ -53,6 +53,7 @@ const component_1 = require("@blocklet/sdk/lib/component");
|
|
|
53
53
|
const child_process_1 = require("child_process");
|
|
54
54
|
const express_1 = require("express");
|
|
55
55
|
const fs_1 = __importDefault(require("fs"));
|
|
56
|
+
const set_1 = __importDefault(require("lodash/set"));
|
|
56
57
|
const path_1 = __importStar(require("path"));
|
|
57
58
|
const helper_1 = require("../utils/helper");
|
|
58
59
|
const DID = 'z2qa7rr3eUyVnWp2PCxEVARuUfLFh6cE5V2xV';
|
|
@@ -153,9 +154,55 @@ exports.initResourceRouter.post('/', (req, res) => __awaiter(void 0, void 0, voi
|
|
|
153
154
|
fs_1.default.rmSync(dir, { recursive: true, force: true });
|
|
154
155
|
fs_1.default.mkdirSync(dir, { recursive: true });
|
|
155
156
|
const rootDir = process.cwd();
|
|
156
|
-
const
|
|
157
|
+
const distDir = (0, path_1.join)(rootDir, helper_1.libDir);
|
|
158
|
+
const tmpPackage = (0, path_1.join)(distDir, 'resource-blocklet');
|
|
159
|
+
fs_1.default.mkdirSync(tmpPackage, { recursive: true });
|
|
160
|
+
const pagesDir = (0, path_1.join)(tmpPackage, 'pages');
|
|
161
|
+
fs_1.default.mkdirSync(pagesDir, { recursive: true });
|
|
162
|
+
const componentsDir = (0, path_1.join)(tmpPackage, 'components');
|
|
163
|
+
fs_1.default.mkdirSync(componentsDir, { recursive: true });
|
|
164
|
+
// get @metadata.json by glob
|
|
165
|
+
const canUseComponents = (0, helper_1.findComponentFiles)({ cwd: rootDir, filter: componentIds });
|
|
166
|
+
// Filter and process metadata files
|
|
167
|
+
const metadataList = canUseComponents.map(({ fullPath, blockName, metadata: _metadata }) => {
|
|
168
|
+
// get metadata
|
|
169
|
+
const metadata = _metadata;
|
|
170
|
+
// get code to metadata
|
|
171
|
+
const code = fs_1.default.readFileSync((0, path_1.join)(distDir, 'es', `${blockName}.js`), 'utf8');
|
|
172
|
+
if (code) {
|
|
173
|
+
(0, set_1.default)(metadata, 'renderer.script', code);
|
|
174
|
+
(0, set_1.default)(metadata, 'renderer.type', 'react-component');
|
|
175
|
+
}
|
|
176
|
+
// write metadata to metadataPath
|
|
177
|
+
const metadataYmlPath = path_1.default.join(componentsDir, `${metadata.name || 'unnamed'}.${metadata.id}.yml`);
|
|
178
|
+
fs_1.default.writeFileSync(metadataYmlPath, (0, helper_1.generateYaml)(metadata));
|
|
179
|
+
// Handle preview image if exists
|
|
180
|
+
if (metadata.previewImage) {
|
|
181
|
+
const imagePath = path_1.default.join(path_1.default.dirname(fullPath), (0, helper_1.getPreviewImageRelativePath)(metadata.previewImage));
|
|
182
|
+
const imageDestPath = path_1.default.join(componentsDir, metadata.previewImage);
|
|
183
|
+
if (fs_1.default.existsSync(imagePath)) {
|
|
184
|
+
fs_1.default.copyFileSync(imagePath, imageDestPath);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return metadata;
|
|
188
|
+
});
|
|
189
|
+
// write pages.config.yml
|
|
190
|
+
const pagesConfigPath = path_1.default.join(tmpPackage, '.blocklet/pages/pages.config.yml');
|
|
191
|
+
fs_1.default.mkdirSync(path_1.default.dirname(pagesConfigPath), { recursive: true });
|
|
192
|
+
const pagesConfig = {
|
|
193
|
+
pages: [],
|
|
194
|
+
components: metadataList.map((metadata) => ({
|
|
195
|
+
id: metadata.id,
|
|
196
|
+
name: metadata.name,
|
|
197
|
+
})),
|
|
198
|
+
supportedLocales: [],
|
|
199
|
+
config: {},
|
|
200
|
+
};
|
|
201
|
+
fs_1.default.writeFileSync(pagesConfigPath, (0, helper_1.generateYaml)(pagesConfig));
|
|
202
|
+
helper_1.logger.info('generate resource blocklet block count:', metadataList.length);
|
|
157
203
|
yield copyRecursive(tmpPackage, dir);
|
|
158
|
-
|
|
204
|
+
// remove tmpPackage
|
|
205
|
+
// fs.rmSync(tmpPackage, { recursive: true, force: true });
|
|
159
206
|
res.json({ success: true });
|
|
160
207
|
}
|
|
161
208
|
catch (error) {
|
|
@@ -37,6 +37,7 @@ exports.initUploaderRouter = void 0;
|
|
|
37
37
|
// @ts-ignore
|
|
38
38
|
const uploader_server_1 = require("@blocklet/uploader-server");
|
|
39
39
|
const express_1 = __importStar(require("express"));
|
|
40
|
+
const constants_1 = require("../constants");
|
|
40
41
|
// init uploader router
|
|
41
42
|
exports.initUploaderRouter = (0, express_1.Router)();
|
|
42
43
|
exports.initUploaderRouter.use('/', (0, uploader_server_1.initProxyToMediaKitUploadsMiddleware)({
|
|
@@ -46,20 +47,20 @@ exports.initUploaderRouter.use('/', (0, uploader_server_1.initProxyToMediaKitUpl
|
|
|
46
47
|
resourceTypes: [
|
|
47
48
|
// image bin resource
|
|
48
49
|
{
|
|
49
|
-
type:
|
|
50
|
-
did:
|
|
50
|
+
type: constants_1.MEDIA_KIT_RESOURCE_TYPE,
|
|
51
|
+
did: constants_1.MEDIA_KIT_DID,
|
|
51
52
|
},
|
|
52
53
|
// pages kit resource (pages and components folder)
|
|
53
54
|
{
|
|
54
|
-
type:
|
|
55
|
-
did:
|
|
55
|
+
type: constants_1.PAGES_KIT_RESOURCE_TYPE,
|
|
56
|
+
did: constants_1.PAGES_KIT_DID,
|
|
56
57
|
folder: ['pages', 'components'],
|
|
57
58
|
blacklist: ['.yml'],
|
|
58
59
|
},
|
|
59
60
|
// pages kit block studio resource
|
|
60
61
|
{
|
|
61
|
-
type:
|
|
62
|
-
did:
|
|
62
|
+
type: constants_1.PAGES_KIT_BLOCK_STUDIO_RESOURCE_TYPE,
|
|
63
|
+
did: constants_1.PAGES_KIT_BLOCK_STUDIO_DID,
|
|
63
64
|
folder: ['pages', 'components'],
|
|
64
65
|
blacklist: ['.yml'],
|
|
65
66
|
},
|
|
@@ -121,6 +121,9 @@ function initBlockStudioPlugins(options) {
|
|
|
121
121
|
'@arcblock/did-connect',
|
|
122
122
|
],
|
|
123
123
|
output: {
|
|
124
|
+
chunkFileNames: () => {
|
|
125
|
+
return '[format]/_chunks/[name]-[hash].js';
|
|
126
|
+
},
|
|
124
127
|
// 为所有外部依赖提供全局变量
|
|
125
128
|
globals: {
|
|
126
129
|
react: 'React',
|
|
@@ -166,6 +169,7 @@ function initBlockStudioPlugins(options) {
|
|
|
166
169
|
? {
|
|
167
170
|
isHtmlPreview: true,
|
|
168
171
|
dataPath: filePath,
|
|
172
|
+
code: (0, vite_plugin_html_transform_1.generateComponent)((0, vite_plugin_html_transform_1.readHtmlFiles)(dirPath.split('?dir=')[1] || '')),
|
|
169
173
|
blockName,
|
|
170
174
|
dirPath,
|
|
171
175
|
metadataPath,
|
|
@@ -10,6 +10,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.RESOLVED_VIRTUAL_MODULE_ID = exports.VIRTUAL_MODULE_ID = void 0;
|
|
13
|
+
exports.readHtmlFiles = readHtmlFiles;
|
|
14
|
+
exports.generateComponent = generateComponent;
|
|
13
15
|
exports.initHtmlPreviewTransformPlugin = initHtmlPreviewTransformPlugin;
|
|
14
16
|
const fs_1 = require("fs");
|
|
15
17
|
const path_1 = require("path");
|
|
@@ -191,6 +193,9 @@ ${content.trim()}`;
|
|
|
191
193
|
function generateComponent(content, _isDev = true) {
|
|
192
194
|
const htmlContent = content.html;
|
|
193
195
|
const { name } = content;
|
|
196
|
+
if (_isDev) {
|
|
197
|
+
// do something
|
|
198
|
+
}
|
|
194
199
|
return `import { createElement } from 'react';
|
|
195
200
|
|
|
196
201
|
export default function HtmlPreview() {
|