Skip to content

Commit

Permalink
Use constants instead of promises for payloads from Elm to webpack, s…
Browse files Browse the repository at this point in the history
…ince they're run sequentially so no need for promises.
  • Loading branch information
dillonkearns committed Jun 16, 2020
1 parent 4962bf6 commit 53d6c99
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 62 deletions.
99 changes: 48 additions & 51 deletions generator/src/add-files-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()


});
}
Expand Down
3 changes: 3 additions & 0 deletions generator/src/elm-pages.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ function run() {
staticRoutes,
markdownContent
).then((payload) => {
global.pagesWithRequests = payload;
global.filesToGenerate = payload.filesToGenerate;

if (cliOptions.watch) {
develop.start({
routes,
Expand Down
13 changes: 2 additions & 11 deletions generator/src/plugin-generate-elm-pages-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand Down

0 comments on commit 53d6c99

Please sign in to comment.