forked from brijeshb42/medium-draft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.config.js
243 lines (219 loc) · 5.79 KB
/
webpack.config.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* eslint-disable */
var path = require('path');
var fs = require('fs');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var ENV_DEV = 'development';
var ENV_PROD = 'production';
var ENV_TEST = 'test';
var BUILD_DIR = path.resolve(__dirname, 'dist');
var APP_DIR = path.resolve(__dirname, 'src');
var env = process.env.NODE_ENV || ENV_DEV;
var isDev = env === ENV_DEV;
var isProd = env === ENV_PROD;
var isTest = env === ENV_TEST;
console.log(env);
var pkg = require('./package.json');
var banner = [
pkg.name,
'Version - ' + pkg.version,
'Author - ' + pkg.author
].join('\n');
var bannerPlugin = new webpack.BannerPlugin(banner);
var definePlugin = new webpack.DefinePlugin({
__DEV__: JSON.stringify(env === ENV_DEV),
__PROD__: JSON.stringify(env === ENV_PROD),
__TEST__: JSON.stringify(env === ENV_TEST),
__PRERELEASE__: JSON.stringify(JSON.parse(process.env.BUILD_PRERELEASE || 'false')),
'process.env.NODE_ENV': '"' +env+ '"'
});
var commonsPlugin = new webpack.optimize.CommonsChunkPlugin({
name: 'common',
minChunks: 3,
});
// var vendorBase = new webpack.optimize.CommonsChunkPlugin("vendor-base", "vendor-base.js", Infinity);
var vendorPlugin = new webpack.optimize.CommonsChunkPlugin({
names: ['vendor-react'],
minChunks: Infinity,
filename: '[name].js',
// filename: isDev ? '[name].js' : '[name].[hash].js'
});
var hashJsonPlugin = function() {
this.plugin("done", function(stats) {
require("fs").writeFileSync(
path.join(__dirname, "hash.json"),
JSON.stringify(stats.toJson()["assetsByChunkName"]));
});
};
function getPlugins(env) {
var plugins = [definePlugin];
if (!isProd) {
plugins.push(new webpack.NoEmitOnErrorsPlugin());
plugins.push(vendorPlugin);
plugins.push(commonsPlugin);
} else {
plugins.push(new ExtractTextPlugin('[name].css'));
// plugins.push(new ExtractTextPlugin(isDev ? '[name].css' : '[name].[hash].css'));
plugins.push(hashJsonPlugin);
plugins.push(new webpack.optimize.UglifyJsPlugin({
sourceMap: false,
output: { comments: false },
debug: false,
compress: { warnings: false, dead_code: true }
}));
plugins.push(bannerPlugin);
}
return plugins;
}
function getEntry(env) {
var entry = {};
var entries = [];
if (env !== ENV_PROD) {
entry['vendor-react'] = [
'babel-polyfill',
'react',
'react-dom',
'immutable',
'draft-js',
]
entries.push('./index');
} else {
entries = ['./index'];
}
entry['medium-draft'] = entries;
entry.example = './example';
entry['basic'] = './basic.scss';
return entry;
}
function getLoaders(env) {
var loaders = [];
loaders.push({
test: /\.jsx?$/,
include: APP_DIR,
loader: env !== ENV_PROD ? 'react-hot-loader!babel-loader' : 'babel-loader',
exclude: /node_modules/
});
// loaders.push({
// test: /\.jsx?$/,
// loaders: 'eslint-loader',
// enforce: "pre",
// include: APP_DIR,
// });
loaders.push({
test: /\.(jpe?g|png|gif|svg)$/i,
loader: 'file-loader'
});
loaders.push({ test: /\.json$/, loader: 'json-loader' });
if (env === ENV_PROD ) {
loaders.push({
test: /(\.css|\.scss)$/,
loader: ExtractTextPlugin.extract("css-loader?sourceMap&minimize!sass-loader?sourceMap")
});
} else {
loaders.push({
test: /(\.css|\.scss)$/,
loaders: ['style-loader', 'css-loader?sourceMap', 'sass-loader?sourceMap']
});
}
return loaders;
}
var options = {
context: APP_DIR,
devtool: isProd ? '' : 'cheap-module-eval-source-map',
entry: getEntry(env),
target: 'web',
output: {
path: BUILD_DIR,
publicPath: '/static/',
filename: '[name].js',
// filename: env === ENV_DEV ? '[name].js' : '[name].[hash].js',
chunkFilename: '[id].[hash].bundle.js',
hotUpdateChunkFilename: 'hot/[id].[hash].hot-update.js',
hotUpdateMainFilename: 'hot/[hash].hot-update.json',
library: ['MediumDraft'],
libraryTarget: 'umd',
},
plugins: getPlugins(env),
module: {
loaders: getLoaders(env),
},
resolve: {
modules: [
APP_DIR,
'node_modules'
],
extensions: ['.js', '.jsx'],
},
devServer: {
historyApiFallback: false,
noInfo: false,
},
};
if (isProd) {
options.externals = [{
react: {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react'
}
},
{
'react-dom': {
root: 'ReactDOM',
commonjs2: 'react-dom',
commonjs: 'react-dom',
amd: 'react-dom'
}
},
{
'react-transition-group/CSSTransitionGroup': {
root: ['React', 'addons', 'CSSTransitionGroup'],
commonjs2: 'react-transition-group/CSSTransitionGroup',
commonjs: 'react-transition-group/CSSTransitionGroup',
amd: 'react-transition-group/CSSTransitionGroup',
}
},
{
immutable: {
root: 'Immutable',
commonjs2: 'immutable',
commonjs: 'immutable',
amd: 'immutable'
}
},
{
'draft-js': {
root: 'Draft',
commonjs2: 'draft-js',
commonjs: 'draft-js',
amd: 'draft-js'
}
}
];
}
var appExportType = process.env.APP_EXPORT_TYPE || '';
if (appExportType === 'exporter') {
console.log('Building HTML Exporter');
options.entry = {
'medium-draft-exporter': './exporter',
};
options.output.library = 'MediumDraftExporter';
options.externals.push({
'react-dom/server': {
root: 'ReactDOMServer',
commonjs2: 'react-dom/server',
commonjs: 'react-dom/server',
amd: 'react-dom/server'
}
});
options.externals.push({
'draft-convert': {
root: 'DraftConvert',
commonjs2: 'draft-convert',
commonjs: 'draft-convert',
amd: 'draft-convert'
}
});
}
module.exports = options;