Grunt指南针dev和prod

时间:2017-05-31 14:33:57

标签: javascript css gruntjs compass-sass compass

GruntJS + SASS指南针有问题。我已设置devprod配置。 对于dev css已outputStyle: 'expanded'prodoutputStyle: 'compressed'。当我做prod时 - 它就像一个魅力。在控制台中我看到了

Running "compass:dist" (compass) task
overwrite css/screen.css (0.392s)
Compilation took 0.4s
压缩css应该是这样的。

但是当我做dev时,它在控制台中显示

Running "compass:dev" (compass) task

Running "autoprefixer:dist" (autoprefixer) task

css仍在压缩。

有我的Gruntfile.js配置:

module.exports = function(grunt) {
grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),

    compass: {
        dev: {
            options: {
                sassDir: 'sass',
                cssDir: 'css',
                imagesDir: 'images',
                fontsDir: 'fonts',
                relativeAssets: true,
                boring: true,
                outputStyle: 'expanded',
                environment: 'development',
                raw: 'preferred_syntax = :sass\n'
            }
        },
        dist: {
            options: {
                sassDir: 'sass',
                cssDir: 'css',
                imagesDir: 'images',
                fontsDir: 'fonts',
                relativeAssets: true,
                boring: true,
                force: true,
                bundleExec: true,
                outputStyle: 'compressed',
                environment: 'production',
                raw: 'preferred_syntax = :sass\n'
            }
        }
    },

    autoprefixer: {
        dist:{
            files:{
                'css/screen.css':'css/screen.css'
            }
        }
    },

    concat: {
        dist: {
            src: [
                'js/vendors/filename.js',
                'js/companyname/filename.js'
            ],
            dest: 'js/companyname/main.js'
        }
    },

    jshint: {
        all: ['Gruntfile.js'],
        beforeconcat: [
            'js/src/companyname/app.js',
            'js/src/companyname/bar.js'
        ]
    },

    uglify: {
        options: {
            mangle: false
        },
        prod: {
            files: [{
                expand: true,
                cwd: 'js',
                src: [
                    'vendors/**/*.js',
                    'companyname/**/*.js'
                ],
                dest: 'js'
            }]
        }
    },

    copy: {
        main: {
            expand: true,
            cwd: 'js/src',
            src: [
                'companyname/**/*.js',
                'vendors/**/*.js'
            ],
            dest: 'js/'
        }
    },

    imagemin: {
        jpg: {
            options: {
                optimizationLevel: 8
            },
            files: [
                {
                    expand: true,
                    cwd: 'images-src/',
                    src: ['**/*.jpg'],
                    dest: 'images/',
                    ext: '.jpg'
                }
            ]
        },
        png: {
            options: {
                optimizationLevel: 8
            },
            files: [
                {
                    expand: true,
                    cwd: 'images-src/',
                    src: ['**/*.png'],
                    dest: 'images/',
                    ext: '.png'
                }
            ]
        }
    },

    clean: {
        images: {
            src: ['images']
        }
    },

    watch: {
        compass: {
            files: [
                'sass/{,*/}*.sass',
                'images-src/{,*/}*.{png,jpg,gif}'
            ],
            tasks: [
                'compass:dev',
                'autoprefixer',
                'clean:images',
                'imagemin'
            ]
        }
    }
});

grunt.loadNpmTasks('grunt-contrib-compass');
grunt.loadNpmTasks('grunt-contrib-imagemin');
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-jshint');
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-autoprefixer');
grunt.loadNpmTasks('grunt-contrib-watch');

grunt.registerTask('default', [
    'compass:dev',
    'autoprefixer',
    'copy',
    'concat',
    'jshint',
    'uglify'
]);

grunt.registerTask('prod', [
    'compass:dist',
    'autoprefixer',
    'copy',
    'concat',
    'jshint',
    'uglify',
    'clean:images',
    'imagemin'
]);
};

我做错了什么?

1 个答案:

答案 0 :(得分:1)

你可以制作两个不同的指南针。

compass: {
   dev: {
     ...
     force: true
     ...
   },
   devWatch: {
   ... ur original ... 
   }
}

watch(compass: {tasks: ['compass:devWatch', ...]})

grunt.registerTask('watch', [
   'compass:dev',
   'watch'
]);

grunt.registerTask('dev', [
   'compass:dev'
]);