Change path path with grunt

2

I have a problem with my application.

It will be all angular, but I need to know how I can change the path of my javascript according to my environment.

Example:

If I'm developing in the dev environment it will use style.css ,

If I run a build it changes to

> style.min.css

I already did this with java where I created a property file and it changed in my 3 environments (dev, devpreview, live)

NOTE: If AngularJS already has a native way of doing it even better.

Does anyone know if using regex or some grunt plugin would work?

Example of my Gruntfile:

/*jshint node: true*/

module.exports = function (grunt) {
    'use strict';

    var jsRoot = 'js/**/*',
        jsBasePath = 'js/src/base.js',
        jsBootstrapPath = 'js/src/bootstrap.js',
        jsSrcPath = 'js/src/app/**/*',
        jsDistPath = 'js/dist/',
        srcMin = 'js/dist/src.min.js',
        jsOutputFile = 'js/dist/<%=pkg.name%>.js',
        jsOutputMinFile = 'js/dist/<%=pkg.name%>.min.js',
        thirdPartyPath = 'js/third-party/**/*',
        devTargetPath = '../../../target/app/',
        gruntFilePath = 'Gruntfile.js',
        cssRoot = 'css/**/*.css',
        cssOutputMinFile = 'css/style.min.css';

    // App source code. This is needed since the order matters when concatenating or minifying files
    var appSrc = [jsBasePath, jsSrcPath, jsBootstrapPath];

    var resources = [
        'bower_components/jquery/dist/jquery.js',
        'bower_components/jquery/dist/jquery.min.map',
        'bower_components/jquery/dist/jquery.min.js',
        'bower_components/jquery.scrollTo/jquery.scrollTo.js',
        'bower_components/jquery.scrollTo/jquery.scrollTo.min.js',
        'bower_components/jquery-ui/ui/jquery-ui.js',
        'bower_components/jquery-ui/ui/minified/jquery-ui.min.js',
        'bower_components/jquery-masonry/dist/masonry.pkgd.js',
        'bower_components/jquery-masonry/dist/masonry.pkgd.min.js',
        'bower_components/moment/moment.js',
        'bower_components/moment/locale/pt-br.js',
        'bower_components/moment/locale/es.js',
        'bower_components/angular/angular.js',
        'bower_components/angular/angular.min.js',
        'bower_components/angular-ui-router/release/angular-ui-router.js',
        'bower_components/angular-ui-router/release/angular-ui-router.min.js',
        'bower_components/angular-ui-sortable/sortable.js',
        'bower_components/angular-ui-sortable/sortable.min.js',
        'bower_components/angular-animate/angular-animate.js',
        'bower_components/angular-animate/angular-animate.min.js',
        'bower_components/angular-animate/angular-animate.min.js.map',
        'bower_components/angular-sanitize/angular-sanitize.js',
        'bower_components/angular-sanitize/angular-sanitize.min.js',
        'bower_components/angular-sanitize/angular-sanitize.min.js.map'
    ];

    var defaultTasks = ['jshint', 'uglify', 'concat', 'copy:mapfiles', 'cssmin'];
    var devTasks = ['jshint', 'concat', 'cssmin', 'copy:main'];
    var timestamp = new Date().getTime();

    // deleting css min file to avoid it to be included in itself next time
    if (grunt.file.exists(cssOutputMinFile)) {
        grunt.file.delete(cssOutputMinFile);
    }

    var config = {
        pkg: grunt.file.readJSON('package.json'),
        watch: {
            files: [jsRoot, cssRoot],
            tasks: devTasks,
            options: {
                livereload: true
            }
        },
        jshint: {
            all: [appSrc, gruntFilePath],
            options: {
                jshintrc: true
            }
        },
        uglify: {
            dist: {
                src: appSrc,
                dest: srcMin,
                filter: 'isFile'
            }
        },
        concat: {
            options: {
                separator: '\n'
            },
            dev: {
                src: [
                    resources.filter(function(e) {return !e.match(/min/);}), // non-minified files
                    thirdPartyPath,
                    appSrc
                ],
                dest: jsOutputFile,
                filter: 'isFile',
                nonullFail: true
            },
            prod: {
                src: [
                    resources.filter(function(e) {return e.match(/min.js$/);}), // minified files
                    thirdPartyPath, 
                    srcMin
                ],
                dest: jsOutputMinFile,
                filter: 'isFile',
                nonullFail: true
            }
        },
        cssmin: {
            main: {
                src: cssRoot,
                dest: cssOutputMinFile
            }
        },
        copy: {
            main: {
                src: [jsRoot, cssRoot],
                dest: devTargetPath,
                options: {
                    process: function (content) {
                        if (!grunt.file.isDir(content)) {
                            return content.replace(/\$\{build\.timestamp\}/g, timestamp);
                        }
                    }
                }
            },
            mapfiles: {
                src: resources.filter(function(e) {return e.match(/map/);}), // map files
                dest: jsDistPath,
                expand: true,
                flatten: true
            }
        }
    };

    grunt.initConfig(config);

    grunt.loadNpmTasks('grunt-contrib-concat');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-uglify');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-cssmin');
    grunt.loadNpmTasks('grunt-contrib-copy');

    grunt.registerTask('default', defaultTasks);
    grunt.registerTask('dev', devTasks);
};
    
asked by anonymous 26.02.2015 / 20:37

1 answer

1

You can do this:

You call the script with node grunt dev and you look for process.argv[2] to use in a ternary.

That is, change the line:

cssOutputMinFile = 'css/style.min.css';

for

cssOutputMinFile = process.argv[2] ? 'css/style.css' : 'css/style.min.css';

node process.argv is an array with the commands you use. in this example it is ['node', 'grunt', 'dev'] , hence you are using process.argv[2] .

    
28.02.2015 / 01:22