Skip to content

Instantly share code, notes, and snippets.

@avshabanov
Last active August 29, 2015 14:18
Show Gist options
  • Save avshabanov/6a02e60fa300b52cfe18 to your computer and use it in GitHub Desktop.
Save avshabanov/6a02e60fa300b52cfe18 to your computer and use it in GitHub Desktop.
Sample gruntfile for mixed (plain JS + reactjs) project processed by browserify
module.exports = function(grunt) {
function prepareSkeleton() {
grunt.file.mkdir('target/web/js');
grunt.file.mkdir('target/web/tmp/js');
}
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
watch: {
scripts: {
files: ['web/js/**/*.js', 'web/html/index.html'],
tasks: ['copy', 'react', 'browserify']
}
},
copy: {
main: {
files: [
{
src: 'web/html/index.html',
dest: 'target/web/index.html'
},
{
cwd: 'web/js', // source js dir
src: ['**', '!**/*.jsx'], // copy all files and subfolders to the temporary dor, except for jsx
dest: 'target/web/tmp/js', // destination folder, used by browserify
expand: true // required when using cwd
}
]
}
},
react: {
dynamic_mappings: {
files: [
{
expand: true,
cwd: 'web/js/view',
src: ['**/*.jsx'],
dest: 'target/web/tmp/js/view',
ext: '.js'
}
]
}
},
browserify: {
dist: {
files: {
'target/web/js/app.js': ['target/web/tmp/js/main.js'],
}
}
}
});
// Load plugins
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-browserify');
grunt.loadNpmTasks('grunt-react');
prepareSkeleton();
// Default task that generates development build
grunt.registerTask('default', [
'copy', 'react', 'browserify'
]);
// Release task that generates production build
grunt.registerTask('dist', [
]);
grunt.registerTask('clean', 'Recursively cleans build folder', function () {
grunt.file.delete('target');
});
};
// Sample entry point
// this file expected to be in 'web/js/main.js'
var ps = require("./service/post-service.js");
var React = require("React")
var MainPage = require("./view/page/main.js");
window.onload = function () {
console.log("main (3)");
var s = new ps.PostService();
document.getElementById('messages-result').textContent = JSON.stringify(s.getPosts());
React.render(React.createElement(MainPage), document.getElementById('main-content'));
}
{
"name": "simple-browserify-app",
"version": "1.0.0",
"devDependencies": {
"grunt": "^0.4.5",
"grunt-cli": "^0.1.13",
"grunt-contrib-watch": "^0.6.1",
"grunt-browserify": "^3.6.0",
"grunt-contrib-concat": "^0.5.1",
"grunt-contrib-copy": "^0.8.0",
"grunt-contrib-jshint": "~0.6.3",
"director": "^1.2.8",
"react": "^0.13.0",
"grunt-react": "^0.12.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment