@blocklet/pages-kit-block-studio 0.1.39 → 0.1.40

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.
@@ -79,9 +79,11 @@ function copyDirectory(src, dest) {
79
79
  const srcPath = path_1.default.join(src, entry.name);
80
80
  const destPath = path_1.default.join(dest, entry.name);
81
81
  if (entry.isDirectory()) {
82
+ // eslint-disable-next-line no-await-in-loop
82
83
  yield copyDirectory(srcPath, destPath);
83
84
  }
84
85
  else {
86
+ // eslint-disable-next-line no-await-in-loop
85
87
  yield copyFile(srcPath, destPath);
86
88
  }
87
89
  }
@@ -47,11 +47,15 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
47
47
  Object.defineProperty(exports, "__esModule", { value: true });
48
48
  exports.initBlockStudioPlugins = initBlockStudioPlugins;
49
49
  // import typescript from '@rollup/plugin-typescript';
50
+ const builtin_module_transformer_1 = require("@blocklet/pages-kit/utils/typescript/builtin-module-transformer");
50
51
  const fs_1 = require("fs");
51
52
  const camelCase_1 = __importDefault(require("lodash/camelCase"));
52
53
  const upperFirst_1 = __importDefault(require("lodash/upperFirst"));
54
+ const p_limit_1 = __importDefault(require("p-limit"));
53
55
  const path = __importStar(require("path"));
54
56
  const rollup_plugin_external_globals_1 = __importDefault(require("rollup-plugin-external-globals"));
57
+ // import { visualizer } from 'rollup-plugin-visualizer';
58
+ const typescript_1 = __importDefault(require("typescript"));
55
59
  const ufo_1 = require("ufo");
56
60
  const vite_plugin_node_polyfills_1 = require("vite-plugin-node-polyfills");
57
61
  const vite_plugin_react_pages_1 = __importStar(require("vite-plugin-react-pages"));
@@ -59,57 +63,42 @@ const helper_1 = require("../utils/helper");
59
63
  const vite_plugin_html_transform_1 = require("./vite-plugin-html-transform");
60
64
  const BUILTIN_MODULES_VAR = '__PAGES_KIT_BUILTIN_MODULES__';
61
65
  helper_1.logger.log('BUILTIN_MODULES_VAR', BUILTIN_MODULES_VAR);
