UNPKG

4.75 kBJavaScriptView Raw
1'use strict'
2
3// source:
4// http://survivejs.com/webpack_react/authoring_libraries/
5
6var path = require('path')
7
8var webpack = require('webpack')
9var HtmlWebpackPlugin = require('html-webpack-plugin')
10var merge = require('webpack-merge')
11var minimist = require('minimist')
12
13var pkg = require('./package.json')
14
15var process_arguments = minimist(process.argv.slice(2))
16
17var action = process_arguments.action
18
19if (!action)
20{
21 console.log('Action required.')
22 console.log('Usage: webpack --target=[dev|gh-pages|build|build-minified]')
23 return
24}
25
26var Root_folder = path.resolve(__dirname)
27var Demo_folder = 'demo'
28
29var babel = 'babel?optional[]=runtime&stage=0'
30
31var config =
32{
33 paths:
34 {
35 dist: path.join(Root_folder, 'build'),
36 src: path.join(Root_folder, 'source'),
37 demo: path.join(Root_folder, Demo_folder),
38 demoIndex: path.join(Root_folder, Demo_folder, '/index')
39 },
40 filename: 'webpack-isomorphic-tools',
41 library: 'webpack-isomorphic-tools'
42}
43
44var merge_demo = merge.bind(null,
45{
46 resolve:
47 {
48 extensions: ['', '.js', '.jsx', '.md', '.css', '.png', '.jpg']
49 },
50 module:
51 {
52 loaders:
53 [
54 {
55 test: /\.css$/,
56 loaders: ['style', 'css']
57 },
58 {
59 test: /\.md$/,
60 loaders: ['html', 'highlight', 'markdown']
61 },
62 {
63 test: /\.png$/,
64 loader: 'url?limit=100000&mimetype=image/png',
65 include: config.paths.demo
66 },
67 {
68 test: /\.jpg$/,
69 loader: 'file',
70 include: config.paths.demo
71 },
72 {
73 test: /\.json$/,
74 loader: 'json'
75 }
76 ]
77 }
78})
79
80var merge_build = merge.bind(null,
81{
82 devtool: 'source-map',
83 output:
84 {
85 path: config.paths.dist,
86 libraryTarget: 'umd',
87 library: config.library
88 },
89 entry: config.paths.src,
90 externals:
91 {
92 //// if you are not testing, just react will do
93 //react: 'react',
94 // 'react/addons': 'react/addons'
95 },
96 module:
97 {
98 loaders:
99 [
100 {
101 test: /\.jsx?$/,
102 loaders: [babel],
103 include: config.paths.src
104 }
105 ]
106 }
107})
108
109switch (action)
110{
111 // Starts WebPack development server
112 case 'dev':
113 var IP = '0.0.0.0'
114 var PORT = 3000
115
116 module.exports = merge_demo
117 ({
118 ip: IP,
119 port: PORT,
120 devtool: 'eval',
121 entry:
122 [
123 'webpack-dev-server/client?http://' + IP + ':' + PORT,
124 'webpack/hot/only-dev-server',
125 config.paths.demoIndex
126 ],
127 output:
128 {
129 path: __dirname,
130 filename: 'bundle.js',
131 publicPath: '/'
132 },
133 plugins:
134 [
135 new webpack.DefinePlugin
136 ({
137 'process.env':
138 {
139 'NODE_ENV': JSON.stringify('development'),
140 }
141 }),
142 new webpack.HotModuleReplacementPlugin(),
143 new webpack.NoErrorsPlugin(),
144 new HtmlWebpackPlugin()
145 ],
146 module:
147 {
148 preLoaders:
149 [
150 {
151 test: /\.jsx?$/,
152 loaders: ['eslint', 'jscs'],
153 include: [config.paths.demo, config.paths.src]
154 }
155 ],
156 loaders:
157 [
158 {
159 test: /\.jsx?$/,
160 loaders: ['react-hot', babel],
161 include: [config.paths.demo, config.paths.src]
162 }
163 ]
164 }
165 })
166
167 break
168
169 // Generates a github pages website
170 case 'gh-pages':
171 module.exports = merge_demo
172 ({
173 entry:
174 {
175 app: config.paths.demoIndex,
176 // tweak this to include your externs unless you load them some other way
177 vendors: ['react/addons']
178 },
179 output:
180 {
181 path: './gh-pages',
182 filename: 'bundle.[chunkhash].js'
183 },
184 plugins:
185 [
186 new webpack.DefinePlugin
187 ({
188 'process.env':
189 {
190 // This has effect on the react lib size
191 'NODE_ENV': JSON.stringify('production'),
192 }
193 }),
194 new webpack.optimize.DedupePlugin(),
195 new webpack.optimize.UglifyJsPlugin
196 ({
197 compress:
198 {
199 warnings: false
200 }
201 }),
202 new webpack.optimize.CommonsChunkPlugin('vendors', 'vendors.[chunkhash].js'),
203 new HtmlWebpackPlugin
204 ({
205 title: pkg.name + ' - ' + pkg.description
206 })
207 ],
208 module:
209 {
210 loaders:
211 [
212 {
213 test: /\.jsx?$/,
214 loaders: [babel],
215 include: [config.paths.demo, config.paths.src]
216 }
217 ]
218 }
219 })
220
221 break
222
223 // Builds the project into a single file
224 case 'build':
225 module.exports = merge_build
226 ({
227 output:
228 {
229 filename: config.filename + '.js'
230 }
231 })
232
233 break
234
235 // Builds the project into a single minified file
236 case 'build-minified':
237 module.exports = merge_build
238 ({
239 output:
240 {
241 filename: config.filename + '.minified.js'
242 },
243 plugins:
244 [
245 new webpack.optimize.UglifyJsPlugin
246 ({
247 compress:
248 {
249 warnings: false
250 }
251 })
252 ]
253 })
254
255 break
256}
\No newline at end of file