Skip to content

Instantly share code, notes, and snippets.

@ronaldcurtis
Last active April 24, 2017 00:10
Show Gist options
  • Save ronaldcurtis/9357054 to your computer and use it in GitHub Desktop.
Save ronaldcurtis/9357054 to your computer and use it in GitHub Desktop.
Ruby on Rails asset Pipeline on Sails.js
// config/express.js
var connectMincer = require('connect-mincer'),
nib = require('nib'),
path = require('path'),
mincer;
// Config for connect-mincer
// See https://github.com/clarkdave/connect-mincer for more details
mincer = new connectMincer({
root: path.join(__dirname, '../'),
production: process.env.NODE_ENV === 'production',
mountPoint: '/assets',
manifestFile: path.join(__dirname, '../.tmp/public/assets/manifest.json'),
paths: ['assets/']
});
// Configure Stylus so it can import css files just like .styl files
// Also allows the use of nib (mixin library for stylus)
// Remove is you're not using nib or stylus
mincer.Mincer.StylusEngine.configure(function(style) {
style.set('include css', true);
style.use(nib());
});
// Uncomment the lines below to top optionally configure Jade or Coffee
// See http://nodeca.github.io/mincer for more details
// mincer.Mincer.JadeEngine.configure({});
// mincer.Mincer.CoffeeEngine.configure({});
module.exports = {
express: {
customMiddleware: function(app){
app.use(mincer.assets());
// Connect-mincer serves our assets in dev
// We must precompile our assets before starting in production
if (process.env.NODE_ENV !== 'production') {
app.use('/assets', mincer.createServer());
}
}
}
};
module.exports = function(grunt) {
grunt.initConfig({
shell: {
precompile: {
command: 'node precompile.js',
options: {
stdout: true,
failOnError: true
}
}
},
clean: {
temp: ['.temp']
}
});
grunt.loadNpmTasks('grunt-shell');
grunt.loadNpmTasks('grunt-contrib-clean');
// Run this task before starting in production
grunt.registerTask('production', [
'clean',
'shell:precompile'
]);
// This is Sails's default task when launching the app in production.
// But Sails runs this task in a different order, and we need to
// precompile our assets before any middleware is run
// therefore we run grunt production manually instead
grunt.registerTask('prod', function() {
return;
});
// Nothing needs to happen in a dev environment :)
grunt.registerTask('default', function() {
return;
});
};
(function() {
"use strict";
console.log('Precompiling...');
var Mincer = require('mincer'),
CleanCss = require('clean-css'),
uglify = require('uglify-js'),
nib = require('nib'),
mountPoint = '/assets',
env;
env = new Mincer.Environment('./');
Mincer.StylusEngine.configure(function(style) {
style.set('include css', true)
style.set('compress', true)
style.use(nib());
});
// Set compressors
env.jsCompressor = 'uglify';
env.cssCompressor = function(context, data) {
var min = '';
if (data) {
min = new CleanCss().minify(data)
}
return min;
}
env.appendPath('assets');
env.registerHelper('asset_path', function(name, opts) {
var asset = env.findAsset(name, opts);
if (!asset){
throw Error("File [" + name + "] not found");
}
return mountPoint + '/' + asset.digestPath;
});
var manifest = new Mincer.Manifest(env, '.tmp/public/assets');
manifest.compile(['*', '*/**'], function(err, data) {
if(err){
console.log(err);
throw err;
}
console.info('Finished precompile.');
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment