diff --git a/build/script/updateHtml.ts b/build/script/updateHtml.ts index f5de56a2..e1375676 100644 --- a/build/script/updateHtml.ts +++ b/build/script/updateHtml.ts @@ -74,9 +74,7 @@ function injectCdnjs(html: string) { export async function runUpdateHtml() { const outDir = viteConfig.outDir || 'dist'; const indexPath = getCwdPath(outDir, 'index.html'); - if (!existsSync(`${indexPath}`)) { - return; - } + if (!existsSync(indexPath)) return; try { let processedHtml = ''; const rawHtml = readFileSync(indexPath, 'utf-8'); @@ -92,7 +90,9 @@ export async function runUpdateHtml() { } if (minify) { const { enable, ...miniOpt } = minify; - processedHtml = HtmlMinifier.minify(processedHtml, miniOpt); + if (enable) { + processedHtml = HtmlMinifier.minify(processedHtml, miniOpt); + } } writeFileSync(indexPath, processedHtml); diff --git a/build/transform/require-context/index.ts b/build/transform/require-context/index.ts new file mode 100644 index 00000000..0800a3ba --- /dev/null +++ b/build/transform/require-context/index.ts @@ -0,0 +1,95 @@ +// https://github.com/luxueyan/vite-transform-globby-import/blob/master/src/index.ts + +// TODO 目前还不能监听文件新增及删除 内容已经改变,缓存问题? +// 可以使用,先不打算集成 +import { join } from 'path'; +import { lstatSync } from 'fs'; +import glob from 'glob'; +import { createResolver, Resolver } from 'vite/dist/node/resolver.js'; +import { Transform } from 'vite/dist/node/transform.js'; + +const modulesDir: string = join(process.cwd(), '/node_modules/'); + +interface SharedConfig { + root?: string; + alias?: Record; + resolvers?: Resolver[]; +} + +function template(template: string) { + return (data: { [x: string]: any }) => { + return template.replace(/#([^#]+)#/g, (_, g1) => data[g1] || g1); + }; +} + +const globbyTransform = function (config: SharedConfig): Transform { + const resolver = createResolver( + config.root || process.cwd(), + config.resolvers || [], + config.alias || {} + ); + const cache = new Map(); + + const urlMap = new Map(); + return { + test({ path }) { + const filePath = path.replace('\u0000', ''); // why some path startsWith '\u0000'? + try { + return ( + !filePath.startsWith(modulesDir) && + /\.(vue|js|jsx|ts|tsx)$/.test(filePath) && + lstatSync(filePath).isFile() + ); + } catch { + return false; + } + }, + transform({ code, path, isBuild }) { + let result = cache.get(path); + if (!result) { + const reg = /import\s+([\w\s{}*]+)\s+from\s+(['"])globby(\?path)?!([^'"]+)\2/g; + const lastImport = urlMap.get(path); + const match = code.match(reg); + if (lastImport && match) { + code = code.replace(lastImport, match[0]); + } + result = code.replace(reg, (_, g1, g2, g3, g4) => { + const filePath = path.replace('\u0000', ''); // why some path startsWith '\u0000'? + // resolve path + const resolvedFilePath = g4.startsWith('.') + ? resolver.resolveRelativeRequest(filePath, g4) + : { pathname: resolver.requestToFile(g4) }; + const files = glob.sync(resolvedFilePath.pathname, { dot: true }); + let templateStr = 'import #name# from #file#'; // import default + let name = g1; + const m = g1.match(/\{\s*(\w+)(\s+as\s+(\w+))?\s*\}/); // import module + const m2 = g1.match(/\*\s+as\s+(\w+)/); // import * as all module + if (m) { + templateStr = `import { ${m[1]} as #name# } from #file#`; + name = m[3] || m[1]; + } else if (m2) { + templateStr = 'import * as #name# from #file#'; + name = m2[1]; + } + const temRender = template(templateStr); + + const groups: Array[] = []; + const replaceFiles = files.map((f, i) => { + const file = g2 + resolver.fileToRequest(f) + g2; + groups.push([name + i, file]); + return temRender({ name: name + i, file }); + }); + urlMap.set(path, replaceFiles.join('\n')); + return ( + replaceFiles.join('\n') + + (g3 ? '\n' + groups.map((v) => `${v[0]}._path = ${v[1]}`).join('\n') : '') + + `\nconst ${name} = { ${groups.map((v) => v[0]).join(',')} }\n` + ); + }); + if (isBuild) cache.set(path, result); + } + return result; + }, + }; +}; +export default globbyTransform; diff --git a/build/utils.ts b/build/utils.ts index 9c44a0e4..11d16441 100644 --- a/build/utils.ts +++ b/build/utils.ts @@ -124,28 +124,24 @@ export function getEnvConfig(match = 'VITE_GLOB_', confFiles = ['.env', '.env.pr return envConfig; } -export function successConsole(message: any) { +function consoleFn(color: string, message: any) { console.log( chalk.blue.bold('**************** ') + - chalk.green.bold('✨ ' + message) + + (chalk as any)[color].bold(message) + chalk.blue.bold(' ****************') ); } +export function successConsole(message: any) { + consoleFn('green', '✨ ' + message); +} + export function errorConsole(message: any) { - console.log( - chalk.blue.bold('**************** ') + - chalk.red.bold('✨ ' + message) + - chalk.blue.bold(' ****************') - ); + consoleFn('red', '✨ ' + message); } export function warnConsole(message: any) { - console.log( - chalk.blue.bold('**************** ') + - chalk.yellow.bold('✨ ' + message) + - chalk.blue.bold(' ****************') - ); + consoleFn('yellow', '✨ ' + message); } export function getCwdPath(...dir: string[]) { diff --git a/src/components/CountTo/src/index.vue b/src/components/CountTo/src/index.vue index 55f8a1c3..bd95b9a9 100644 --- a/src/components/CountTo/src/index.vue +++ b/src/components/CountTo/src/index.vue @@ -5,7 +5,6 @@ diff --git a/src/views/sys/redirect/index.vue b/src/views/sys/redirect/index.vue index 099df59e..58e316dc 100644 --- a/src/views/sys/redirect/index.vue +++ b/src/views/sys/redirect/index.vue @@ -2,7 +2,7 @@