comment-stripper
Version:
Webpack loader that strips comments which can be used on its own too
114 lines (101 loc) • 3.5 kB
JavaScript
/*!
* strip-comments <https://github.com/jonschlinkert/strip-comments>
* Copyright (c) 2014-present, Jon Schlinkert.
* Released under the MIT License.
*/
const loaderUtils = require('loader-utils');
const compile = require('./lib/compile');
const parse = require('./lib/parse');
/**
* Strip all code comments from the given `input`, including protected
* comments that start with `!`, unless disabled by setting `options.keepProtected`
* to true.
*
* ```js
* const str = strip('const foo = "bar";// this is a comment\n /* me too *\/');
* console.log(str);
* // => 'const foo = "bar";'
* ```
* @name strip
* @param {String} `input` string from which to strip comments
* @param {Object} `[options]` optional options, passed to [extract-comments][extract-comments]
* @option {Boolean} [options] `line` if `false` strip only block comments, default `true`
* @option {Boolean} [options] `block` if `false` strip only line comments, default `true`
* @option {Boolean} [options] `keepProtected` Keep ignored comments (e.g. `/*!` and `//!`)
* @option {Boolean} [options] `preserveNewlines` Preserve newlines after comments are stripped
* @return {String} modified input
* @api public
*/
const strip = (input, options = {}) => {
const opts = { ...options, block: true, line: true };
return compile(parse(input, opts), opts);
};
/**
* Strip only block comments.
*
* ```js
* const strip = require('..');
* const str = strip.block('const foo = "bar";// this is a comment\n /* me too *\/');
* console.log(str);
* // => 'const foo = "bar";// this is a comment'
* ```
* @name .block
* @param {String} `input` string from which to strip comments
* @param {Object} `options` pass `opts.keepProtected: true` to keep ignored comments (e.g. `/*!`)
* @return {String} modified string
* @api public
*/
strip.block = (input, options) => {
const opts = { ...options, block: true };
return compile(parse(input, opts), opts);
};
/**
* Strip only line comments.
*
* ```js
* const str = strip.line('const foo = "bar";// this is a comment\n /* me too *\/');
* console.log(str);
* // => 'const foo = "bar";\n/* me too *\/'
* ```
* @name .line
* @param {String} `input` string from which to strip comments
* @param {Object} `options` pass `opts.keepProtected: true` to keep ignored comments (e.g. `//!`)
* @return {String} modified string
* @api public
*/
strip.line = (input, options) => {
const opts = { ...options, line: true };
return compile(parse(input, opts), opts);
};
/**
* Parses a string and returns a basic CST (Concrete Syntax Tree).
*
* ```js
* const strip = require('..');
* const str = strip.block('const foo = "bar";// this is a comment\n /* me too *\/');
* console.log(str);
* // => 'const foo = "bar";// this is a comment'
* ```
* @name .block
* @param {String} `input` string from which to strip comments
* @param {Object} `options` pass `opts.keepProtected: true` to keep ignored comments (e.g. `/*!`)
* @return {String} modified string
* @api public
*/
strip.parse = parse;
module.exports = function (code) {
this.cacheable?.();
const options = loaderUtils.getOptions(this);
let func;
if (options.line === false && options.block !== false) {
// Only line comments
func = strip.line;
} else if (options.block === false && options.line !== false) {
func = strip.block;
} else {
func = strip;
}
const output = func(code, options);
return output;
}
module.exports.strip = strip;