| 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | Author Tobias Koppers @sokra
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | "use strict";
|
|---|
| 7 |
|
|---|
| 8 | const util = require("util");
|
|---|
| 9 | const Watchpack = require("watchpack");
|
|---|
| 10 |
|
|---|
| 11 | /** @typedef {import("watchpack").TimeInfoEntries} TimeInfoEntries */
|
|---|
| 12 | /** @typedef {import("watchpack").WatchOptions} WatchOptions */
|
|---|
| 13 | /** @typedef {import("../util/fs").InputFileSystem} InputFileSystem */
|
|---|
| 14 | /** @typedef {import("../util/fs").WatchMethod} WatchMethod */
|
|---|
| 15 | /** @typedef {import("../util/fs").Changes} Changes */
|
|---|
| 16 | /** @typedef {import("../util/fs").Removals} Removals */
|
|---|
| 17 |
|
|---|
| 18 | class NodeWatchFileSystem {
|
|---|
| 19 | /**
|
|---|
| 20 | * Creates an instance of NodeWatchFileSystem.
|
|---|
| 21 | * @param {InputFileSystem} inputFileSystem input filesystem
|
|---|
| 22 | */
|
|---|
| 23 | constructor(inputFileSystem) {
|
|---|
| 24 | /** @type {InputFileSystem} */
|
|---|
| 25 | this.inputFileSystem = inputFileSystem;
|
|---|
| 26 | /** @type {WatchOptions} */
|
|---|
| 27 | this.watcherOptions = {
|
|---|
| 28 | aggregateTimeout: 0
|
|---|
| 29 | };
|
|---|
| 30 | /** @type {Watchpack | null} */
|
|---|
| 31 | this.watcher = new Watchpack(this.watcherOptions);
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | /** @type {WatchMethod} */
|
|---|
| 35 | watch(
|
|---|
| 36 | files,
|
|---|
| 37 | directories,
|
|---|
| 38 | missing,
|
|---|
| 39 | startTime,
|
|---|
| 40 | options,
|
|---|
| 41 | callback,
|
|---|
| 42 | callbackUndelayed
|
|---|
| 43 | ) {
|
|---|
| 44 | if (!files || typeof files[Symbol.iterator] !== "function") {
|
|---|
| 45 | throw new Error("Invalid arguments: 'files'");
|
|---|
| 46 | }
|
|---|
| 47 | if (!directories || typeof directories[Symbol.iterator] !== "function") {
|
|---|
| 48 | throw new Error("Invalid arguments: 'directories'");
|
|---|
| 49 | }
|
|---|
| 50 | if (!missing || typeof missing[Symbol.iterator] !== "function") {
|
|---|
| 51 | throw new Error("Invalid arguments: 'missing'");
|
|---|
| 52 | }
|
|---|
| 53 | if (typeof callback !== "function") {
|
|---|
| 54 | throw new Error("Invalid arguments: 'callback'");
|
|---|
| 55 | }
|
|---|
| 56 | if (typeof startTime !== "number" && startTime) {
|
|---|
| 57 | throw new Error("Invalid arguments: 'startTime'");
|
|---|
| 58 | }
|
|---|
| 59 | if (typeof options !== "object") {
|
|---|
| 60 | throw new Error("Invalid arguments: 'options'");
|
|---|
| 61 | }
|
|---|
| 62 | if (typeof callbackUndelayed !== "function" && callbackUndelayed) {
|
|---|
| 63 | throw new Error("Invalid arguments: 'callbackUndelayed'");
|
|---|
| 64 | }
|
|---|
| 65 | const oldWatcher = this.watcher;
|
|---|
| 66 | this.watcher = new Watchpack(options);
|
|---|
| 67 |
|
|---|
| 68 | if (callbackUndelayed) {
|
|---|
| 69 | this.watcher.once("change", callbackUndelayed);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | const fetchTimeInfo = () => {
|
|---|
| 73 | /** @type {TimeInfoEntries} */
|
|---|
| 74 | const fileTimeInfoEntries = new Map();
|
|---|
| 75 | /** @type {TimeInfoEntries} */
|
|---|
| 76 | const contextTimeInfoEntries = new Map();
|
|---|
| 77 | if (this.watcher) {
|
|---|
| 78 | this.watcher.collectTimeInfoEntries(
|
|---|
| 79 | fileTimeInfoEntries,
|
|---|
| 80 | contextTimeInfoEntries
|
|---|
| 81 | );
|
|---|
| 82 | }
|
|---|
| 83 | return { fileTimeInfoEntries, contextTimeInfoEntries };
|
|---|
| 84 | };
|
|---|
| 85 | this.watcher.once(
|
|---|
| 86 | "aggregated",
|
|---|
| 87 | /**
|
|---|
| 88 | * Handles the callback logic for this hook.
|
|---|
| 89 | * @param {Changes} changes changes
|
|---|
| 90 | * @param {Removals} removals removals
|
|---|
| 91 | */
|
|---|
| 92 | (changes, removals) => {
|
|---|
| 93 | // pause emitting events (avoids clearing aggregated changes and removals on timeout)
|
|---|
| 94 | /** @type {Watchpack} */
|
|---|
| 95 | (this.watcher).pause();
|
|---|
| 96 |
|
|---|
| 97 | const fs = this.inputFileSystem;
|
|---|
| 98 | if (fs && fs.purge) {
|
|---|
| 99 | for (const item of changes) {
|
|---|
| 100 | fs.purge(item);
|
|---|
| 101 | }
|
|---|
| 102 | for (const item of removals) {
|
|---|
| 103 | fs.purge(item);
|
|---|
| 104 | }
|
|---|
| 105 | }
|
|---|
| 106 | const { fileTimeInfoEntries, contextTimeInfoEntries } = fetchTimeInfo();
|
|---|
| 107 | callback(
|
|---|
| 108 | null,
|
|---|
| 109 | fileTimeInfoEntries,
|
|---|
| 110 | contextTimeInfoEntries,
|
|---|
| 111 | changes,
|
|---|
| 112 | removals
|
|---|
| 113 | );
|
|---|
| 114 | }
|
|---|
| 115 | );
|
|---|
| 116 |
|
|---|
| 117 | this.watcher.watch({ files, directories, missing, startTime });
|
|---|
| 118 |
|
|---|
| 119 | if (oldWatcher) {
|
|---|
| 120 | oldWatcher.close();
|
|---|
| 121 | }
|
|---|
| 122 | return {
|
|---|
| 123 | close: () => {
|
|---|
| 124 | if (this.watcher) {
|
|---|
| 125 | this.watcher.close();
|
|---|
| 126 | this.watcher = null;
|
|---|
| 127 | }
|
|---|
| 128 | },
|
|---|
| 129 | pause: () => {
|
|---|
| 130 | if (this.watcher) {
|
|---|
| 131 | this.watcher.pause();
|
|---|
| 132 | }
|
|---|
| 133 | },
|
|---|
| 134 | getAggregatedRemovals: util.deprecate(
|
|---|
| 135 | () => {
|
|---|
| 136 | const items = this.watcher && this.watcher.aggregatedRemovals;
|
|---|
| 137 | const fs = this.inputFileSystem;
|
|---|
| 138 | if (items && fs && fs.purge) {
|
|---|
| 139 | for (const item of items) {
|
|---|
| 140 | fs.purge(item);
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 | return items;
|
|---|
| 144 | },
|
|---|
| 145 | "Watcher.getAggregatedRemovals is deprecated in favor of Watcher.getInfo since that's more performant.",
|
|---|
| 146 | "DEP_WEBPACK_WATCHER_GET_AGGREGATED_REMOVALS"
|
|---|
| 147 | ),
|
|---|
| 148 | getAggregatedChanges: util.deprecate(
|
|---|
| 149 | () => {
|
|---|
| 150 | const items = this.watcher && this.watcher.aggregatedChanges;
|
|---|
| 151 | const fs = this.inputFileSystem;
|
|---|
| 152 | if (items && fs && fs.purge) {
|
|---|
| 153 | for (const item of items) {
|
|---|
| 154 | fs.purge(item);
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 | return items;
|
|---|
| 158 | },
|
|---|
| 159 | "Watcher.getAggregatedChanges is deprecated in favor of Watcher.getInfo since that's more performant.",
|
|---|
| 160 | "DEP_WEBPACK_WATCHER_GET_AGGREGATED_CHANGES"
|
|---|
| 161 | ),
|
|---|
| 162 | getFileTimeInfoEntries: util.deprecate(
|
|---|
| 163 | () => fetchTimeInfo().fileTimeInfoEntries,
|
|---|
| 164 | "Watcher.getFileTimeInfoEntries is deprecated in favor of Watcher.getInfo since that's more performant.",
|
|---|
| 165 | "DEP_WEBPACK_WATCHER_FILE_TIME_INFO_ENTRIES"
|
|---|
| 166 | ),
|
|---|
| 167 | getContextTimeInfoEntries: util.deprecate(
|
|---|
| 168 | () => fetchTimeInfo().contextTimeInfoEntries,
|
|---|
| 169 | "Watcher.getContextTimeInfoEntries is deprecated in favor of Watcher.getInfo since that's more performant.",
|
|---|
| 170 | "DEP_WEBPACK_WATCHER_CONTEXT_TIME_INFO_ENTRIES"
|
|---|
| 171 | ),
|
|---|
| 172 | getInfo: () => {
|
|---|
| 173 | const removals = this.watcher && this.watcher.aggregatedRemovals;
|
|---|
| 174 | const changes = this.watcher && this.watcher.aggregatedChanges;
|
|---|
| 175 | const fs = this.inputFileSystem;
|
|---|
| 176 | if (fs && fs.purge) {
|
|---|
| 177 | if (removals) {
|
|---|
| 178 | for (const item of removals) {
|
|---|
| 179 | fs.purge(item);
|
|---|
| 180 | }
|
|---|
| 181 | }
|
|---|
| 182 | if (changes) {
|
|---|
| 183 | for (const item of changes) {
|
|---|
| 184 | fs.purge(item);
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 | }
|
|---|
| 188 | const { fileTimeInfoEntries, contextTimeInfoEntries } = fetchTimeInfo();
|
|---|
| 189 | return {
|
|---|
| 190 | changes,
|
|---|
| 191 | removals,
|
|---|
| 192 | fileTimeInfoEntries,
|
|---|
| 193 | contextTimeInfoEntries
|
|---|
| 194 | };
|
|---|
| 195 | }
|
|---|
| 196 | };
|
|---|
| 197 | }
|
|---|
| 198 | }
|
|---|
| 199 |
|
|---|
| 200 | module.exports = NodeWatchFileSystem;
|
|---|