62
- // import initRemoteScriptLocalizerPlugin from './vite-plugin-remote-script-localizer';
63
- // react: `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/react"]`,
64
- // 'react-router-dom': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/react-router-dom"]`,
65
66
  const defaultBlockExternals = {
66
67
  // 核心 React 相关
67
68
  react: '@blocklet/pages-kit/builtin/react',
68
69
  'react-router-dom': '@blocklet/pages-kit/builtin/react-router-dom',
69
- // '@blocklet/pages-kit': `@blocklet/pages-kit`,
70
- // '@blocklet/ai-runtime': `@blocklet/pages-kit/builtin/ai-runtime`,
71
- // '@mui/material': `@blocklet/pages-kit/builtin/mui/material`,
72
- // '@mui/lab': `@blocklet/pages-kit/builtin/mui/lab`,
73
- // '@arcblock/ux': `@blocklet/pages-kit/builtin/arcblock/ux`,
74
- // '@arcblock/did-connect/lib/Session': `@blocklet/pages-kit/builtin/session`,
75
- // '@arcblock/ux/lib/Locale/context': `@blocklet/pages-kit/builtin/locale`,
76
- // '@blocklet/pages-kit/builtin/components': `@blocklet/pages-kit/builtin/components`,
77
- // 'react-dom': `@blocklet/pages-kit/builtin/react-dom`,
78
- // 'react-hook-form': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/react-hook-form"]`,
79
- // 'react-wrap-balancer': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/react-wrap-balancer"]`,
70
+ 'react-hook-form': '@blocklet/pages-kit/builtin/react-hook-form',
71
+ 'react-wrap-balancer': '@blocklet/pages-kit/builtin/react-wrap-balancer',
80
72
  // // ArcBlock 相关
81
- // '@arcblock/ux': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/arcblock/ux"]`,
82
- // '@arcblock/did-connect/lib/Session': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/session"]`,
83
- // '@arcblock/ux/lib/Locale/context': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/locale"]`,
84
- // // MUI 相关
85
- // // '@mui/material': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/mui/material"]`,
86
- // // '@mui/lab': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/mui/lab"]`,
87
- // // 其他工具库
88
- // dayjs: `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/dayjs"]`,
89
- // zustand: `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/zustand"]`,
90
- // 'zustand/middleware/immer': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/zustand/middleware/immer"]`,
91
- // immer: `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/immer"]`,
92
- // stream: `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/stream"]`,
93
- // '@iconify/react': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/iconify/react"]`,
94
- // // 页面相关组件
95
- // '@blocklet/pages-kit/builtin/components': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/components"]`,
96
- // '@blocklet/pages-kit/builtin/page/header': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/page/header"]`,
97
- // '@blocklet/pages-kit/builtin/pages-kit': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/pages-kit"]`,
98
- // // 异步组件
99
- // 'react-markdown': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/async/react-markdown"]`,
100
- // 'react-syntax-highlighter': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/async/react-syntax-highlighter"]`,
101
- // 'react-scroll-to-bottom': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/async/react-scroll-to-bottom"]`,
102
- // // 浏览器原生
103
- // crypto: 'window.crypto2',
104
- // '@blocklet/sdk/lib/component': 'window.abc',
105
- // '@blocklet/pages-kit': 'window.abc',
106
- // 'node:crypto': 'window.crypto', // 也处理 node: 前缀版本
107
- // crypto: '',
73
+ '@arcblock/ux': '@blocklet/pages-kit/builtin/arcblock/ux',
74
+ '@arcblock/did-connect/lib/Session': '@blocklet/pages-kit/builtin/session',
75
+ '@arcblock/ux/lib/Locale/context': '@blocklet/pages-kit/builtin/locale',
76
+ // MUI 相关
77
+ '@mui/material': '@blocklet/pages-kit/builtin/mui/material',
78
+ '@mui/lab': '@blocklet/pages-kit/builtin/mui/lab',
79
+ // 其他工具库
80
+ dayjs: '@blocklet/pages-kit/builtin/dayjs',
81
+ zustand: '@blocklet/pages-kit/builtin/zustand',
82
+ 'zustand/middleware/immer': '@blocklet/pages-kit/builtin/zustand/middleware/immer',
83
+ immer: '@blocklet/pages-kit/builtin/immer',
84
+ stream: '@blocklet/pages-kit/builtin/stream',
85
+ '@iconify/react': '@blocklet/pages-kit/builtin/iconify/react',
86
+ // 页面相关组件
87
+ '@blocklet/pages-kit/builtin/components': '@blocklet/pages-kit/builtin/components',
88
+ '@blocklet/pages-kit/builtin/page/header': '@blocklet/pages-kit/builtin/page/header',
89
+ '@blocklet/pages-kit/builtin/pages-kit': '@blocklet/pages-kit/builtin/pages-kit',
90
+ '@blocklet/pages-kit/builtin/async/ai-runtime': '@blocklet/pages-kit/builtin/async/ai-runtime',
91
+ // 异步组件
92
+ 'react-markdown': '@blocklet/pages-kit/builtin/async/react-markdown',
93
+ 'react-syntax-highlighter': '@blocklet/pages-kit/builtin/async/react-syntax-highlighter',
94
+ 'react-scroll-to-bottom': '@blocklet/pages-kit/builtin/async/react-scroll-to-bottom',
95
+ '@blocklet/pages-kit': '@blocklet/pages-kit',
108
96
  };
