UNPKG

react-imported-component

Version:
33 lines (32 loc) 1.68 kB
import { commentsToConfiguration } from './utils'; const preservePrefetch = (_, __, options) => !!options.webpackPrefetch; const preservePreload = (_, __, options) => !!options.webpackPreload; const preserveChunkName = (_, __, options) => options.webpackChunkName || options.chunkName; const chunkComment = (chunk) => `webpackChunkName: "${chunk}"`; const preloadComment = () => `webpackPreload: true`; const prefetchComment = () => `webpackPrefetch: true`; const knownMagics = ['webpackChunkName', 'webpackPrefetch', 'webpackPreload']; const toComments = (conf) => Object.keys(conf) .filter((key) => !knownMagics.includes(key)) .reduce((acc, key) => { acc.concat(`${key}:${JSON.stringify(conf[key])}`); return acc; }, []); const nullish = (a, b) => { if (a === undefined) { return b; } return a; }; export const processComment = (configuration, comments, importName, fileName, options) => { const { shouldPrefetch = preservePrefetch, shouldPreload = preservePreload, chunkName = preserveChunkName, } = configuration; const importConfiguration = commentsToConfiguration(comments); const newChunkName = nullish(chunkName(importName, fileName, importConfiguration), preserveChunkName(importName, fileName, importConfiguration)); const { isBootstrapFile } = options; return [ ...toComments(importConfiguration), !isBootstrapFile && shouldPrefetch(importName, fileName, importConfiguration) ? prefetchComment() : '', !isBootstrapFile && shouldPreload(importName, fileName, importConfiguration) ? preloadComment() : '', newChunkName ? chunkComment(newChunkName) : '', ].filter((x) => !!x); };