Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
151 views
in Technique[技术] by (71.8m points)

javascript - Makefile to combine js files and make a compressed version

I am trying to write a basic makefile that combines multiple js files into a single one and then does the same but compresses them.

So far I have this one that can make the compressed version fine.

# Set the source directory
srcdir = src/

# Create the list of modules
modules =   ${srcdir}core.js
            ${srcdir}sizzle.js
            ${srcdir}json2.js
            ${srcdir}ajax.js
            ${srcdir}attribute.js
            ${srcdir}content.js
            ${srcdir}cookie.js
            ${srcdir}css.js
            ${srcdir}event.js
            ${srcdir}json.js
            ${srcdir}location.js
            ${srcdir}opacity.js
            ${srcdir}ready.js
            ${srcdir}size.js
            ${srcdir}init.js

# Compress all of the modules into spark.js
spark.js: ${modules}
    java -jar yuicompressor.jar -o $@ $^

Does anyone know how I would go about adding an uncompressed version called spark-dev.js? I have been trying to use cat but I didn't get very far. This is my first makefile I have ever written.

EDIT I tried this code with cat

spark-dev.js: ${modules}
    cat $@ $^
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You were almost there :-) This should work:

spark-dev.js: ${modules}
    cat > $@ $^

Background: The function of cat is to (try to) open all the files listed on its command line, and dump the contents to stdout. The > $@ syntax is understood by the shell to mean "create the file $@, and connect this command's stdout to it", so now we end up with the contents of $^ combined together into $@.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...