@blocklet/pages-kit-block-studio 0.0.5 → 0.0.7

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.
@@ -44,7 +44,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
44
44
  Object.defineProperty(exports, "__esModule", { value: true });
45
45
  exports.generateWrapperCode = generateWrapperCode;
46
46
  function generateWrapperCode(_a) {
47
- return __awaiter(this, arguments, void 0, function* ({ project, state, version }) {
47
+ return __awaiter(this, arguments, void 0, function* ({ project, version }) {
48
48
  const projectName = (project.name || project.id).toLowerCase().replaceAll(/[^a-z0-9]/g, '_');
49
49
  const packageName = `@pages-kit-project/${projectName}`;
50
50
  const packageJson = JSON.stringify({
@@ -57,6 +57,7 @@ function generateWrapperCode(_a) {
57
57
  '@blocklet/pages-kit-runtime': 'latest',
58
58
  '@blocklet/pages-kit-inner-components': 'latest',
59
59
  '@blocklet/pages-kit': 'latest',
60
+ '@blocklet/uploader-server': 'latest',
60
61
  },
61
62
  exports: {
62
63
  '.': {
@@ -69,37 +70,152 @@ function generateWrapperCode(_a) {
69
70
  import: './client.js',
70
71
  types: './client.d.ts',
71
72
  },
73
+ './middleware': {
74
+ require: './middleware.cjs',
75
+ import: './middleware.js',
76
+ types: './middleware.d.ts',
77
+ },
72
78
  },
73
79
  }, null, 2);
74
80
  const index = `\
75
- import { ProjectRuntime } from '@blocklet/pages-kit-runtime/client';
76
- export * from '@blocklet/pages-kit-runtime/client';
81
+ import HomeComponent from '@blocklet/pages-kit-runtime/client';
82
+
83
+ export default HomeComponent;
84
+ `;
85
+ const client = index;
86
+ const middleware = `
87
+ import { initResourceRouter } from '@blocklet/pages-kit-block-studio/block-studio/init-resource-router';
88
+ import { getPreloadComponents } from '@blocklet/pages-kit-inner-components/components';
89
+ import { initPackResourceStates } from '@blocklet/pages-kit-inner-components/site-state';
90
+ import { Router } from 'express';
77
91
 
78
- window.__PAGE_STATE__ = ${JSON.stringify(state, null, 2)};
79
- window.__PROJECT_ID__ = "${project.id}";
92
+ const { initProxyToMediaKitUploadsMiddleware, initStaticResourceMiddleware } = require('@blocklet/uploader-server');
80
93
 
81
94
 
82
- const initProjectRuntime = new ProjectRuntime({
83
- state: window.__PAGE_STATE__,
84
- projectId: window.__PROJECT_ID__,
95
+ const router = Router();
96
+
97
+ router.use('/api/resources', initResourceRouter);
98
+
99
+ let tempStates: any[] = [];
100
+ let allResourcesComponents: any[] = [];
101
+
102
+ initPackResourceStates(({ states }: any) => {
103
+ allResourcesComponents = states.reduce((acc, { state }) => {
104
+ if (state.components) {
105
+ return { ...acc, ...state.components };
106
+ }
107
+ return acc;
108
+ }, {});
109
+ tempStates = states;
85
110
  });
86
111
 
87
- export default initProjectRuntime;
112
+ router.get('/api/pages', (req, res) => {
113
+ const { did } = req.query;
114
+
115
+ if (!did) {
116
+ return res.status(400).json({ message: 'did is required' });
117
+ }
118
+
119
+ const data = tempStates.find((state) => state.did === did);
120
+
121
+ res.json(data);
122
+ });
123
+
124
+ router.post('/api/components/preload', async (req, res) => {
125
+ const { mode, module, instances: inputInstances, locale } = req.body;
126
+
127
+ // Find state by matching component IDs with tempStates
128
+ const state = tempStates.find((s) =>
129
+ inputInstances.some((instance) => {
130
+ return s.state.components && Object.keys(s.state.components).includes(instance.component.id);
131
+ }),
132
+ )?.state;
133
+
134
+ const instances = inputInstances.map((instance) => {
135
+ const component = allResourcesComponents[instance.component.id].data;
136
+
137
+ if (!component) return null;
138
+
139
+ return {
140
+ id: instance.id,
141
+ componentId: component.id,
142
+ properties: instance.properties,
143
+ useCache: instance.useCache,
144
+ cacheDuration: instance.cacheDuration,
145
+ };
146
+ });
147
+
148
+ if (!state) {
149
+ return res.status(400).json({ message: 'state not found' });
150
+ }
151
+
152
+ const result = await getPreloadComponents({
153
+ mode,
154
+ req,
155
+ state: state,
156
+ locale: locale,
157
+ module: module,
158
+ instances: instances,
159
+ });
160
+
161
+ res.json(result);
162
+ });
163
+
164
+
165
+ router.use(
166
+ '/uploads',
167
+ // initProxyToMediaKitUploadsMiddleware({
168
+ // express,
169
+ // }),
170
+ initStaticResourceMiddleware({
171
+ express,
172
+ resourceTypes: [
173
+ // image bin resource
174
+ {
175
+ type: 'imgpack',
176
+ did: 'z8ia1mAXo8ZE7ytGF36L5uBf9kD2kenhqFGp9',
177
+ },
178
+ // pages kit resource (pages and components folder)
179
+ {
180
+ type: 'page',
181
+ did: 'z8iZiDFg3vkkrPwsiba1TLXy3H9XHzFERsP8o',
182
+ folder: ['pages', 'components'],
183
+ blacklist: ['.yml'],
184
+ },
185
+ ],
186
+ }),
187
+ (_req, res) => {
188
+ res.status(404).send('404 NOT FOUND').end();
189
+ },
190
+ );
191
+
192
+ export default router;
88
193
  `;
89
- const client = index;
90
194
  const tsFiles = [
91
195
  { fileName: 'index.ts', content: index },
92
196
  { fileName: 'client.ts', content: client },
197
+ { fileName: 'middleware.ts', content: middleware },
93
198
  ];
94
199
  const ts = yield Promise.resolve().then(() => __importStar(require('typescript')));
200
+ const compilerOptions = {
201
+ jsx: ts.JsxEmit.React,
202
+ esModuleInterop: true,
203
+ skipLibCheck: true,
204
+ declaration: true,
205
+ };
95
206
  const result = (yield Promise.all(tsFiles.map((_a) => __awaiter(this, [_a], void 0, function* ({ fileName, content }) {
96
207
  const cjs = ts.transpileModule(content, {
97
208
  fileName,
98
- compilerOptions: { module: ts.ModuleKind.CommonJS },
209
+ compilerOptions: Object.assign(Object.assign({}, compilerOptions), { module: ts.ModuleKind.CommonJS }),
99
210
  });
100
211
  const esm = ts.transpileModule(content, {
101
212
  fileName,
102
- compilerOptions: { module: ts.ModuleKind.ESNext },
213
+ compilerOptions: Object.assign(Object.assign({}, compilerOptions), { module: ts.ModuleKind.ESNext }),
214
+ });
215
+ // Generate proper type declarations
216
+ const dts = ts.transpileModule(content, {
217
+ fileName,
218
+ compilerOptions: Object.assign(Object.assign({}, compilerOptions), { declaration: true, emitDeclarationOnly: true }),
103
219
  });
104
220
  return [
105
221
  {
@@ -112,7 +228,7 @@ export default initProjectRuntime;
112
228
  },
113
229
  {
114
230
  fileName: fileName.replace(/\.ts$/, '.d.ts'),
115
- content,
231
+ content: dts.outputText || content, // Fallback to original content if declaration fails
116
232
  },
117
233
  ];
118
234
  })))).flat();
@@ -101,7 +101,7 @@ const getExportDir = (projectId, releaseId) => {
101
101
  return dir;
102
102
  };
103
103
  exports.initResourceRouter = (0, express_1.Router)();
104
- exports.initResourceRouter.get('/resources', (_req, res) => __awaiter(void 0, void 0, void 0, function* () {
104
+ exports.initResourceRouter.get('/', (_req, res) => __awaiter(void 0, void 0, void 0, function* () {
105
105
  const resources = [];
106
106
  const rootDir = process.cwd();
107
107
  const files = (0, utils_1.findComponentFiles)({ cwd: rootDir });
@@ -130,7 +130,7 @@ exports.initResourceRouter.get('/resources', (_req, res) => __awaiter(void 0, vo
130
130
  ],
131
131
  });
132
132
  }));
133
- exports.initResourceRouter.post('/resources', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
133
+ exports.initResourceRouter.post('/', (req, res) => __awaiter(void 0, void 0, void 0, function* () {
134
134
  const { resources, projectId, releaseId } = req.body;
135
135
  // ignore all tag
136
136
  const componentIds = resources.filter((resource) => resource.id !== allTag);
@@ -57,7 +57,7 @@ function initBlockStudioPlugins(options) {
57
57
  (0, utils_1.setBlockEntryFilesPattern)(entryFilesPattern);
58
58
  // fallback to __dirname if _theme.tsx not exists
59
59
  const pagesDir = (0, fs_1.existsSync)(path.join(workingDir, '_theme.tsx')) ? workingDir : __dirname.replace('cjs', 'esm');
60
- utils_1.logger.log('initBlockStudioPlugins options', {
60
+ utils_1.logger.log('initBlockStudioPlugins options: ', {
61
61
  cwd: workingDir,
62
62
  entryFilesPattern: (0, utils_1.getBlockEntryFilesPattern)(),
63
63
  pagesDir,
@@ -90,9 +90,7 @@ function initBlockStudioPlugins(options) {
90
90
  const multiMode = !!process.argv.includes('--multi');
91
91
  const name = yield Promise.resolve(`${`${workingDir}/package.json`}`).then(s => __importStar(require(s))).then((res) => res.name).catch(() => 'MyLib');
92
92
  return {
93
- build: {
94
- cssCodeSplit: false,
95
- lib: {
93
+ build: Object.assign({ cssCodeSplit: false, lib: {
96
94
  name,
97
95
  entry: Object.assign({}, Object.fromEntries((0, utils_1.findComponentFiles)({ cwd: workingDir })
98
96
  .map((entry) => {
@@ -109,40 +107,41 @@ function initBlockStudioPlugins(options) {
109
107
  // multiMode not support umd
110
108
  formats: ['es', multiMode ? 'umd' : 'cjs'],
111
109
  fileName: (format, entryName) => `${format}/${entryName}.js`,
112
- },
113
- rollupOptions: {
110
+ }, rollupOptions: {
114
111
  external: [
115
112
  'react',
116
113
  'react-router-dom',
117
114
  'react-dom',
115
+ 'react-is',
116
+ 'react/jsx-runtime',
118
117
  'crypto',
119
- '@emotion/react',
120
- '@emotion/styled',
118
+ // '@emotion/react',
119
+ // '@emotion/styled',
121
120
  '@arcblock/ux',
122
- /^@mui\/.*/, // 所有 MUI 相关包
123
- '@mui/material/styles', // 明确指定这个路径
121
+ '@arcblock/did-connect',
124
122
  ],
125
123
  output: {
126
124
  // 为所有外部依赖提供全局变量
127
125
  globals: {
128
126
  react: 'React',
129
127
  'react-dom': 'ReactDOM',
128
+ 'react-is': 'ReactIs',
130
129
  'react-router-dom': 'ReactRouterDOM',
131
- '@emotion/react': 'emotionReact',
132
- '@emotion/styled': 'emotionStyled',
130
+ 'react/jsx-runtime': 'ReactJsxRuntime',
131
+ // '@emotion/react': 'emotionReact',
132
+ // '@emotion/styled': 'emotionStyled',
133
133
  '@mui/material': 'MUI',
134
134
  '@arcblock/ux': 'ArcBlockUX',
135
+ '@arcblock/did-connect': 'ArcBlockDidConnect',
135
136
  },
136
137
  paths: {
137
138
  // Redirect 'react' imports to '@blocklet/pages-kit/builtin/react'
138
139
  // react: '@blocklet/pages-kit/builtin/react',
139
140
  },
140
141
  // 确保正确处理命名导出和默认导出
141
- interop: 'auto',
142
- preserveModules: true, // 保持模块结构
142
+ // interop: 'auto',
143
143
  },
144
- },
145
- },
144
+ } }, _config === null || _config === void 0 ? void 0 : _config.build),
146
145
  };
147
146
  });
148
147
  },