index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*!
  2. * strip-comments <https://github.com/jonschlinkert/strip-comments>
  3. * Copyright (c) 2014-present, Jon Schlinkert.
  4. * Released under the MIT License.
  5. */
  6. 'use strict';
  7. const compile = require('./lib/compile');
  8. const parse = require('./lib/parse');
  9. /**
  10. * Strip all code comments from the given `input`, including protected
  11. * comments that start with `!`, unless disabled by setting `options.keepProtected`
  12. * to true.
  13. *
  14. * ```js
  15. * const str = strip('const foo = "bar";// this is a comment\n /* me too *\/');
  16. * console.log(str);
  17. * // => 'const foo = "bar";'
  18. * ```
  19. * @name strip
  20. * @param {String} `input` string from which to strip comments
  21. * @param {Object} `options` optional options, passed to [extract-comments][extract-comments]
  22. * @option {Boolean} [options] `line` if `false` strip only block comments, default `true`
  23. * @option {Boolean} [options] `block` if `false` strip only line comments, default `true`
  24. * @option {Boolean} [options] `keepProtected` Keep ignored comments (e.g. `/*!` and `//!`)
  25. * @option {Boolean} [options] `preserveNewlines` Preserve newlines after comments are stripped
  26. * @return {String} modified input
  27. * @api public
  28. */
  29. const strip = module.exports = (input, options) => {
  30. const opts = { ...options, block: true, line: true };
  31. return compile(parse(input, opts), opts);
  32. };
  33. /**
  34. * Strip only block comments.
  35. *
  36. * ```js
  37. * const strip = require('..');
  38. * const str = strip.block('const foo = "bar";// this is a comment\n /* me too *\/');
  39. * console.log(str);
  40. * // => 'const foo = "bar";// this is a comment'
  41. * ```
  42. * @name .block
  43. * @param {String} `input` string from which to strip comments
  44. * @param {Object} `options` pass `opts.keepProtected: true` to keep ignored comments (e.g. `/*!`)
  45. * @return {String} modified string
  46. * @api public
  47. */
  48. strip.block = (input, options) => {
  49. const opts = { ...options, block: true };
  50. return compile(parse(input, opts), opts);
  51. };
  52. /**
  53. * Strip only line comments.
  54. *
  55. * ```js
  56. * const str = strip.line('const foo = "bar";// this is a comment\n /* me too *\/');
  57. * console.log(str);
  58. * // => 'const foo = "bar";\n/* me too *\/'
  59. * ```
  60. * @name .line
  61. * @param {String} `input` string from which to strip comments
  62. * @param {Object} `options` pass `opts.keepProtected: true` to keep ignored comments (e.g. `//!`)
  63. * @return {String} modified string
  64. * @api public
  65. */
  66. strip.line = (input, options) => {
  67. const opts = { ...options, line: true };
  68. return compile(parse(input, opts), opts);
  69. };
  70. /**
  71. * Strip the first comment from the given `input`. Or, if `opts.keepProtected` is true,
  72. * the first non-protected comment will be stripped.
  73. *
  74. * ```js
  75. * const output = strip.first(input, { keepProtected: true });
  76. * console.log(output);
  77. * // => '//! first comment\nfoo; '
  78. * ```
  79. * @name .first
  80. * @param {String} `input`
  81. * @param {Object} `options` pass `opts.keepProtected: true` to keep comments with `!`
  82. * @return {String}
  83. * @api public
  84. */
  85. strip.first = (input, options) => {
  86. const opts = { ...options, block: true, line: true, first: true };
  87. return compile(parse(input, opts), opts);
  88. };
  89. /**
  90. * Parses a string and returns a basic CST (Concrete Syntax Tree).
  91. *
  92. * ```js
  93. * const strip = require('..');
  94. * const str = strip.block('const foo = "bar";// this is a comment\n /* me too *\/');
  95. * console.log(str);
  96. * // => 'const foo = "bar";// this is a comment'
  97. * ```
  98. * @name .block
  99. * @param {String} `input` string from which to strip comments
  100. * @param {Object} `options` pass `opts.keepProtected: true` to keep ignored comments (e.g. `/*!`)
  101. * @return {String} modified string
  102. * @api public
  103. */
  104. strip.parse = parse;