Amaimersion / remove-files-webpack-plugin

A plugin for webpack that removes files and folders before and after compilation.

Home Page:https://www.npmjs.com/package/remove-files-webpack-plugin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Keep generated files when in watch mode

YannPl opened this issue · comments

Hi !
I have setup the remove plugin do clean old generated files before the build.
This works fine, but I would like it to be executed only once when I launch it in watch mode.
When the rebuild occurs on only one of the entry points, the others gets deleted.

I haven't found an option to keep it from executing to every single rebuild.

Hello!

Please provide a plugin config.

Hi, there is my config.
I have commented the 3 folders I'd like to delete only once when I execute webopack in watch mode

new RemovePlugin({
        before: {
          root   : './public',
          include: [
            // 'css', // TODO : prevent these folders to be deleted in watch mode
            // 'images', // TODO : prevent these folders to be deleted in watch mode
            // 'js', // TODO : prevent these folders to be deleted in watch mode
            'mix.app.js',
            'mix.app.js.map',
            'mix-manifest.json'
          ],
          trash  : false
        },
        after : {
          // parameters for "after compilation" stage.
          root   : './public',
          include: [
            'mix.app.js',
            'mix.app.js.map',
            'mix-manifest.json'
          ]
        }
      })
    ],

If i'm get it right: you don't want to delete css, images and js folders in "watch" mode, because you only watchs for changes in mix.app.js, min.app.js.map and mix-manifest.json files.

I wonder many times about separate watch parameter (similar to before and after), because before can have conflicts with "watch" mode (for example: you want to delete some items before actual compilation, but at the same time you want to keep some of these items in "watch" mode).

Will the next configuration to meet your expectations?

new RemovePlugin({
  /**
   * This will delete `css`, `images`, `js`,
   * `mix.app.js`, `mix.app.js.map` and `mix-manifest.json`
   * when you run `webpack`. 
   * Removing will occur only once.
   * Not worked in "watch" mode.
   */
  before: {
    root: './public',
    include: [
      'css',
      'images',
      'js',
      'mix.app.js',
      'mix.app.js.map',
      'mix-manifest.json'
    ],
    trash: false
  },

  /**
   * This will delete `mix.app.js`, `mix.app.js.map` and 
   * `mix-manifest.json` when you run `webpack` or `webpack --watch`.
   * Removing will occur every time after compilation and recompilation.
   */
  after: {
    root: './public',
    include: [
      /* you really need to delete this in both stages: before and after?
      'mix.app.js',
      'mix.app.js.map',
      'mix-manifest.json'
      */
    ]
  },

  /**
   * This will delete `mix.app.js`, `mix.app.js.map` and 
   * `mix-manifest.json` when you run `webpack --watch`
   * (i.e. `css`, `images` and `js` folders will be keeped).
   * Removing will occur every time before recompilation
   * (and first time when you run `webpack --watch`).
   * Works only in "watch" mode.
   */
  watch: {
    root: './public',
    include: [
      'mix.app.js',
      'mix.app.js.map',
      'mix-manifest.json'
    ]
  }
})

Note: watch not implemented yet. So, don't try to use this configuration. Just imagine your webpack lifecycle.

I have updated code example from my reply.

If i'm get it right: you don't want to delete css, images and js folders in "watch" mode, because you only watchs for changes in mix.app.js, min.app.js.map and mix-manifest.json files.

Those are files generated by mix, the tool I use to ease webpack configuration so I just want to delete them for a clean /public directory.

(for example: you want to delete some items before actual compilation, but at the same time you want to keep some of these items in "watch" mode).

This is exactly the problem I have:

  • I generate several JS files in the /js dir
  • When I modify my code, the watch only build 1 file
  • It triggers the delete for all js files and only 1 is recreated

Will the next configuration to meet your expectations?

It would be perfect to be able to delete different files at each watch rebuild like you show yes.

But is it possible to have the "full clean" executed at the start of the watch command but only the "watch" clean executed at rebuilds ?

(I don't know if I'm clear enough feel free to ask for clarification)

/* you really need to delete this in both stages: before and after?
'mix.app.js',
'mix.app.js.map',
'mix-manifest.json'
*/

You are right, I might delete this in the "after" step if I can declare a separate watch option

It triggers the delete for all js files and only 1 is recreated
When I modify my code, the watch only build 1 file

I think it will be solved if i will add watch key. Please take a look at this:

new RemovePlugin({
  // will remove entire `public/js` folder 
  // in normal compilation (`webpack`).
  before: {
    root: './public',
    include: [
      './js'
    ]
  },
  
  // every time will remove entire `public/js/watch-file.js` 
  // file in watch compilation (`webpack --watch`).
  watch: {
    root: './public',
    include: [
      './js/watch-file.js'
    ]
  }
})

But is it possible to have the "full clean" executed at the start of the watch command but only the "watch" clean executed at rebuilds ?

You want to delete all items at initial "watch" step and only some items at future "watch" steps? I'm got it right?

You can use configuration like this when i implement watch key:

// ...

let watchStepsCount = 0;

module.exports = {
  // ...
  plugins: [
    new RemovePlugin({
      watch: {
        test: [
          {
            folder: './public',
            method: (absPath) => {
              if (watchStepsCount === 0) {
                // remove everything.
                return true;
              } else {
                const filesToDelete = [
                  'mix.app.js',
                  'mix.app.js.map',
                  'mix-manifest.json'
                ];

                for (const file of filesToDelete) {
                  const includes = absPath.includes(file);

                  if (includes) {
                    return true;
                  }
                }

                return false;
              }
            },
            recursive: true
          }
        ],
        afterRemove: () => watchStepsCount++
      }
    })
  ]
}

Just tested it at my local build. Works as expected (as i described above).

Actually, you can implement this for current version (1.3.0), but for before key, not watch. Because at the moment (1.3.0) before also works for "watch" mode, not just for normal compilation.

You are right, I might delete this in the "after" step if I can declare a separate watch option

These files will be removed twice at one compilation lifecycle. i.e., once before "watch" and right after the output from "watch".

Thanks for your time and help, I will implement it in my project, and update if you release the "watch" option later !

You can try to implement this at the moment for before key. By the way, release with watch key will be released soon (probably today).

Nice ! Thanks !

Please upgrade to 1.4.0 and use watch key.

I'm closing this. If you still have troubles with that issue, then feel free to reopen.