Server render the main page

Also, add hashes to asset file names and use the "entry manifest" plugin
in dev to get auto-reloading.
This commit is contained in:
Michael Jackson
2019-01-12 19:27:28 -08:00
parent 45c48cba26
commit 09914c1db4
176 changed files with 2725 additions and 119822 deletions

View File

@ -5,100 +5,132 @@ const commonjs = require('rollup-plugin-commonjs');
const json = require('rollup-plugin-json');
const replace = require('rollup-plugin-replace');
const resolve = require('rollup-plugin-node-resolve');
const sizeSnapshot = require('rollup-plugin-size-snapshot').sizeSnapshot;
const url = require('rollup-plugin-url');
const entryManifest = require('./plugins/entryManifest');
const env = process.env.NODE_ENV || 'development';
const dev = env === 'development';
// Allow storing env vars in .env in dev.
if (dev) require('dotenv').config();
const secretKey = require('./secretKey');
const manifest = entryManifest();
const functionsIndex = {
external: id => true,
input: path.resolve(__dirname, 'modules/functions/index.js'),
output: {
file: 'functions/index.js',
format: 'cjs'
},
const client = {
input: ['modules/client/main.js', 'modules/client/autoIndex.js'],
output: [
{
// ESM version for modern browsers
format: 'esm',
dir: 'public/_client',
entryFileNames: '[name]-[hash].js',
chunkFileNames: '[name]-[hash].js'
},
{
// SystemJS version for older browsers
format: 'system',
dir: 'public/_client',
entryFileNames: '[name]-[hash].system.js',
chunkFileNames: '[name]-[hash].system.js'
}
],
plugins: [
babel(),
manifest.record({ publicPath: '/_client/' }),
babel({ exclude: /node_modules/ }),
json(),
resolve(),
commonjs({
namedExports: {
'node_modules/react/index.js': [
'createContext',
'createElement',
'forwardRef',
'Component',
'Fragment'
]
}
}),
replace({
'process.env.NODE_ENV': JSON.stringify(env)
}),
url({
limit: 5 * 1024,
publicPath: '/_client/'
})
]
};
const secretKey = require('./secretKey');
const fnsPkg = require('./functions/package.json');
const fnsDeps = (dev
? Object.keys(fnsPkg.dependencies).concat(Object.keys(fnsPkg.devDependencies))
? Object.keys(fnsPkg.dependencies).concat(
Object.keys(fnsPkg.devDependencies || {})
)
: Object.keys(fnsPkg.dependencies)
).concat('react-dom/server');
const functions = [
// 'serveAuth',
'serveAutoIndexPage',
'serveNpmPackageFile',
'servePublicKey',
'serveStats'
].map(functionName => {
return {
external: builtinModules.concat(fnsDeps),
input: path.resolve(__dirname, `modules/functions/${functionName}.js`),
output: {
file: `functions/${functionName}.js`,
format: 'cjs'
},
{
external: id => true,
input: path.resolve(__dirname, 'modules/functions/index.js'),
output: { file: 'functions/index.js', format: 'cjs' },
plugins: [
babel(),
json(),
replace({
'process.env.NODE_ENV': JSON.stringify(env),
'process.env.CLOUDFLARE_EMAIL': JSON.stringify(
process.env.CLOUDFLARE_EMAIL
),
'process.env.CLOUDFLARE_KEY': JSON.stringify(
process.env.CLOUDFLARE_KEY
),
'process.env.SECRET_KEY': JSON.stringify(secretKey)
'process.env.NODE_ENV': JSON.stringify(env)
})
]
};
});
}
].concat(
[
// 'serveAuth',
'serveAutoIndexPage',
'serveMainPage',
'serveNpmPackageFile',
'servePublicKey',
'serveStats'
].map(functionName => {
return {
external: builtinModules.concat(fnsDeps),
input: path.resolve(__dirname, `modules/functions/${functionName}.js`),
output: { file: `functions/${functionName}.js`, format: 'cjs' },
plugins: [
manifest.inject({ virtualId: 'entry-manifest' }),
babel({ exclude: /node_modules/ }),
json(),
resolve(),
commonjs({
// namedExports: {
// 'node_modules/react/index.js': [
// 'createContext',
// 'createElement',
// 'forwardRef',
// 'Component',
// 'Fragment'
// ]
// }
}),
url({
limit: 5 * 1024,
publicPath: '/_client/',
emitFiles: false
}),
replace({
'process.env.NODE_ENV': JSON.stringify(env),
'process.env.CLOUDFLARE_EMAIL': JSON.stringify(
process.env.CLOUDFLARE_EMAIL
),
'process.env.CLOUDFLARE_KEY': JSON.stringify(
process.env.CLOUDFLARE_KEY
),
'process.env.SECRET_KEY': JSON.stringify(secretKey)
})
]
};
})
);
const clients = ['main', 'autoIndex'].map(bundleName => {
return {
external: ['react', 'react-dom', 'react-router-dom', 'react-motion'],
input: path.resolve(__dirname, `modules/client/${bundleName}.js`),
output: {
file: `public/_assets/${bundleName}.js`,
format: 'iife',
globals: {
react: 'React',
'react-dom': 'ReactDOM',
'react-router-dom': 'ReactRouterDOM',
'react-motion': 'ReactMotion'
}
},
plugins: [
babel({ exclude: /node_modules/ }),
json(),
resolve(),
commonjs(),
replace({
'process.env.NODE_ENV': JSON.stringify(env)
}),
url({
limit: 5 * 1024,
publicPath: '/_assets/'
}),
sizeSnapshot()
]
};
});
module.exports = [functionsIndex].concat(functions, clients);
module.exports = [client].concat(functions);