Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server-side render POC #172

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions config/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
var path = require('path');
var autoprefixer = require('autoprefixer');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently HTML must be generated on the server.

// TODO: hide this behind a flag and eliminate dead code on eject.
// This shouldn't be exposed to the user.
Expand All @@ -25,16 +24,14 @@ if (isInDebugMode) {
}
var srcPath = path.resolve(__dirname, relativePath, 'src');
var nodeModulesPath = path.join(__dirname, '..', 'node_modules');
var indexHtmlPath = path.resolve(__dirname, relativePath, 'index.html');
var faviconPath = path.resolve(__dirname, relativePath, 'favicon.ico');
var buildPath = path.join(__dirname, isInNodeModules ? '../../..' : '..', 'build');

module.exports = {
devtool: 'eval',
entry: [
require.resolve('webpack-dev-server/client') + '?http://localhost:3000',
require.resolve('webpack/hot/dev-server'),
path.join(srcPath, 'index')
path.join(srcPath, 'client/index')
],
output: {
// Next line is not used in dev but WebpackDevServer crashes without it:
Expand Down Expand Up @@ -92,11 +89,6 @@ module.exports = {
return [autoprefixer];
},
plugins: [
new HtmlWebpackPlugin({
inject: true,
template: indexHtmlPath,
favicon: faviconPath,
}),
new webpack.DefinePlugin({ 'process.env.NODE_ENV': '"development"' }),
// Note: only CSS is currently hot reloaded
new webpack.HotModuleReplacementPlugin()
Expand Down
33 changes: 10 additions & 23 deletions config/webpack.config.prod.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
var path = require('path');
var autoprefixer = require('autoprefixer');
var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var url = require('url');

Expand All @@ -24,9 +23,7 @@ if (process.argv[2] === '--debug-template') {
}
var srcPath = path.resolve(__dirname, relativePath, 'src');
var nodeModulesPath = path.join(__dirname, '..', 'node_modules');
var indexHtmlPath = path.resolve(__dirname, relativePath, 'index.html');
var faviconPath = path.resolve(__dirname, relativePath, 'favicon.ico');
var buildPath = path.join(__dirname, isInNodeModules ? '../../..' : '..', 'build');
var buildPath = path.join(__dirname, isInNodeModules ? '../../..' : '..', 'build/client');
var homepagePath = require(path.resolve(__dirname, relativePath, 'package.json')).homepage;
var publicPath = homepagePath ? url.parse(homepagePath).pathname : '/';
if (!publicPath.endsWith('/')) {
Expand All @@ -37,7 +34,7 @@ if (!publicPath.endsWith('/')) {
module.exports = {
bail: true,
devtool: 'source-map',
entry: path.join(srcPath, 'index'),
entry: path.join(srcPath, 'client/index'),
output: {
path: buildPath,
filename: '[name].[chunkhash].js',
Expand Down Expand Up @@ -98,23 +95,6 @@ module.exports = {
return [autoprefixer];
},
plugins: [
new HtmlWebpackPlugin({
inject: true,
template: indexHtmlPath,
favicon: faviconPath,
minify: {
removeComments: true,
collapseWhitespace: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeStyleLinkTypeAttributes: true,
keepClosingSlash: true,
minifyJS: true,
minifyCSS: true,
minifyURLs: true
}
}),
new webpack.DefinePlugin({ 'process.env.NODE_ENV': '"production"' }),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.optimize.DedupePlugin(),
Expand All @@ -131,6 +111,13 @@ module.exports = {
screw_ie8: true
}
}),
new ExtractTextPlugin('[name].[contenthash].css')
new ExtractTextPlugin('[name].[contenthash].css'),
function() {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the "manual" part of passing file names to static file which can then be loaded on the server.

this.plugin('done', function(stats) {
require('fs').writeFileSync(
path.join(buildPath, 'stats.json'),
JSON.stringify(stats.toJson().assetsByChunkName));
});
}
]
};
68 changes: 68 additions & 0 deletions config/webpack.config.server.dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
var path = require('path');
var fs = require('fs');
var webpack = require('webpack');

var isInNodeModules = 'node_modules' ===
path.basename(path.resolve(path.join(__dirname, '..', '..')));
var relativePath = isInNodeModules ? '../../..' : '..';
var isInDebugMode = process.argv.some(arg =>
arg.indexOf('--debug-template') > -1
);
if (isInDebugMode) {
relativePath = '../template';
}

var srcPath = path.resolve(__dirname, relativePath, 'src');
var nodeModulesPath = path.join(__dirname, '..', 'node_modules');
var buildPath = path.join(__dirname, isInNodeModules ? '../../..' : '..', 'build/server');

const nodeModules = fs.readdirSync(nodeModulesPath)
.filter(entry => ['.bin'].indexOf(entry) === -1)
.reduce((reduction, entry, foo) => {
const objectWithCommonJsModule = {};
objectWithCommonJsModule[entry] = `commonjs ${entry}`;
return Object.assign(reduction, objectWithCommonJsModule);
}, {});

module.exports = {
entry: path.join(srcPath, 'server/server'),
target: 'node',
debug: true,
watch: true,
inline: true,
devtool: 'eval',
output: {
path: buildPath,
filename: 'server-dev.js',
publicPath: '/'
},
externals: nodeModules,
module: {
loaders: [
{
test: /\.js$/,
include: srcPath,
loader: 'babel',
query: require('./babel.dev')
},
{
test: /\.json$/,
loader: 'json'
},
{
test: /\.(jpg|png|gif|eot|svg|ttf|woff|woff2)$/,
loader: 'file?emitFile=false',
},
{
test: /\.(mp4|webm)$/,
loader: 'url?limit=10000'
}
]
},
resolve: {
extensions: ['', '.js']
},
plugins: [
new webpack.DefinePlugin({ 'process.env.NODE_ENV': '"development"' }),
]
};
80 changes: 80 additions & 0 deletions config/webpack.config.server.prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
var path = require('path');
var fs = require('fs');
var webpack = require('webpack');

var isInNodeModules = 'node_modules' ===
path.basename(path.resolve(path.join(__dirname, '..', '..')));
var relativePath = isInNodeModules ? '../../..' : '..';
var isInDebugMode = process.argv.some(arg =>
arg.indexOf('--debug-template') > -1
);
if (isInDebugMode) {
relativePath = '../template';
}

var srcPath = path.resolve(__dirname, relativePath, 'src');
var nodeModulesPath = path.join(__dirname, '..', 'node_modules');
var buildPath = path.join(__dirname, isInNodeModules ? '../../..' : '..', 'build/server');

const nodeModules = fs.readdirSync(nodeModulesPath)
.filter(entry => ['.bin'].indexOf(entry) === -1)
.reduce((reduction, entry, foo) => {
const objectWithCommonJsModule = {};
objectWithCommonJsModule[entry] = `commonjs ${entry}`;
return Object.assign(reduction, objectWithCommonJsModule);
}, {});

module.exports = {
entry: path.join(srcPath, 'server/server'),
target: 'node',
devtool: 'source-map',
output: {
path: buildPath,
filename: 'server.js',
publicPath: '/'
},
externals: nodeModules,
module: {
loaders: [
{
test: /\.js$/,
include: srcPath,
loader: 'babel',
query: require('./babel.prod')
},
{
test: /\.json$/,
loader: 'json'
},
{
test: /\.(jpg|png|gif|eot|svg|ttf|woff|woff2)$/,
loader: 'file?emitFile=false',
},
{
test: /\.(mp4|webm)$/,
loader: 'url?limit=10000'
}
]
},
resolve: {
extensions: ['', '.js']
},
plugins: [
new webpack.DefinePlugin({ 'process.env.NODE_ENV': '"production"' }),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
compressor: {
screw_ie8: true,
warnings: false
},
mangle: {
screw_ie8: true
},
output: {
comments: false,
screw_ie8: true
}
})
]
};
1 change: 1 addition & 0 deletions foobar/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"name":"foobar","version":"0.0.1","private":true}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@
"extract-text-webpack-plugin": "1.0.1",
"file-loader": "0.9.0",
"fs-extra": "^0.30.0",
"html-webpack-plugin": "2.22.0",
"json-loader": "0.5.4",
"opn": "4.0.2",
"postcss-loader": "0.9.1",
"request": "^2.74.0",
"rimraf": "2.5.3",
"single-child": "^0.3.4",
"style-loader": "0.13.1",
"url-loader": "0.5.7",
"webpack": "1.13.1",
Expand Down
7 changes: 5 additions & 2 deletions scripts/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ process.env.NODE_ENV = 'production';
var path = require('path');
var rimrafSync = require('rimraf').sync;
var webpack = require('webpack');
var config = require('../config/webpack.config.prod');
var configClient = require('../config/webpack.config.prod');
var configServer = require('../config/webpack.config.server.prod');

var isInNodeModules = 'node_modules' ===
path.basename(path.resolve(path.join(__dirname, '..', '..')));
Expand All @@ -24,7 +25,9 @@ var packageJsonPath = path.join(__dirname, relative, 'package.json');
var buildPath = path.join(__dirname, relative, 'build');
rimrafSync(buildPath);

webpack(config).run(function(err, stats) {
webpack(configServer).run(() => {});
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obviously this file needs a lot of love, but the whole idea is that we need to build Client and Server independently.


webpack(configClient).run(function(err, stats) {
if (err) {
console.error('Failed to create a production build. Reason:');
console.error(err.message || err);
Expand Down
Loading