Use a promise to subscribe to static http data as it is updated.

This commit is contained in:
Dillon Kearns 2020-04-18 10:30:09 -07:00
parent e395638565
commit 33272b6c54
2 changed files with 15 additions and 8 deletions

View File

@ -32,18 +32,18 @@ module.exports = class AddFilesPlugin {
.sync(["content/**/*.*", "!content/**/*.emu"], {})
.map(unpackFile);
compilation.contextDependencies.add(path.resolve(process.cwd(), './content'));
global.pagesWithRequests.then(pageWithRequests => {
files.forEach(file => {
// Couldn't find this documented in the webpack docs,
// but I found the example code for it here:
// https://github.com/jantimon/html-webpack-plugin/blob/35a154186501fba3ecddb819b6f632556d37a58f/index.js#L470-L478
let route = file.baseRoute.replace(/\/$/, '');
const staticRequests = this.pagesWithRequests[route];
const staticRequests = pageWithRequests[route];
const filename = path.join(file.baseRoute, "content.json");
// compilation.fileDependencies.add(filename);
compilation.fileDependencies.add(path.resolve(process.cwd(), file.filePath));
compilation.fileDependencies.add(path.resolve(file.filePath));
const rawContents = JSON.stringify({
body: file.content,
staticData: staticRequests || {}
@ -55,7 +55,7 @@ module.exports = class AddFilesPlugin {
};
});
(this.filesToGenerate || []).forEach(file => {
(global.filesToGenerate || []).forEach(file => {
// Couldn't find this documented in the webpack docs,
// but I found the example code for it here:
// https://github.com/jantimon/html-webpack-plugin/blob/35a154186501fba3ecddb819b6f632556d37a58f/index.js#L470-L478

View File

@ -81,15 +81,22 @@ function run() {
app.ports.writeFile.subscribe(contents => {
const routes = toRoutes(markdownContent.concat(content));
let resolvePageRequests;
global.pagesWithRequests = new Promise(function (resolve, reject) {
resolvePageRequests = resolve;
});
doCliStuff(
contents.watch ? "dev" : "prod",
staticRoutes,
markdownContent,
content,
function(payload) {
function (payload) {
if (contents.watch) {
startWatchIfNeeded();
resolvePageRequests(payload.pages);
global.filesToGenerate = payload.filesToGenerate;
if (!devServerRunning) {
devServerRunning = true;
develop.start({
@ -113,7 +120,7 @@ function run() {
routesWithRequests: payload.pages,
filesToGenerate: payload.filesToGenerate
},
() => {}
() => { }
);
}
@ -148,13 +155,13 @@ function startWatchIfNeeded() {
if (!watcher) {
console.log("Watching...");
watcher = chokidar
.watch(["content/**/*.*"], {
.watch(["content/**/*.*", "src/**/*.elm"], {
awaitWriteFinish: {
stabilityThreshold: 500
},
ignoreInitial: true
})
.on("all", function(event, filePath) {
.on("all", function (event, filePath) {
console.log(`Rerunning for ${filePath}...`);
run();
console.log("Done!");