From 53d6c9999ad3f345813e8beaa88ac800ef18cb86 Mon Sep 17 00:00:00 2001 From: Dillon Kearns Date: Mon, 15 Jun 2020 19:45:30 -0700 Subject: [PATCH] Use constants instead of promises for payloads from Elm to webpack, since they're run sequentially so no need for promises. --- generator/src/add-files-plugin.js | 99 +++++++++---------- generator/src/elm-pages.js | 3 + .../src/plugin-generate-elm-pages-build.js | 13 +-- 3 files changed, 53 insertions(+), 62 deletions(-) diff --git a/generator/src/add-files-plugin.js b/generator/src/add-files-plugin.js index 9f57664c5..5dfb1c9b8 100644 --- a/generator/src/add-files-plugin.js +++ b/generator/src/add-files-plugin.js @@ -31,59 +31,56 @@ module.exports = class AddFilesPlugin { let staticRequestData = {} - global.pagesWithRequests.then(payload => { - - if (payload.type === 'error') { - compilation.errors.push(new Error(payload.message)) - } else if (payload.errors && payload.errors.length > 0) { - compilation.errors.push(new Error(payload.errors[0])) + const payload = global.pagesWithRequests; + + if (payload.type === 'error') { + compilation.errors.push(new Error(payload.message)) + } else if (payload.errors && payload.errors.length > 0) { + compilation.errors.push(new Error(payload.errors[0])) + } + else { + staticRequestData = payload.pages + } + + 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 = staticRequestData[route]; + + const filename = path.join(file.baseRoute, "content.json"); + if (compilation.contextDependencies) { + compilation.contextDependencies.add('content') } - else { - staticRequestData = payload.pages + // compilation.fileDependencies.add(filename); + if (compilation.fileDependencies) { + compilation.fileDependencies.add(path.resolve(file.filePath)); } - }) - .finally(() => { - - 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 = staticRequestData[route]; - - const filename = path.join(file.baseRoute, "content.json"); - if (compilation.contextDependencies) { - compilation.contextDependencies.add('content') - } - // compilation.fileDependencies.add(filename); - if (compilation.fileDependencies) { - compilation.fileDependencies.add(path.resolve(file.filePath)); - } - const rawContents = JSON.stringify({ - body: file.content, - staticData: staticRequests || {} - }); - - compilation.assets[filename] = { - source: () => rawContents, - size: () => rawContents.length - }; - }); - - (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 - compilation.assets[file.path] = { - source: () => file.content, - size: () => file.content.length - }; - }); - - callback() - - }) + const rawContents = JSON.stringify({ + body: file.content, + staticData: staticRequests || {} + }); + + compilation.assets[filename] = { + source: () => rawContents, + size: () => rawContents.length + }; + }); + + (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 + compilation.assets[file.path] = { + source: () => file.content, + size: () => file.content.length + }; + }); + + callback() + }); } diff --git a/generator/src/elm-pages.js b/generator/src/elm-pages.js index c09d457ed..78777e83b 100755 --- a/generator/src/elm-pages.js +++ b/generator/src/elm-pages.js @@ -89,6 +89,9 @@ function run() { staticRoutes, markdownContent ).then((payload) => { + global.pagesWithRequests = payload; + global.filesToGenerate = payload.filesToGenerate; + if (cliOptions.watch) { develop.start({ routes, diff --git a/generator/src/plugin-generate-elm-pages-build.js b/generator/src/plugin-generate-elm-pages-build.js index 748604c19..bd20b29f9 100644 --- a/generator/src/plugin-generate-elm-pages-build.js +++ b/generator/src/plugin-generate-elm-pages-build.js @@ -21,26 +21,17 @@ module.exports = class PluginGenerateElmPagesBuild { return parseMarkdown(path, contents); }); - let resolvePageRequests; - let rejectPageRequests; - global.pagesWithRequests = new Promise(function (resolve, reject) { - resolvePageRequests = resolve; - rejectPageRequests = reject; - }); doCliStuff( global.mode, staticRoutes, markdownContent ).then((payload) => { - // console.log('PROMISE RESOLVED doCliStuff'); - - resolvePageRequests(payload); + global.pagesWithRequests = payload; global.filesToGenerate = payload.filesToGenerate; done() - }).catch(function (errorPayload) { - resolvePageRequests({ type: 'error', message: errorPayload }); + global.pagesWithRequests = { type: 'error', message: errorPayload }; done() })