@blocklet/pages-kit-block-studio 0.1.34 → 0.1.36

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.
@@ -11,14 +11,58 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  // import typescript from '@rollup/plugin-typescript';
12
12
  import { readFileSync, existsSync } from 'fs';
13
13
  import * as path from 'path';
14
+ import externalGlobals from 'rollup-plugin-external-globals';
14
15
  import { joinURL } from 'ufo';
15
16
  import pages, { DefaultPageStrategy } from 'vite-plugin-react-pages';
16
17
  import { findComponentFiles, setBlockEntryFilesPattern, getBlockEntryFilesPattern, getBlockName, logger, } from '../utils/helper';
17
18
  import { initHtmlPreviewTransformPlugin, VIRTUAL_MODULE_ID, readHtmlFiles, generateComponent, } from './vite-plugin-html-transform';
19
+ const BUILTIN_MODULES_VAR = '__PAGES_KIT_BUILTIN_MODULES__';
18
20
  // import initRemoteScriptLocalizerPlugin from './vite-plugin-remote-script-localizer';
21
+ const defaultBlockExternals = {
22
+ // 核心 React 相关
23
+ react: `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/react"]`,
24
+ 'react-dom': `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/react-dom"]`,
25
+ 'react-router-dom': `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/react-router-dom"]`,
26
+ 'react-hook-form': `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/react-hook-form"]`,
27
+ 'react-wrap-balancer': `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/react-wrap-balancer"]`,
28
+ // ArcBlock 相关
29
+ '@arcblock/ux': `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/arcblock/ux"]`,
30
+ '@arcblock/did-connect/lib/Session': `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/session"]`,
31
+ '@arcblock/ux/lib/Locale/context': `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/locale"]`,
32
+ // MUI 相关
33
+ // '@mui/material': `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/mui/material"]`,
34
+ // '@mui/lab': `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/mui/lab"]`,
35
+ // 其他工具库
36
+ dayjs: `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/dayjs"]`,
37
+ zustand: `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/zustand"]`,
38
+ 'zustand/middleware/immer': `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/zustand/middleware/immer"]`,
39
+ immer: `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/immer"]`,
40
+ stream: `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/stream"]`,
41
+ '@iconify/react': `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/iconify/react"]`,
42
+ // 页面相关组件
43
+ '@blocklet/pages-kit/builtin/components': `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/components"]`,
44
+ '@blocklet/pages-kit/builtin/page/header': `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/page/header"]`,
45
+ '@blocklet/pages-kit/builtin/pages-kit': `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/pages-kit"]`,
46
+ // 异步组件
47
+ 'react-markdown': `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/async/react-markdown"]`,
48
+ 'react-syntax-highlighter': `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/async/react-syntax-highlighter"]`,
49
+ 'react-scroll-to-bottom': `window[${BUILTIN_MODULES_VAR}].modules["@blocklet/pages-kit/builtin/async/react-scroll-to-bottom"]`,
50
+ // 浏览器原生
51
+ crypto: 'window.crypto',
52
+ };
19
53
  export function initBlockStudioPlugins(options) {
20
54
  const workingDir = (options === null || options === void 0 ? void 0 : options.cwd) || process.cwd();
21
55
  const entryFilesPattern = (options === null || options === void 0 ? void 0 : options.entryFilesPattern) || getBlockEntryFilesPattern();
56
+ // 处理 blockExternals 参数
57
+ let externalMappings = defaultBlockExternals;
58
+ if (typeof (options === null || options === void 0 ? void 0 : options.blockExternals) === 'function') {
59
+ // 如果是函数,则调用它并传入默认值
60
+ externalMappings = options.blockExternals(defaultBlockExternals);
61
+ }
62
+ else if (options === null || options === void 0 ? void 0 : options.blockExternals) {
63
+ // 如果是对象,直接使用
64
+ externalMappings = options.blockExternals;
65
+ }
22
66
  setBlockEntryFilesPattern(entryFilesPattern);
23
67
  // fallback to __dirname if _theme.tsx not exists
24
68
  const pagesDir = existsSync(path.join(workingDir, '_theme.tsx')) ? workingDir : __dirname.replace('cjs', 'esm');
@@ -26,6 +70,7 @@ export function initBlockStudioPlugins(options) {
26
70
  cwd: workingDir,
27
71
  entryFilesPattern: getBlockEntryFilesPattern(),
28
72
  pagesDir,
73
+ blockExternals: externalMappings, // 添加日志输出
29
74
  });
30
75
  return [
31
76
  // {
@@ -48,7 +93,7 @@ export function initBlockStudioPlugins(options) {
48
93
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
49
94
  config(_config) {
50
95
  return __awaiter(this, void 0, void 0, function* () {
51
- var _a;
96
+ var _a, _b, _c, _d;
52
97
  const filterModules = process.argv.includes('--filter')
53
98
  ? (_a = process.argv[process.argv.indexOf('--filter') + 1]) === null || _a === void 0 ? void 0 : _a.split(',')
54
99
  : null;
@@ -66,36 +111,35 @@ export function initBlockStudioPlugins(options) {
66
111
  .filter(Boolean)
67
112
  .filter(([blockName]) => !filterModules || filterModules.includes(blockName || ''));
68
113
  const multiMode = entryList.length > 1;
114
+ // 将 externalGlobals 添加到插件中
115
+ const plugins = [...(_config.plugins || []), externalGlobals(externalMappings)];
116
+ // 为 globals 创建一个无前缀的版本
117
+ const externalMappingsWithoutWindow = Object.fromEntries(Object.entries(externalMappings).map(([key, value]) => {
118
+ // 移除 "window." 前缀
119
+ const globalName = String(value).replace(/^window\./, '');
120
+ return [key, globalName];
121
+ }));
69
122
  return {
70
- build: Object.assign({ cssCodeSplit: false, lib: {
123
+ plugins,
124
+ build: Object.assign(Object.assign({}, _config === null || _config === void 0 ? void 0 : _config.build), { cssCodeSplit: false, lib: {
71
125
  name,
72
126
  entry: Object.assign({}, Object.fromEntries(entryList)),
73
127
  formats: ['es', !multiMode ? 'umd' : 'cjs'],
74
128
  fileName: (format, entryName) => `${format}/${entryName}.js`,
75
- }, rollupOptions: {
76
- external: (id) => {
77
- const skip = ['react', 'crypto'];
78
- if (skip.some((s) => id === s)) {
129
+ }, 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) => {
130
+ // externalMappings 中的键也添加到 external 中
131
+ const skip = [...Object.keys(externalMappings)];
132
+ if (skip.some((s) => id === s || id.startsWith(`${s}/`))) {
79
133
  return true;
80
134
  }
81
135
  return false;
82
- },
83
- output: {
84
- chunkFileNames: () => {
136
+ }, 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: () => {
85
137
  return '[format]/_chunks/[name]-[hash].js';
86
- },
87
- // 为所有外部依赖提供全局变量
88
- globals: {
89
- react: 'React',
90
- },
91
- paths: {
92
- // Redirect 'react' imports to '@blocklet/pages-kit/builtin/react'
93
- react: '@blocklet/pages-kit/builtin/react',
94
- },
138
+ },
139
+ // 使用没有 window. 前缀的映射用于 globals 配置
140
+ globals: externalMappingsWithoutWindow,
95
141
  // 确保正确处理命名导出和默认导出
96
- interop: 'auto',
97
- },
98
- } }, _config === null || _config === void 0 ? void 0 : _config.build),
142
+ interop: 'auto' }) }) }),
99
143
  };
100
144
  });
101
145
  },