阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

如何在Web项目中使用Makefile

414次阅读
没有评论

共计 3758 个字符,预计需要花费 10 分钟才能阅读完成。

个创建好的项目都有各式各样的构建过程,不论它是大型的 web 应用,或者小型可重用的 AMD 组建。已有一大堆的工具可帮你自动的完成构建过程,很多流行的工具例如非常抢眼的工具 grunt。

不论如何,一些项目中 grunt 工具未必就是你需要的,有时候对你而言简单的 shell 脚本会比 node.js 的工具更具有生产力。今天我将非常简洁的介绍 GNU Make 工具,以及你如何使用它来创建很爽的自动化构建几个步骤。

makefile 的概念

一个 makefile 保存了一堆很有特殊目的的 词目。正如我们相当喜爱 UNIX,现在真是时候提醒你自己:“写一些词目来做一件事儿,并且把它做好”的规则。这些词目包括:

  • 一个 目标 ,你知道make install 吗?install 就是目标。它也可能是一个文件,例如:make bundle.min.js
  • 依赖,一些由 目标 所依赖的一些可选的文件。
  • 以及最后运行的命令。

当你创建词目的时候,要尽可能的简单,同时要注意灵活性。例如,你想默认最小化一些预定义的文件,但偶尔你想配置其他的一些文件给 makefile,并且弄出这些命令。

创建灵活的 目标

为了更具灵活性,预定义的目标依赖可以被命令行列出的覆盖掉,要把依赖列表写在目标名称的后边。

$ make min index.js plugins.js
$ make min // 默认依赖 index.js

还有一种常见玩法,就是在 makefile 文件中把路径设置成变量,然后允许用户像如下的方式来覆盖:

$ make JSMIN=./bin/jsmin min

实现 min 目标

OK,那么我们开始创建一个 Makefile。
一个 目标 的格式是:

<target>: <dependencies>
    commands...

As I mentioned earlier, the target can be a general name or a file, for now we’ll keep to names such as min or install. Dependencies are a space separated list of files you want to process or of other targets you want to run. Let’s create our first target.

min: index.js plugins.js
    uglify --output scripts.min.js $^

he $^ is a variable which holds a space separated list of the dependencies. It will be expanded into: uglify --output scripts.min.js index.js plugins.js.

Now this is entry is somewhat flexible as you could run make min plugins.js to replace the default dependencies and only minimize plugins.js. However, let’s refactor this into something prettier.

A web project makefile

First lets define some variables.

# ?= means the variable can be overridden from the command line like:
# make UGLIFY=./node_modules/.bin/uglify min
UGLIFY ?= uglify
OUTPUT ?= scripts.min.js
JS_FILES := index.js plugins.js

min: $(JS_FILES)
    $(UGLIFY) --output $(OUTPUT) $^

Now let’s split out the concat task so we can use it separately

UGLIFY ?= uglify
OUTPUT ?= scripts.min.js
JS_FILES := index.js plugins.js

min: $(JS_FILES)
    make concat $(JS_FILES)
    $(UGLIFY) --output $(OUTPUT) $(OUTPUT)

concat: $(JS_FILES)
    cat $^ > $(OUTPUT)

Now we’re getting somewhere, however the min task looks really ugly. Let’s put in some awesome sauce.

UGLIFY ?= uglify
OUTPUT ?= script.js
JS_FILES := index.js plugins.js

# For each file in JS_FILES, replace .js by .min.js and run it as the target
# with the original as a dependency.
# In plain English, iterate over each file and minimize as well as rename it.
min: $(JS_FILES:.js=.min.js)

# $@ is the target and $< is the dependency.
%.min.js: %.js
    $(UGLIFY) --output $@ $<

concat: $(JS_FILES)
    cat $^ > $(OUTPUT)

You better let that sink in for a bit. Next up is tying everything up in a nice bow.

UGLIFY ?= uglify
DIST ?= dist
OUTPUT ?= $(DIST)/script.js
JS_FILES := index.js plugins.js

# When you run make without a target, the first entry is the one to run,
# usually this is named `all` by convention.
all: concat min

# Rather than minifying every file, default to only minifying the final output.
min: $(OUTPUT:.js=.min.js)

# $@ is the target and $< is the dependency.
# When you put a `@` in front of a command, the stdout will be suppressed,
# making for a cleaner terminal.
%.min.js: %.js
    @$(UGLIFY) --output $@ $<

concat: $(JS_FILES)
    @cat $^ > $(OUTPUT)

