Skip to content

Instantly share code, notes, and snippets.

@crebuh
Created September 30, 2016 10:47
Show Gist options
  • Save crebuh/25878e08e712a56fdc1a701130103be1 to your computer and use it in GitHub Desktop.
Save crebuh/25878e08e712a56fdc1a701130103be1 to your computer and use it in GitHub Desktop.
var webpack = require('webpack');
var path = require('path');
var resolveNgRoute = require('@angularclass/resolve-angular-routes');
var LiveReloadPlugin = require('webpack-livereload-plugin');
var CopyWebpackPlugin = require('copy-webpack-plugin');
var commonConfig = {
resolve: {
extensions: ['', '.ts', '.js', '.json']
},
module: {
preLoaders: [
],
loaders: [
// TypeScript
{ test: /\.ts$/, loaders: ['ts-loader', 'angular2-template-loader'] },
{ test: /\.html$/, loader: 'raw-loader' },
{ test: /\.css$/, loader: 'style-loader!css-loader!resolve-url' },
{ test: /\.scss$/, exclude: /node_modules/, loaders: ['raw-loader', 'sass-loader'] },
{ test: /\.json$/, loader: 'raw-loader' },
{ test: /\.(png|woff|woff2|eot|ttf|svg)(\?v=\d+\.\d+\.\d+)?$/, loader: 'url-loader?limit=100000' }
],
},
plugins: [
new webpack.ContextReplacementPlugin(
// The (\\|\/) piece accounts for path separators in *nix and Windows
/angular(\\|\/)core(\\|\/)src(\\|\/)linker/,
root('./src'),
resolveNgRoute(root('./src'))
),
new LiveReloadPlugin({
appendScriptTag: true
}),
new CopyWebpackPlugin([
{
from: root('src/assets'), // replace with your folder
to: root('dist/client')
}, {
from: root('src/assets'), // replace with your folder
to: root('dist/server')
}
])
]
};
var clientConfig = {
target: 'web',
entry: ['bootstrap-loader', 'font-awesome-sass-loader!./font-awesome-sass.config.js', './src/client'],
// entry: ['./src/client'],
output: {
path: root('dist/client')
},
node: {
global: true,
__dirname: true,
__filename: true,
process: true,
Buffer: false
}
};
var serverConfig = {
target: 'node',
entry: ['bootstrap-loader', './src/server'], // use the entry file of the node server if everything is ts rather than es5
output: {
path: root('dist/server'),
libraryTarget: 'commonjs2'
},
externals: checkNodeImport,
node: {
global: true,
__dirname: true,
__filename: true,
process: true,
Buffer: true
}
};
var testConfig = {
target: 'web',
entry: ['./src/test'],
devtool: 'inline-source-map',
resolve: {
extensions: ['', '.ts', '.js']
},
module: {
loaders: [
{
test: /\.ts$/,
loaders: ['ts-loader', 'angular2-template-loader']
},
{
test: /\.html$/,
loader: 'html'
},
{
test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
loader: 'null'
},
{
test: /\.css$/,
exclude: root('./src'),
loader: 'null'
},
{
test: /\.css$/,
include: root('./src'),
loader: 'raw'
}
]
}
};
// Default config
var defaultConfig = {
context: __dirname,
resolve: {
root: root('/src')
},
output: {
publicPath: path.resolve(__dirname),
filename: 'index.js'
}
}
var webpackMerge = require('webpack-merge');
module.exports = [
// Client
webpackMerge({}, defaultConfig, commonConfig, clientConfig),
// Server
webpackMerge({}, defaultConfig, commonConfig, serverConfig)
]
// Helpers
function includeClientPackages(packages) {
return function (context, request, cb) {
if (packages && packages.indexOf(request) !== -1) {
return cb();
}
return checkNodeImport(context, request, cb);
};
}
function checkNodeImport(context, request, cb) {
if (!path.isAbsolute(request) && request.charAt(0) !== '.') {
cb(null, 'commonjs ' + request); return;
}
cb();
}
function root(args) {
args = Array.prototype.slice.call(arguments, 0);
return path.join.apply(path, [__dirname].concat(args));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment