问题
I am fairly new to using Grunt but have a fairly good understanding of how tasks are installed and run. So far I am able to run everything as I want which includes minifying js, copying files and running jshint.
I now want to run 'clean' to remove files from a folder but only an array of selected files which I hope to generate dynamically.
So if I had a dynamically generated array e.g.
var deleteFilenames = ["file3.js","file2.jpg","file7.html"] etc.
How would I set a grunt clean task to just delete these files?
Any help much appreciated.
回答1:
You can use a variable to construct the config passed initConfig
:
var deleteFilenames = ["foo", "bar", "baz"];
grunt.initConfig({
clean: deleteFilenames
});
In the code above, the list is static but deleteFilenames
could be constructed from an algorithm that computes file names.
回答2:
You can try using a wildcard token:
clean: ["file*"]
If you want something cleaner, you should include your Gruntfile.js
or try to ouput your generated assets into a specific directory (such as dist/
).
来源:https://stackoverflow.com/questions/22270567/how-can-i-run-a-task-in-grunt-using-an-array-of-filenames-which-are-dynamically