-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathwebpack.config.js
190 lines (169 loc) · 4.19 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
var libraryName = 'Kattappa';
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__: env === ENV_DEV,
__PROD__: env === ENV_PROD,
__TEST__: 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 vendorPlugin = new webpack.optimize.CommonsChunkPlugin({
names: ['vendor-react'],
minChunks: Infinity,
filename: '[name].js',
children: true
});
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 (env !== ENV_PROD) {
plugins.push(commonsPlugin);
plugins.push(vendorPlugin);
plugins.push(new webpack.NoErrorsPlugin());
} else {
plugins.push(new ExtractTextPlugin(libraryName + '-style.css'));
plugins.push(hashJsonPlugin);
plugins.push(new webpack.optimize.DedupePlugin());
plugins.push(new webpack.optimize.UglifyJsPlugin({
output: {comments: false},
mangle: true,
}));
plugins.push(bannerPlugin);
}
return plugins;
}
function getEntry(env) {
var entry = {
'vendor-react': [
'react',
'react-dom',
]
};
var entries = [];
if (env === ENV_PROD) {
entry = {};
// entry[libraryName+'-style'] = ['./kattappa.scss'];
} else {
entries.push('webpack-dev-server/client?http://localhost:8080/');
entries.push('webpack/hot/only-dev-server');
}
entries.push('./index');
entry[libraryName] = entries;
entry.demo = ['./demo'];
return entry;
}
function getLoaders(env) {
var loaders = [];
loaders.push({
test: /\.jsx?$/,
include: APP_DIR,
loader: env !== ENV_PROD ? 'react-hot!babel' : 'babel',
exclude: /node_modules/
});
loaders.push({
test: /\.(jpe?g|png|gif|svg)$/i,
loader: 'file'
});
if (env === ENV_PROD ) {
loaders.push({
test: /(\.css|\.scss)$/,
loader: ExtractTextPlugin.extract("css?sourceMap!sass?sourceMap"),
});
// loaders.push({
// test: require.resolve("kattappa"),
// loader: "imports?this=>window",
// });
} else {
loaders.push({
test: /(\.css|\.scss)$/,
loaders: ['style', 'css?sourceMap', 'sass?sourceMap']
});
}
return loaders;
}
var config = {
context: APP_DIR,
debug: true,
devtool: env === ENV_PROD ? '' : 'cheap-module-eval-source-map',
entry: getEntry(env),
target: 'web',
output: {
path: BUILD_DIR,
publicPath: '',
filename: '[name].js',
sourceMapFile: '[file].map',
library: [libraryName],
libraryTarget: 'umd'
},
plugins: getPlugins(env),
module: {
loaders: getLoaders(env),
},
resolve: {
extensions: ['', '.js', '.jsx'],
root: APP_DIR,
modulesDirectories: ['node_modules'],
},
devServer: {
historyApiFallback: true
}
};
if (env === ENV_PROD) {
config.externals = [
{
react: {
root: 'React',
commonjs2: 'react',
commonjs: 'react',
amd: 'react'
}
},
{
'react-dom': {
root: 'ReactDOM',
commonjs2: 'react-dom',
commonjs: 'react-dom',
amd: 'react-dom'
}
},
{
'medium-editor': {
root: 'MediumEditor',
commonjs2: 'medium-editor',
commonjs: 'medium-editor',
amd: 'medium-editor'
}
}
];
}
module.exports = config;