npm install -g grunt
command npm install -g grunt
. The -g
flag means installation globally, that is, Grunt will always be available from the command line, since it is installed in the root node_modules
folder. If you want to run Grunt only in a specific folder, then in it, run the same command without the -g
flag. After successful installation, the console output looks like this:commonjs
, jquery
and node
. For example, let's create a jQuery project. Create a folder for the project and execute grunt init:jquery
in it. You will be asked a few questions. The default value of Grunt is shown in brackets, and if you don’t want to change it, just hit enter. I looked like this:grunt.js
(also called gruntfile
). Its content may not seem very clear, do not worry about it. Most importantly, Grunt added the qunit
section to the file and created the test
folder in the project. He also added instructions for merging files and tracking files. The latter means automatic start of tasks at any change of files: watch: { files: '<config:lint.files>', tasks: 'lint qunit' }
config:lint.files
, that is, refers to the following section of the config:lint.files
: lint: { files: ['grunt.js', 'src/**/*.js', 'test/**/*.js'] }
lint
and qunit
(which do exactly what you thought) as soon as any of these files change. Very graceful! I will demonstrate it a little later. grunt.registerTask('default', 'lint qunit concat min');
lint
, qunit
, concat
and min
must be executed.grunt
in the terminal and hit enter. Unfortunately, it didn't work as expected for me: Running "lint:files" (lint) task Lint free. Running "qunit:files" (qunit) task Testing jquery.jsplayground-demo.html Running PhantomJS...ERROR
grunt
command will be:grunt.js
. Find the watch
section, which looks like this: watch: { files: '<config:lint.files>', tasks: 'lint qunit' },
concat
and min
tasks here, but as you remember, we defined a default
task that performs all of these actions. Therefore, when changing files, I can simply run default
: watch: { files: '<config:lint.files>', tasks: 'default' }
concat
and min
every time you save it is too, I just wanted to show that it is possible. You can create several tasks, one to run by default, another to run when released, a third to run during development, etc.src/jquery.jsplayground-demo.js
. Inside there is an indication of the license, copyright and a link to github - all this is added automatically by the grunt init:jquery
command. Now let's make changes to this file to see watch
in action. First, you need to run watch
in the terminal: grunt watch
. Now we’ll make a change: I’m going to enter invalid javascript so we can see the JSLint error. I typed some rubbish stuff
in the file and saved it. The output in the terminal is automatically updated:$.fn.awesome
. Grunt automatically generated several tests, so when I save the file, you will see the tests drop. Since the code being tested will be deleted.grunt watch
and knowing that all the code has been tested, tested and minified.Source: https://habr.com/ru/post/148274/
All Articles