UNPKG

crootfast

Version:

大前端工程化命令行脚手架

54 lines (49 loc) 1.77 kB
const glob = require('glob'); const Terser = require('terser'); const fs = require('fs'); const path = require('path'); // const { pathJoin } = require('../../common/utils/path'); const inputPattern = './{bin,lib,common,template}/**/*.js'; const outputDir = './dist'; const ignorePattern = [ '**/node_modules/**', './node_modules/**', './{bin,lib,common,template}/node_modules/**' ]; glob(inputPattern, { ignore: ignorePattern }, (err, files) => { if (err) { console.error(err); return; } files.forEach((file) => { const code = fs.readFileSync(file, 'utf8'); Terser.minify(code).then((result) => { const relativePath = path.relative('.', file); const outputPath = path.join(outputDir, relativePath); fs.mkdirSync(path.dirname(outputPath), { recursive: true }); fs.writeFileSync(outputPath, result.code, 'utf8'); console.log(`Minified ${file} to ${outputPath}`); }).catch((error) => { console.error(`Error minifying ${file}: ${error}`); }); }); // copyDirectory(pathJoin('./webProject'), pathJoin('./dist'), [pathJoin('./webProject/node_modules')]); }); // function copyDirectory(srcDir, destDir, excludeDirs = []) { // fs.readdirSync(srcDir).forEach(file => { // const fullPath = path.join(srcDir, file); // const destPath = path.join(destDir, file); // if (fs.statSync(fullPath).isDirectory()) { // // 如果是特殊文件夹,则忽略 // if (excludeDirs.includes(file)) return; // // 递归调用 // if (!fs.existsSync(destPath)) { // fs.mkdirSync(destPath); // } // copyDirectory(fullPath, destPath, excludeDirs); // } else { // // 复制文件 // fs.copyFileSync(fullPath, destPath); // } // }); // };