Can watch mode support watching new file automatically?

Do you want to request a feature or report a bug?
feature

What is the current behavior?
watch mode can not support watching new file automatically

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?

Watch mode can support add new file ?
Or I can tell webpack know when to run the function again to get new file in watch mode , so I can do something with webpack plugin or loader?

And I have a comment in issue#370 #370 (comment)

If this is a feature request, what is motivation or use case for changing the behavior?

I want to use watch mode, because it support incremental compilation and rebuild so fast when I modify a file.
But now I must restart webpack when add new file. If there are many enties, it will spend a lot of time !!!

Please mention other relevant information such as the browser version, Node.js version, webpack version and Operating System.

Author: Fantashit

2 thoughts on “Can watch mode support watching new file automatically?

  1. compiler.plugin("compilation", compilation => {
      compilation.contextDependencies.push(path.resolve(__dirname, "path/to/directory"));
    });

    This watches a directory.

  2. I found another way to get changed/removed file name:

    const SingleEntryPlugin = require("webpack/lib/SingleEntryPlugin");
    const path = require('path');
    const del = require('del');
    
    function AddWatchWebpackPlugin() {
    };
    
    let changeFileName;
    AddWatchWebpackPlugin.prototype.apply = function (compiler) {
        compiler.plugin("after-compile", function (compilation, callback) {
            // Watch for root project dir
            compilation.contextDependencies.push(path.resolve(buildConf.folders.main.src.dir));
            callback();
        });
    
        compiler.plugin("make", (compilation, callback) => {
            if (changeFileName) {
                let name = path.basename(changeFileName).replace('.ts', '');
                const dep = SingleEntryPlugin.createDependency(changeFileName, name);
                compilation.addEntry(buildConf.folders.main.src.dir, dep, name, callback);
                changeFileName = "";
            } else {
                callback();
            }
        });
    
        compiler.plugin('watch-run', function (compilation, callback) {
            /**
             * If file was changed, then compiler.watchFileSystem.watcher will object:
             * mtimes: {
             *   'PathToFileWhichWasChange': 'time in mls'
             * }
             * For example:
             *   mtimes: {'D:\Development\Projects\main\src\a2.ts': 1503649122899
             * }
             *
             * If you will watch to dir, it will have 2 objects:
             *
             * For example:
             *   mtimes: {
             *   'D:\Development\Projects\main\src\a2.ts___jb_tmp___': null,
             *   'D:\Development\Projects\main\src\a2.ts': 1503649122899
             * }
             *
             */
            console.log(compilation.compiler.watchFileSystem.watcher.mtimes );
            if (compilation.compiler.watchFileSystem.watcher.mtimes !== {}) {
                for (let file of Object.keys(compilation.compiler.watchFileSystem.watcher.mtimes)) {
                    // I will watch for js, ts files, and not dir.
                    if (/\.ts|\.js/.test(file) && !/___jb_tmp___/.test(file)) {
                        // If mtimes exist, and time file changed is not null, then we save filePath
                        if (compilation.compiler.watchFileSystem.watcher.mtimes[file] !== null) {
                            changeFileName = file;
                        } else {
                            // If mtimes exist, and time file changed time is NULL, then we delete builded file
                            del.sync(file.replace(path.dirname(file), yourBuildedDir).replace('.ts', '.js'), {force: true});
                        }
                    }
                }
            }
            callback();
        })
    };
    

Comments are closed.