compile.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  3. var path = require('path');
  4. var fs = require('fs');
  5. var svelte = require('../compiler/svelte.js');
  6. const $ = { enabled:true };
  7. const CODES = {
  8. // modifiers
  9. reset: fmt(0, 0),
  10. bold: fmt(1, 22),
  11. dim: fmt(2, 22),
  12. italic: fmt(3, 23),
  13. underline: fmt(4, 24),
  14. inverse: fmt(7, 27),
  15. hidden: fmt(8, 28),
  16. strikethrough: fmt(9, 29),
  17. // colors
  18. black: fmt(30, 39),
  19. red: fmt(31, 39),
  20. green: fmt(32, 39),
  21. yellow: fmt(33, 39),
  22. blue: fmt(34, 39),
  23. magenta: fmt(35, 39),
  24. cyan: fmt(36, 39),
  25. white: fmt(37, 39),
  26. gray: fmt(90, 39),
  27. // background colors
  28. bgBlack: fmt(40, 49),
  29. bgRed: fmt(41, 49),
  30. bgGreen: fmt(42, 49),
  31. bgYellow: fmt(43, 49),
  32. bgBlue: fmt(44, 49),
  33. bgMagenta: fmt(45, 49),
  34. bgCyan: fmt(46, 49),
  35. bgWhite: fmt(47, 49)
  36. };
  37. function fmt(x, y) {
  38. return {
  39. open: `\x1b[${x}m`,
  40. close: `\x1b[${y}m`,
  41. rgx: new RegExp(`\\x1b\\[${y}m`, 'g')
  42. }
  43. }
  44. function run(key, str) {
  45. let tmp = CODES[key];
  46. return tmp.open + str.replace(tmp.rgx, tmp.open) + tmp.close;
  47. }
  48. function exec(key, str) {
  49. str += '';
  50. if (!$.enabled) return str;
  51. let arr = this.keys;
  52. while (arr.length > 0) {
  53. str = run(arr.shift(), str);
  54. }
  55. this.keys.push(key);
  56. return str;
  57. }
  58. function attach(key) {
  59. let ctx = { keys:[key] };
  60. let fn = exec.bind(ctx, key);
  61. for (let k in CODES) {
  62. Reflect.defineProperty(fn, k, {
  63. get() {
  64. ctx.keys.push(k);
  65. return fn;
  66. }
  67. });
  68. }
  69. return fn;
  70. }
  71. for (let k in CODES) {
  72. $[k] = attach(k);
  73. }
  74. var kleur = $;
  75. function stderr(msg) {
  76. console.error(msg); // eslint-disable-line no-console
  77. }
  78. function error(err) {
  79. stderr(kleur.red(err.message || err));
  80. if (err.frame) {
  81. stderr(err.frame);
  82. }
  83. else if (err.stack) {
  84. stderr(kleur.gray(err.stack));
  85. }
  86. process.exit(1);
  87. }
  88. function mkdirp(dir) {
  89. const parent = path.dirname(dir);
  90. if (dir === parent)
  91. return;
  92. mkdirp(parent);
  93. if (!fs.existsSync(dir))
  94. fs.mkdirSync(dir);
  95. }
  96. function compile(input, opts) {
  97. if (opts._.length > 0) {
  98. error(`Can only compile a single file or directory`);
  99. }
  100. const output = opts.output;
  101. const stats = fs.statSync(input);
  102. const isDir = stats.isDirectory();
  103. if (isDir) {
  104. if (!output) {
  105. error(`You must specify an --output (-o) option when compiling a directory of files`);
  106. }
  107. if (opts.name || opts.amdId) {
  108. error(`Cannot specify --${opts.name ? 'name' : 'amdId'} when compiling a directory`);
  109. }
  110. }
  111. const globals = {};
  112. if (opts.globals) {
  113. opts.globals.split(',').forEach(pair => {
  114. const [key, value] = pair.split(':');
  115. globals[key] = value;
  116. });
  117. }
  118. const options = {
  119. name: opts.name,
  120. format: opts.format,
  121. sourceMap: opts.sourcemap,
  122. globals,
  123. amd: opts.amdId
  124. ? {
  125. id: opts.amdId,
  126. }
  127. : undefined,
  128. css: opts.css !== false,
  129. dev: opts.dev,
  130. immutable: opts.immutable,
  131. generate: opts.generate || 'dom',
  132. customElement: opts.customElement,
  133. store: opts.store,
  134. shared: opts.shared
  135. };
  136. if (isDir) {
  137. mkdirp(output);
  138. compileDirectory(input, output, options);
  139. }
  140. else {
  141. compileFile(input, output, options);
  142. }
  143. }
  144. function compileDirectory(input, output, options) {
  145. fs.readdirSync(input).forEach(file => {
  146. const src = path.resolve(input, file);
  147. const dest = path.resolve(output, file);
  148. if (path.extname(file) === '.html') {
  149. compileFile(src, dest.substring(0, dest.lastIndexOf('.html')) + '.js', options);
  150. }
  151. else {
  152. const stats = fs.statSync(src);
  153. if (stats.isDirectory()) {
  154. compileDirectory(src, dest, options);
  155. }
  156. }
  157. });
  158. }
  159. let SOURCEMAPPING_URL = 'sourceMa';
  160. SOURCEMAPPING_URL += 'ppingURL';
  161. function compileFile(input, output, options) {
  162. console.error(`compiling ${path.relative(process.cwd(), input)}...`); // eslint-disable-line no-console
  163. options = Object.assign({}, options);
  164. if (!options.name)
  165. options.name = getName(input);
  166. options.filename = input;
  167. options.outputFilename = output;
  168. const { sourceMap } = options;
  169. const inline = sourceMap === 'inline';
  170. let source = fs.readFileSync(input, 'utf-8');
  171. if (source[0] === 0xfeff)
  172. source = source.slice(1);
  173. let compiled;
  174. try {
  175. compiled = svelte.compile(source, options);
  176. }
  177. catch (err) {
  178. error(err);
  179. }
  180. const { js } = compiled;
  181. if (sourceMap) {
  182. js.code += `\n//# ${SOURCEMAPPING_URL}=${inline || !output
  183. ? js.map.toUrl()
  184. : `${path.basename(output)}.map`}\n`;
  185. }
  186. if (output) {
  187. const outputDir = path.dirname(output);
  188. mkdirp(outputDir);
  189. fs.writeFileSync(output, js.code);
  190. console.error(`wrote ${path.relative(process.cwd(), output)}`); // eslint-disable-line no-console
  191. if (sourceMap && !inline) {
  192. fs.writeFileSync(`${output}.map`, js.map);
  193. console.error(`wrote ${path.relative(process.cwd(), `${output}.map`)}`); // eslint-disable-line no-console
  194. }
  195. }
  196. else {
  197. process.stdout.write(js.code);
  198. }
  199. }
  200. function getName(input) {
  201. return path.basename(input)
  202. .replace(path.extname(input), '')
  203. .replace(/[^a-zA-Z_$0-9]+/g, '_')
  204. .replace(/^_/, '')
  205. .replace(/_$/, '')
  206. .replace(/^(\d)/, '_$1');
  207. }
  208. exports.compile = compile;