分享一个Nodejs自动压缩编译JS的小工具

发布于 1周前 作者 ionicwang 来自 nodejs/Nestjs

分享一个Nodejs自动压缩编译JS的小工具

本人是新手中的新手,刚开始学习Node。 此工具基于NodeJS以及Google Closure Compiler。 欢迎板砖,吐槽,任何伺候! 希望可以跟大家多学习。

此工具用于开发阶段,可以在用户在编辑JS时,自动合并压缩编译成一个文件。

亲们使用需要安装Node 以及 JRE 将待编译的js文件放入lib中,并编辑_jsfiles.list文件,按顺序添加他们的文件名。 完后打开compress.bat即可自动进行编译。

地址为: https://github.com/popeyeaj/SEEUTOPIA/tree/master/nodeJsCompress


3 回复

分享一个Nodejs自动压缩编译JS的小工具

大家好,我是新手中的新手,刚开始学习Node.js。今天想跟大家分享一个我制作的小工具,它可以帮助我们在开发过程中自动合并、压缩和编译JavaScript文件。

工具介绍

此工具基于Node.js以及Google Closure Compiler,旨在简化开发过程中的静态资源处理工作。它能够自动将多个JS文件合并并压缩成一个文件,从而减少HTTP请求的数量,提高页面加载速度。

使用说明

  • 环境需求:使用本工具前,需要先安装Node.js和JRE(Java Runtime Environment)。

  • 文件结构

    • lib目录下存放待编译的JS文件。
    • _jsfiles.list文件用于指定待编译文件的顺序。
  • 操作步骤

    1. 将需要编译的JS文件放入lib目录。
    2. 编辑_jsfiles.list文件,按顺序列出所有需要编译的JS文件名称。
    3. 运行compress.bat脚本,开始自动编译过程。

示例代码

_jsfiles.list
// 按顺序列出所有JS文件
app.js
utils.js
main.js
compress.bat
@echo off
node compress.js
compress.js
const fs = require('fs');
const path = require('path');
const { exec } = require('child_process');

const fileList = fs.readFileSync('_jsfiles.list', 'utf8').split('\n').map(file => file.trim()).filter(file => file !== '');
const outputFilePath = path.join(__dirname, 'output.min.js');

let combinedContent = '';

// 合并文件内容
fileList.forEach(file => {
    const filePath = path.join(__dirname, 'lib', file);
    const fileContent = fs.readFileSync(filePath, 'utf8');
    combinedContent += fileContent + '\n';
});

// 将合并后的文件内容写入临时文件
fs.writeFileSync(path.join(__dirname, 'temp.js'), combinedContent);

// 使用Google Closure Compiler压缩JS文件
exec(`java -jar compiler.jar --js=temp.js --js_output_file=${outputFilePath}`, (error, stdout, stderr) => {
    if (error) {
        console.error(`执行错误: ${stderr}`);
        return;
    }
    console.log(`输出文件路径: ${outputFilePath}`);
});

获取源码

如果您对这个小工具感兴趣,可以前往以下GitHub链接获取完整源码:

GitHub 地址


希望这个小工具能对大家有所帮助,欢迎大家提出宝贵的意见和建议。


用UglifyJS吧,不需要JRE的~~

为了实现一个Node.js自动压缩编译JS的小工具,我们可以利用一些现有的库,比如google-closure-compiler-js来处理JS文件的压缩。下面是一个简单的示例,展示如何创建这样一个工具。

工具功能

  1. 自动检测JS文件变化:当开发者修改JS文件时,自动触发压缩过程。
  2. 合并多个JS文件到一个文件:根据_jsfiles.list中的文件列表顺序合并文件。
  3. 使用Google Closure Compiler进行压缩:确保最终生成的JS文件尽可能小。

示例代码

首先,我们需要安装必要的依赖:

npm install google-closure-compiler-js chokidar --save-dev

然后,创建一个脚本文件(例如compress.js):

const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const chokidar = require('chokidar');

// 配置
const inputDir = './lib';
const outputDir = './dist';
const fileListPath = './_jsfiles.list';

// 确保输出目录存在
if (!fs.existsSync(outputDir)) {
    fs.mkdirSync(outputDir);
}

// 读取文件列表
let jsFiles = fs.readFileSync(fileListPath, 'utf-8').split('\n').filter(line => line.trim() !== '');

// 创建合并后的文件
function compileFiles(files) {
    let content = '';
    files.forEach(file => {
        content += fs.readFileSync(path.join(inputDir, file), 'utf-8');
    });
    // 使用Google Closure Compiler进行压缩
    const result = execSync(`google-closure-compiler-js --js_output_file ${outputDir}/compiled.js`, { input: content });
    console.log('Compilation successful.');
}

// 初始化压缩
compileFiles(jsFiles);

// 监控文件更改
const watcher = chokidar.watch([inputDir, fileListPath], {
    persistent: true,
    ignoreInitial: true
});

watcher.on('all', (event, filePath) => {
    if (filePath.endsWith('.js')) {
        // 如果是JS文件被修改,重新读取文件列表并编译
        jsFiles = fs.readFileSync(fileListPath, 'utf-8').split('\n').filter(line => line.trim() !== '');
        compileFiles(jsFiles);
    }
});

使用说明

  1. 将所有需要编译的JS文件放入./lib目录。
  2. ./_jsfiles.list文件中列出需要编译的JS文件,每行一个文件名。
  3. 运行node compress.js启动监视器。
  4. 每当有新的JS文件被添加或修改时,会自动触发压缩过程。

注意事项

  • 确保已安装Node.js和Java Runtime Environment (JRE),因为google-closure-compiler-js需要JRE。
  • 你可以通过调整google-closure-compiler-js的参数来自定义压缩行为。

这样,我们就创建了一个简单但功能强大的自动压缩编译JS的工具。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!