-
-
Notifications
You must be signed in to change notification settings - Fork 18
complete "yarn dev" to watch every last thing, including transitive less and pug imports #7556
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hatton
wants to merge
2
commits into
master
Choose a base branch
from
bl-15641_more-vite-dev
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,611
−131
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import * as fs from "node:fs"; | ||
| import * as os from "node:os"; | ||
| import * as path from "node:path"; | ||
| import { describe, it, expect, beforeEach, afterEach } from "vitest"; | ||
| import { compilePugFiles } from "../compilePug.mjs"; | ||
|
|
||
| let tempDir: string; | ||
| let browserUIRoot: string; | ||
| let contentRoot: string; | ||
| let outputBase: string; | ||
|
|
||
| function makeDir(dirPath: string) { | ||
| fs.mkdirSync(dirPath, { recursive: true }); | ||
| } | ||
|
|
||
| function writeFile(filePath: string, contents: string) { | ||
| makeDir(path.dirname(filePath)); | ||
| fs.writeFileSync(filePath, contents); | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "compile-pug-")); | ||
| browserUIRoot = path.join(tempDir, "browserUI"); | ||
| contentRoot = path.join(tempDir, "content"); | ||
| outputBase = path.join(tempDir, "out"); | ||
| makeDir(browserUIRoot); | ||
| makeDir(contentRoot); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| fs.rmSync(tempDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| describe("compilePugFiles", () => { | ||
| it("recompiles dependents when an included pug file changes", async () => { | ||
| const partialPath = path.join(browserUIRoot, "partials", "partial.pug"); | ||
| const mainPath = path.join(browserUIRoot, "pages", "main.pug"); | ||
|
|
||
| writeFile(partialPath, "p Partial A\n"); | ||
|
|
||
| writeFile( | ||
| mainPath, | ||
| [ | ||
| "doctype html", | ||
| "html", | ||
| " body", | ||
| " include ../partials/partial.pug", | ||
| "", | ||
| ].join("\n"), | ||
| ); | ||
|
|
||
| await compilePugFiles({ browserUIRoot, contentRoot, outputBase }); | ||
|
|
||
| const outPath = path.join(outputBase, "pages", "main.html"); | ||
| expect(fs.existsSync(outPath)).toBe(true); | ||
| const firstHtml = fs.readFileSync(outPath, "utf8"); | ||
| expect(firstHtml).toContain("Partial A"); | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, 30)); | ||
| writeFile(partialPath, "p Partial B\n"); | ||
|
|
||
| await compilePugFiles({ browserUIRoot, contentRoot, outputBase }); | ||
| const secondHtml = fs.readFileSync(outPath, "utf8"); | ||
| expect(secondHtml).toContain("Partial B"); | ||
| expect(secondHtml).not.toContain("Partial A"); | ||
| }); | ||
| }); |
188 changes: 188 additions & 0 deletions
188
src/BloomBrowserUI/scripts/__tests__/watchLess.test.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| import fs from "fs"; | ||
| import os from "os"; | ||
| import path from "path"; | ||
| import { describe, it, expect, beforeEach, afterEach } from "vitest"; | ||
| import { LessWatchManager } from "../watchLess.mjs"; | ||
|
|
||
| const silentLogger = { | ||
| log: () => {}, | ||
| warn: () => {}, | ||
| error: () => {}, | ||
| }; | ||
|
|
||
| let tempDir; | ||
| let sourceRoot; | ||
| let outputRoot; | ||
| let metadataPath; | ||
|
|
||
| function makeDir(dirPath) { | ||
| fs.mkdirSync(dirPath, { recursive: true }); | ||
| } | ||
|
|
||
| function writeFile(filePath, contents) { | ||
| makeDir(path.dirname(filePath)); | ||
| fs.writeFileSync(filePath, contents); | ||
| } | ||
|
|
||
| function makeManager(overrides = {}) { | ||
| return new LessWatchManager({ | ||
| repoRoot: tempDir, | ||
| metadataPath, | ||
| logger: silentLogger, | ||
| targets: [ | ||
| { | ||
| name: "test", | ||
| root: sourceRoot, | ||
| outputBase: outputRoot, | ||
| }, | ||
| ], | ||
| ...overrides, | ||
| }); | ||
| } | ||
|
|
||
| function getEntryId(manager, filePath) { | ||
| const key = path | ||
| .relative(manager.repoRoot, path.resolve(filePath)) | ||
| .replace(/\\/g, "/"); | ||
| return key; | ||
| } | ||
|
|
||
| beforeEach(() => { | ||
| tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "watch-less-")); | ||
| sourceRoot = path.join(tempDir, "src"); | ||
| outputRoot = path.join(tempDir, "out"); | ||
| metadataPath = path.join(outputRoot, ".state.json"); | ||
| makeDir(sourceRoot); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| fs.rmSync(tempDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| describe("LessWatchManager", () => { | ||
| it("compiles missing outputs and records metadata", async () => { | ||
| const entryPath = path.join(sourceRoot, "pages", "main.less"); | ||
| const partialPath = path.join(sourceRoot, "partials", "colors.less"); | ||
| writeFile(partialPath, "@primary: #ff0000;\n"); | ||
| writeFile( | ||
| entryPath, | ||
| '@import "../partials/colors.less";\nbody { color: @primary; }\n', | ||
| ); | ||
|
|
||
| const manager = makeManager(); | ||
| await manager.initialize(); | ||
|
|
||
| const cssPath = path.join(outputRoot, "pages", "main.css"); | ||
| expect(fs.existsSync(cssPath)).toBe(true); | ||
| const css = fs.readFileSync(cssPath, "utf8"); | ||
| expect(css).toContain("body"); | ||
| expect(fs.existsSync(`${cssPath}.map`)).toBe(true); | ||
|
|
||
| const state = JSON.parse(fs.readFileSync(metadataPath, "utf8")); | ||
| const entryId = getEntryId(manager, entryPath); | ||
| expect(state.entries[entryId]).toContain( | ||
| path.relative(tempDir, partialPath).replace(/\\/g, "/"), | ||
| ); | ||
| }); | ||
|
|
||
| it("rebuilds when dependency is newer on startup", async () => { | ||
| const entryPath = path.join(sourceRoot, "main.less"); | ||
| const partialPath = path.join(sourceRoot, "dep.less"); | ||
| writeFile(partialPath, "@val: blue;\n"); | ||
| writeFile(entryPath, '@import "dep.less";\nbody { color: @val; }\n'); | ||
|
|
||
| const firstManager = makeManager(); | ||
| await firstManager.initialize(); | ||
| const cssPath = path.join(outputRoot, "main.css"); | ||
| const initialMTime = fs.statSync(cssPath).mtimeMs; | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, 30)); | ||
| writeFile(partialPath, "@val: green;\n"); | ||
|
|
||
| const secondManager = makeManager(); | ||
| await secondManager.initialize(); | ||
| const rebuiltMTime = fs.statSync(cssPath).mtimeMs; | ||
| expect(rebuiltMTime).toBeGreaterThan(initialMTime); | ||
| }); | ||
|
|
||
| it("updates dependency graph when imports change", async () => { | ||
| const entryPath = path.join(sourceRoot, "main.less"); | ||
| const depPath = path.join(sourceRoot, "dep.less"); | ||
| writeFile(depPath, "@val: blue;\n"); | ||
| writeFile(entryPath, '@import "dep.less";\nbody { color: @val; }\n'); | ||
|
|
||
| const manager = makeManager(); | ||
| await manager.initialize(); | ||
| const entryId = getEntryId(manager, entryPath); | ||
| const cssPath = path.join(outputRoot, "main.css"); | ||
| const baselineMTime = fs.statSync(cssPath).mtimeMs; | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, 30)); | ||
| writeFile(entryPath, "body { color: black; }\n"); | ||
| await manager.handleFileChanged(entryPath, "entry updated"); | ||
| const deps = manager.entryDependencies.get(entryId) ?? []; | ||
| expect(deps.length).toBe(1); | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, 30)); | ||
| writeFile(depPath, "@val: red;\n"); | ||
| await manager.handleFileChanged(depPath, "dep changed"); | ||
| const afterMTime = fs.statSync(cssPath).mtimeMs; | ||
| expect(afterMTime).toBe(baselineMTime); | ||
| }); | ||
|
|
||
| it("adds new dependencies and rebuilds when partial changes", async () => { | ||
| const entryPath = path.join(sourceRoot, "main.less"); | ||
| const depA = path.join(sourceRoot, "depA.less"); | ||
| const depB = path.join(sourceRoot, "depB.less"); | ||
| writeFile(depA, "@val: blue;\n"); | ||
| writeFile(depB, "@alt: red;\n"); | ||
| writeFile(entryPath, '@import "depA.less";\nbody { color: @val; }\n'); | ||
|
|
||
| const manager = makeManager(); | ||
| await manager.initialize(); | ||
| const cssPath = path.join(outputRoot, "main.css"); | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, 30)); | ||
| writeFile( | ||
| entryPath, | ||
| '@import "depA.less";\n@import "depB.less";\nbody { color: @alt; }\n', | ||
| ); | ||
| await manager.handleFileChanged(entryPath, "entry changed"); | ||
| const entryId = getEntryId(manager, entryPath); | ||
| const deps = manager.entryDependencies.get(entryId) ?? []; | ||
| expect(deps.some((dep) => dep.endsWith("depB.less"))).toBe(true); | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, 30)); | ||
| writeFile(depB, "@alt: purple;\n"); | ||
| const before = fs.statSync(cssPath).mtimeMs; | ||
| await manager.handleFileChanged(depB, "depB updated"); | ||
| const after = fs.statSync(cssPath).mtimeMs; | ||
| expect(after).toBeGreaterThan(before); | ||
| }); | ||
|
|
||
| it("removes outputs when an entry is deleted", async () => { | ||
| const entryPath = path.join(sourceRoot, "main.less"); | ||
| writeFile(entryPath, "body { color: blue; }\n"); | ||
| const manager = makeManager(); | ||
| await manager.initialize(); | ||
| const cssPath = path.join(outputRoot, "main.css"); | ||
| expect(fs.existsSync(cssPath)).toBe(true); | ||
|
|
||
| fs.unlinkSync(entryPath); | ||
| await manager.handleFileRemoved(entryPath); | ||
| expect(fs.existsSync(cssPath)).toBe(false); | ||
| expect(fs.existsSync(`${cssPath}.map`)).toBe(false); | ||
| }); | ||
|
|
||
| it("builds new entries on the fly", async () => { | ||
| const manager = makeManager(); | ||
| await manager.initialize(); | ||
|
|
||
| const entryPath = path.join(sourceRoot, "new.less"); | ||
| writeFile(entryPath, "body { color: orange; }\n"); | ||
| await manager.handleFileAdded(manager.targets[0], entryPath); | ||
|
|
||
| const cssPath = path.join(outputRoot, "new.css"); | ||
| expect(fs.existsSync(cssPath)).toBe(true); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The variable
baselineMTimeis declared withconstbut reassigned on line 124. This should be declared withletinstead ofconstto allow reassignment.