Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove // js and php comments #3

Closed
trompx opened this issue Sep 19, 2016 · 2 comments
Closed

Remove // js and php comments #3

trompx opened this issue Sep 19, 2016 · 2 comments

Comments

@trompx
Copy link

trompx commented Sep 19, 2016

Hello,

Is there a way to allow htmlclean to remove // comments inside and inline javascript <script>... </script>?
That would be awesome.

Thank you for the plugin

@anseki
Copy link
Owner

anseki commented Sep 19, 2016

Hi @trompx,

Since htmlclean protects those, you can add minifier for those to your task.
For example, following task minifies INPUT.html that contains inline JavaScript, and writes it to OUTPUT.html.

Install packages if you don't have.

npm install grunt-task-helper htmlclean uglify-js
module.exports = grunt => {
  const htmlclean = require('htmlclean'),
    uglify = require('uglify-js');

  grunt.initConfig({
    taskHelper: {
      minHTML: {
        options: {
          handlerByContent: content => {
            return htmlclean(content.replace(/(<script\b[^>]*>)([\s\S]*?)(<\/script>)/gi,
              (s, open, code, close) => `${open}${code ? uglify.minify(code, {fromString: true}).code : ''}${close}`));
          }
        },
        src: 'INPUT.html',
        dest: 'OUTPUT.html'
      }
    }
  });

  grunt.loadNpmTasks('grunt-task-helper');
  grunt.registerTask('default', ['taskHelper:minHTML']);
};

CSS code also can be minified by this way with CSS minifier (e.g. clean-css).

@anseki
Copy link
Owner

anseki commented Sep 26, 2016

No reply came, and I close this issue.

@anseki anseki closed this as completed Sep 26, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants