gatsby-plugin-gatsby-cloud
Version:
A Gatsby plugin which optimizes working with Gatsby Cloud
51 lines (45 loc) • 1.55 kB
JavaScript
;
exports.__esModule = true;
exports.default = writeRedirectsFile;
var _fsExtra = require("fs-extra");
var _constants = require("./constants");
var _ipc = require("./ipc");
async function writeRedirectsFile(pluginData, redirects, rewrites) {
const {
publicFolder
} = pluginData;
// gatsby adds path-prefix to redirects so we need to remove them again
if (redirects && pluginData.pathPrefix) {
redirects = redirects.map(redirect => {
if (redirect.fromPath.startsWith(pluginData.pathPrefix)) {
redirect.fromPath = redirect.fromPath.slice(pluginData.pathPrefix.length);
}
if (redirect.toPath.startsWith(pluginData.pathPrefix)) {
redirect.toPath = redirect.toPath.slice(pluginData.pathPrefix.length);
}
return redirect;
});
}
/**
* IPC Emit for redirects
*/
let lastMessageSent;
redirects.forEach(redirect => {
lastMessageSent = (0, _ipc.emitRedirects)(redirect);
});
/**
* IPC Emit for rewrites
*/
rewrites.forEach(rewrite => {
lastMessageSent = (0, _ipc.emitRewrites)(rewrite);
});
// This prevents process from exiting before handling the last IPC message
await lastMessageSent;
// Is it ok to pass through the data or should we format it so that we don't have dependencies
// between the redirects and rewrites formats? What are the chances those will change?
const FILE_PATH = publicFolder(_constants.REDIRECTS_FILENAME);
return (0, _fsExtra.writeFile)(FILE_PATH, JSON.stringify({
redirects,
rewrites
}));
}