新增前端vue

This commit is contained in:
2025-11-26 13:55:01 +08:00
parent ae391f1b94
commit ffd5a6ad66
781 changed files with 83348 additions and 0 deletions

View File

@@ -0,0 +1,78 @@
/**
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
* No deletion without permission, or be held responsible to law.
* @author ThinkGem
*/
import { type PluginOption } from 'vite';
import { PackageJson } from 'pkg-types';
import { getEnvConfig } from './index';
import { getEnvConfigName } from './getEnvConfigName';
import { version } from '../package.json';
import colors from 'picocolors';
const PLUGIN_NAME = 'app-config';
const APP_CONFIG_FILE_NAME = '_app.config.js';
export function appConfigPlugin(isBuild: boolean, viteEnv: ViteEnv): PluginOption {
if (!isBuild) {
return {
name: PLUGIN_NAME,
};
}
let publicPath: string;
let appConfigContent: string;
return {
name: PLUGIN_NAME,
async configResolved(_config) {
publicPath = _config.base;
appConfigContent = await getConfigContent(viteEnv);
},
async transformIndexHtml(html) {
publicPath = (publicPath.endsWith('/') ? publicPath : `${publicPath}/`) || '/';
const src = `${publicPath}${APP_CONFIG_FILE_NAME}?v=${version}-${new Date().getTime()}`;
return { html, tags: [{ tag: 'script', attrs: { src } }] };
},
async generateBundle() {
try {
this.emitFile({
type: 'asset',
fileName: APP_CONFIG_FILE_NAME,
source: appConfigContent,
});
this.emitFile({
type: 'asset',
fileName: 'timestamp.txt',
source: `${new Date().getTime()}`,
});
console.log(colors.cyan(`✨configuration file is build successfully!`));
} catch (error) {
console.log(colors.red('configuration file configuration file failed to package:\n' + error));
}
},
};
}
async function getConfigContent(env: ViteEnv) {
const config = await getEnvConfig();
const envConfigName = getEnvConfigName(env);
const windowVariable = `window.${envConfigName}`;
// Ensure that the variable will not be modified
let source = `${windowVariable}=${JSON.stringify(config)};`;
source += `
Object.freeze(${windowVariable});
Object.defineProperty(window, "${envConfigName}", {
configurable: false,
writable: false,
});
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://hm.baidu.com/hm.js?65b88e88a94e0118de2962f328f17622";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
`.replace(/(?!var|\b)\s/g, '');
return source;
}

View File

@@ -0,0 +1,8 @@
/**
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
* No deletion without permission, or be held responsible to law.
* @author ThinkGem
*/
export const getEnvConfigName = (env: Record<string, any>) => {
return `__PRODUCTION__${env.VITE_GLOB_APP_SHORT_NAME || '__APP'}__CONF__`.toUpperCase().replace(/\s/g, '');
};

View File

@@ -0,0 +1,75 @@
/**
* Copyright (c) 2013-Now http://jeesite.com All rights reserved.
* No deletion without permission, or be held responsible to law.
* @author ThinkGem
*/
import fs from 'fs-extra';
import dotenv from 'dotenv';
import { join } from 'node:path';
// Read all environment variable configuration files to process.env
export function wrapperEnv(envConf: Recordable): ViteEnv {
const ret: any = {};
for (const envName of Object.keys(envConf)) {
let realName = envConf[envName].replace(/\\n/g, '\n');
realName = realName === 'true' ? true : realName === 'false' ? false : realName;
if (envName === 'VITE_PORT') {
realName = Number(realName);
}
if (envName === 'VITE_PROXY' && realName) {
try {
realName = JSON.parse(realName.replace(/'/g, '"'));
} catch (error) {
realName = '';
}
}
ret[envName] = realName;
if (typeof realName === 'string') {
process.env[envName] = realName;
} else if (typeof realName === 'object') {
process.env[envName] = JSON.stringify(realName);
}
}
return ret;
}
/**
* 获取当前环境下生效的配置文件名
*/
function getEnvConfigFiles() {
const script = process.env.npm_lifecycle_script;
const reg = new RegExp('--mode ([a-z_\\d]+)');
const result = reg.exec(script as string) as any;
if (result) {
const mode = result[1] as string;
return ['.env', `.env.${mode}`];
}
return ['.env', '.env.production'];
}
/**
* Get the environment variables starting with the specified prefix
* @param match prefix
* @param confFiles ext
*/
export async function getEnvConfig(match = 'VITE_(GLOB|FILE)_', confFiles = getEnvConfigFiles()) {
let envConfig = {};
for (const item of confFiles) {
try {
const envPath = await fs.readFile(join(process.cwd(), item), { encoding: 'utf8' });
const env = dotenv.parse(envPath);
envConfig = { ...envConfig, ...env };
} catch (e) {
console.error(`Error in parsing ${item}`, e);
}
}
const reg = new RegExp(`^(${match})`);
Object.keys(envConfig).forEach((key) => {
if (!reg.test(key)) {
Reflect.deleteProperty(envConfig, key);
}
});
return envConfig;
}