UNPKG

3.44 kBJavaScriptView Raw
1'use strict';
2
3var _ = require('lodash');
4var path = require('path');
5var webpack = require('webpack');
6var ExtractTextPlugin = require('extract-text-webpack-plugin');
7
8module.exports = function (options) {
9 options = _.defaults({}, options, {
10 __dirname: __dirname,
11 entry: ['./client/index.es6'],
12 externalStylesheet: true,
13 hotloadPort: 8888
14 });
15
16 options.output = _.defaults({}, options.output, {
17 publicPath: '/static/',
18 filename: 'client.js'
19 });
20
21 options.output.path =
22 path.join(options.__dirname, options.output.publicPath + '/build/')
23
24 var HOTLOAD = process.env.NODE_ENV === 'development';
25
26 var loaders = {
27 es6: {
28 test: /\.(es6|jsx)$/,
29 loaders: ['babel?stage=0', 'eslint-loader'] },
30
31 json: {
32 test: /\.json$/, loaders: ['json']},
33
34 less: {
35 test: /\.less$/,
36 loader:
37 options.externalStylesheet ?
38 ExtractTextPlugin.extract(
39 'css!autoprefixer!less', { publicPath: './static/build/' })
40 : 'style!css!autoprefixer!less'
41 },
42
43 markdown: {
44 test: /\.md$/,
45 loaders: ['html', 'remarkable']},
46
47 svg: {
48 test: /\.svg$/, loader: 'html'}
49 }
50
51 var webpackConfig = {
52 cache: true,
53 devtool: 'source-map',
54 entry: options.entry,
55 noParse: [/moment.js/],
56 module: {
57 loaders: [
58 loaders.es6,
59 loaders.json,
60 loaders.less,
61 loaders.markdown,
62 loaders.svg ] },
63 output: {
64 path: path.join(options.__dirname, options.output.publicPath + '/build/'),
65 publicPath: options.output.publicPath,
66 filename: options.output.filename },
67 plugins: [],
68 resolve: {
69 extensions: ['', '.js', '.jsx', '.es', '.es6'],
70 alias: {app: path.join(options.__dirname, 'client')}
71 },
72 resolveLoader: {
73 root: path.join(__dirname, 'node_modules')},
74 target: 'web',
75
76 // Hotload specific customization
77 displayName: options.displayName,
78 hotloadPort: options.hotloadPort,
79
80 // Loader specific customization
81 eslint: {
82 configFile: path.join(__dirname, './.eslintrc')
83 },
84 remarkable: {
85 preset: 'full',
86 html: true
87 }
88 };
89
90 if (HOTLOAD) {
91 webpackConfig.devtool = 'eval-source-map'; // This is not as dirty as it looks. It just generates source maps without being crazy slow.
92 webpackConfig.entry = [
93 'webpack-dev-server/client?http://localhost:' + webpackConfig.hotloadPort,
94 'webpack/hot/dev-server',
95 ].concat(webpackConfig.entry);
96
97 webpackConfig.output.publicPath = 'http://localhost:' +
98 webpackConfig.hotloadPort +
99 webpackConfig.output.publicPath;
100 loaders.es6.loaders = ['react-hot', 'babel?stage=0&optional=runtime'];
101 loaders.less.loader = 'style!css!autoprefixer!less';
102
103 webpackConfig.plugins = [
104 new webpack.HotModuleReplacementPlugin(),
105 new webpack.NoErrorsPlugin()
106 ];
107 }
108 else {
109 if (options.externalStylesheet) {
110 webpackConfig.plugins.push(
111 // Adds support for 'require(*.less)' from '.jsx' files
112 new ExtractTextPlugin(
113 'style', 'main.css', {disable: false, allChunks: true}));
114 }
115
116 webpackConfig.plugins.push(
117 new webpack.optimize.OccurenceOrderPlugin());
118
119 webpackConfig.plugins.push(
120 new webpack.optimize.UglifyJsPlugin({warnings: false, comments: false}));
121 }
122
123 return webpackConfig;
124};