Skip to content

Instantly share code, notes, and snippets.

@Haraldson
Last active December 28, 2015 19:09
Show Gist options
  • Save Haraldson/7547952 to your computer and use it in GitHub Desktop.
Save Haraldson/7547952 to your computer and use it in GitHub Desktop.
root
www
assets
js
my-app
app.js
lib
init.js
module.exports = function(grunt)
{
grunt.initConfig(
{
pkg: grunt.file.readJSON('package.json'),
watch: {
css: {
files: ['www/assets/css/sass/**/*.scss'],
tasks: ['sass', 'autoprefixer', 'imageEmbed']
}
},
sass: {
dist: {
files: {
'www/assets/css/admin-interface.css': 'www/assets/css/sass/admin-interface.scss'
}
}
},
autoprefixer: {
dist: {
files: {
'www/assets/css/admin-interface.css': 'www/assets/css/admin-interface.css'
}
}
},
imageEmbed: {
dist: {
src: ['www/assets/css/admin-interface.css'],
dest: 'www/assets/css/admin-interface.css',
options: {
baseDir: 'www',
deleteAfterEncoding: false
}
}
},
requirejs: {
options: {
baseUrl: 'www/assets/js/',
mainConfigFile: 'www/assets/js/init.js',
include: ['app'],
keepBuildDir: true
},
dev: {
options: {
out: 'www/assets/js/dev.js'
}
},
prod: {
options: {
out: 'www/assets/js/prod.js'
}
}
},
retire: {
files: ['www/assets/js/lib/*.js']
}
});
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-sass');
grunt.loadNpmTasks('grunt-autoprefixer');
grunt.loadNpmTasks('grunt-image-embed');
grunt.loadNpmTasks('grunt-contrib-requirejs');
grunt.loadNpmTasks('grunt-retire');
grunt.registerTask('default', ['watch']);
grunt.registerTask('build', ['requirejs', 'retire', 'sass', 'autoprefixer', 'imageEmbed']);
};
// requirejs data-main
requirejs.config(
{
baseUrl: '/assets/js/lib/',
paths: {
'so-much-stuff': '../my-app[/...]',
'underscore': 'lodash',
'marionette': 'backbone.marionette',
'Handlebars': 'handlebars',
'domReady': 'require.domReady',
},
shim: {
// shim all the things
}
});
requirejs(['app', 'domReady!'], function(App)
{
App.start();
});
@VirtueMe
Copy link

init.js => best practice suggestion: rename it to main.js

rewrite requirejs([], function... section to:

define(['app', 'domReady!'], function(App)
{
    App.start();
});

Remove the baseUrl, it actually makes it harder for requirejs to auto resolve paths

after rename of init.js to main.js you should be able to run a config in your grunt file based on this:

 options: {
                baseUrl: 'www/assets/js/',
                include: ['main'],
                keepBuildDir: true
            },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment