@10yun/open-sdk
Version:
开放平台接入sdk
156 lines (152 loc) • 6.51 kB
JavaScript
import path from 'path';
import { buildRbac } from './build-rbac.js';
import { parseDomainUrl } from './common.js';
/**
* 通用
*/
export function shiyunPluginCommon(ENV_ARR_VITE, ENV_ARR_CICD, ENV_MODE, params) {
const { CICD_BUILD_DOMAIN_SCHEME, CICD_BUILD_DOMAIN_ALONE, CICD_BUILD_VERS_FULL, CICD_BUILD_VERS_CODE } = ENV_ARR_CICD;
const { VITE_SY_PRODUCT_PORT, VITE_SY_PRODUCT_SIGN } = ENV_ARR_VITE;
// 根据当前工作目录中的 `mode` 加载 .env 文件
const PROCESS_CWD = process.cwd();
const BASENAME_SIGN = path.basename(PROCESS_CWD);
const LAST_BUILD_SIGN = VITE_SY_PRODUCT_SIGN || BASENAME_SIGN;
let LAST_BUILD_DOMAIN = '';
/**
* 非独立[子应用情况]
*/
if (CICD_BUILD_DOMAIN_ALONE == 'on') {
LAST_BUILD_DOMAIN = CICD_BUILD_DOMAIN_SCHEME;
} else {
LAST_BUILD_DOMAIN = `${CICD_BUILD_DOMAIN_SCHEME}/${LAST_BUILD_SIGN}/${CICD_BUILD_VERS_FULL}/`;
}
let basePath = '/';
if (ENV_MODE === 'production') {
basePath = parseDomainUrl(`${LAST_BUILD_DOMAIN}/`);
buildRbac(params.menu_arr, PROCESS_CWD, {
RBAC_BASE_SIGN: LAST_BUILD_SIGN
});
}
return {
name: 'vite-plugin-shiyun-release-common',
// apply: 'build', // 'build' 或 'serve' ,在什么情况下使用
config: (config, { mode, command }) => {
return {
base: basePath,
server: {
port: VITE_SY_PRODUCT_PORT
},
esbuild: {
drop: ['console', 'debugger']
},
// oxc: {
// dropConsole: true,
// compress: {
// drop_console: true, // Remove console statements
// drop_debugger: true, // Remove debugger statements
// // passes: 2 // Number of compression passes
// dropConsole: true,
// dropDebugger: true,
// treeshake: {
// manualPureFunctions: ['console']
// }
// }
// },
optimizeDeps: {
// 确保 open-sdk 包在依赖优化中
// include: ['@10yun/open-sdk']
exclude: ['@10yun/open-sdk/uncompiled']
/**
* TODO
* 确保 open-sdk 包不在依赖优化中,不然造成修改 【项目@/下的相关文件】,vite无法及时热更新响应
* 添加下列 exclude 会爆 dayjs 问题
*/
// exclude: ['@10yun/open-sdk']
},
build: {
minify: 'esbuild', // oxc 、 esbuild
// target: ['chrome89', 'edge89', 'firefox89', 'safari15', 'chrome87', 'edge88', 'es2020', 'firefox78', 'safari14'],
target: 'esnext',
sourcemap: false,
chunkSizeWarningLimit: 2200
// rolldownOptions: {
// output: {
// advancedChunks: {
// groups: [
// { name: 'vue-vendor', test: /[\\/]node_modules[\\/](vue)[\\/]/ },
// { name: 'pinia', test: /[\\/]node_modules[\\/]pinia[\\/]/ },
// // { name: '@10yun-ui', test: /[\\/]node_modules[\\/]@10yun[\\/]/ },
// { name: '@10yun-ui', test: /[\\/]node_modules[\\/]@10yun\/cv-pc-ui[\\/]/ },
// { name: '@10yun-opensdk', test: /[\\/]node_modules[\\/]@10yun\/open-sdk[\\/]/ },
// { name: '@10yun-utils', test: /[\\/]node_modules[\\/]@10yun\/cv-js-utils[\\/]/ },
// // 把 element 单独拆分
// { name: 'element-plus', test: /[\\/]node_modules[\\/]element-plus[\\/]/ }
// ]
// }
// }
// }
},
output: {
// 把子应用打包成 umd 库格式
// library: `${子应用名}-[name]`,
// libraryTarget: 'umd',
// jsonpFunction: `webpackJsonp_${子应用名}`
library: `${LAST_BUILD_SIGN}-[name]`,
libraryTarget: 'umd',
jsonpFunction: `webpackJsonp_${LAST_BUILD_SIGN}`
}
};
},
configResolved(config) {
// console.log('---111---');
// console.log('configResolved', config.base, config.mode, config.command, config.esbuild);
// config.base = 'asdad';
// isProduction = config.command === 'build' || config.isProduction;
// base = config.base;
},
configureServer(server) {
// 在开发服务器配置时可以执行的逻辑
// console.log('开发的时候', server);
},
transformIndexHtml(html) {
if (ENV_MODE === 'development') {
// 删除带 mode="pro" 的标签
// html = html.replace(/<(script)\b[^>]*?mode="pro"[^>]*>[\s\S]*?<\/script>/gi, '');
html = html.replace(/<script\b[^>]*?mode="pro"[^>]*>[\s\S]*?<\/script>/gi, '');
html = html.replace(/<(link|script)\b[^>]*?mode="pro"[^>]*>/gi, '');
}
if (ENV_MODE === 'production') {
// 删除带 mode="dev" 的标签
if (CICD_BUILD_DOMAIN_ALONE !== 'on') {
// html = html.replace(/<(script)\b[^>]*?mode="dev"[^>]*>[\s\S]*?<\/script>/gi, '');
html = html.replace(/<script\b[^>]*?mode="dev"[^>]*>[\s\S]*?<\/script>/gi, '');
html = html.replace(/<(link|script)\b[^>]*?mode="dev"[^>]*>/gi, '');
}
}
/**
* 新增十云相关信息
*/
const introductionInfo = 'saas软件,软件开发,软件制定,公众号,小程序,app,十云,十云科技,福州十云,ctocode,10yun.com';
let shiyunInfo = ` <meta name="description" content="${introductionInfo}" />\n`;
shiyunInfo += ` <meta name="keywords" content="${introductionInfo}" />\n`;
shiyunInfo += ` <meta name="author" content="十云_ctocode_技术支持_https://www.10yun.com" />\n`;
shiyunInfo += ` <meta name="copyright" content="福州十云科技有限公司" />\n`;
shiyunInfo += ` <meta name="website" content="https://www.10yun.com/" />\n`;
if (CICD_BUILD_VERS_FULL) {
shiyunInfo += ` <meta name="version" content="${CICD_BUILD_VERS_FULL}_${CICD_BUILD_VERS_CODE}" />\n`;
}
// 将meta标签插入到head标签内(</head>闭合标签之前)
html = html.replace('</head>', `\n${shiyunInfo}\n</head>`);
// 删除空行
html = html.replace(/\n\s*\n/g, '\n');
return html;
}
// transform(code, id) {
// // console.log('transform');
// if (id.endsWith('.example')) {
// // 对特定文件进行转换逻辑
// return code + '\n// This is an example plugin transformation';
// }
// }
};
}