Handle dependent files with Grunt and rewrite paths
Asked Answered
R

1

0

This is my Gruntfile.js (assets is the Bower folder):

module.exports = function(grunt) {
    grunt.initConfig({
        distFolder: 'dist',
        pkg: grunt.file.readJSON('package.json'),
        concat: {
            options: {
                separator: ';',
            },
            dist: {
                src: [
                    'assets/jquery/dist/jquery.js',
                    'assets/jquery-ui/ui/jquery-ui.js',
                    'assets/jsplumb/dist/js/jquery.jsPlumb-1.5.5.js'
                ],
                dest: '<%= distFolder %>/main.js',
            },
        },
        uglify: {
            dist: {
                src: 'dist/main.js',
                dest: 'dist/main.min.js',
            },
        },
        cssmin: {
            combine: {
                files: {
                    'dist/main.min.css': [
                        'assets/font-awesome/css/font-awesome.min.css',
                        'assets/jquery-ui/themes/base/jquery-ui.css',
                    ],
                }
            }
        },
    });

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

    grunt.registerTask('build', ['concat', 'uglify']);
};

All works fine, but as I suspected, Font-awesome will lose it's connection with its fonts when using grunt-contrib-mincss.

Can I automate with Grunt that it will do something like;

  • Copy font files from assets/font-awesome/fonts/ to dist/fonts/
  • Rewrite url(../fonts/ to url(fonts/

Thanks in advance!

Riess answered 1/4, 2014 at 16:38 Comment(0)
C
1

fonts, images, @imports in bower components or vendors may be relative or absolute depend on the component itself, you should config your cssmin task to rewrite relative resources to a correct path in dist path. using following code, all resources urls will be rewritten using absolute urls.

cssmin: {
    options: {
        root: 'app'
    },
    combine: {
        files: {
            'dist/main.min.css': [
                'app/bower_components/lib1/main.css',
                'app/bower_components/jquery-ui/themes/base/jquery-ui.css',
            ]
        }
    }
}

it's important to set the root option to the base path of your input files. in your case I'm believe you should set the root option to "/" or ""

If you tell me your project structure, I can help more.

I though your project structure is something like this

/
/assets
/other_files
/dist

Carilyn answered 7/4, 2014 at 10:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.