|
| 1 | +import React from 'react'; |
| 2 | +import PropTypes from 'prop-types'; |
| 3 | + |
| 4 | +import createHTML from './utils/createHTML'; |
| 5 | +import x from './utils/execScript'; |
| 6 | + |
| 7 | +const promiseShim = |
| 8 | + 'window.Promise || document.write(\'\\x3Cscript src="/[email protected]/dist/es6-promise.min.js">\\x3C/script>\\x3Cscript>ES6Promise.polyfill()\\x3C/script>\')'; |
| 9 | +const fetchShim = |
| 10 | + 'window.fetch || document.write(\'\\x3Cscript src="/[email protected]/dist/fetch.umd.js">\\x3C/script>\')'; |
| 11 | + |
| 12 | +export default function MainTemplate({ |
| 13 | + title, |
| 14 | + description, |
| 15 | + favicon, |
| 16 | + data, |
| 17 | + content, |
| 18 | + globalScripts, |
| 19 | + entryPoints |
| 20 | +}) { |
| 21 | + return ( |
| 22 | + <html lang="en"> |
| 23 | + <head> |
| 24 | + <meta charSet="utf-8" /> |
| 25 | + <meta httpEquiv="X-UA-Compatible" content="IE=edge,chrome=1" /> |
| 26 | + {description && <meta name="description" content={description} />} |
| 27 | + <meta |
| 28 | + name="viewport" |
| 29 | + content="width=device-width,initial-scale=1,maximum-scale=1" |
| 30 | + /> |
| 31 | + <meta name="timestamp" content={new Date().toISOString()} /> |
| 32 | + {favicon && <link rel="shortcut icon" href={favicon} />} |
| 33 | + <title>{title}</title> |
| 34 | + {x(promiseShim)} |
| 35 | + {x(fetchShim)} |
| 36 | + {data && x(`window.__DATA__ = ${JSON.stringify(data)}`)} |
| 37 | + </head> |
| 38 | + <body> |
| 39 | + <div id="root" dangerouslySetInnerHTML={content} /> |
| 40 | + |
| 41 | + {globalScripts.map(src => ( |
| 42 | + <script key={src} src={src} /> |
| 43 | + ))} |
| 44 | + |
| 45 | + {entryPoints.module && |
| 46 | + x(` |
| 47 | + import('${entryPoints.module}'); |
| 48 | + window.supportsDynamicImport = true; |
| 49 | + `)} |
| 50 | + {entryPoints.nomodule && |
| 51 | + x(` |
| 52 | + if (!window.supportsDynamicImport) { |
| 53 | + var s = document.createElement('script'); |
| 54 | + s.src = '/[email protected]/dist/s.min.js'; |
| 55 | + s.addEventListener('load', function() { |
| 56 | + System.import('${entryPoints.nomodule}'); |
| 57 | + }); |
| 58 | + document.head.appendChild(s); |
| 59 | + } |
| 60 | + `)} |
| 61 | + </body> |
| 62 | + </html> |
| 63 | + ); |
| 64 | +} |
| 65 | + |
| 66 | +MainTemplate.defaultProps = { |
| 67 | + title: 'UNPKG', |
| 68 | + description: 'The CDN for everything on npm', |
| 69 | + favicon: '/favicon.ico', |
| 70 | + content: createHTML(''), |
| 71 | + globalScripts: [] |
| 72 | +}; |
| 73 | + |
| 74 | +const htmlType = PropTypes.shape({ |
| 75 | + __html: PropTypes.string |
| 76 | +}); |
| 77 | + |
| 78 | +MainTemplate.propTypes = { |
| 79 | + title: PropTypes.string, |
| 80 | + description: PropTypes.string, |
| 81 | + favicon: PropTypes.string, |
| 82 | + data: PropTypes.any, |
| 83 | + content: htmlType, |
| 84 | + globalScripts: PropTypes.arrayOf(PropTypes.string), |
| 85 | + entryPoints: PropTypes.shape({ |
| 86 | + module: PropTypes.string, |
| 87 | + nomodule: PropTypes.string |
| 88 | + }).isRequired |
| 89 | +}; |
0 commit comments