Using Grunt grunt-contrib-less) for compiling Bootstrap 3.1 LESS in Visual Studio 2013

China☆狼群 提交于 2019-11-30 04:07:01

I've got this working in a slightly different way:

  • Ensure you've got grunt-cli installed globally (npm install -g grunt-cli)
  • Create a Gruntfile.js in your project or solution, and define a target to do whatever less compiling you want (e.g. less)
  • Add call grunt less to your pre-build event (if you don't specify CALL, then the process doesn't return after grunt)

You can add different targets to the development and production build processes if you like. You can also set up more targets for other tasks - I have one so I can run grunt watch to automatically recompile my CSS if I edit less files.

Step-by-step guide to converting the VS2013 sample project to use less and Grunt:

  1. Remove bootstrap and install bootstrap less:

    Uninstall-Package bootstrap
    Install-Package Twitter.Bootstrap.less
    
  2. Open a command prompt and cd to your project directory
  3. Ensure grunt-cli is installed globally:

    npm install -g grunt-cli
    
  4. Create a package.json file:

    npm init
    
  5. Install grunt and grunt-contrib-less locally: npm install grunt grunt-contrib-less`
  6. Create a file in your project called Gruntfile.js with the following contents:

    module.exports = function (grunt) {
        grunt.initConfig({
            pkg: grunt.file.readJSON('package.json'),
            less: {
                dev: {
                    options: {
                        sourceMap: true,
                        dumpLineNumbers: 'comments',
                        relativeUrls: true
                    },
                    files: {
                        'Content/bootstrap.debug.css': 'Content/bootstrap/bootstrap.less',
                    }
                },
                production: {
                    options: {
                        cleancss: true,
                        compress: true,
                        relativeUrls: true
                    },
                    files: {
                        'Content/bootstrap.css': 'Content/bootstrap/bootstrap.less',
                    }
                }
            },
    
        });
    
        grunt.loadNpmTasks('grunt-contrib-less');
    
        // Default task(s).
        grunt.registerTask('default', ['less']);
        grunt.registerTask('production', ['less:production']);
        grunt.registerTask('dev', ['less:dev']);
    };
    
  7. Edit your Visual Studio pre-build event to include:

    cd $(ProjectDir)
    call grunt --no-color
    

    (--no-color removes some of the control characters from the Visual Studio build output)

  8. Build your project, then enable show all files, and incldue the two compiled css files in your project (so that web deploy picks them up).
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!