@10yun/open-sdk 0.3.89

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.
@@ -0,0 +1,104 @@
1
+ #### 简介
2
+
3
+ > vite-plugin-qiankun: 帮助应用快速接入乾坤的vite插件
4
+
5
+ - 保留vite构建es模块的优势
6
+ - 一键配置,不影响已有的vite配置
7
+ - 支持vite开发环境
8
+
9
+ #### 快速开始
10
+
11
+ ###### 1、在 `vite.config.ts` 中安装插件
12
+ ```typescript
13
+ // vite.config.ts
14
+
15
+ import qiankun from 'vite-plugin-qiankun';
16
+
17
+ export default {
18
+ // 这里的 'myMicroAppName' 是子应用名,主应用注册时AppName需保持一致
19
+ plugins: [qiankun('myMicroAppName')],
20
+ // 生产环境需要指定运行域名作为base
21
+ base: 'http://xxx.com/'
22
+ }
23
+ ```
24
+ ###### 2、在入口文件里面写入乾坤的生命周期配置
25
+
26
+ ```typescript
27
+ // main.ts
28
+ import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun/dist/helper';
29
+
30
+ // some code
31
+ renderWithQiankun({
32
+ mount(props) {
33
+ console.log('mount');
34
+ render(props);
35
+ },
36
+ bootstrap() {
37
+ console.log('bootstrap');
38
+ },
39
+ unmount(props: any) {
40
+ console.log('unmount');
41
+ const { container } = props;
42
+ const mountRoot = container?.querySelector('#root');
43
+ ReactDOM.unmountComponentAtNode(
44
+ mountRoot || document.querySelector('#root'),
45
+ );
46
+ },
47
+ });
48
+
49
+ if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
50
+ render({});
51
+ }
52
+ ```
53
+
54
+ ###### 3、dev下作为子应用调试
55
+
56
+ > 因为开发环境作为子应用时与热更新插件(可能与其他修改html的插件也会存在冲突)有冲突,所以需要额外的调试配置
57
+
58
+ ```typescript
59
+ // useDevMode 开启时与热更新插件冲突,使用变量切换
60
+ const useDevMode = true
61
+
62
+ const baseConfig: UserConfig = {
63
+ plugins: [
64
+ ...(
65
+ useDevMode ? [] : [
66
+ reactRefresh()
67
+ ]
68
+ ),
69
+ qiankun('viteapp', {
70
+ useDevMode
71
+ })
72
+ ],
73
+ }
74
+ ```
75
+ 上面例子中 `useDevMode = true` 则不使用热更新插件,`useDevMode = false` 则能使用热更新,但无法作为子应用加载。
76
+
77
+ ###### 4、其它使用注意点 `qiankunWindow`
78
+
79
+ 因为es模块加载与`qiankun`的实现方式有些冲突,所以使用本插件实现的`qiankun`微应用里面没有运行在js沙盒中。所以在不可避免需要设置window上的属性时,尽量显示的操作js沙盒,否则可能会对其它子应用产生副作用。qiankun沙盒使用方式
80
+ ```typescript
81
+ import { qiankunWindow } from 'vite-plugin-qiankun/dist/helper';
82
+
83
+ qiankunWindow.customxxx = 'ssss'
84
+
85
+ if (qiankunWindow.__POWERED_BY_QIANKUN__) {
86
+ console.log('我正在作为子应用运行')
87
+ }
88
+
89
+ ```
90
+
91
+ #### 例子
92
+
93
+ 详细的信息可以参考例子里面的使用方式
94
+ ```
95
+ git clone xx
96
+ npm install
97
+ npm run example:install
98
+ # 生产环境调试demo
99
+ npm run example:start
100
+ # vite开发环境demo, demo中热更新已经关闭
101
+ npm run example:start-vite-dev
102
+ ```
103
+
104
+
@@ -0,0 +1,162 @@
1
+ import { loadEnv } from 'vite';
2
+ import path from 'path';
3
+ import qiankun from './vite-plugin-qiankun/es/index.js';
4
+ export { qiankunWindow } from './vite-plugin-qiankun/es/helper.js';
5
+ import { encryptStrEncode } from './common/encipher.js';
6
+ import { buildRbac } from './common/buildRbac.js';
7
+ /**
8
+ * 使用 plugins: [shiyunInjectHtml({ key: 'value' })]
9
+ * @param {*} data
10
+ * @returns
11
+ */
12
+ export function shiyunInjectHtml(data) {
13
+ return {
14
+ name: 'vite-plugin-shiyun-inject-html',
15
+ transformIndexHtml(html) {
16
+ Object.keys(data).forEach((key) => {
17
+ html = html.replace(`<!--%${key}%-->`, data[key]);
18
+ });
19
+ return html;
20
+ }
21
+ };
22
+ }
23
+ /**
24
+ *
25
+ * @param {*} params
26
+ * @returns
27
+ */
28
+ export function shiyunRelease(params = { menu_arr: [] }) {
29
+ // 根据当前工作目录中的 `mode` 加载 .env 文件
30
+ const PROCESS_CWD = process.cwd();
31
+ const BASENAME_SIGN = path.basename(PROCESS_CWD);
32
+ const ENV_MODE = process.env.NODE_ENV;
33
+ // 设置第三个参数为 '' 来加载所有环境变量,而不管是否有 `VITE_` 前缀。
34
+ const ENV_ARR_VITE = loadEnv(ENV_MODE, PROCESS_CWD, 'VITE_');
35
+ const {
36
+ VITE_SY_PRODUCT_PORT,
37
+ VITE_SY_PRODUCT_SIGN,
38
+ VITE_SY_PRODUCT_VERS_DOMAIN,
39
+ VITE_SY_PRODUCT_VERS_FULL,
40
+ VITE_SY_PRODUCT_VERS_ALONE,
41
+ VITE_SY_API_DEV,
42
+ VITE_SY_APP_PROJECT_SIGN,
43
+ VITE_SY_APP_CONNECT_ID,
44
+ VITE_SY_APP_CONNECT_ROLE
45
+ } = ENV_ARR_VITE;
46
+ const LAST_PRODUCT_SIGN = VITE_SY_PRODUCT_SIGN || BASENAME_SIGN;
47
+ const LAST_BUILD_VERS_DOMAIN = VITE_SY_PRODUCT_VERS_DOMAIN || '';
48
+ const LAST_BUILD_VERS_SIGN = VITE_SY_PRODUCT_VERS_ALONE == 'on' ? '' : LAST_PRODUCT_SIGN;
49
+ const LAST_BUILD_VERS_FULL = VITE_SY_PRODUCT_VERS_FULL || '';
50
+ if (ENV_MODE == 'production') {
51
+ buildRbac(params.menu_arr, PROCESS_CWD, {
52
+ RBAC_BASE_SIGN: LAST_PRODUCT_SIGN
53
+ });
54
+ }
55
+
56
+ // const sign_flag = `${VITE_SY_APP_PROJECT_SIGN}---${VITE_SY_APP_CONNECT_ID}---${VITE_SY_APP_CONNECT_ROLE}`;
57
+ // const sign_param = encryptStrEncode(sign_flag);
58
+ // const get_api = `${VITE_SY_API_DEV}/base.base_load/common`;
59
+
60
+ /**
61
+ * useDevMode 开启时与热更新插件冲突
62
+ * useDevMode = true 时不开启热更新
63
+ * 如果是在主应用中加载子应用vite,必须打开这个,否则vite加载不成功, 单独运行没影响
64
+ */
65
+ const useDevMode = true;
66
+
67
+ return [
68
+ // 微应用名字,与主应用注册的微应用名字保持一致
69
+ qiankun(LAST_PRODUCT_SIGN, {
70
+ useDevMode
71
+ }),
72
+ {
73
+ name: 'vite-plugin-shiyun-release',
74
+ // apply: 'build', // 'build' 或 'serve' ,在什么情况下使用
75
+ config: (config, { mode, command }) => {
76
+ let basePath = '/';
77
+ if (mode == 'production') {
78
+ let buildPath = `${LAST_BUILD_VERS_DOMAIN}/${LAST_BUILD_VERS_SIGN}/${LAST_BUILD_VERS_FULL}/`;
79
+ /**
80
+ * 使用正则表达式进行过滤
81
+ * 过滤除了 https:// 和 http:// 以外的 // 和 ///
82
+ */
83
+ buildPath = buildPath.replace(/\/(undefined\/)+/g, '/');
84
+ buildPath = buildPath.replace(/([^:]\/)\/+/g, '$1');
85
+ basePath = buildPath;
86
+ }
87
+ return {
88
+ base: basePath,
89
+ server: {
90
+ port: VITE_SY_PRODUCT_PORT
91
+ },
92
+ esbuild: {
93
+ drop: ['console', 'debugger']
94
+ },
95
+ optimizeDeps: {
96
+ esbuildOptions: {
97
+ target: 'esnext'
98
+ }
99
+ // 确保 open-sdk 包在依赖优化中
100
+ // include: ['@10yun/open-sdk']
101
+ /**
102
+ * TODO
103
+ * 确保 open-sdk 包不在依赖优化中,不然造成修改 【项目@/下的相关文件】,vite无法及时热更新响应
104
+ * 添加下列 exclude 会爆 dayjs 问题
105
+ */
106
+ // exclude: ['@10yun/open-sdk']
107
+ },
108
+ build: {
109
+ // target: ['chrome89', 'edge89', 'firefox89', 'safari15', 'chrome87', 'edge88', 'es2020', 'firefox78', 'safari14'],
110
+ target: 'esnext',
111
+ sourcemap: false,
112
+ chunkSizeWarningLimit: 2000
113
+ },
114
+ output: {
115
+ // 把子应用打包成 umd 库格式
116
+ // library: `${子应用名}-[name]`,
117
+ // libraryTarget: 'umd',
118
+ // jsonpFunction: `webpackJsonp_${子应用名}`
119
+ library: `${BASENAME_SIGN}-[name]`,
120
+ libraryTarget: 'umd',
121
+ jsonpFunction: `webpackJsonp_${BASENAME_SIGN}`
122
+ }
123
+ };
124
+ },
125
+ configResolved(config) {
126
+ // console.log('---111---');
127
+ // console.log('configResolved', config.base, config.mode, config.command, config.esbuild);
128
+ // config.base = 'asdad';
129
+ // isProduction = config.command === 'build' || config.isProduction;
130
+ // base = config.base;
131
+ },
132
+ configureServer(server) {
133
+ // 在开发服务器配置时可以执行的逻辑
134
+ // console.log('开发的时候', server);
135
+ },
136
+ transformIndexHtml(html) {
137
+ if (ENV_MODE == 'development') {
138
+ // 删除带 mode="pro" 的标签
139
+ html = html.replace(/<(script)\b[^>]*?mode="pro"[^>]*>[\s\s]*?<\/script>/gi, '');
140
+ html = html.replace(/<(link|script)\b[^>]*?mode="pro"[^>]*>/gi, '');
141
+ }
142
+ if (ENV_MODE == 'production') {
143
+ // 删除带 mode="dev" 的标签
144
+ if (VITE_SY_PRODUCT_VERS_ALONE != 'on') {
145
+ html = html.replace(/<(script)\b[^>]*?mode="dev"[^>]*>[\s\S]*?<\/script>/gi, '');
146
+ html = html.replace(/<(link|script)\b[^>]*?mode="dev"[^>]*>/gi, '');
147
+ }
148
+ }
149
+ // 删除空行
150
+ html = html.replace(/\n\s*\n/g, '\n');
151
+ return html;
152
+ }
153
+ // transform(code, id) {
154
+ // // console.log('transform');
155
+ // if (id.endsWith('.example')) {
156
+ // // 对特定文件进行转换逻辑
157
+ // return code + '\n// This is an example plugin transformation';
158
+ // }
159
+ // }
160
+ }
161
+ ];
162
+ }