-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix unchanged revision hashes when watching
- Loading branch information
Showing
5 changed files
with
268 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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
11 changes: 11 additions & 0 deletions
11
test/integration/fixtures/eleventy-project-default/config-for-watcher-with-rev.js
This file contains 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,11 @@ | ||
const sass = require("../../../.."); | ||
const pluginRev = require("eleventy-plugin-rev"); | ||
|
||
console.log(`Eleventy PID: ${ process.pid }`); | ||
|
||
module.exports = function(eleventyConfig) { | ||
eleventyConfig.addPlugin(pluginRev); | ||
eleventyConfig.addPlugin(sass, { | ||
rev: true | ||
}); | ||
}; |
114 changes: 114 additions & 0 deletions
114
test/integration/watcher-with-rev-dependency-file-update.js
This file contains 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,114 @@ | ||
if (parseInt(process.version.match(/^v(\d+)/)[1]) < 16) { | ||
const test = require("ava"); | ||
test("tests don't support node version < 16", async t => { | ||
t.pass(); | ||
}); | ||
return; | ||
} | ||
|
||
const { spawn } = require("child_process"); | ||
const path = require("path"); | ||
const { promises: fs } = require("fs"); | ||
const { setTimeout } = require("timers/promises"); | ||
const { createHash } = require("crypto"); | ||
|
||
const test = require("ava"); | ||
const Semaphore = require("@debonet/es6semaphore"); | ||
|
||
const createProject = require("./_create-project-default"); | ||
let dir; | ||
let proc; | ||
let pid; | ||
|
||
const headerCssContent = "header{background-color:pink}"; | ||
const headerRevHash = createHash("sha256").update(headerCssContent).digest("hex").slice(0, 8); | ||
|
||
const updatedHeaderCssContent = "header{background-color:red}"; | ||
const updatedHeaderRevHash = createHash("sha256").update(updatedHeaderCssContent).digest("hex").slice(0, 8); | ||
|
||
const styleCssContent = "header{background-color:pink}body{background-color:red}"; | ||
const styleRevHash = createHash("sha256").update(styleCssContent).digest("hex").slice(0, 8); | ||
|
||
const updatedStyleCssContent = "header{background-color:red}body{background-color:red}"; | ||
const updatedStyleRevHash = createHash("sha256").update(updatedStyleCssContent).digest("hex").slice(0, 8); | ||
|
||
test.after.always("cleanup child process", t => { | ||
if (proc && proc.exitCode === null) { | ||
pid ? process.kill(pid, "SIGINT") : proc.kill(); | ||
} | ||
}); | ||
|
||
test.before(async t => { | ||
let sem = new Semaphore(1); | ||
await sem.wait(); | ||
dir = createProject("watcher-with-rev-dependency-file-update"); | ||
proc = spawn("npx", ["@11ty/eleventy", "--config=config-for-watcher-with-rev.js", "--watch"], { cwd: dir, shell: true, timeout: 20000 }); | ||
proc.on("exit", (code, signal) => { | ||
if (process.platform === "darwin") | ||
pid = undefined; | ||
sem.signal(); | ||
}); | ||
proc.stdout.on("data", function(data) { | ||
let str = data.toString(); | ||
|
||
let match = str.match(/^Eleventy PID: (\d+)/); | ||
if (match) { | ||
pid = parseInt(match[1]); | ||
} | ||
|
||
if (str.trim() === "[11ty] Watching…") { | ||
sem.signal(); | ||
} | ||
}); | ||
await sem.wait(); | ||
await setTimeout(300); | ||
|
||
|
||
let stylesheetsDir = path.join(dir, "_site", "stylesheets"); | ||
let csses = await fs.readdir(stylesheetsDir); | ||
t.deepEqual(csses, [`header-${ headerRevHash }.css`, `style-${ styleRevHash }.css`]); | ||
|
||
|
||
let headerSCSS = path.join(dir, "stylesheets", "header.scss"); | ||
fs.writeFile(headerSCSS, `header { | ||
background-color: red; | ||
}`); | ||
|
||
await sem.wait(); | ||
await setTimeout(300); | ||
|
||
if (pid) { | ||
process.kill(pid, "SIGINT"); | ||
pid = undefined; | ||
await sem.wait(); | ||
} | ||
}); | ||
|
||
test("write CSS files with correct revision hashes", async t => { | ||
let stylesheetsDir = path.join(dir, "_site", "stylesheets"); | ||
let csses = await fs.readdir(stylesheetsDir); | ||
t.is(csses.length, 4); | ||
t.true(csses.includes(`header-${ headerRevHash }.css`)); | ||
t.true(csses.includes(`header-${ updatedHeaderRevHash }.css`)); | ||
t.true(csses.includes(`style-${ styleRevHash }.css`)); | ||
t.true(csses.includes(`style-${ updatedStyleRevHash }.css`)); | ||
}); | ||
|
||
|
||
test("watcher works", async t => { | ||
let headerPath = path.join(dir, "_site", "stylesheets", `header-${ headerRevHash }.css`); | ||
let headerCss = await fs.readFile(headerPath, { encoding: "utf8" }); | ||
t.is(headerCss, headerCssContent); | ||
|
||
let updatedHeaderPath = path.join(dir, "_site", "stylesheets", `header-${ updatedHeaderRevHash }.css`); | ||
let updatedHeaderCss = await fs.readFile(updatedHeaderPath, { encoding: "utf8" }); | ||
t.is(updatedHeaderCss, updatedHeaderCssContent); | ||
|
||
let stylePath = path.join(dir, "_site", "stylesheets", `style-${ styleRevHash }.css`); | ||
let styleCss = await fs.readFile(stylePath, { encoding: "utf8" }); | ||
t.is(styleCss, styleCssContent); | ||
|
||
let updatedStylePath = path.join(dir, "_site", "stylesheets", `style-${ updatedStyleRevHash }.css`); | ||
let updatedStyleCss = await fs.readFile(updatedStylePath, { encoding: "utf8" }); | ||
t.is(updatedStyleCss, updatedStyleCssContent); | ||
}); |
This file contains 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,105 @@ | ||
if (parseInt(process.version.match(/^v(\d+)/)[1]) < 16) { | ||
const test = require("ava"); | ||
test("tests don't support node version < 16", async t => { | ||
t.pass(); | ||
}); | ||
return; | ||
} | ||
|
||
const { spawn } = require("child_process"); | ||
const path = require("path"); | ||
const { promises: fs } = require("fs"); | ||
const { setTimeout } = require("timers/promises"); | ||
const { createHash } = require("crypto"); | ||
|
||
const test = require("ava"); | ||
const Semaphore = require("@debonet/es6semaphore"); | ||
|
||
const createProject = require("./_create-project-default"); | ||
let dir; | ||
let proc; | ||
let pid; | ||
|
||
const headerCssContent = "header{background-color:pink}"; | ||
const headerRevHash = createHash("sha256").update(headerCssContent).digest("hex").slice(0, 8); | ||
|
||
const styleCssContent = "header{background-color:pink}body{background-color:red}"; | ||
const styleRevHash = createHash("sha256").update(styleCssContent).digest("hex").slice(0, 8); | ||
|
||
const updatedStyleCssContent = "header{background-color:pink}body{background-color:red;color:blue}"; | ||
const updatedStyleRevHash = createHash("sha256").update(updatedStyleCssContent).digest("hex").slice(0, 8); | ||
|
||
test.after.always("cleanup child process", t => { | ||
if (proc && proc.exitCode === null) { | ||
pid ? process.kill(pid, "SIGINT") : proc.kill(); | ||
} | ||
}); | ||
|
||
test.before(async t => { | ||
let sem = new Semaphore(1); | ||
await sem.wait(); | ||
dir = createProject("watcher-with-rev"); | ||
proc = spawn("npx", ["@11ty/eleventy", "--config=config-for-watcher-with-rev.js", "--watch"], { cwd: dir, shell: true, timeout: 20000 }); | ||
proc.on("exit", (code, signal) => { | ||
console.debug("exit"); | ||
if (process.platform === "darwin") | ||
pid = undefined; | ||
sem.signal(); | ||
}); | ||
proc.stdout.on("data", function(data) { | ||
let str = data.toString(); | ||
|
||
let match = str.match(/^Eleventy PID: (\d+)/); | ||
if (match) { | ||
pid = parseInt(match[1]); | ||
} | ||
|
||
if (str.trim() === "[11ty] Watching…") | ||
sem.signal(); | ||
}); | ||
await sem.wait(); | ||
await setTimeout(300); | ||
|
||
|
||
let stylesheetsDir = path.join(dir, "_site", "stylesheets"); | ||
let csses = await fs.readdir(stylesheetsDir); | ||
t.deepEqual(csses, [`header-${ headerRevHash }.css`, `style-${ styleRevHash }.css`]); | ||
|
||
|
||
let styleSCSS = path.join(dir, "stylesheets", "style.scss"); | ||
fs.writeFile(styleSCSS, `@use "colors"; | ||
@use "header"; | ||
body { | ||
background-color: colors.$background; | ||
color: blue; | ||
}`); | ||
|
||
await sem.wait(); | ||
await setTimeout(300); | ||
|
||
if (pid) { | ||
process.kill(pid, "SIGINT"); | ||
pid = undefined; | ||
await sem.wait(); | ||
} | ||
}); | ||
|
||
test("write CSS files with correct revision hashes", async t => { | ||
let stylesheetsDir = path.join(dir, "_site", "stylesheets"); | ||
let csses = await fs.readdir(stylesheetsDir); | ||
t.is(csses.length, 3); | ||
t.true(csses.includes(`header-${ headerRevHash }.css`)); | ||
t.true(csses.includes(`style-${ styleRevHash }.css`)); | ||
t.true(csses.includes(`style-${ updatedStyleRevHash }.css`)); | ||
}); | ||
|
||
test("watcher works", async t => { | ||
let stylePath = path.join(dir, "_site", "stylesheets", `style-${ styleRevHash }.css`); | ||
let styleCss = await fs.readFile(stylePath, { encoding: "utf8" }); | ||
t.is(styleCss, styleCssContent); | ||
|
||
let updatedStylePath = path.join(dir, "_site", "stylesheets", `style-${ updatedStyleRevHash }.css`); | ||
let updatedStyleCss = await fs.readFile(updatedStylePath, { encoding: "utf8" }); | ||
t.is(updatedStyleCss, updatedStyleCssContent); | ||
}); |