index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*!
  2. * strip-comments <https://github.com/jonschlinkert/strip-comments>
  3. *
  4. * Copyright (c) 2014-2016, Jon Schlinkert.
  5. * Released under the MIT license.
  6. */
  7. 'use strict';
  8. var extend = require('extend-shallow');
  9. var extract = require('extract-comments');
  10. /**
  11. * Strip all code comments from the given `input`,
  12. * including these that are ignored.
  13. * Pass `opts.safe: true` to keep them.
  14. *
  15. * **Example**
  16. *
  17. * ```js
  18. * var str = strip('foo; // this is a comment\n /* me too *\/');
  19. * console.log(str);
  20. * // => 'foo; \n '
  21. * ```
  22. *
  23. * @name strip
  24. * @param {String} `<input>` string from which to strip comments
  25. * @param {Object} `opts` optional options, passed to [extract-comments][extract-comments]
  26. * @option {Boolean} [opts] `line` if `false` strip only block comments, default `true`
  27. * @option {Boolean} [opts] `block` if `false` strip only line comments, default `true`
  28. * @option {Boolean} [opts] `safe` pass `true` to keep ignored comments (e.g. `/*!` and `//!`)
  29. * @option {Boolean} [opts] `preserveNewlines` if `true` preserve newlines after comments are stripped
  30. * @return {String} modified input
  31. * @api public
  32. */
  33. exports = module.exports = function stripAllComments(input, opts) {
  34. opts = extend({block: true, line: true}, opts);
  35. return stripComments(input, opts);
  36. };
  37. /**
  38. * Strip only block comments.
  39. *
  40. * **Example**
  41. *
  42. * ```js
  43. * var output = strip('foo; // this is a comment\n /* me too *\/', { line: false });
  44. * console.log(output);
  45. * // => 'foo; // this is a comment\n '
  46. * ```
  47. *
  48. * **Example**
  49. *
  50. * ```js
  51. * var output = strip.block('foo; // this is a comment\n /* me too *\/');
  52. * console.log(output);
  53. * // => 'foo; // this is a comment\n '
  54. * ```
  55. *
  56. * @name .block
  57. * @param {String} `<input>` string from which to strip comments
  58. * @param {Object} `[opts]` pass `opts.safe: true` to keep ignored comments (e.g. `/*!`)
  59. * @return {String} modified string
  60. * @api public
  61. */
  62. exports.block = function stripBlockComments(input, opts) {
  63. opts = extend({block: true}, opts);
  64. return stripComments(input, opts);
  65. };
  66. /**
  67. * Strip only line comments.
  68. *
  69. * **Example**
  70. *
  71. * ```js
  72. * var output = strip('foo; // this is a comment\n /* me too *\/', { block: false });
  73. * console.log(output);
  74. * // => 'foo; \n /* me too *\/'
  75. * ```
  76. *
  77. * **Example**
  78. *
  79. * ```js
  80. * var output = strip.line('foo; // this is a comment\n /* me too *\/');
  81. * console.log(output);
  82. * // => 'foo; \n /* me too *\/'
  83. * ```
  84. *
  85. * @name .line
  86. * @param {String} `<input>` string from which to strip comments
  87. * @param {Object} `[opts]` pass `opts.safe: true` to keep ignored comments (e.g. `//!`)
  88. * @return {String} modified string
  89. * @api public
  90. */
  91. exports.line = function stripLineComments(input, opts) {
  92. opts = extend({line: true}, opts);
  93. return stripComments(input, opts);
  94. };
  95. /**
  96. * Strip the first comment from the given `input`.
  97. * If `opts.safe: true` is passed, will strip the first that is not ignored.
  98. *
  99. * **Example**
  100. *
  101. * ```js
  102. * var str = '//! first comment\nfoo; // this is a comment';
  103. * var output = strip(str, {
  104. * first: true
  105. * });
  106. * console.log(output);
  107. * // => '\nfoo; // this is a comment'
  108. * ```
  109. *
  110. * **Example**
  111. *
  112. * ```js
  113. * var str = '//! first comment\nfoo; // this is a comment';
  114. * var output = strip.first(str, { safe: true });
  115. * console.log(output);
  116. * // => '//! first comment\nfoo; '
  117. * ```
  118. *
  119. * @name .first
  120. * @param {String} `<input>`
  121. * @param {Object} `[opts]` pass `opts.safe: true` to keep comments with `!`
  122. * @return {String}
  123. * @api public
  124. */
  125. exports.first = function stripFirstComment(input, opts) {
  126. opts = extend({block: true, line: true, first: true}, opts);
  127. return stripComments(input, opts);
  128. };
  129. /**
  130. * Private function for stripping comments.
  131. *
  132. * @param {String} `<input>`
  133. * @param {Object} `[opts]`
  134. * @return {String}
  135. * @api private
  136. */
  137. function stripComments(input, opts) {
  138. // strip all by default, including `ingored` comments.
  139. opts = extend({
  140. block: false,
  141. line: false,
  142. safe: false,
  143. first: false
  144. }, opts);
  145. // compatibility with `extract-comments`
  146. opts.keepProtected = opts.safe;
  147. var comments = extract(input, opts);
  148. var len = comments.length;
  149. var i = 0;
  150. while (i < len) {
  151. var comment = comments[i++];
  152. input = discard(input, comment, opts);
  153. }
  154. return input;
  155. }
  156. /**
  157. * Remove a comment from the given string.
  158. *
  159. * @param {String} `str`
  160. * @param {Object} `comment`
  161. * @param {Object} `opts`
  162. * @return {String}
  163. */
  164. function discard(str, comment, opts) {
  165. var ch = comment.value.charAt(0);
  166. if (opts && opts.safe === true && ch === '!') {
  167. return str;
  168. }
  169. var nl = '';
  170. if (opts && opts.preserveNewlines) {
  171. nl = comment.raw.replace(/[^\r\n]/g, '');
  172. }
  173. if (comment.type === 'line') {
  174. str = str.replace('//' + comment.raw, nl);
  175. }
  176. if (comment.type === 'block') {
  177. str = str.replace('/*' + comment.raw + '*/', nl);
  178. }
  179. return str;
  180. }