grunt-build.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. module.exports = function (grunt) {
  2. 'use strict';
  3. var requirejs = require('requirejs');
  4. var path = require('path');
  5. var rDefineStart = /define\([^{]*?{/;
  6. var rDefineEndWithReturn = /\s*return\s+[^\}]+(\}\);[^\w\}]*)$/;
  7. var rDefineEnd = /\}\);[^}\w]*$/;
  8. grunt.registerMultiTask('build', 'concatenate source: summernote.js', function () {
  9. /**
  10. * Strip all definitions generated by requirejs
  11. *
  12. * @param {String} name
  13. * @param {String} path
  14. * @param {String} contents The contents to be written (including their AMD wrappers)
  15. */
  16. var convert = function (name, path, contents) {
  17. contents = contents.replace(rDefineStart, '');
  18. if (rDefineEndWithReturn.test(contents)) {
  19. contents = contents.replace(rDefineEndWithReturn, '');
  20. } else {
  21. contents = contents.replace(rDefineEnd, '');
  22. }
  23. return contents;
  24. };
  25. var outputPath = this.data.outFile;
  26. /**
  27. * Handle final output from the optimizer
  28. */
  29. var out = function (compiled) {
  30. // 01. Embed version
  31. var version = grunt.config('pkg.version');
  32. compiled = compiled.replace(/@VERSION/g, version);
  33. // 02. Embed Date
  34. var date = (new Date()).toISOString().replace(/:\d+\.\d+Z$/, 'Z');
  35. compiled = compiled.replace(/@DATE/g, date);
  36. grunt.file.write(outputPath, compiled);
  37. };
  38. var config = {
  39. name: 'summernote/summernote',
  40. baseUrl: this.data.baseUrl,
  41. out: out,
  42. optimize: 'none',
  43. wrap: {
  44. startFile: path.join(this.data.baseUrl, this.data.startFile),
  45. endFile: path.join(this.data.baseUrl, this.data.endFile)
  46. },
  47. findNestedDependencies: true,
  48. skipSemiColonInsertion: true,
  49. onBuildWrite: convert,
  50. excludeShallow: ['jquery', 'CodeMirror', 'app'],
  51. paths: {
  52. jquery: 'empty:',
  53. CodeMirror: 'empty:'
  54. },
  55. packages: [{
  56. name: 'summernote',
  57. location: './',
  58. main: 'summernote'
  59. }]
  60. };
  61. var done = this.async();
  62. requirejs.optimize(config, function () {
  63. done();
  64. }, function (err) {
  65. done(err);
  66. });
  67. });
  68. };