compile.ts.js 5.9 KB

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