chore: detail optimization

This commit is contained in:
vben
2020-10-14 21:08:07 +08:00
parent 7437896034
commit 31e2715e67
27 changed files with 304 additions and 93 deletions

View File

@@ -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);

View File

@@ -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<string, string>;
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<string>[] = [];
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;

View File

@@ -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[]) {