109
97
  function initBlockStudioPlugins(options) {
110
- var _a;
98
+ var _a, _b;
111
99
  const workingDir = (options === null || options === void 0 ? void 0 : options.cwd) || process.cwd();
112
100
  const entryFilesPattern = (options === null || options === void 0 ? void 0 : options.entryFilesPattern) || (0, helper_1.getBlockEntryFilesPattern)();
101
+ const transpileBuiltinModule = (_a = options === null || options === void 0 ? void 0 : options.transpileBuiltinModule) !== null && _a !== void 0 ? _a : true;
113
102
  // 处理 blockExternals 参数
114
103
  let externalMappings = defaultBlockExternals;
115
104
  if (typeof (options === null || options === void 0 ? void 0 : options.blockExternals) === 'function') {
@@ -128,6 +117,7 @@ function initBlockStudioPlugins(options) {
128
117
  entryFilesPattern: (0, helper_1.getBlockEntryFilesPattern)(),
129
118
  pagesDir,
130
119
  blockExternals: externalMappings, // 添加日志输出
120
+ transpileBuiltinModule,
131
121
  });
132
122
  // 为 globals 创建一个无前缀的版本
133
123
  const pathMappings = Object.fromEntries(Object.entries(externalMappings).filter(([, value]) => !value.startsWith('window.') && value.length > 0));
@@ -144,7 +134,7 @@ function initBlockStudioPlugins(options) {
144
134
  })
145
135
  .filter((item) => item.length > 0));
146
136
  const filterModules = process.argv.includes('--filter')
147
- ? (_a = process.argv[process.argv.indexOf('--filter') + 1]) === null || _a === void 0 ? void 0 : _a.split(',')
137
+ ? (_b = process.argv[process.argv.indexOf('--filter') + 1]) === null || _b === void 0 ? void 0 : _b.split(',')
148
138
  : null;
149
139
  const entryList = (0, helper_1.findComponentFiles)({ cwd: workingDir })
150
140
  .map((entry) => {
@@ -160,7 +150,8 @@ function initBlockStudioPlugins(options) {
160
150
  .filter(([blockName]) => !filterModules || filterModules.includes(blockName || ''));
161
151
  // @ts-ignore
162
152
  const multiMode = entryList.length > 1;
163
- const formats = ['es', multiMode ? 'cjs' : 'umd'];
153
+ const formats = (options === null || options === void 0 ? void 0 : options.formats) || ['es', multiMode ? 'cjs' : 'umd'];
154
+ // @ts-ignore
164
155
  return [
165
156
  // {
166
157
  // name: 'vite-plugin-block-studio-dev',
@@ -204,6 +195,10 @@ function initBlockStudioPlugins(options) {
204
195
  entry: Object.assign({}, Object.fromEntries(entryList)),
205
196
  formats,
206
197
  fileName: (format, entryName) => `${format}/${entryName}.js`,
198
+ }, terserOptions: {
199
+ // 保留导出名
200
+ keep_fnames: true,
201
+ keep_classnames: true,
207
202
  }, rollupOptions: Object.assign(Object.assign({}, (_a = _config === null || _config === void 0 ? void 0 : _config.build) === null || _a === void 0 ? void 0 : _a.rollupOptions), { external: ((_c = (_b = _config === null || _config === void 0 ? void 0 : _config.build) === null || _b === void 0 ? void 0 : _b.rollupOptions) === null || _c === void 0 ? void 0 : _c.external) ||
208
203
  ((id) => {
209
204
  // 是否匹配后缀
@@ -227,7 +222,8 @@ function initBlockStudioPlugins(options) {
227
222
  helper_1.logger.info('config.build', config.build);
228
223
  },
229
224
  },
230
- Object.assign(Object.assign({}, (0, rollup_plugin_external_globals_1.default)(externalGlobalMappings)), { apply: 'build', enforce: 'pre' }),
225
+ transpileBuiltinModule
226
+ ? Object.assign(Object.assign({}, (0, rollup_plugin_external_globals_1.default)(externalGlobalMappings)), { apply: 'build', enforce: 'pre' }) : undefined,
231
227
  // @ts-ignore
232
228
  (0, vite_plugin_react_pages_1.default)({
233
229
  pagesDir,
@@ -283,14 +279,57 @@ function initBlockStudioPlugins(options) {
283
279
  apply: 'build',
284
280
  enforce: 'post',
285
281
  writeBundle(options) {
282
+ // 对所有生成的 JavaScript 文件进行 transpileModule 处理
283
+ const transpileBuiltinModuleFn = (dir, format) => {
284
+ const files = (0, fs_1.readdirSync)(dir);
285
+ files.forEach((file) => {
286
+ const filePath = path.join(dir, file);
287
+ const stats = (0, fs_1.statSync)(filePath);
288
+ if (stats.isDirectory()) {
289
+ transpileBuiltinModuleFn(filePath, format); // 递归处理子目录
290
+ }
291
+ else if (file.endsWith('.js')) {
292
+ try {
293
+ const script = (0, fs_1.readFileSync)(filePath, 'utf8');
294
+ const moduleMap = {
295
+ es: typescript_1.default.ModuleKind.ESNext,
296
+ cjs: typescript_1.default.ModuleKind.CommonJS,
297
+ umd: typescript_1.default.ModuleKind.ESNext,
298
+ };
299
+ const code = typescript_1.default.transpileModule(script, {
300
+ compilerOptions: {
301
+ jsx: typescript_1.default.JsxEmit.React,
302
+ target: typescript_1.default.ScriptTarget.ES2016,
303
+ module: moduleMap[format],
304
+ },
305
+ transformers: {
306
+ // @ts-ignore
307
+ before: [(0, builtin_module_transformer_1.createBuiltinModuleTransformer)(typescript_1.default)],
308
+ },
309
+ }).outputText;
310
+ (0, fs_1.writeFileSync)(filePath, code);
311
+ helper_1.logger.info(`Transpiled: ${filePath}`);
312
+ // updateStatus(`Transpiled: ${filePath}`);
313
+ }
314
+ catch (error) {
315
+ helper_1.logger.error(`Failed to transpile ${filePath}:`, error);
316
+ }
317
+ }
318
+ });
319
+ };
286
320
  // 获取输出目录
287
321
  const outDir = options.dir || 'dist';
322
+ const limit = (0, p_limit_1.default)(20);
288
323
  formats.forEach((format) => {
289
- const chunkDir = path.resolve(outDir, `${format}/_chunks`);
324
+ const formatDir = path.resolve(outDir, `${format}`);
325
+ const chunkDir = path.resolve(formatDir, '_chunks');
290
326
  // if not exists, create it
291
327
  if (!(0, fs_1.existsSync)(chunkDir)) {
292
328
  (0, fs_1.mkdirSync)(chunkDir, { recursive: true });
293
329
  }
330
+ if (transpileBuiltinModule) {
331
+ limit(() => transpileBuiltinModuleFn(formatDir, format));
332
+ }
294
333
  });
295
334
  },
296
335
  closeBundle() {
@@ -301,6 +340,12 @@ function initBlockStudioPlugins(options) {
301
340
  }, 1000);
302
341
  },
303
342
  },
304
- ];
343
+ // visualizer({
344
+ // filename: 'dist/stats.html',
345
+ // open: true,
346
+ // gzipSize: true,
347
+ // brotliSize: true,
348
+ // }),
349
+ ].filter(Boolean);
305
350
  }
306
351
  exports.default = initBlockStudioPlugins;
@@ -111,6 +111,7 @@ function initRemoteScriptLocalizerPlugin(options = {}) {
111
111
  }
112
112
  }));
113
113
  downloadQueue.push(...promises);
114
+ // eslint-disable-next-line no-await-in-loop
114
115
  yield Promise.all(promises);
115
116
  }
116
117
  return results;