/
githubmirror
/
webpack
Обзор
Документация
Войти
/
githubmirror
/
webpack
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
test/WatchDetection.test.js
200 строк
4 KB
Alexander Akait
Reduce allocations and redundant work across compiler hot paths (#21516)
27 июл 2026, 00:23
Не верифицирован
27 июл 2026, 00:23
fbcbe3e
Код
Авторство
О чём код?
"use strict"; const path = require("path"); const fs = require("graceful-fs"); const { Volume, createFsFromVolume } = require("memfs"); const webpack = require(".."); const expectNoDeprecations = require("./helpers/expectNoDeprecations"); expectNoDeprecations(); describe("WatchDetection", () => { if (process.env.NO_WATCH_TESTS) { // eslint-disable-next-line jest/no-disabled-tests it.skip("long running tests excluded", () => {}); return; } createTestCase(100, true); createTestCase(10, true); createTestCase(600, true); for (let changeTimeout = 10; changeTimeout < 100; changeTimeout += 10) { createTestCase(changeTimeout); } for (let changeTimeout = 200; changeTimeout <= 2000; changeTimeout += 200) { createTestCase(changeTimeout); } /** * @param {number} changeTimeout change timeout * @param {boolean=} invalidate need invalidate? */ function createTestCase(changeTimeout, invalidate) { describe(`time between changes ${changeTimeout}ms${ invalidate ? " with invalidate call" : "" }`, () => { const fixturePath = path.join( __dirname, "fixtures", `temp-${changeTimeout}` ); const filePath = path.join(fixturePath, "file.js"); const file2Path = path.join(fixturePath, "file2.js"); const loaderPath = path.join(__dirname, "fixtures", "delay-loader.js"); beforeAll(() => { try { fs.mkdirSync(fixturePath); } catch (_err) { // empty } fs.writeFileSync(filePath, "require('./file2')", "utf8"); fs.writeFileSync(file2Path, "original", "utf8"); }); afterAll((done) => { setTimeout(() => { try { fs.unlinkSync(filePath); } catch (_err) { // empty } try { fs.unlinkSync(file2Path); } catch (_err) { // empty } try { fs.rmdirSync(fixturePath); } catch (_err) { // empty } done(); }, 100); // cool down a bit }); it("should build the bundle correctly", (done) => { const compiler = webpack({ mode: "development", entry: `${loaderPath}!${filePath}`, output: { path: "/directory", filename: "bundle.js" } }); const memfs = (compiler.outputFileSystem = /** @type {import("../").OutputFileSystem & import("memfs").IFs} */ ( /** @type {unknown} */ (createFsFromVolume(new Volume())) )); /** @type {(() => void) | null | undefined} */ let onChange; compiler.hooks.done.tap("WatchDetectionTest", () => { if (onChange) onChange(); }); /** @type {import("../").Watching} */ let watcher; step1(); /** * @returns {void} */ function step1() { onChange = () => { if ( memfs.readFileSync("/directory/bundle.js") && memfs .readFileSync("/directory/bundle.js") .toString() .includes("original") ) { step2(); } }; watcher = /** @type {import("../").Watching} */ ( compiler.watch( { aggregateTimeout: 50, // Deno's node:fs.watch compat drops/delays change events, so // native detection is flaky here; poll for deterministic pickup. ...(process.versions.deno ? { poll: 100 } : {}) }, () => {} ) ); } /** * @returns {void} */ function step2() { onChange = () => { expect(compiler.modifiedFiles).toBeDefined(); expect(compiler.removedFiles).toBeDefined(); }; fs.writeFile( filePath, "require('./file2'); again", "utf8", handleError ); setTimeout(step3, changeTimeout); } /** * @returns {void} */ function step3() { if (invalidate) watcher.invalidate(); fs.writeFile(file2Path, "wrong", "utf8", handleError); setTimeout(step4, changeTimeout); } /** * @returns {void} */ function step4() { onChange = () => { expect(compiler.modifiedFiles).toBeDefined(); expect(compiler.removedFiles).toBeDefined(); if ( memfs .readFileSync("/directory/bundle.js") .toString() .includes("correct") ) { step5(); } }; fs.writeFile(file2Path, "correct", "utf8", handleError); } /** * @returns {void} */ function step5() { onChange = null; watcher.close(() => { setTimeout(done, 500); }); } /** * @param {unknown} err err */ function handleError(err) { if (err) done(err); } }); }); } });