1

This is my makefile

file1:
    uglifyjs myfile1.js -c | gzip -c -9 > myfile1.min.js

file2:
    uglifyjs myfile2.js -c | gzip -c -9 > myfile2.min.js

How can I change my makefile to remove duplicate code:

file1:
    FILE=myfile1.js
    #How to call build target?

file2:
    FILE=myfile2.js

build:
    uglifyjs $(FILE).js -c | gzip -c -9 > $(FILE).min.js

I know I can use make build but is there another way to do this without invoking make recursively?

2 Answers 2

1

Use automatic variables:

file1 file2:
        uglifyjs [email protected] -c | gzip -c -9 > [email protected]

I don't know why you're using targets like file1 when the file you're actually building is myfile1.min.js. That's not a good makefile.

But, that's not the question you asked.

Sign up to request clarification or add additional context in comments.

2 Comments

since my file goes from myfile.js to myfile.min.js, how should I name the target? myfile? Obviously this is an example, but I would prefer to be as idiomatic as possible.
Your target should always be the file that you're creating. If it's not, then every time you run make it will rebuild the file (because as far as it's concerned the file it wants to build, which doesn't exist, is out of date). If you just want the commands to always run you might as well just write a little shell script containing your commands, and not worry about using make.
1

Use a pattern rule to run the command, and then make your targets depend on the files you want:

file1: myfile1.min.js
file2: myfile2.min.js

%.min.js: %.js
        uglifyjs $< -c | gzip -c -9 >$@

The pattern rule tells make how to build a .min.js file from a .js file, and the other rules tell it to build specific files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.