forked from busyorg/busy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmakeConfig.js
222 lines (207 loc) · 5.95 KB
/
makeConfig.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* eslint-disable global-require */
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const LodashModuleReplacementPlugin = require('lodash-webpack-plugin');
const Visualizer = require('webpack-visualizer-plugin');
const _ = require('lodash');
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const DEFAULTS = {
isDevelopment: process.env.NODE_ENV !== 'production',
baseDir: path.resolve(__dirname, '..'),
};
const POSTCSS_LOADER = {
loader: 'postcss-loader',
options: {
ident: 'postcss', // https://webpack.js.org/guides/migrating/#complex-options
plugins: () => [
require('autoprefixer')({
browsers: [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 9', // React doesn't support IE8 anyway
],
}),
],
},
};
function isVendor({ resource }) {
return resource &&
resource.indexOf('node_modules') >= 0 &&
resource.match(/\.jsx?$/);
}
function makePlugins(options) {
const isDevelopment = options.isDevelopment;
let plugins = [
new webpack.DefinePlugin({
'process.env': {
// This has effect on the react lib size
NODE_ENV: isDevelopment ? JSON.stringify('development') : JSON.stringify('production'),
ENABLE_LOGGER: JSON.stringify(process.env.ENABLE_LOGGER),
STEEMCONNECT_IMG_HOST: JSON.stringify(process.env.STEEMCONNECT_IMG_HOST || 'https://img.busy.org'),
SENTRY_PUBLIC_DSN: isDevelopment ? null : JSON.stringify(process.env.SENTRY_PUBLIC_DSN),
STEEMCONNECT_HOST: JSON.stringify(process.env.STEEMCONNECT_HOST || 'https://v2.steemconnect.com'),
STEEMCONNECT_REDIRECT_URL: JSON.stringify(process.env.STEEMCONNECT_REDIRECT_URL || 'https://busy.org/callback'),
STEEMJS_URL: JSON.stringify(process.env.STEEMJS_URL),
IS_BROWSER: JSON.stringify(true),
PUSHPAD_PROJECT_ID: process.env.PUSHPAD_PROJECT_ID,
BUSYPUSH_ENDPOINT: process.env.BUSYPUSH_ENDPOINT,
},
}),
new LodashModuleReplacementPlugin({
collections: true,
paths: true,
shorthands: true,
flattening: true,
}),
new Visualizer({
filename: './statistics.html',
}),
];
if (isDevelopment) {
plugins = plugins.concat([
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
]);
} else {
plugins = plugins.concat([
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks(module) {
// this assumes your vendor imports exist in the node_modules directory
return isVendor(module);
},
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
}),
new webpack.optimize.AggressiveMergingPlugin(),
new ExtractTextPlugin({
allChunks: true,
filename: '../css/style.[contenthash].css',
}),
new HtmlWebpackPlugin({
title: 'Busy',
filename: '../index.html',
template: path.join(options.baseDir, '/templates/index.html'),
}),
]);
}
return plugins;
}
function makeStyleLoaders(options) {
if (options.isDevelopment) {
return [
{
test: /\.css|.less$/,
use: [
{
loader: 'style-loader',
},
{
loader: 'css-loader',
options: {
sourceMap: true,
importLoaders: 1,
},
},
POSTCSS_LOADER,
{
loader: 'less-loader',
},
],
},
];
}
return [
{
test: /\.css|.less$/,
loader: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [
{
loader: 'css-loader',
options: {
importLoaders: 1,
},
},
POSTCSS_LOADER,
{
loader: 'less-loader',
},
],
}),
},
];
}
function makeConfig(options = {}) {
_.defaults(options, DEFAULTS);
const isDevelopment = options.isDevelopment;
return {
devtool: isDevelopment ? 'eval-source-map' : 'source-map',
entry: {
main: (isDevelopment ? [
'webpack-hot-middleware/client?reload=true',
'react-hot-loader/patch',
// activate HMR for React
'webpack/hot/only-dev-server',
// bundle the client for hot reloading
// only- means to only hot reload for successful updates
] : []).concat([
path.join(options.baseDir, 'src/index.js')],
),
},
output: {
path: path.join(options.baseDir, '/public/js'),
filename: options.isDevelopment ? 'busyapp-[name].js' : 'busyapp-[name].[chunkhash].js',
publicPath: '/js/',
},
plugins: makePlugins(options),
module: {
rules: [
{
test: /\.js?$/,
exclude: /node_modules/,
use: (options.isDevelopment ? [{ loader: 'react-hot-loader/webpack' }] : []).concat(
[
{
loader: 'babel-loader',
},
],
),
},
{
test: /\.(eot|ttf|woff|woff2|svg)(\?.+)?$/,
loader: 'url-loader',
options: {
name: '../fonts/[name].[ext]',
// load fonts through data-url in development
limit: options.isDevelopment ? 5000000 : 1,
},
},
{
test: /\.png$/,
loader: 'file-loader',
},
{
test: /\.html$/,
loader: 'html-loader',
options: {
removeComments: false,
},
},
].concat(makeStyleLoaders(options)),
},
};
}
if (!module.parent) {
console.log(makeConfig({
isDevelopment: process.env.NODE_ENV !== 'production',
}));
}
exports = module.exports = makeConfig;
exports.DEFAULTS = DEFAULTS;
exports.POSTCSS_LOADER = POSTCSS_LOADER;
exports.makeStyleLoaders = makeStyleLoaders;