Skip to content

Instantly share code, notes, and snippets.

@s-panferov
Created February 15, 2015 16:33
Show Gist options
  • Save s-panferov/476edf52f425a8e12671 to your computer and use it in GitHub Desktop.
Save s-panferov/476edf52f425a8e12671 to your computer and use it in GitHub Desktop.
Gruntfile
var convertSM = require('convert-source-map');
var fs = require('fs');
var rewrite = require('connect-modrewrite');
module.exports = function (grunt) {
grunt.registerTask('tsmap', function() {
var existingMap;
var fileName = process.cwd() + '/dist/app.js';
var fileContent = fs.readFileSync(fileName).toString();
existingMap = convertSM.fromMapFileSource(fileContent, process.cwd() + '/dist');
fileContent = fileContent.replace('//# sourceMappingURL=app.js.map', '');
var cwd = process.cwd();
process.chdir('dist');
existingMap.setProperty('sourcesContent', existingMap.getProperty('sources').map(function(source) {
return fs.readFileSync(source).toString();
}));
process.chdir(cwd);
fs.writeFileSync(fileName, fileContent + '\n' + existingMap.toComment());
});
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
jasmine: {
chatlab: {
src: 'dist/app.js',
options: {
specs: 'dist/specs/*.spec.js',
vendor: [
"dist/libs.js"
]
}
}
},
watch: {
js: {
files: [
'src/app/**/*.js',
'src/app/**/*.ts',
'src/fake/**/*.ts'
],
options: {
livereload: true
},
tasks: ['build']
},
scss: {
files: [
'src/app/**/*.scss',
'src/styles/**/*.scss'
],
options: {
livereload: true
},
tasks: 'build_styles'
},
public: {
files: [
'index.html',
'public/**/*'
],
options: {
livereload: true
},
tasks: 'copy'
}
},
notify: {
compile: {
options: {
duration: 0.5,
message: 'TS is OK' //required
}
},
specs: {
options: {
duration: 0.5,
title: 'Specs complete', // optional
message: 'Compiling and specs finished running' //required
}
},
watch: {
options: {
duration: 0.5,
title: 'Watch started', // optional
message: 'Start watch iteration' //required
}
}
},
concat: {
libs: {
src: [
"bower_components/jquery/dist/jquery.min.js",
"bower_components/modernizr/modernizr.js",
"bower_components/URIjs/src/URI.min.js",
"bower_components/lodash/dist/lodash.min.js",
"bower_components/underscore.string/dist/underscore.string.min.js",
"bower_components/react/react-with-addons.js",
"bower_components/bacon/dist/Bacon.min.js",
"bower_components/xregexp/xregexp-all.js",
"bower_components/codemirror/lib/codemirror.js",
"bower_components/codemirror/addon/edit/continuelist.js",
"bower_components/codemirror/addon/comment/comment.js",
"bower_components/codemirror/addon/mode/overlay.js",
"bower_components/codemirror/addon/mode/multiplex.js",
"bower_components/codemirror/addon/hint/show-hint.js",
"bower_components/codemirror/addon/mode/loadmode.js",
"bower_components/codemirror/addon/display/placeholder.js",
"bower_components/codemirror/addon/hint/show-hint.js",
"bower_components/codemirror/mode/meta.js",
"bower_components/codemirror/mode/markdown/markdown.js",
"bower_components/codemirror/mode/javascript/javascript.js",
"bower_components/codemirror/mode/clike/clike.js",
"bower_components/codemirror/mode/gfm/gfm.js",
"bower_components/select2/select2.min.js",
"bower_components/moment/min/moment-with-locales.min.js",
"bower_components/iscroll/build/iscroll-probe.js",
"bower_components/tether/tether.js",
"bower_components/jquery-mockjax/jquery.mockjax.js",
"bower_components/tv4/tv4.js",
"bower_components/i18next/i18next.js",
"bower_components/moment-duration-format/lib/moment-duration-format.js",
"bower_components/node-uuid/uuid.js",
"bower_components/stomp-websocket/lib/stomp.js",
"bower_components/bragi-browser/dist/bragi.js",
"bower_components/nprogress/nprogress.js",
"src/vendor/**/*.js"
],
dest: 'dist/libs.js',
nonull: true,
},
},
sass: {
app: {
options: {
compass: true,
require: ['sass-globbing', 'sass-css-importer'],
loadPath: 'src/styles/',
},
files: {
'dist/app.css': 'src/styles/app.scss'
}
}
},
copy: {
main: {
files: [
{src: ['index.html'], dest: 'dist/index.html'},
],
},
fonts: {
files: [
{
cwd: 'src/styles/icomoon/fonts/',
src: ['**/*'],
dest: 'dist/fonts',
expand: true
},
],
},
public: {
files: [
{
cwd: 'public',
src: ['**/*'],
dest: 'dist/public',
expand: true
},
],
},
},
connect: {
server: {
options: {
port: 3000,
base: 'dist',
livereload: true,
middleware: function(connect, options, middlewares) {
// the rules that shape our mod-rewrite behavior
var rules = [
'!\\.html|\\.js|\\.woff|\\.ttf|\\.css|\\.svg|\\.jp(e?)g|\\.png|\\.gif$ /index.html'
];
// add rewrite as first item in the chain of middlewares
middlewares.unshift(rewrite(rules));
return middlewares;
}
}
}
}
});
function executeTsc(args, resolve) {
grunt.util.spawn({
cmd: 'tsc',
args: args
}, function (error, result, code) {
var ret = {
code: code,
// New TypeScript compiler uses stdout for user code errors. Old one used stderr.
output: result.stdout || result.stderr
};
resolve(ret);
});
}
grunt.registerTask('ts:dev', 'Build library.', function() {
var done = this.async();
executeTsc(["--target", "ES5",
"--sourceMap",
"--declaration",
"--noImplicitAny",
"--preserveConstEnums",
"--module", "commonjs",
"--out", "dist/app.js",
"src/app/run.ts"
], function(ret) {
grunt.log.writeln(ret.output);
if (ret.code != 0) {
grunt.fail.fatal("TSC error", ret.code);
}
done()
})
});
grunt.registerTask('ts:specs', 'Build specs.', function() {
var done = this.async();
executeTsc([
"--target", "ES5",
"--module", "commonjs",
"--outDir", "dist/specs",
].concat(grunt.file.expand("specs/**/*.spec.ts")), function(ret) {
grunt.log.writeln(ret.output);
if (ret.code != 0) {
grunt.fail.fatal("TSC error", ret.code);
}
done()
})
});
function wrap(item) {
return '/// <reference path="' + item.replace(".d.ts", "").replace(".ts", "") + '" />'
}
function relocate(from, to) {
return function(item) {
return item.replace(from, to)
}
}
grunt.registerTask('ts:extref', 'Build ts reference file.', function() {
var files = grunt.file.expand("typings/**/*.ts").map(relocate("typings", "../../typings")).map(wrap).join("\n");
files = '/// <reference path="../../tsd/tsd" />\n' + files;
grunt.log.writeln(files);
grunt.file.write("src/app/extref.d.ts", files);
});
grunt.loadNpmTasks('grunt-contrib-uglify');
grunt.loadNpmTasks('grunt-contrib-jasmine');
grunt.loadNpmTasks('grunt-contrib-watch');
grunt.loadNpmTasks('grunt-contrib-concat');
grunt.loadNpmTasks('grunt-contrib-sass');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-connect');
grunt.loadNpmTasks('grunt-notify');
grunt.registerTask('buildts', ['ts:dev', 'tsmap']);
grunt.registerTask('build', ['buildts', 'notify:compile']);
grunt.registerTask('build_libs', ['concat:libs']);
grunt.registerTask('build_styles', ['sass:app']);
grunt.registerTask('specs', ['buildts', 'ts:specs', 'jasmine']);
grunt.registerTask('w', ['specs', 'build_styles', 'watch']);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment