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

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.
@@ -46,50 +46,68 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
46
46
  };
47
47
  Object.defineProperty(exports, "__esModule", { value: true });
48
48
  exports.initBlockStudioPlugins = initBlockStudioPlugins;
49
- // eslint-disable-next-line import/no-unresolved
50
49
  // import typescript from '@rollup/plugin-typescript';
51
50
  const fs_1 = require("fs");
51
+ const camelCase_1 = __importDefault(require("lodash/camelCase"));
52
+ const upperFirst_1 = __importDefault(require("lodash/upperFirst"));
52
53
  const path = __importStar(require("path"));
53
54
  const rollup_plugin_external_globals_1 = __importDefault(require("rollup-plugin-external-globals"));
54
55
  const ufo_1 = require("ufo");
56
+ const vite_plugin_node_polyfills_1 = require("vite-plugin-node-polyfills");
55
57
  const vite_plugin_react_pages_1 = __importStar(require("vite-plugin-react-pages"));
56
58
  const helper_1 = require("../utils/helper");
57
59
  const vite_plugin_html_transform_1 = require("./vite-plugin-html-transform");
58
60
  const BUILTIN_MODULES_VAR = '__PAGES_KIT_BUILTIN_MODULES__';
61
+ helper_1.logger.log('BUILTIN_MODULES_VAR', BUILTIN_MODULES_VAR);
59
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"]`,
60
65
  const defaultBlockExternals = {
61
66
  // 核心 React 相关
62
- react: `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/react"]`,
63
- 'react-dom': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/react-dom"]`,
64
- 'react-router-dom': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/react-router-dom"]`,
65
- 'react-hook-form': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/react-hook-form"]`,
66
- 'react-wrap-balancer': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/react-wrap-balancer"]`,
67
- // ArcBlock 相关
68
- '@arcblock/ux': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/arcblock/ux"]`,
69
- '@arcblock/did-connect/lib/Session': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/session"]`,
70
- '@arcblock/ux/lib/Locale/context': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/locale"]`,
71
- // MUI 相关
72
- // '@mui/material': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/mui/material"]`,
73
- // '@mui/lab': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/mui/lab"]`,
74
- // 其他工具库
75
- dayjs: `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/dayjs"]`,
76
- zustand: `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/zustand"]`,
77
- 'zustand/middleware/immer': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/zustand/middleware/immer"]`,
78
- immer: `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/immer"]`,
79
- stream: `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/stream"]`,
80
- '@iconify/react': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/iconify/react"]`,
81
- // 页面相关组件
82
- '@blocklet/pages-kit/builtin/components': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/components"]`,
83
- '@blocklet/pages-kit/builtin/page/header': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/page/header"]`,
84
- '@blocklet/pages-kit/builtin/pages-kit': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/pages-kit"]`,
85
- // 异步组件
86
- 'react-markdown': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/async/react-markdown"]`,
87
- 'react-syntax-highlighter': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/async/react-syntax-highlighter"]`,
88
- 'react-scroll-to-bottom': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/async/react-scroll-to-bottom"]`,
89
- // 浏览器原生
90
- crypto: 'window.crypto',
67
+ react: '@blocklet/pages-kit/builtin/react',
68
+ '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"]`,
80
+ // // 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: '',
91
108
  };
92
109
  function initBlockStudioPlugins(options) {
110
+ var _a;
93
111
  const workingDir = (options === null || options === void 0 ? void 0 : options.cwd) || process.cwd();
94
112
  const entryFilesPattern = (options === null || options === void 0 ? void 0 : options.entryFilesPattern) || (0, helper_1.getBlockEntryFilesPattern)();
95
113
  // 处理 blockExternals 参数
@@ -111,6 +129,38 @@ function initBlockStudioPlugins(options) {
111
129
  pagesDir,
112
130
  blockExternals: externalMappings, // 添加日志输出
113
131
  });
132
+ // 为 globals 创建一个无前缀的版本
133
+ const pathMappings = Object.fromEntries(Object.entries(externalMappings).filter(([, value]) => !value.startsWith('window.') && value.length > 0));
134
+ const externalGlobalMappings = Object.fromEntries(Object.entries(externalMappings).filter(([, value]) => value.startsWith('window.') && value.length > 0));
135
+ const globalsMappings = Object.fromEntries(Object.entries(externalMappings)
136
+ .map(([key, value]) => {
137
+ if (!(value === null || value === void 0 ? void 0 : value.length)) {
138
+ return [];
139
+ }
140
+ // 移除 "window." 前缀并转换为首字母大写的驼峰式
141
+ const rawName = String(key);
142
+ const globalName = (0, upperFirst_1.default)((0, camelCase_1.default)(rawName));
143
+ return [key, globalName];
144
+ })
145
+ .filter((item) => item.length > 0));
146
+ const filterModules = process.argv.includes('--filter')
147
+ ? (_a = process.argv[process.argv.indexOf('--filter') + 1]) === null || _a === void 0 ? void 0 : _a.split(',')
148
+ : null;
149
+ const entryList = (0, helper_1.findComponentFiles)({ cwd: workingDir })
150
+ .map((entry) => {
151
+ const { blockName } = entry;
152
+ const { isHtml } = entry;
153
+ const { file } = entry;
154
+ if (isHtml) {
155
+ return [blockName, `${vite_plugin_html_transform_1.VIRTUAL_MODULE_ID}?dir=${path.dirname(file)}`];
156
+ }
157
+ return [blockName, path.resolve(workingDir, file)];
158
+ })
159
+ .filter(Boolean)
160
+ .filter(([blockName]) => !filterModules || filterModules.includes(blockName || ''));
161
+ // @ts-ignore
162
+ const multiMode = entryList.length > 1;
163
+ const formats = ['es', multiMode ? 'cjs' : 'umd'];
114
164
  return [
115
165
  // {
116
166
  // name: 'vite-plugin-block-studio-dev',
@@ -126,63 +176,58 @@ function initBlockStudioPlugins(options) {
126
176
  // return html;
127
177
  // },
128
178
  // },
179
+ (0, vite_plugin_node_polyfills_1.nodePolyfills)({
180
+ include: ['crypto'],
181
+ }),
129
182
  {
130
183
  name: 'vite-plugin-block-studio-build',
184
+ apply: 'build',
131
185
  enforce: 'pre',
132
186
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
133
187
  config(_config) {
134
188
  return __awaiter(this, void 0, void 0, function* () {
135
- var _a, _b, _c, _d;
136
- const filterModules = process.argv.includes('--filter')
137
- ? (_a = process.argv[process.argv.indexOf('--filter') + 1]) === null || _a === void 0 ? void 0 : _a.split(',')
138
- : null;
189
+ var _a, _b, _c, _d, _e;
139
190
  const name = yield Promise.resolve(`${`${workingDir}/package.json`}`).then(s => __importStar(require(s))).then((res) => res.name).catch(() => 'MyLib');
140
- const entryList = (0, helper_1.findComponentFiles)({ cwd: workingDir })
141
- .map((entry) => {
142
- const { blockName } = entry;
143
- const { isHtml } = entry;
144
- const { file } = entry;
145
- if (isHtml) {
146
- return [blockName, `${vite_plugin_html_transform_1.VIRTUAL_MODULE_ID}?dir=${path.dirname(file)}`];
147
- }
148
- return [blockName, path.resolve(workingDir, file)];
149
- })
150
- .filter(Boolean)
151
- .filter(([blockName]) => !filterModules || filterModules.includes(blockName || ''));
152
- const multiMode = entryList.length > 1;
153
- // 将 externalGlobals 添加到插件中
154
- const plugins = [...(_config.plugins || []), (0, rollup_plugin_external_globals_1.default)(externalMappings)];
155
- // 为 globals 创建一个无前缀的版本
156
- const externalMappingsWithoutWindow = Object.fromEntries(Object.entries(externalMappings).map(([key, value]) => {
157
- // 移除 "window." 前缀
158
- const globalName = String(value).replace(/^window\./, '');
159
- return [key, globalName];
160
- }));
161
191
  return {
162
- plugins,
163
- build: Object.assign(Object.assign({}, _config === null || _config === void 0 ? void 0 : _config.build), { cssCodeSplit: false, lib: {
192
+ resolve: {
193
+ alias: {
194
+ // crypto: 'crypto-browserify',
195
+ },
196
+ },
197
+ optimizeDeps: {
198
+ // include: ['crypto-browserify'],
199
+ },
200
+ build: Object.assign(Object.assign({}, _config === null || _config === void 0 ? void 0 : _config.build), {
201
+ // sourcemap: true,
202
+ cssCodeSplit: false, lib: {
164
203
  name,
165
204
  entry: Object.assign({}, Object.fromEntries(entryList)),
166
- formats: ['es', !multiMode ? 'umd' : 'cjs'],
205
+ formats,
167
206
  fileName: (format, entryName) => `${format}/${entryName}.js`,
168
- }, rollupOptions: Object.assign(Object.assign({}, (_b = _config === null || _config === void 0 ? void 0 : _config.build) === null || _b === void 0 ? void 0 : _b.rollupOptions), { external: (id) => {
169
- // externalMappings 中的键也添加到 external 中
170
- const skip = [...Object.keys(externalMappings)];
171
- if (skip.some((s) => id === s || id.startsWith(`${s}/`))) {
172
- return true;
173
- }
174
- return false;
175
- }, output: Object.assign(Object.assign({}, (_d = (_c = _config === null || _config === void 0 ? void 0 : _config.build) === null || _c === void 0 ? void 0 : _c.rollupOptions) === null || _d === void 0 ? void 0 : _d.output), { chunkFileNames: () => {
207
+ }, 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
+ ((id) => {
209
+ // 是否匹配后缀
210
+ const isMatchAfterSlash = false;
211
+ const skipList = [...Object.keys(externalMappings)];
212
+ if (skipList.some((item) => (isMatchAfterSlash && id.startsWith(`${item}/`)) || id === item)) {
213
+ return true;
214
+ }
215
+ return false;
216
+ }), treeshake: true, output: Object.assign(Object.assign({}, (_e = (_d = _config === null || _config === void 0 ? void 0 : _config.build) === null || _d === void 0 ? void 0 : _d.rollupOptions) === null || _e === void 0 ? void 0 : _e.output), { chunkFileNames: () => {
176
217
  return '[format]/_chunks/[name]-[hash].js';
177
218
  },
178
219
  // 使用没有 window. 前缀的映射用于 globals 配置
179
- globals: externalMappingsWithoutWindow,
220
+ globals: globalsMappings,
180
221
  // 确保正确处理命名导出和默认导出
181
- interop: 'auto' }) }) }),
222
+ interop: 'auto', paths: pathMappings, esModule: true }) }) }),
182
223
  };
183
224
  });
184
225
  },
226
+ configResolved(config) {
227
+ helper_1.logger.info('config.build', config.build);
228
+ },
185
229
  },
230
+ Object.assign(Object.assign({}, (0, rollup_plugin_external_globals_1.default)(externalGlobalMappings)), { apply: 'build', enforce: 'pre' }),
186
231
  // @ts-ignore
187
232
  (0, vite_plugin_react_pages_1.default)({
188
233
  pagesDir,
@@ -237,9 +282,23 @@ function initBlockStudioPlugins(options) {
237
282
  name: 'build-force-exit',
238
283
  apply: 'build',
239
284
  enforce: 'post',
285
+ writeBundle(options) {
286
+ // 获取输出目录
287
+ const outDir = options.dir || 'dist';
288
+ formats.forEach((format) => {
289
+ const chunkDir = path.resolve(outDir, `${format}/_chunks`);
290
+ // if not exists, create it
291
+ if (!(0, fs_1.existsSync)(chunkDir)) {
292
+ (0, fs_1.mkdirSync)(chunkDir, { recursive: true });
293
+ }
294
+ });
295
+ },
240
296
  closeBundle() {
241
297
  // ensure vite build exit
242
- process.exit(0);
298
+ setTimeout(() => {
299
+ // wait for 1 second, to ensure all logs are flushed
300
+ process.exit(0);
301
+ }, 1000);
243
302
  },
244
303
  },
245
304
  ];