Created
April 3, 2020 02:14
-
-
Save developit/a7382e4a6367817b61ab8be1b33c873c to your computer and use it in GitHub Desktop.
mock version of prerender.js support for preact-cli
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { h } = require('preact'); | |
const app = require('preact-cli-entrypoint-prerendering'); | |
const prerender = app && app.default || app; | |
if (typeof prerender !== 'function') { | |
throw Error('prerender module must export a function, but exported '+(typeof prerender)); | |
} | |
module.exports = function(props) { | |
const result = prerender(props); | |
// there's no way to get around this wrapping div, and that probably breaks hydration. | |
return h('div', { | |
id: 'preact_root', | |
dangerouslySetInnerHTML: { __html: result.html } | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path'); | |
const fs = require('fs'); | |
const entries = ['prerender.ts', 'prerender.js'] | |
entries = entries.concat(entries.map(f => f + 'x')); | |
entries = entries.concat(entries.map(f => 'src/' + f); | |
module.exports = function(config, env) { | |
const cwd = env ? env.cwd : process.cwd(); | |
const isSsr = !env || env.ssr === true; | |
const source = f => existsSync(f = path.resolve(cwd, f)) && f; | |
const entry = entries.map(source).find(Boolean); | |
if (env) { | |
const definePlugin = config.plugins.filter(p => /^DefinePlugin$/.test(p && p.constructor && p.constructor.name))[0]; | |
if (definePlugin) { | |
Object.assign(definePlugin.plugin.definitions, { | |
'process.env.SSR': String(env.ssr), | |
'process.env.PRERENDER': String(env.ssr), | |
PRERENDER: String(env.ssr) | |
}); | |
} | |
} | |
if (entry) { | |
console.log('Using custom prerender entry: ' + path.relative(cwd, entry)); | |
config.resolve.alias['preact-cli-entrypoint-prerendering'] = entry; | |
config.resolve.alias['preact-cli-entrypoint'] = require.resolve('./_internal_entry.js'); | |
} | |
else { | |
console.log('Prerendering using default settings.'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const prerender = require('preact-cli-plugin-prerender'); | |
module.exports = function(config, env, helpers) { | |
prerender(config, env); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment