1 | import * as path from 'path';
|
2 |
|
3 | import webpack from 'webpack';
|
4 | import ExtractTextPlugin from 'extract-text-webpack-plugin';
|
5 | import HtmlWebpackPlugin from 'html-webpack-plugin';
|
6 | import SystemBellPlugin from 'system-bell-webpack-plugin';
|
7 | import CleanWebpackPlugin from 'clean-webpack-plugin';
|
8 | import merge from 'webpack-merge';
|
9 |
|
10 | const pkg = require('./package.json');
|
11 |
|
12 | const TARGET = process.env.npm_lifecycle_event || '';
|
13 | const ROOT_PATH = __dirname;
|
14 | const config = {
|
15 | paths: {
|
16 | dist: path.join(ROOT_PATH, 'dist'),
|
17 | src: path.join(ROOT_PATH, 'src'),
|
18 | docs: path.join(ROOT_PATH, 'docs')
|
19 | },
|
20 | filename: 'boilerplate',
|
21 | library: 'Boilerplate'
|
22 | };
|
23 |
|
24 | process.env.BABEL_ENV = TARGET;
|
25 |
|
26 | const common = {
|
27 | resolve: {
|
28 | extensions: ['', '.js', '.jsx', '.css', '.png', '.jpg']
|
29 | },
|
30 | module: {
|
31 | preLoaders: [
|
32 | {
|
33 | test: /\.jsx?$/,
|
34 | loaders: ['eslint'],
|
35 | include: [
|
36 | config.paths.docs,
|
37 | config.paths.src
|
38 | ]
|
39 | }
|
40 | ],
|
41 | loaders: [
|
42 | {
|
43 | test: /\.md$/,
|
44 | loaders: ['catalog/lib/loader', 'raw']
|
45 | },
|
46 | {
|
47 | test: /\.png$/,
|
48 | loader: 'url?limit=100000&mimetype=image/png',
|
49 | include: config.paths.docs
|
50 | },
|
51 | {
|
52 | test: /\.jpg$/,
|
53 | loader: 'file',
|
54 | include: config.paths.docs
|
55 | },
|
56 | {
|
57 | test: /\.json$/,
|
58 | loader: 'json',
|
59 | include: path.join(ROOT_PATH, 'package.json')
|
60 | }
|
61 | ]
|
62 | },
|
63 | plugins: [
|
64 | new SystemBellPlugin()
|
65 | ]
|
66 | };
|
67 |
|
68 | const siteCommon = {
|
69 | plugins: [
|
70 | new HtmlWebpackPlugin({
|
71 | template: require('html-webpack-template'),
|
72 | inject: false,
|
73 | mobile: true,
|
74 | title: pkg.name,
|
75 | appMountId: 'app'
|
76 | }),
|
77 | new webpack.DefinePlugin({
|
78 | NAME: JSON.stringify(pkg.name),
|
79 | USER: JSON.stringify(pkg.user),
|
80 | VERSION: JSON.stringify(pkg.version)
|
81 | })
|
82 | ]
|
83 | };
|
84 |
|
85 | if (TARGET === 'start') {
|
86 | module.exports = merge(common, siteCommon, {
|
87 | devtool: 'eval-source-map',
|
88 | entry: {
|
89 | docs: [config.paths.docs]
|
90 | },
|
91 | plugins: [
|
92 | new webpack.DefinePlugin({
|
93 | 'process.env.NODE_ENV': '"development"'
|
94 | }),
|
95 | new webpack.HotModuleReplacementPlugin()
|
96 | ],
|
97 | module: {
|
98 | loaders: [
|
99 | {
|
100 | test: /\.css$/,
|
101 | loaders: ['style', 'css']
|
102 | },
|
103 | {
|
104 | test: /\.jsx?$/,
|
105 | loaders: ['babel?cacheDirectory'],
|
106 | include: [
|
107 | config.paths.docs,
|
108 | config.paths.src
|
109 | ]
|
110 | }
|
111 | ]
|
112 | },
|
113 | devServer: {
|
114 | historyApiFallback: true,
|
115 | hot: true,
|
116 | inline: true,
|
117 | progress: true,
|
118 | host: process.env.HOST,
|
119 | port: process.env.PORT,
|
120 | stats: 'errors-only'
|
121 | }
|
122 | });
|
123 | }
|
124 |
|
125 | if (TARGET === 'gh-pages' || TARGET === 'gh-pages:stats') {
|
126 | module.exports = merge(common, siteCommon, {
|
127 | entry: {
|
128 | app: config.paths.docs,
|
129 | vendors: [
|
130 | 'react',
|
131 | 'react-dom'
|
132 | ]
|
133 | },
|
134 | output: {
|
135 | path: './gh-pages',
|
136 | filename: '[name].[chunkhash].js',
|
137 | chunkFilename: '[chunkhash].js'
|
138 | },
|
139 | plugins: [
|
140 | new CleanWebpackPlugin(['gh-pages'], {
|
141 | verbose: false
|
142 | }),
|
143 | new ExtractTextPlugin('[name].[chunkhash].css'),
|
144 | new webpack.DefinePlugin({
|
145 |
|
146 | 'process.env.NODE_ENV': '"production"'
|
147 | }),
|
148 | new webpack.optimize.DedupePlugin(),
|
149 | new webpack.optimize.UglifyJsPlugin({
|
150 | compress: {
|
151 | warnings: false
|
152 | }
|
153 | }),
|
154 | new webpack.optimize.CommonsChunkPlugin(
|
155 | 'vendor',
|
156 | '[name].[chunkhash].js'
|
157 | )
|
158 | ],
|
159 | module: {
|
160 | loaders: [
|
161 | {
|
162 | test: /\.css$/,
|
163 | loader: ExtractTextPlugin.extract('style', 'css')
|
164 | },
|
165 | {
|
166 | test: /\.jsx?$/,
|
167 | loaders: ['babel'],
|
168 | include: [
|
169 | config.paths.docs,
|
170 | config.paths.src
|
171 | ]
|
172 | }
|
173 | ]
|
174 | }
|
175 | });
|
176 | }
|
177 |
|
178 | const distCommon = {
|
179 | devtool: 'source-map',
|
180 | output: {
|
181 | path: config.paths.dist,
|
182 | libraryTarget: 'umd',
|
183 | library: config.library
|
184 | },
|
185 | entry: config.paths.src,
|
186 | externals: {
|
187 | react: {
|
188 | commonjs: 'react',
|
189 | commonjs2: 'react',
|
190 | amd: 'React',
|
191 | root: 'React'
|
192 | }
|
193 | },
|
194 | module: {
|
195 | loaders: [
|
196 | {
|
197 | test: /\.jsx?$/,
|
198 | loaders: ['babel'],
|
199 | include: config.paths.src
|
200 | }
|
201 | ]
|
202 | },
|
203 | plugins: [
|
204 | new SystemBellPlugin()
|
205 | ]
|
206 | };
|
207 |
|
208 | if (TARGET === 'dist') {
|
209 | module.exports = merge(distCommon, {
|
210 | output: {
|
211 | filename: `${config.filename}.js`
|
212 | }
|
213 | });
|
214 | }
|
215 |
|
216 | if (TARGET === 'dist:min') {
|
217 | module.exports = merge(distCommon, {
|
218 | output: {
|
219 | filename: `${config.filename}.min.js`
|
220 | },
|
221 | plugins: [
|
222 | new webpack.optimize.UglifyJsPlugin({
|
223 | compress: {
|
224 | warnings: false
|
225 | }
|
226 | })
|
227 | ]
|
228 | });
|
229 | }
|
230 |
|
231 | if (!TARGET) {
|
232 | module.exports = common;
|
233 | }
|