Minify some files, combine all, with Grunt.JS

后端 未结 2 1330
心在旅途
心在旅途 2021-01-11 10:51

I\'m moving a dev team away from Chirpy, an add-in for visual studio, for combination & minification of CSS/JS files, over to grunt as part of a workflow automation proc

2条回答
  •  执笔经年
    2021-01-11 11:23

    1. Install node.js
    2. Add gruntfile.js and package.json to root of project (modify the paths of the files as you want in gruntfile.js(may need to open in textpad as may not show in visual studio)

    3. Open Node.js terminal (command prompt) and go to project directory

    4. type:

      npm install grunt-ts --save [press enter]

      npm install grunt-cli -g [press enter]

      grunt [press enter]

    (if there are missing modules install them typing: npm install [module name] )

    eg npm install grunt-contrib-uglify npm install grunt-contrib-watch

    Then type grunt

    should minify files with no errors as long as javascript files are missing erros(run through jsHint.com)

    this is my gruntfile.js.....

    module.exports = function (grunt) {
    
        // 1. All configuration goes here 
        grunt.initConfig({
            pkg: grunt.file.readJSON('package.json'),
            concat: {
                css: {
                    src: ['archive/trunk_archived_16_06/Grouptree.UI.Web/UI.core/assets/css/*.css'],
                    dest: 'archive/trunk_archived_16_06/Grouptree.UI.Web/UI.core/assets/css/production.css'
                },
                js: {
                    src: ['archive/trunk_archived_16_06/Grouptree.UI.Web/UI.core/assets/js/*.js'],
                    dest: 'archive/trunk_archived_16_06/Grouptree.UI.Web/UI.core/assets/js/production.js'
                }
            },
            cssmin: {
                css: {
                    src: 'archive/trunk_archived_16_06/Grouptree.UI.Web/UI.core/assets/css/production.css',
                    dest: 'archive/trunk_archived_16_06/Grouptree.UI.Web/UI.core/assets/css/min/production.min.css'
                }
            },
            uglify: {
                js: {
                    src: 'archive/trunk_archived_16_06/Grouptree.UI.Web/UI.core/assets/js/production.js',
                    dest: 'archive/trunk_archived_16_06/Grouptree.UI.Web/UI.core/assets/js/min/production.min.js'
                }
            },
            watch: {
                css: {
                    files: ['archive/trunk_archived_16_06/Grouptree.UI.Web/UI.core/assets/css/*.css'],
                    tasks: ['concat:css', 'cssmin:css']
                },
                js: {
                    files: ['archive/trunk_archived_16_06/Grouptree.UI.Web/UI.core/assets/js/*.js'],
                    tasks: ['concat:js', 'uglify:js']
                }
            }
        });
    
        // 2. Where we tell Grunt we plan to use this plug-in.
        grunt.loadNpmTasks('grunt-contrib-concat');
        grunt.loadNpmTasks('grunt-contrib-cssmin');
        grunt.loadNpmTasks('grunt-contrib-uglify');
        grunt.loadNpmTasks('grunt-contrib-watch');
    
        // 3. Where we tell Grunt what to do when we type "grunt" into the terminal.
        grunt.registerTask('default', ['concat', 'cssmin', 'uglify']);
    
    };
    

    THIS IS MY package.json file

    {
      "name": "Grunt",
      "version": "0.1.0",
      "devDependencies": {
        "grunt": "~0.4.5",
        "grunt-contrib-concat": "^0.4.0",
        "grunt-contrib-cssmin": "^0.10.0",
        "grunt-contrib-uglify": "^0.5.0",
        "grunt-contrib-watch": "*"
      },
      "dependencies": {
        "grunt": "^0.4.5",
        "grunt-ts": "^1.11.13"
      }
    }
    

提交回复
热议问题