Giter Site home page Giter Site logo

jnthnjns / grunt-stripcomments Goto Github PK

View Code? Open in Web Editor NEW
12.0 2.0 12.0 46 KB

removes single line "//" style comments and multiline "/** */" style comments from code

License: MIT License

JavaScript 93.11% CSS 1.75% PHP 5.13%
javascript grunt-plugins grunt gruntfile

grunt-stripcomments's Introduction

grunt-stripcomments

Build Status Dependency Status CircleCI

Remove comments from code

Getting Started

This plugin requires Grunt =>0.4.0

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-stripcomments --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-stripcomments');

The "comments" task

Overview

In your project's Gruntfile, add a section named comments to the data object passed into grunt.initConfig().

grunt.initConfig({
  comments: {
    your_target: {
      // Target-specific file lists and/or options go here.
      options: {
          singleline: true,
          multiline: true,
          keepSpecialComments: false
      },
      src: [ 'src/*.js'] // files to remove comments from
    },
  },
});

Options

options.keepSpecialComments

Type: Boolean Default value: true

Determines whether or not to remove comments starting with /*!.

Note: NO special comments should be removed if the code is not yours. Special comments are used as attribution and you should consult with the authors before even considering stripping them from the source.

options.singleline

Type: Boolean Default value: true

Determines whether or not to remove single line comments

options.multiline

Type: Boolean Default value: true

Determines whether or not to remove multi line comments

Usage Examples

grunt.initConfig({
  comments: {
    js: {
      options: {
        singleline: true,
        multiline: false
      },
      src: [ 'src/*.js' ]
    },
    php: {
      options: {
        singleline: true,
        multiline: true
      },
      src: [ 'lib/*.php' ]
    }
  },
});

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

  • v 0.1.0 - alpha release

  • v 0.5.0 - update to support Grunt 1.0

  • v 0.5.1 - update ownership

  • v 0.6.0 - added support for file destination and nodeunit testing

  • v 0.7.0 - added support for special comments

  • v 0.7.1 - lint update

  • v 0.7.2 - EOL bug fix in unit tests

grunt-stripcomments's People

Contributors

eheikes avatar jnthnjns avatar kkemple avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

grunt-stripcomments's Issues

line comments still exist after running.

Fails to remove comments like the following:

$scope.someObj.loaded = false; // Show when loaded.
$scope.someObj.loaded = false; /* Show when loaded. */

Maybe a slight update to the regEx? I have a working example broken out for my testing, although you may want to combine. Also, this is not thoroughly tested, granted I'm sure you unit tests will work that out.

module.exports = function(grunt, options) {
  const multilineComment   = /^[\t\s]*\/\*\*?[^!][\s\S]*?\*\/[\r\n]/gm;
  const multilineComment2  = /[\t\s]*\/\*\s[^\*\/]*\*\/[\t\s]*[\n\r]/g;
  const singleLineComment  = /^[\t\s]*(\/\/)[^\n\r]*[\n\r]/gm;
  const singleLineComment2 = /[\t\s]+\/\/\s[^\n]*\n/g;

    return function(file) {
        var contents = grunt.file.read(file);

        if (options.multiline) {
            contents = contents.replace(multilineComment,'').replace(multilineComment2,'\n');
        }

        if (options.singleline) {
            contents = contents.replace(singleLineComment,'').replace(singleLineComment2,'\n');
        }

        grunt.file.write(file, contents);
    };
};

wildcards for subfolders not working

I believe that this src: ['dist/**/*.js'] is not working, or may be i'm doing something wrong… how can i include all js files located in 'dist' and all subfolders??

Support for HTML comments?

Is there any desire to add support for stripping HTML comments? I often heavily comment my HTML for development notes but don't want it making it out into production code.

Single Line:

<!-- Single Line Comment. -->

Multiline:

<!--
    This is a multiple line HTML comment
    which would be stripped from the multiline option.
-->

I guess a modification to the REGEX would need to be made to search for this comment syntax, including the invisible line breaks?

Regex for block comments

The regex for block comments is not working on my css files. I'm not a regex expert, but this change works for me:
var mulitlineComment = //[^_]_+([^\/][^*]__+)*//g;
Do you see any problems with it?

Regex for multiline comments

The following regex worked better for my purposes:

var multilineComment = /\/\*[^*]*\*+([^\/*][^*]*\*+)*\//gm;

It allows me to strip comments from my .css and .scss files such as this:

/* ============================================================================
   @MASTER STYLESHEET
   ========================================================================= */

What do you think?

Grunt-StripComments don`t stripping comment in CSS files

Hello. Thanks for your module )

I try to use it in my project but it don`t work :(
code:

        comments: {
            css: {
                options: {
                    singleline: true,
                    multiline:  true
                },
                src: ['build/css/*.css']
            }
        },

run grunt result

grunt comments
Running "comments:css" (comments) task

Done, without errors.

And then in CSS files no changes :( Comments like:
/* Extra small devices (phones, less than 768px) */
or
/*! normalize.css v3.0.1 | MIT License | git.io/normalize */
steal present.

Also i try to change options value to:

singleline: false,
multiline:  false

but situation the same

Multiline comments not stripped when having trailing spaces

try stripping the below comments

//this will be removed
var v="sss"; //this will not be removed
/*
* This comment is not removed.
*/

        /**
        *This will be removed.
        */

sdasd
/*
** will not remove
*/

the plugin is so unpredictable, could you please debug and check ?

Strip comments between /* & */

Hello, I like the idea of the plugin but for me it does not strip the following comments:

  /*
  Abc

  Xyz
   */

Can you please add the feature to strip comments between /* & */?

Slash double asterisk slash suppose to remove the GOOD CODE.

Hi,
I faced wrong behavior of module.
I use :

options: {
				singleline: true,
				multiline: true
			},

If following lines exists in code it will remove it:
/asteriskAsterisk/
CODE will REMOVED!
/asteriskAsterisk/

screenshot101

Except of it things works awesome!

Comments starting with /*! Not Being Removed

I realized that in one of js files comment was starting with /*! and your grunt plugin wasn't removing it. Checked the source code and found out it was explicitly added. Why was it written like that?
Thanks.

Grunt-StripComments is stripping URLs

Noticed there are some bugs with the way the comments are being stripped. Here is an example:

var url = 'http://' + host + '/helloWorld';

Expected: Should stay the same
Actual Behavior: Notices the // and strips everything to:
var url = http

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.