asgeo1
asgeo1

Reputation: 9078

grunt-requirejs - how to optimize CSS?

I'm using Yeoman, and are trying to setup requirejs to optimise my CSS files.

I have this in my Gruntfile.js:

requirejs: {
  css: {
    options: {
      optimizeCss: 'standard',
      cssIn: 'app/styles/main.css',
      out: 'app/styles/main.min.css'
    }
  }
}

When I run the build command I get an error:

grunt build

Running "requirejs:css" (requirejs) task
>> Error: TypeError: new path must be a string
>>     at Object.fs.renameSync (fs.js:439:18)
Warning: Task "requirejs:css" failed

I'm not really sure what's happening there, or how to debug that.

Does anyone know what the problem could be?

Upvotes: 2

Views: 2218

Answers (1)

asgeo1
asgeo1

Reputation: 9078

Never mind, I worked it out.

The silly useminPrepare task that ships with Yeoman stuffs around with the grunt-requirejs configuration. Kind of annoying, esp. since the point of me wanting to use grunt-requirejs was because I didn't like how usemin worked to begin with.

UPDATE:

This is my configuration that works:

//index.html:

<!-- build:js scripts/scripts.js -->
<script data-main="scripts/main" src="components/requirejs/require.js"></script>
<!-- endbuild -->


//Gruntfile.js:

requirejs: {
  js: {
    options: {
      baseUrl: '<%= yeoman.tmp %>/scripts',
      out: '<%= yeoman.dist %>/scripts/scripts.js',
      name: 'main',
      optimize: 'none',
      mainConfigFile: '<%= yeoman.app %>/scripts/main.js',
      useStrict: true,
      wrap: true
    }
  },
  css: {
    options: {
      optimizeCss: 'standard',
      cssIn: '<%= yeoman.app %>/styles/main.css',
      out: '<%= yeoman.dist %>/styles/main.css'
    }
  }
},
useminPrepare: {
  html: '<%= yeoman.app %>/index.html',
  options: {
    dest: '<%= yeoman.dist %>'
  }
},
usemin: {
  html: '<%= yeoman.dist %>/index.html',
  options: {
    dirs: ['<%= yeoman.dist %>']
  }
}

grunt.registerTask('build', [
  'requirejs:js',
  'requirejs:css',
  'useminPrepare', //must be after requirejs:css as it breaks its config params
  'usemin'
]);

NOTE: There is still an underlying bug in grunt-usemin where it (incorrectly) tries to alter the requirejs config for the requirejs:css configuration. I've reported that here

Upvotes: 3

Related Questions