6

I've been struggling for hours trying to set up proper minification that actually rewrites urls. I've used useref and usemin, and they do good job of scanning html, aggregating all JS and CSS and outputting into one file. But, for the life of me, I cannot make the url rewrite to work properly. My structure is simple:

\root
   index.html
   application.css       // minified
   application.js        // minified
   \vendor
       \bootstrap
           \fonts        // font files here
           bootstrap.css // pre-minified 

bootstrap.css refers to font files by using relative url - font/bootstrap_font.ttf When bootstrap gets minified, it lands as part of application css, that is in my root now, so the path would point from root to /font/bootstrap_font.ttf. Original directory hierarchy stays, so I would basically like to have this url rewritten to /vendor/bootstrap/font/bootstrap_font.ttf

And, oh, why cssmin task doesn't accept more than one file?

UPDATE Here's my current grunt file:

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        useminPrepare: {
            html: 'web/public/index.html',
            options: {
                dest: 'web/public-dist'
            }
        },
        usemin: {
            html: 'web/public-dist/index.html',
        },
        copy: {
            all: {
                files: [{
                    expand: true,
                    cwd: 'web/public/',
                    src: ['**'],
                    dest: 'web/public-dist/'
                }]
            },
            resources: {
                files: [{
                    expand: true,
                    cwd: 'web/public/',
                    src: ['**/*.*', '!**/*.js', '!**/*.css', '!**/*.txt'],
                    dest: 'web/public-dist/'
                }]
            }
        },
        uglify: {
            options: {
                mangle: true,
                sourceMap: false,
                compress: true,
                banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n'
            },
            standard: {
                files: [{
                    expand: true,
                    cwd: 'web/public-dist/',
                    src: ['**/*.js'],
                    dest: 'web/public-dist/'
                }]
            }
        },
        cssmin: {
            options: {
                banner: '/*! <%= pkg.name %> <%= grunt.template.today("yyyy-mm-dd") %> */\n',
            },
            standard: {
                files: [{
                    expand: true,
                    cwd: 'web/public-dist/',
                    src: ['**/*.css'],
                    dest: 'web/public-dist/'
                }]
            },
        }
    });

    grunt.registerTask('package', [ 'copy:resources', 'useminPrepare', 'concat:generated','cssmin:generated', 'uglify:generated', 'usemin']);
};

In this form, cssmin cannot be even used as separately called target, because apparently its configuration is wrong - it complains that it cannot accept many files. What am I doing wrong here?

From the bits and pieces I've gathered, apparently it's crucial to change usemin flow and not allow it to concatenate all the css and cssmin later - because this way, it would obviously lose the vital information about the directory origin of every css file. I've tried changing the flow, but then it doesn't work because of the same cssmin error - cannot accept many files.

8
  • does application.css contain more than just bootstrap.css? Nov 13, 2014 at 15:16
  • It would be helpful to see the Gruntfile.
    – steveax
    Nov 13, 2014 at 16:11
  • Well, yes, of course. It combines many scripts. I just used Bootstrap as example, but the solution has to be generic. Whatever it encounters, it has to be rewritten properly. I would imagine that the cssmin (or other plugin) would have to take some kind of "relativeRoot" parameter (in this case it would be my \root) to relate encountered links - so in this case : "what's the relation of fonts/fontfile.ttf to my given root which is \root? I'm currently in \root\vendor\bootstrap. What should the link look like, if I were in \root?"
    – rattkin
    Nov 13, 2014 at 16:11
  • Updated original question with some more data and Gruntfile.
    – rattkin
    Nov 13, 2014 at 16:17
  • Question: You want one file that contains both the css and js? Nov 13, 2014 at 16:22

1 Answer 1

1

Okay, I has definitely same problem when started to build my css and js with grunt. Here is my solution of "relative urls" problem. Please, note that this post is not answering your actual question, but provide another way of problem solution. I have even more nested folder structure but it works well for me, and hope it helps you.

The gist is to build all css/js to another folder and copy assets files relatively to this new folder. Let give "build" name for it:

\root
   \build
       application.css - minified
       application.js  - minified       
       \fonts
           ...
       \img
           ...
       ...
   index.html
   \ ...

Using grunt-contrib-copy plugin copy all your assets to /build/assets directory without breaking their original structure. So relative passes for your css saves, fonts are still in ./fonts/ folder.

The problem you'll faced to with such approach is saving folder structure for assets. Well, it is solved with detalization of your build configuration in your gruntfile. Now you can not say "okay grunt, build all /**/*.css files to application.css" but have to describe different cases for different options of file structures. If your project have obvious and logical file structure it is not complicated to add them.

I used rule that every css file must have assets directory as it sibling. So gruntfile expanded just by several lines and build structure look something like this

\root
   \build
       \css
           \assets
               \fonts
               \img
           application.css - minified    //all relative passes saved
   \foo
       \bar
           \biz
               \assets
                   \fonts
               first.css
       \row
           \assets
               \img
           second.css
   index.html

Obviously you must have assets names naming rules to prevent overriding files.

Hope, this helps you

1
  • Sorry, it doesn't, but thanks. It's not a solution I can accept, you've just worked around the problem.
    – rattkin
    Nov 18, 2014 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.