Skip to content

Instantly share code, notes, and snippets.

@s-panferov
Last active August 29, 2015 14:24
Show Gist options
  • Save s-panferov/8a7e26350ddb31254269 to your computer and use it in GitHub Desktop.
Save s-panferov/8a7e26350ddb31254269 to your computer and use it in GitHub Desktop.
var webpack = require('webpack');
var path = require("path");
var fs = require("fs");
var nodeModules = {};
fs.readdirSync('node_modules')
.filter(function (x) {
return ['.bin'].indexOf(x) === -1;
})
.forEach(function (mod) {
nodeModules[mod] = 'commonjs ' + mod;
});
var typeLib = path.join(__dirname, 'src', 'defines.d.ts');
module.exports = {
entry: {
server: './src/server/index.node.js'
},
target: "node",
externals: nodeModules,
output: {
path: path.join(__dirname, 'dist'),
publicPath: '/assets/',
filename: '[name].js',
libraryTarget: "commonjs2"
},
resolve: {
extensions: ['', '.ts', '.js'],
alias: {
'alfa-react-ui': path.join(__dirname, 'bower_components', 'alfa-react-ui')
}
},
// Source maps support (or 'inline-source-map' also works)
devtool: 'inline-source-map',
module: {
loaders: [
{
test: /\.ts$/,
loader: "awesome-typescript?rewriteImports=alfa-react-ui&externals=" + typeLib
},
{
test: /\.js$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader'
},
{
test: /\.json$/,
loader: "json-loader"
},
{
test: /\.handlebars$/,
loader: "handlebars-loader"
}
]
},
plugins: [
new webpack.NormalModuleReplacementPlugin(/\.styl$/, 'node-noop'),
new webpack.IgnorePlugin(/\.(styl)$/),
new webpack.DefinePlugin({
ENV: {
debug: true
}
}),
new webpack.BannerPlugin('require("source-map-support").install();',
{ raw: true, entryOnly: false })
]
};
var webpack = require('webpack');
var nodemon = require('nodemon');
var path = require('path');
var _ = require('lodash');
var WebpackDevServer = require('webpack-dev-server');
var webpackBackConfig = require('./webpack.backend.config.js');
var nm;
var optimist = require('optimist');
require('webpack/bin/config-optimist')(optimist);
var argv = optimist.argv;
var cliOptions = require('webpack/bin/convert-argv')(optimist, argv);
var devServerConfing = {
publicPath: '/assets',
contentBase: './dist',
historyApiFallback: true,
inline: true,
hot: true,
stats: { colors: true },
proxy: {
'*': 'http://localhost:3000'
}
};
new WebpackDevServer(webpack(cliOptions), devServerConfing).listen(8080, 'localhost', function (err, result) {
if (err) {
console.log(err);
}
else {
console.log('Webpack dev server listening at localhost:8080');
}
});
var backendCompiler = webpack(webpackBackConfig);
backendCompiler.plugin('compile', function() {
console.log('Building server...');
});
Error.stackTraceLimit = 30;
var lastHash = null;
function compilerCallback(err, stats) {
if(err) {
lastHash = null;
console.error(err.stack || err);
if(err.details) console.error(err.details);
return;
}
if(stats.hash !== lastHash) {
lastHash = stats.hash;
process.stdout.write(stats.toString({
colors: true
}) + "\n");
}
}
backendCompiler.plugin('done', function(res) {
console.log('Restarting server...');
if (!nm) {
nm = nodemon({
execMap: {
js: 'node'
},
script: path.join(__dirname, 'dist/server.js'),
ignore: ['*'],
ext: 'noop'
})
} else {
nm.restart()
}
});
backendCompiler.watch(100, compilerCallback);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment