0.0.3: Fixed a bug that showed up in watch mode
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2023-05-20 21:05:37 +02:00
parent b18ac5c361
commit 52c104f781
10 changed files with 183 additions and 5 deletions

View File

@@ -0,0 +1,7 @@
<html>
<head>
</head>
<body>
<script src="./watched-file.js" type="module"></script>
</body>
</html>

View File

@@ -0,0 +1,3 @@
export const a = 1; // DO NOT CHANGE ME HERE, but in ../test.js

View File

@@ -0,0 +1,53 @@
# Snapshot report for `test/watch/test.js`
The actual snapshot is saved in `test.js.snap`.
Generated by [AVA](https://avajs.dev).
## watch
> Snapshot 1
[
{
code: `const a = 2; // If i show up as a changed file, then the watch test has gone wrong!␊
export { a };␊
//# sourceMappingURL=watched-file-8c4729c5.js.map␊
`,
fileName: 'watched-file-8c4729c5.js',
map: SourceMap {
file: 'watched-file-8c4729c5.js',
mappings: 'AACgB,MAAC,CAAC,GAAG,EAAE;;;;',
names: [],
sources: [
'../watched-file.js',
],
sourcesContent: [
`␊
export const a = 2; // If i show up as a changed file, then the watch test has gone wrong!␊
`,
],
version: 3,
},
source: undefined,
},
{
code: undefined,
fileName: 'watched-file-8c4729c5.js.map',
map: undefined,
source: '{"version":3,"file":"watched-file-8c4729c5.js","sources":["../watched-file.js"],"sourcesContent":["\\n export const a = 2; // If i show up as a changed file, then the watch test has gone wrong!\\n "],"names":[],"mappings":"AACgB,MAAC,CAAC,GAAG,EAAE;;;;"}',
},
{
code: undefined,
fileName: 'index.html',
map: undefined,
source: `<html><head>␊
</head>␊
<body>␊
<script src="watched-file-8c4729c5.js" type="module"></script>␊
</body></html>`,
},
]

Binary file not shown.

97
test/watch/test.js Normal file
View File

@@ -0,0 +1,97 @@
import {join, dirname} from "node:path";
import test from "ava";
import * as rollup from "rollup";
import {debugPrintOutput, getCode} from "../util/test.js";
import {resolve} from "node:path";
import {writeFile} from "node:fs/promises";
import html from "../../src/index.ts";
const output = {
dir: 'output', // Output all files
format: 'es', // iifi and cjs should be added to tests
sourcemap: true,// Test if #sourcemapUrl is not accidentally included in the html-output
};
import {fileURLToPath} from "node:url";
import {pathToFileURL} from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
process.chdir(join(__dirname, 'fixtures'));
test.serial('watch', async (t) => {
const origContent = `
export const a = 1; // DO NOT CHANGE ME HERE, but in ../test.js
`;
const changeContent = `
export const a = 2; // If i show up as a changed file, then the watch test has gone wrong!
`
const path = resolve(__dirname, 'fixtures/watched-file.js');
await writeFile(path, origContent, {encoding: 'utf-8'});
const watcher = rollup.watch({
input: 'index.html',
output,
plugins: [
html({
}),
],
watch: {
skipWrite: true,
}
});
const steps = [
async (bundle)=>{
await writeFile(path, changeContent, {encoding: 'utf-8'});
// Just wait on the watch mode to pick up on the changes
},
async (bundle)=>{
const code = await getCode(bundle, output, true);
debugPrintOutput('watch',code);
// Reset the source file
await writeFile(path, origContent, {encoding: 'utf-8'});
// Assert the output is what we exapect;
t.snapshot(code);
watcher
},
];
await new Promise((resolve, reject)=>{
watcher.on('event', async (event) => {
const {result} = event;
switch (event.code) {
case "START":
t.log(`WATCH STARTED`);
break;
case "BUNDLE_START":
t.log(`REBUILDING...`);
break;
case "BUNDLE_END":
t.log(`Rebuilt...`);
const cb = steps.shift();
const generated = await result.generate(output);
const cbResult = await cb(result);
if(steps.length===0){
watcher.close();
resolve();
}
break;
case "ERROR":
reject(event.error);
break;
}
if (result) {
result.close();
}
});
});
});