srod/node-minify

Applying minification to all files in a directory

arnog opened this issue · 4 comments

arnog commented

I'm trying to apply html-minifier to all files inside a directory (recursively). I wasn't able to figure out how to do it.

I tried:

minify({
    compressor: htmlMinifier,
    publicFolder: __dirname + '/../stage/**',
    input: '*.html',
    output: '$1.min.html',
    callback: function(err, _min) {
        if (err) console.log(err)
    }
});

But this results in

(node:19076) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open 'C:\project\stage\**C:\project\stage\index.html'

Trying:

minify({
    compressor: htmlMinifier,
    publicFolder: __dirname + '/../stage/',
    input: '**/*.html',
    output: '$1.min.html',
    callback: function(err, _min) {
        if (err) console.log(err)
    }
});

does not error out, but the output file is created in the top level of the repo.

It would be nice to have an option, like replace-in-file for example, where the list of files to be processed can be specified as a files property which is a glob of the eligible files.

Alternatively, the output property could be optional, in which case the input file is replaced in place.

arnog commented

Thanks!

srod commented

Landed in 5.0.0

arnog commented

I don't get quite the result I was hoping for. Using the code as in the initial report (second block), it looks like all the output file are created in top-level (public) folder, not in the directory from which they came (when using a glob). So, I'm still looking for a solution that would allow me to apply the minifier to a set of a file in a directory, specified with a glob pattern, and replace them in place with the value of output. Should I re-open a bug or do you want to track it with this one?

srod commented

I have published 5.2.0 with a new option : replaceInPlace.
This option will save files in same directory than source.

minify({
  compressor: htmlMinifier,
  publicFolder: 'public/html/',
  input: '**/*.html',
  output: '$1.min.html',
  replaceInPlace: true,
  callback: function(err, _min) {
    if (err) console.log(err);
    console.log(_min);
  }
});