# It's useful to have a cleanup task as well.
clean:
    @rm -f $(DIST)/*

# Phony targets are targets that do not accept dependencies, it's good
# practice to define this as targets might have the same name as files from time
# to time and therefore create confusion. Also it's a small performance
# improvement.
.PHONY: clean all

Alright, that’s our makefile.

How to use it

Concat and minify index.js and plugins.js into dist/script.min.js (these all do the same thing).

$ make
$ make all
$ make concat min

Specify the location of uglify

$ make UGLIFY=./node_modules/.bin/uglify

Concat some other files

$ make OUTPUT=temp.js concat js/*

Minify all files but put them in a separate directory

$ make DIST=minified min js/*

Clean up all the distribution files created

$ make clean

 

u-boot Makefile 完全解读 http://www.linuxidc.com/Linux/2013-04/83529.htm

实验平台上 Makefile 详细的解释 http://www.linuxidc.com/Linux/2014-01/94827.htm

Makefile 之 Linux 内核模块的 Makefile 写法分析 http://www.linuxidc.com/Linux/2013-06/85842.htm

Makefile 之写 demo 时的通用 Makefile 写法 http://www.linuxidc.com/Linux/2013-05/84679.htm

Makefile 之大型工程项目子目录 Makefile 的一种通用写法 http://www.linuxidc.com/Linux/2013-05/84678.htm

本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-03/115251.htm

正文完
星哥玩云-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计3758字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中

星哥玩云

星哥玩云
星哥玩云
分享互联网知识
用户数
4
文章数
19350
评论数
4
阅读量
7960680
文章搜索
热门文章
星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛NAS-6:抖音视频同步工具,视频下载自动下载保存

星哥带你玩飞牛 NAS-6:抖音视频同步工具,视频下载自动下载保存 前言 各位玩 NAS 的朋友好,我是星哥!...
星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛NAS-3:安装飞牛NAS后的很有必要的操作

星哥带你玩飞牛 NAS-3:安装飞牛 NAS 后的很有必要的操作 前言 如果你已经有了飞牛 NAS 系统,之前...
我把用了20年的360安全卫士卸载了

我把用了20年的360安全卫士卸载了

我把用了 20 年的 360 安全卫士卸载了 是的,正如标题你看到的。 原因 偷摸安装自家的软件 莫名其妙安装...
再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见zabbix!轻量级自建服务器监控神器在Linux 的完整部署指南

再见 zabbix!轻量级自建服务器监控神器在 Linux 的完整部署指南 在日常运维中,服务器监控是绕不开的...
飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛NAS中安装Navidrome音乐文件中文标签乱码问题解决、安装FntermX终端

飞牛 NAS 中安装 Navidrome 音乐文件中文标签乱码问题解决、安装 FntermX 终端 问题背景 ...
阿里云CDN
阿里云CDN-提高用户访问的响应速度和成功率
随机文章
每年0.99刀,拿下你的第一个顶级域名,详细注册使用

每年0.99刀,拿下你的第一个顶级域名,详细注册使用

每年 0.99 刀,拿下你的第一个顶级域名,详细注册使用 前言 作为长期折腾云服务、域名建站的老玩家,星哥一直...
星哥带你玩飞牛NAS-8:有了NAS你可以干什么?软件汇总篇

星哥带你玩飞牛NAS-8:有了NAS你可以干什么?软件汇总篇

星哥带你玩飞牛 NAS-8:有了 NAS 你可以干什么?软件汇总篇 前言 哈喽各位玩友!我是是星哥,不少朋友私...
星哥带你玩飞牛NAS-14:解锁公网自由!Lucky功能工具安装使用保姆级教程

星哥带你玩飞牛NAS-14:解锁公网自由!Lucky功能工具安装使用保姆级教程

星哥带你玩飞牛 NAS-14:解锁公网自由!Lucky 功能工具安装使用保姆级教程 作为 NAS 玩家,咱们最...
星哥带你玩飞牛NAS-2:飞牛配置RAID磁盘阵列

星哥带你玩飞牛NAS-2:飞牛配置RAID磁盘阵列

星哥带你玩飞牛 NAS-2:飞牛配置 RAID 磁盘阵列 前言 大家好,我是星哥之前星哥写了《星哥带你玩飞牛 ...
把小米云笔记搬回家:飞牛 NAS 一键部署,小米云笔记自动同步到本地

把小米云笔记搬回家:飞牛 NAS 一键部署,小米云笔记自动同步到本地

把小米云笔记搬回家:飞牛 NAS 一键部署,小米云笔记自动同步到本地 大家好,我是星哥,今天教大家在飞牛 NA...

免费图片视频管理工具让灵感库告别混乱

一言一句话
-「
手气不错
让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级

让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级

让微信公众号成为 AI 智能体:从内容沉淀到智能问答的一次升级 大家好,我是星哥,之前写了一篇文章 自己手撸一...
零成本上线!用 Hugging Face免费服务器+Docker 快速部署HertzBeat 监控平台

零成本上线!用 Hugging Face免费服务器+Docker 快速部署HertzBeat 监控平台

零成本上线!用 Hugging Face 免费服务器 +Docker 快速部署 HertzBeat 监控平台 ...
恶意团伙利用 PHP-FPM 未授权访问漏洞发起大规模攻击

恶意团伙利用 PHP-FPM 未授权访问漏洞发起大规模攻击

恶意团伙利用 PHP-FPM 未授权访问漏洞发起大规模攻击 PHP-FPM(FastCGl Process M...
国产开源公众号AI知识库 Agent:突破未认证号限制,一键搞定自动回复,重构运营效率

国产开源公众号AI知识库 Agent:突破未认证号限制,一键搞定自动回复,重构运营效率

国产开源公众号 AI 知识库 Agent:突破未认证号限制,一键搞定自动回复,重构运营效率 大家好,我是星哥,...
安装Black群晖DSM7.2系统安装教程(在Vmware虚拟机中、实体机均可)!

安装Black群晖DSM7.2系统安装教程(在Vmware虚拟机中、实体机均可)!

安装 Black 群晖 DSM7.2 系统安装教程(在 Vmware 虚拟机中、实体机均可)! 前言 大家好,...