简体   繁体   English

Makefile 压缩 javascript

[英]Makefile which compresses javascript

I want to compress javascript in yui compressor, How to write Make file for compress javascript.我想在 yui 压缩器中压缩 javascript,如何编写用于压缩 javascript 的 Make 文件。

Because grammar is difficult and does not understand it, Could you give me a sample Makefile for me?因为语法难,看不懂,可以给我样例Makefile吗?

Your makefile would look something like你的 makefile 看起来像

code.compressed.js: code.js
    compressor -o $@ $<

Note that the second line is indented with a tab character , not just spaces.请注意,第二行缩进了制表符,而不仅仅是空格。 The make utility cares about this. make 实用程序关心这一点。

code.compressed.js is the name that the file should be written to, code.js is the file to compress, and compressor is the program doing the compression. code.compressed.js是应该写入文件的名称, code.js是要压缩的文件, compressor是进行压缩的程序。

The -o flag indicates the output file, following the convention of compilers and similar tools. -o标志表示 output 文件,遵循编译器和类似工具的约定。 Yours may differ;你的可能不同; check its documentation.检查它的文档。

The variable $@ is Makefile shorthand for "this rule's target", code.compressed.js in this case.变量$@是 Makefile “此规则的目标”的简写,在本例中为code.compressed.js Similarly, $< is an abbreviation for "this rule's first dependency".同样, $<是“此规则的第一个依赖项”的缩写。 These variables are useful so that you needn't repeat yourself, nor make duplicate changes when files get renamed.这些变量很有用,因此您无需重复自己,也无需在文件重命名时进行重复更改。

If you have multiple files that will all be compressed into a single output file, you can put them all on the dependency line, and then use the special variable $^ in the build rule to specify all of them:如果你有多个文件都将被压缩到一个 output 文件中,你可以将它们全部放在依赖行上,然后在构建规则中使用特殊变量$^来指定所有这些文件:

code.compressed.js: code1.js code2.js
    compressor -o $@ $^

Alternately, if you want them each compressed separately, you can write a pattern rule and use it for all of them:或者,如果您希望它们分别压缩,您可以编写一个模式规则并将其用于所有这些规则:

TARGETS = code1.cjs code2.cjs code3.cjs

all: $(TARGETS)

%.cjs: %.js
    compressor -o $@ $<

Make defaults to building the first target that it sees, which is all in this case.默认构建它看到的第一个目标,在本例中就是all The list of files to compress to is given by the contents of the TARGET variable.要压缩的文件列表由TARGET变量的内容给出。 The % is a wildcard that make will substitute to generate rules for matching source and target file names. %是一个通配符,make 将替代它来生成匹配源文件名和目标文件名的规则。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM