build.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. var pkg = require('./package.json');
  2. var fs = require('fs');
  3. var ugly = require('uglify-js');
  4. var jshint = require('jshint').JSHINT;
  5. var babel = require('babel');
  6. var gaze = require('gaze');
  7. function writeBower() {
  8. var bower = {
  9. name: pkg.config.bower.name,
  10. description: pkg.description,
  11. dependencies: pkg.dependencies,
  12. keywords: pkg.keywords,
  13. authors: [pkg.author],
  14. license: pkg.license,
  15. homepage: pkg.homepage,
  16. ignore: pkg.config.bower.ignore,
  17. repository: pkg.repository,
  18. main: pkg.main,
  19. moduleType: pkg.config.bower.moduleType,
  20. };
  21. fs.writeFile('bower.json', JSON.stringify(bower, null, '\t'));
  22. return true;
  23. }
  24. function lint(full) {
  25. jshint(full.toString(), {
  26. browser: true,
  27. undef: true,
  28. unused: true,
  29. immed: true,
  30. eqeqeq: true,
  31. eqnull: true,
  32. noarg: true,
  33. predef: ['define', 'module', 'exports', 'Set']
  34. });
  35. if (jshint.errors.length) {
  36. jshint.errors.forEach(function (err) {
  37. console.log(err.line+':'+err.character+' '+err.reason);
  38. });
  39. } else {
  40. console.log('linted')
  41. }
  42. return true;
  43. }
  44. function build(code) {
  45. var minified = ugly.minify(code, {fromString: true}).code;
  46. var header = [
  47. '/*!',
  48. ' '+pkg.config.title+' '+pkg.version,
  49. ' license: MIT',
  50. ' '+pkg.homepage,
  51. '*/',
  52. ''
  53. ].join('\n');
  54. fs.writeFile('dist/'+pkg.config.filename+'.js', header+code);
  55. fs.writeFile('dist/'+pkg.config.filename+'.min.js', header+minified);
  56. writeBower();
  57. console.log('dist built');
  58. }
  59. function transform(filepath) {
  60. babel.transformFile(filepath, {modules: 'umd'}, function (err,res) {
  61. if (err) {
  62. return console.log(err);
  63. } else {
  64. lint(res.code);
  65. build(res.code);
  66. }
  67. });
  68. }
  69. gaze('src/'+pkg.config.filename+'.js', function(err, watcher){
  70. // On file changed
  71. this.on('changed', function(filepath) {
  72. transform(filepath);
  73. });
  74. console.log('watching');
  75. });
  76. transform('src/'+pkg.config.filename+'.js');