@blocklet/pages-kit-block-studio 0.1.38 → 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,7 +46,6 @@ 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");
52
51
  const camelCase_1 = __importDefault(require("lodash/camelCase"));
@@ -54,16 +53,28 @@ const upperFirst_1 = __importDefault(require("lodash/upperFirst"));
54
53
  const path = __importStar(require("path"));
55
54
  const rollup_plugin_external_globals_1 = __importDefault(require("rollup-plugin-external-globals"));
56
55
  const ufo_1 = require("ufo");
56
+ const vite_plugin_node_polyfills_1 = require("vite-plugin-node-polyfills");
57
57
  const vite_plugin_react_pages_1 = __importStar(require("vite-plugin-react-pages"));
58
58
  const helper_1 = require("../utils/helper");
59
59
  const vite_plugin_html_transform_1 = require("./vite-plugin-html-transform");
60
- // const BUILTIN_MODULES_VAR = '__PAGES_KIT_BUILTIN_MODULES__';
60
+ const BUILTIN_MODULES_VAR = '__PAGES_KIT_BUILTIN_MODULES__';
61
+ helper_1.logger.log('BUILTIN_MODULES_VAR', BUILTIN_MODULES_VAR);
61
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"]`,
62
65
  const defaultBlockExternals = {
63
66
  // 核心 React 相关
64
- react: `@blocklet/pages-kit/builtin/react`,
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`,
65
77
  // 'react-dom': `@blocklet/pages-kit/builtin/react-dom`,
66
- 'react-router-dom': `@blocklet/pages-kit/builtin/react-router-dom`,
67
78
  // 'react-hook-form': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/react-hook-form"]`,
68
79
  // 'react-wrap-balancer': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/react-wrap-balancer"]`,
69
80
  // // ArcBlock 相关
@@ -89,9 +100,14 @@ const defaultBlockExternals = {
89
100
  // 'react-syntax-highlighter': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/async/react-syntax-highlighter"]`,
90
101
  // 'react-scroll-to-bottom': `window.${BUILTIN_MODULES_VAR}.modules["@blocklet/pages-kit/builtin/async/react-scroll-to-bottom"]`,
91
102
  // // 浏览器原生
92
- crypto: 'window.crypto',
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: '',
93
108
  };
94
109
  function initBlockStudioPlugins(options) {
110
+ var _a;
95
111
  const workingDir = (options === null || options === void 0 ? void 0 : options.cwd) || process.cwd();
96
112
  const entryFilesPattern = (options === null || options === void 0 ? void 0 : options.entryFilesPattern) || (0, helper_1.getBlockEntryFilesPattern)();
97
113
  // 处理 blockExternals 参数
@@ -114,12 +130,37 @@ function initBlockStudioPlugins(options) {
114
130
  blockExternals: externalMappings, // 添加日志输出
115
131
  });
116
132
  // 为 globals 创建一个无前缀的版本
117
- const externalMappingsWithoutWindow = Object.fromEntries(Object.entries(externalMappings).filter(([, value]) => !value.startsWith('window.')));
118
- const externalMappingsWithWindow = Object.fromEntries(Object.entries(externalMappings).filter(([, value]) => value.startsWith('window.')));
119
- console.warn({
120
- externalMappingsWithoutWindow,
121
- externalMappingsWithWindow,
122
- });
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'];
123
164
  return [
124
165
  // {
125
166
  // name: 'vite-plugin-block-studio-dev',
@@ -135,6 +176,9 @@ function initBlockStudioPlugins(options) {
135
176
  // return html;
136
177
  // },
137
178
  // },
179
+ (0, vite_plugin_node_polyfills_1.nodePolyfills)({
180
+ include: ['crypto'],
181
+ }),
138
182
  {
139
183
  name: 'vite-plugin-block-studio-build',
140
184
  apply: 'build',
@@ -142,50 +186,48 @@ function initBlockStudioPlugins(options) {
142
186
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
143
187
  config(_config) {
144
188
  return __awaiter(this, void 0, void 0, function* () {
145
- var _a, _b, _c, _d;
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;
189
+ var _a, _b, _c, _d, _e;
149
190
  const name = yield Promise.resolve(`${`${workingDir}/package.json`}`).then(s => __importStar(require(s))).then((res) => res.name).catch(() => 'MyLib');
150
- const entryList = (0, helper_1.findComponentFiles)({ cwd: workingDir })
151
- .map((entry) => {
152
- const { blockName } = entry;
153
- const { isHtml } = entry;
154
- const { file } = entry;
155
- if (isHtml) {
156
- return [blockName, `${vite_plugin_html_transform_1.VIRTUAL_MODULE_ID}?dir=${path.dirname(file)}`];
157
- }
158
- return [blockName, path.resolve(workingDir, file)];
159
- })
160
- .filter(Boolean)
161
- .filter(([blockName]) => !filterModules || filterModules.includes(blockName || ''));
162
- const multiMode = entryList.length > 1;
163
191
  return {
164
- 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: {
165
203
  name,
166
204
  entry: Object.assign({}, Object.fromEntries(entryList)),
167
- formats: ['es', !multiMode ? 'umd' : 'cjs'],
205
+ formats,
168
206
  fileName: (format, entryName) => `${format}/${entryName}.js`,
169
- }, rollupOptions: Object.assign(Object.assign({}, (_b = _config === null || _config === void 0 ? void 0 : _config.build) === null || _b === void 0 ? void 0 : _b.rollupOptions), { external: [...Object.keys(externalMappings)], 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: () => {
170
217
  return '[format]/_chunks/[name]-[hash].js';
171
218
  },
172
219
  // 使用没有 window. 前缀的映射用于 globals 配置
173
- globals: Object.fromEntries(Object.entries(externalMappings).map(([key]) => {
174
- // 移除 "window." 前缀并转换为首字母大写的驼峰式
175
- const rawName = String(key);
176
- const globalName = (0, upperFirst_1.default)((0, camelCase_1.default)(rawName));
177
- return [key, globalName];
178
- })),
220
+ globals: globalsMappings,
179
221
  // 确保正确处理命名导出和默认导出
180
- interop: 'auto', paths: externalMappingsWithoutWindow }) }) }),
222
+ interop: 'auto', paths: pathMappings, esModule: true }) }) }),
181
223
  };
182
224
  });
183
225
  },
184
226
  configResolved(config) {
185
- console.info(config.build.rollupOptions);
227
+ helper_1.logger.info('config.build', config.build);
186
228
  },
187
229
  },
188
- Object.assign(Object.assign({}, (0, rollup_plugin_external_globals_1.default)(externalMappingsWithWindow)), { apply: 'build', enforce: 'post' }),
230
+ Object.assign(Object.assign({}, (0, rollup_plugin_external_globals_1.default)(externalGlobalMappings)), { apply: 'build', enforce: 'pre' }),
189
231
  // @ts-ignore
190
232
  (0, vite_plugin_react_pages_1.default)({
191
233
  pagesDir,
@@ -240,9 +282,23 @@ function initBlockStudioPlugins(options) {
240
282
  name: 'build-force-exit',
241
283
  apply: 'build',
242
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
+ },
243
296
  closeBundle() {
244
297
  // ensure vite build exit
245
- process.exit(0);
298
+ setTimeout(() => {
299
+ // wait for 1 second, to ensure all logs are flushed
300
+ process.exit(0);
301
+ }, 1000);
246
302
  },
247
303
  },
248
304
  ];