惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
博客园 - 【当耐特】
Cloudbric
Cloudbric
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Attack and Defense Labs
Attack and Defense Labs
爱范儿
爱范儿
The Cloudflare Blog
腾讯CDC
Security Archives - TechRepublic
Security Archives - TechRepublic
TaoSecurity Blog
TaoSecurity Blog
云风的 BLOG
云风的 BLOG
Recent Announcements
Recent Announcements
C
Check Point Blog
Schneier on Security
Schneier on Security
S
Schneier on Security
J
Java Code Geeks
B
Blog RSS Feed
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
Stack Overflow Blog
Stack Overflow Blog
博客园_首页
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
A
About on SuperTechFans
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
A
Arctic Wolf
S
Secure Thoughts
P
Palo Alto Networks Blog
The Last Watchdog
The Last Watchdog
SecWiki News
SecWiki News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 三生石上(FineUI控件)
D
Darknet – Hacking Tools, Hacker News & Cyber Security
量子位
U
Unit 42
I
InfoQ
D
DataBreaches.Net
P
Privacy International News Feed
T
Troy Hunt's Blog
博客园 - 叶小钗
T
Threatpost
博客园 - Franky
K
Kaspersky official blog
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家
www.infosecurity-magazine.com
www.infosecurity-magazine.com
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Cisco Blogs

博客园 - 豆豆の爸爸

IDEA 2024的零卡死配置 10分钟揭秘大模型的原理 苹果容器Apple container是做什么用的? pnpm 10.14 支持JavaScript运行时的安装了 白话Docker:用Web应用实例深入容器 HTML 5 就是 Web Application JS程序员的一天 Google Map 类实例在类式继承中的实现 写入 cookie 的过期时间时在GMT或UTC时间格式上的兼容问题 “当 HTML 5 来敲门”专题沙龙(上海)活动 PHP 的 Smarty 模板页中分离JS并避开literal标签的解决方法 Google Maps(Google 地图) V3 在 IE7 浏览器中拖放其容器时图块被覆盖的 bug 2010年我的个人总结 [译]用 Closure Compiler 编写更好的 OO 的 JavaScript 使用 IronScheme 进入 Scheme 编程语言的世界 - 豆豆の爸爸 《JS高级程序设计(第2版)》书评 [译]在 Firebug 中的表格化日志 在 Notepad++ 运行 Closure Linter 来校验JS代码 在 Notepad++ 运行 Closure Compiler 工具来解析并压缩JS
用 rake 合并多个 JS 文件,并且用 Google Closure Compiler 压缩代码
豆豆の爸爸 · 2011-05-16 · via 博客园 - 豆豆の爸爸

使用一款自动化的构建工具,已经成为专业的JS程序员的必备技能之一。在国内,每个前端团队都学会了用 Ant 来自动生成一系列构建的任务。但是由于 Ant 对 XML的依赖,这一大关键缺点,使得 Ant 脚本既难写又难读,也难进行重构,甚至难进行 diff。所以,rake(用Ruby编程的构建工具)越来越流行了。

下面的代码是合并多个 JS 文件的 rake 任务:

namespace :js do
    desc "合并多个 JS 文件"
    task :concatenate do
        filename3 = "E:\\all.js"
        logfile = File.new(filename3, 'w');
        joinFiles = ["E:\\a.js", "E:\\b.js"];
        for files in joinFiles
            if File.exists?(files)
                file = File.new(files, 'r')
                file.each_line do |line1|
                    logfile.puts(line1)
                end
            end
        end
    end
end

如何执行 rake呢?

假如 Ruby 源代码文件为 build.rb,那么在 windows 命令行中输入命令:

rake –f build.rb js:concatenate

然后回车。

可能就会有人问了:可以结合 Google Closure Compiler 使用来进行代码压缩吗?答案是肯定的!

下面是带传参的调用 GCC 的 rake 任务:

desc "用 Google Closure Compiler 进行编译"
task :gcc, :compiled_path, :needs => :concatenate do |t, args|
    require 'rubygems'
    require 'win32/open3'
   
    cmd = "java -jar D:\\compiler\\compiler.jar --js " + concatenateFilename + " --js_output_file " + args.compiled_path
    Open3.popen3(cmd)
end

在 windows 命令行中输入命令:

rake –f build.rb "js:gcc[E:\all-min.js]"

然后回车。

对JS程序员来说,使用rake的唯一缺点是学习Ruby的成本了。所以,用nodeJS来实现相同的功能也是在国内前端界同行们越来越流行的趋势。

(完)