gatsby-adapter-netlify
Version:
Gatsby adapter for Netlify
37 lines (36 loc) • 2.21 kB
JavaScript
;
exports.__esModule = true;
exports.handleAllowedRemoteUrlsNetlifyConfig = handleAllowedRemoteUrlsNetlifyConfig;
async function handleAllowedRemoteUrlsNetlifyConfig({
remoteFileAllowedUrls,
reporter
}) {
const {
resolveConfig
} = await import(`@netlify/config`);
const cfg = await resolveConfig();
if (cfg !== null && cfg !== void 0 && cfg.config) {
var _cfg$config$images$re, _cfg$config$images;
const allowedUrlsInNetlifyToml = (_cfg$config$images$re = (_cfg$config$images = cfg.config.images) === null || _cfg$config$images === void 0 ? void 0 : _cfg$config$images.remote_images) !== null && _cfg$config$images$re !== void 0 ? _cfg$config$images$re : [];
const allowedUrlsInNetlifyTomlRegexes = allowedUrlsInNetlifyToml.map(regexSource => new RegExp(regexSource));
const missingAllowedUrlsInNetlifyToml = [];
for (const remoteFileAllowedUrl of remoteFileAllowedUrls) {
// test if url pattern already passes one of the regexes in netlify.toml
const isAlreadyAllowed = allowedUrlsInNetlifyTomlRegexes.some(allowedRegex => allowedRegex.test(remoteFileAllowedUrl.urlPattern));
if (!isAlreadyAllowed) {
missingAllowedUrlsInNetlifyToml.push(remoteFileAllowedUrl.regexSource);
}
}
if (missingAllowedUrlsInNetlifyToml.length > 0) {
var _cfg$config$images2;
const entriesToAddToToml = `${missingAllowedUrlsInNetlifyToml.map(missingAllowedUrlInNetlifyToml => ` ${JSON.stringify(missingAllowedUrlInNetlifyToml)}`).join(`,\n`)},\n`;
if (typeof ((_cfg$config$images2 = cfg.config.images) === null || _cfg$config$images2 === void 0 ? void 0 : _cfg$config$images2.remote_images) === `undefined`) {
reporter.warn(`Missing allowed URLs in your Netlify configuration. Add following to your netlify.toml:\n\`\`\`toml\n[images]\nremote_images = [\n${entriesToAddToToml}]\n\`\`\``);
} else {
reporter.warn(`Missing allowed URLs in your Netlify configuration. Add following entries to your existing \`images.remote_images\` configuration in netlify.toml:\n\`\`\`toml\n${entriesToAddToToml}\`\`\``);
}
}
} else {
reporter.verbose(`[gatsby-adapter-netlify] no netlify.toml found`);
}
}