vite-plugin-react18-pages
Version:
<p> <a href="https://www.npmjs.com/package/vite-plugin-react-pages" target="_blank" rel="noopener"><img src="https://img.shields.io/npm/v/vite-plugin-react-pages.svg" alt="npm package" /></a> </p>
151 lines • 5.92 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.VirtualModulesManager = void 0;
const path = __importStar(require("path"));
const chokidar_1 = __importDefault(require("chokidar"));
const slash_1 = __importDefault(require("slash"));
const VirtualModules_1 = require("./VirtualModules");
const utils_1 = require("./utils");
const utils_2 = require("./utils");
let nextWatcherId = 0;
/**
* watch fs and update corresponding virtule module when a file changed
*/
class VirtualModulesManager {
constructor() {
this.watchers = new Set();
this.virtuleModules = new VirtualModules_1.VirtualModuleGraph();
this.fileCache = {};
/**
* don't return half-finished data when there are pending tasks
*/
this.pendingTaskCounter = new utils_1.PendingTaskCounter();
this.pendingTaskCounter.countPendingState(this.virtuleModules.updateExecutingState);
this.pendingTaskCounter.countPendingState(this.virtuleModules.updateQueueEmptyState);
}
addFSWatcher(baseDir, globs, fileHandler) {
const watcherId = String(nextWatcherId++);
// should wait for a complete fs scan
// before returning the page data
const fsScanFinish = this.pendingTaskCounter.countTask();
this.watchers.add(chokidar_1.default
.watch(globs, {
cwd: baseDir,
ignored: ['**/node_modules/**/*', '**/.git/**'],
})
.on('add', this.handleFileChange(baseDir, fileHandler, watcherId))
.on('change', this.handleFileChange(baseDir, fileHandler, watcherId))
.on('unlink', this.handleFileUnLink(baseDir, watcherId))
.on('ready', () => {
setTimeout(() => {
// ready event may be fired too early,
// before initial scan callbacks are called
// https://github.com/paulmillr/chokidar/issues/1011
fsScanFinish();
}, 10);
}));
}
getModules(cb, filter) {
this.callOnceWhenIdle(() => {
cb(this.virtuleModules.getModules(filter));
});
}
getModule(moduleId, cb) {
this.callOnceWhenIdle(() => {
cb(this.virtuleModules.getModuleData(moduleId));
});
}
/**
* Idle means:
* fs watcher is ready
* no update is executing
* update queue is empty
*/
callOnceWhenIdle(cb) {
this.pendingTaskCounter.callOnceWhenIdle(cb);
}
/**
* return the current state of modules.
* it doesn't wait for update task to finish
* so it may see intermediate state.
* use it carefully.
*/
_getModulesNow(filter) {
return this.virtuleModules.getModules(filter);
}
/**
* return the current state of module.
* it doesn't wait for update task to finish
* so it may see intermediate state.
* use it carefully.
*/
_getModuleDataNow(moduleId) {
return this.virtuleModules.getModuleData(moduleId);
}
addModuleListener(handler, filter) {
return this.virtuleModules.addModuleListener(handler, filter);
}
close() {
this.watchers.forEach((w) => w.close());
}
scheduleUpdate(updaterId, updater) {
return this.virtuleModules.scheduleUpdate(updaterId, updater);
}
handleFileChange(baseDir, fileHandler, watcherId) {
return (filePath) => {
filePath = (0, slash_1.default)(path.join(baseDir, filePath));
const file = this.fileCache[filePath] ||
(this.fileCache[filePath] = new utils_2.File(filePath, baseDir));
// update content cache
file.content = null;
file.read();
this.virtuleModules.scheduleUpdate(`${watcherId}-${filePath}`, async (apis) => {
const handlerAPI = {
addModuleData(moduleId, data) {
apis.addModuleData(moduleId, data, filePath);
},
getModuleData: apis.getModuleData,
};
await fileHandler(file, handlerAPI);
});
};
}
handleFileUnLink(baseDir, watcherId) {
return (filePath) => {
filePath = (0, slash_1.default)(path.join(baseDir, filePath));
this.virtuleModules.scheduleUpdate(`${watcherId}-${filePath}-unlink`, async (apis) => {
// delete the node that represent this fs file in the virtule modules graph
// also delete all outcome edges
apis.deleteModule(filePath);
});
};
}
}
exports.VirtualModulesManager = VirtualModulesManager;
//# sourceMappingURL=VirtualModulesManager.js.map