Makefile.dryice.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* -*- Mode: js; js-indent-level: 2; -*- */
  2. /*
  3. * Copyright 2011 Mozilla Foundation and contributors
  4. * Licensed under the New BSD license. See LICENSE or:
  5. * http://opensource.org/licenses/BSD-3-Clause
  6. */
  7. var path = require('path');
  8. var fs = require('fs');
  9. var copy = require('dryice').copy;
  10. function removeAmdefine(src) {
  11. src = String(src).replace(
  12. /if\s*\(typeof\s*define\s*!==\s*'function'\)\s*{\s*var\s*define\s*=\s*require\('amdefine'\)\(module,\s*require\);\s*}\s*/g,
  13. '');
  14. src = src.replace(
  15. /\b(define\(.*)('amdefine',?)/gm,
  16. '$1');
  17. return src;
  18. }
  19. removeAmdefine.onRead = true;
  20. function makeNonRelative(src) {
  21. return src
  22. .replace(/require\('.\//g, 'require(\'source-map/')
  23. .replace(/\.\.\/\.\.\/lib\//g, '');
  24. }
  25. makeNonRelative.onRead = true;
  26. function buildBrowser() {
  27. console.log('\nCreating dist/source-map.js');
  28. var project = copy.createCommonJsProject({
  29. roots: [ path.join(__dirname, 'lib') ]
  30. });
  31. copy({
  32. source: [
  33. 'build/mini-require.js',
  34. {
  35. project: project,
  36. require: [ 'source-map/source-map-generator',
  37. 'source-map/source-map-consumer',
  38. 'source-map/source-node']
  39. },
  40. 'build/suffix-browser.js'
  41. ],
  42. filter: [
  43. copy.filter.moduleDefines,
  44. removeAmdefine
  45. ],
  46. dest: 'dist/source-map.js'
  47. });
  48. }
  49. function buildBrowserMin() {
  50. console.log('\nCreating dist/source-map.min.js');
  51. copy({
  52. source: 'dist/source-map.js',
  53. filter: copy.filter.uglifyjs,
  54. dest: 'dist/source-map.min.js'
  55. });
  56. }
  57. function buildFirefox() {
  58. console.log('\nCreating dist/SourceMap.jsm');
  59. var project = copy.createCommonJsProject({
  60. roots: [ path.join(__dirname, 'lib') ]
  61. });
  62. copy({
  63. source: [
  64. 'build/prefix-source-map.jsm',
  65. {
  66. project: project,
  67. require: [ 'source-map/source-map-consumer',
  68. 'source-map/source-map-generator',
  69. 'source-map/source-node' ]
  70. },
  71. 'build/suffix-source-map.jsm'
  72. ],
  73. filter: [
  74. copy.filter.moduleDefines,
  75. removeAmdefine,
  76. makeNonRelative
  77. ],
  78. dest: 'dist/SourceMap.jsm'
  79. });
  80. // Create dist/test/Utils.jsm
  81. console.log('\nCreating dist/test/Utils.jsm');
  82. project = copy.createCommonJsProject({
  83. roots: [ __dirname, path.join(__dirname, 'lib') ]
  84. });
  85. copy({
  86. source: [
  87. 'build/prefix-utils.jsm',
  88. 'build/assert-shim.js',
  89. {
  90. project: project,
  91. require: [ 'test/source-map/util' ]
  92. },
  93. 'build/suffix-utils.jsm'
  94. ],
  95. filter: [
  96. copy.filter.moduleDefines,
  97. removeAmdefine,
  98. makeNonRelative
  99. ],
  100. dest: 'dist/test/Utils.jsm'
  101. });
  102. function isTestFile(f) {
  103. return /^test\-.*?\.js/.test(f);
  104. }
  105. var testFiles = fs.readdirSync(path.join(__dirname, 'test', 'source-map')).filter(isTestFile);
  106. testFiles.forEach(function (testFile) {
  107. console.log('\nCreating', path.join('dist', 'test', testFile.replace(/\-/g, '_')));
  108. copy({
  109. source: [
  110. 'build/test-prefix.js',
  111. path.join('test', 'source-map', testFile),
  112. 'build/test-suffix.js'
  113. ],
  114. filter: [
  115. removeAmdefine,
  116. makeNonRelative,
  117. function (input, source) {
  118. return input.replace('define(',
  119. 'define("'
  120. + path.join('test', 'source-map', testFile.replace(/\.js$/, ''))
  121. + '", ["require", "exports", "module"], ');
  122. },
  123. function (input, source) {
  124. return input.replace('{THIS_MODULE}', function () {
  125. return "test/source-map/" + testFile.replace(/\.js$/, '');
  126. });
  127. }
  128. ],
  129. dest: path.join('dist', 'test', testFile.replace(/\-/g, '_'))
  130. });
  131. });
  132. }
  133. function ensureDir(name) {
  134. var dirExists = false;
  135. try {
  136. dirExists = fs.statSync(name).isDirectory();
  137. } catch (err) {}
  138. if (!dirExists) {
  139. fs.mkdirSync(name, 0777);
  140. }
  141. }
  142. ensureDir("dist");
  143. ensureDir("dist/test");
  144. buildFirefox();
  145. buildBrowser();
  146. buildBrowserMin();