-
Notifications
You must be signed in to change notification settings - Fork 1
/
webpack.config.babel.js
140 lines (129 loc) · 4.4 KB
/
webpack.config.babel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//Copyright (c) 2016 TimTheSinner All Rights Reserved.
'use strict';
/**
* Copyright (c) 2016 TimTheSinner All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*
* @author TimTheSinner
*/
import PostBuildPlugin from './plugins/post-build-plugin';
var webpack = require('webpack');
var devserver = require('webpack-dev-server');
var dashboard = require('webpack-dashboard');
var dashboardPlugin = require('webpack-dashboard/plugin');
var bundleTracker = require('webpack-bundle-tracker');
var htmlWebpackPlugin = require('html-webpack-plugin');
var StaticSiteGeneratorPlugin = require('static-site-generator-webpack-plugin');
var path = require('path');
var fs = require('fs');
var handlebars = require('handlebars');
handlebars.registerHelper('custom_title', function(title) {
return title || 'React VirtualDOM';
});
handlebars.registerHelper('custom_path', function(path) {
return path || '';
});
const template = handlebars.compile(fs.readFileSync('example/index.handlebars', 'utf-8'));
var config = {
context: __dirname,
entry: {
main: [ './example/index.jsx' ],
vendor: [
'react',
'react-dom',
'react-router',
'react-syntax-highlighter',
'd3',
],
},
output: {},
module: {
loaders: [
{
test: /\.js|\.jsx|\.es6$/,
loaders: [ 'babel' ],
exclue: /(node_modules|bower_components)/,
},
],
},
resolveLoader: { alias: { 'source-loader': path.join(__dirname, 'plugins', 'source-loader') } },
resolve: { root: [ path.resolve('./src') ], extensions: [ '', '.js', '.jsx' ] },
};
if (process.argv.indexOf('-prod') !== -1) {
console.log('Building static pages for production');
const production = {
entry: './example/index.jsx',
output: { path: path.resolve('./__build__/'), filename: 'bundle.js', libraryTarget: 'umd' },
module: { loaders: [ { test: /\.jsx?$/, exclude: /node_modules/, loader: 'babel' } ] },
resolve: { extensions: [ '', '.js', '.jsx' ] },
};
config = { ...config, ...production };
config.plugins = [
new webpack.DefinePlugin({ 'process.env': { NODE_ENV: JSON.stringify('production') } }),
new webpack.optimize.UglifyJsPlugin({
beautify: true,
comments: false,
compress: { dead_code: true },
}),
new StaticSiteGeneratorPlugin(
config.output.filename,
[ '/', '/static-bar-chart.html', '/dynamic-bar-chart.html', '/epicyclic-gearing.html', '404.html' ],
{
title: 'React + D3 with VirtualDOM',
production: true,
root_path: 'react-vdom',
template: template,
},
),
new PostBuildPlugin(),
];
} else {
var port = 1337;
console.log('Starting HotLoad Dev-Server on ' + port);
var dash = new dashboard();
config.devtool = 'source-map';
config.entry.main.unshift('webpack/hot/only-dev-server');
config.entry.main.unshift('webpack-dev-server/client?http://127.0.0.1:' + port);
config.output.path = path.resolve('./dist/');
config.output.publicPath = 'http://127.0.0.1:' + port + '/';
config.output.filename = '[name].[hash].js';
config.output.chunkFilename = '[name].[hash].js';
if (!fs.existsSync(config.output.path)) {
fs.mkdirSync(config.output.path);
}
var index = path.join(config.output.path, 'index.html');
fs.writeFileSync(index, template());
config.plugins = [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoErrorsPlugin(),
new dashboardPlugin(dash.setData),
new bundleTracker({ filename: '../dist/webpack-stats.json' }),
new htmlWebpackPlugin({ template: index }),
];
new devserver(webpack(config), {
publicPath: config.output.publicPath,
hot: true,
inline: true,
historyApiFallback: true,
contentBase: './',
quiet: true,
}).listen(port, '127.0.0.1', function(err, result) {
if (err) {
console.log(err);
} else {
console.log('Listening for clients at 127.0.0.1:' + port, result);
}
});
}
export default config;