]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/config/webpack.common.js
Bundle webtorrent with webpack. Closes https://github.com/Chocobozzz/PeerTube/issues/50
[github/Chocobozzz/PeerTube.git] / client / config / webpack.common.js
CommitLineData
4a6995be
C
1const helpers = require('./helpers')
2
3/*
4 * Webpack Plugins
5 */
6
2e92c10b 7const AssetsPlugin = require('assets-webpack-plugin')
b99290b1 8const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
d268c551 9const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin')
c16ce1de
C
10const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin')
11const CopyWebpackPlugin = require('copy-webpack-plugin')
12const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin
13const HtmlWebpackPlugin = require('html-webpack-plugin')
4d19d2f1
C
14const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin')
15const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin')
c16ce1de
C
16const ngcWebpack = require('ngc-webpack')
17
66698b83 18const WebpackNotifierPlugin = require('webpack-notifier')
4a6995be
C
19
20/*
21 * Webpack Constants
22 */
23const METADATA = {
24 title: 'PeerTube',
ab32b0fc
C
25 baseUrl: '/',
26 isDevServer: helpers.isWebpackDevServer()
4a6995be
C
27}
28
29/*
30 * Webpack configuration
31 *
32 * See: http://webpack.github.io/docs/configuration.html#cli
33 */
2e92c10b 34module.exports = function (options) {
c16ce1de
C
35 const isProd = options.env === 'production'
36 const AOT = isProd
2e92c10b
C
37
38 return {
4a6995be 39
2e92c10b
C
40 /*
41 * Cache generated modules and chunks to improve performance for multiple incremental builds.
42 * This is enabled by default in watch mode.
43 * You can pass false to disable it.
44 *
45 * See: http://webpack.github.io/docs/configuration.html#cache
46 */
47 // cache: false,
4a6995be 48
2e92c10b
C
49 /*
50 * The entry point for the bundle
51 * Our Angular.js app
52 *
53 * See: http://webpack.github.io/docs/configuration.html#entry
54 */
55 entry: {
c16ce1de
C
56 'polyfills': './src/polyfills.browser.ts',
57 'main': AOT
58 ? './src/main.browser.aot.ts'
59 : './src/main.browser.ts'
2e92c10b 60 },
b20b5fed 61
4a6995be 62 /*
2e92c10b 63 * Options affecting the resolving of modules.
4a6995be 64 *
2e92c10b 65 * See: http://webpack.github.io/docs/configuration.html#resolve
4a6995be 66 */
2e92c10b
C
67 resolve: {
68 /*
69 * An array of extensions that should be used to resolve modules.
70 *
71 * See: http://webpack.github.io/docs/configuration.html#resolve-extensions
72 */
4d19d2f1 73 extensions: [ '.ts', '.js', '.json', '.scss' ],
2e92c10b 74
240c64c5 75 modules: [ helpers.root('src'), helpers.root('node_modules') ]
2e92c10b 76 },
4a6995be
C
77
78 /*
2e92c10b 79 * Options affecting the normal modules.
4a6995be 80 *
2e92c10b 81 * See: http://webpack.github.io/docs/configuration.html#module
4a6995be 82 */
2e92c10b 83 module: {
4a6995be 84
4d19d2f1 85 rules: [
4a6995be 86
2e92c10b 87 /*
8635a2c7 88 * Typescript loader support for .ts and Angular async routes via .async.ts
2e92c10b
C
89 *
90 * See: https://github.com/s-panferov/awesome-typescript-loader
91 */
92 {
93 test: /\.ts$/,
c16ce1de 94 use: [
8635a2c7
C
95 {
96 loader: '@angularclass/hmr-loader',
97 options: {
98 pretty: !isProd,
99 prod: isProd
100 }
101 },
c16ce1de
C
102 {
103 loader: 'ng-router-loader',
104 options: {
8635a2c7 105 loader: 'async-import',
c16ce1de
C
106 genDir: 'compiled',
107 aot: AOT
108 }
8635a2c7
C
109 },
110 {
111 loader: 'awesome-typescript-loader',
112 options: {
113 configFileName: 'tsconfig.webpack.json',
114 useCache: !isProd
115 }
116 },
117 {
118 loader: 'angular2-template-loader'
c16ce1de 119 }
2e92c10b
C
120 ],
121 exclude: [/\.(spec|e2e)\.ts$/]
122 },
4a6995be 123
2e92c10b
C
124 /*
125 * Json loader support for *.json files.
126 *
127 * See: https://github.com/webpack/json-loader
128 */
129 {
130 test: /\.json$/,
8635a2c7 131 use: 'json-loader'
2e92c10b 132 },
4a6995be 133
2e92c10b 134 {
ad22074a 135 test: /\.(sass|scss)$/,
383bfc83
C
136 use: [
137 'css-to-string-loader',
138 'css-loader?sourceMap',
139 'resolve-url-loader',
140 {
141 loader: 'sass-loader',
142 options: {
143 sourceMap: true
144 }
145 },
146 {
147 loader: 'sass-resources-loader',
148 options: {
149 resources: [
150 helpers.root('src/sass/_variables.scss')
151 ]
152 }
153 }
154 ]
2e92c10b 155 },
c16ce1de
C
156 { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, use: 'url-loader?limit=10000&minetype=application/font-woff' },
157 { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, use: 'file-loader' },
4a6995be 158
2e92c10b
C
159 /* Raw loader support for *.html
160 * Returns file content as string
161 *
162 * See: https://github.com/webpack/raw-loader
163 */
164 {
165 test: /\.html$/,
8635a2c7 166 use: 'raw-loader',
383bfc83
C
167 exclude: [
168 helpers.root('src/index.html'),
169 helpers.root('src/standalone/videos/embed.html')
170 ]
1840c2f7
C
171 },
172
173 /* File loader for supporting images, for example, in CSS files.
174 */
175 {
176 test: /\.(jpg|png|gif)$/,
177 use: 'file-loader'
2e92c10b 178 }
4a6995be 179
2e92c10b
C
180 ]
181
182 },
183
4a6995be 184 /*
2e92c10b 185 * Add additional plugins to the compiler.
4a6995be 186 *
2e92c10b 187 * See: http://webpack.github.io/docs/configuration.html#plugins
4a6995be 188 */
2e92c10b
C
189 plugins: [
190 new AssetsPlugin({
191 path: helpers.root('dist'),
192 filename: 'webpack-assets.json',
193 prettyPrint: true
194 }),
195
196 /*
197 * Plugin: ForkCheckerPlugin
198 * Description: Do type checking in a separate process, so webpack don't need to wait.
199 *
200 * See: https://github.com/s-panferov/awesome-typescript-loader#forkchecker-boolean-defaultfalse
201 */
c16ce1de 202 new CheckerPlugin(),
2e92c10b
C
203
204 /*
205 * Plugin: CommonsChunkPlugin
206 * Description: Shares common code between the pages.
207 * It identifies common modules and put them into a commons chunk.
208 *
209 * See: https://webpack.github.io/docs/list-of-plugins.html#commonschunkplugin
210 * See: https://github.com/webpack/docs/wiki/optimization#multi-page-app
211 */
c16ce1de
C
212 new CommonsChunkPlugin({
213 name: 'polyfills',
214 chunks: ['polyfills']
215 }),
216
217 // This enables tree shaking of the vendor modules
218 new CommonsChunkPlugin({
219 name: 'vendor',
220 chunks: ['main'],
221 minChunks: module => /node_modules\//.test(module.resource)
222 }),
223
224 // Specify the correct order the scripts will be injected in
225 new CommonsChunkPlugin({
226 name: ['polyfills', 'vendor'].reverse()
2e92c10b
C
227 }),
228
d268c551
C
229 /**
230 * Plugin: ContextReplacementPlugin
231 * Description: Provides context to Angular's use of System.import
232 *
233 * See: https://webpack.github.io/docs/list-of-plugins.html#contextreplacementplugin
234 * See: https://github.com/angular/angular/issues/11580
4d19d2f1 235 */
d268c551 236 new ContextReplacementPlugin(
8635a2c7
C
237 /**
238 * The (\\|\/) piece accounts for path separators in *nix and Windows
239 */
240 /angular(\\|\/)core(\\|\/)@angular/,
c16ce1de
C
241 helpers.root('src'), // location of your src
242 {
8635a2c7
C
243 /**
244 * Your Angular Async Route paths relative to this root directory
245 */
c16ce1de 246 }
d268c551
C
247 ),
248
2e92c10b
C
249 /*
250 * Plugin: CopyWebpackPlugin
251 * Description: Copy files and directories in webpack.
252 *
253 * Copies project static assets.
254 *
255 * See: https://www.npmjs.com/package/copy-webpack-plugin
256 */
a8644408 257 // Used by embed.html
2e92c10b
C
258 new CopyWebpackPlugin([
259 {
260 from: 'src/assets',
261 to: 'assets'
262 },
3bb2c7f9
C
263 {
264 from: 'src/standalone',
265 to: 'standalone'
2e92c10b
C
266 }
267 ]),
268
8635a2c7
C
269 /*
270 * Plugin: ScriptExtHtmlWebpackPlugin
271 * Description: Enhances html-webpack-plugin functionality
272 * with different deployment options for your scripts including:
273 *
274 * See: https://github.com/numical/script-ext-html-webpack-plugin
275 */
276 new ScriptExtHtmlWebpackPlugin({
1840c2f7 277 sync: [ /polyfill|vendor/ ],
8635a2c7
C
278 defaultAttribute: 'async',
279 preload: [/polyfill|vendor|main/],
280 prefetch: [/chunk/]
281 }),
282
2e92c10b
C
283 /*
284 * Plugin: HtmlWebpackPlugin
285 * Description: Simplifies creation of HTML files to serve your webpack bundles.
286 * This is especially useful for webpack bundles that include a hash in the filename
287 * which changes every compilation.
288 *
289 * See: https://github.com/ampedandwired/html-webpack-plugin
290 */
291 new HtmlWebpackPlugin({
292 template: 'src/index.html',
4d19d2f1
C
293 title: METADATA.title,
294 chunksSortMode: 'dependency',
8635a2c7
C
295 metadata: METADATA,
296 inject: 'body'
2e92c10b
C
297 }),
298
4d19d2f1
C
299 new WebpackNotifierPlugin({ alwaysNotify: true }),
300
301 /**
302 * Plugin LoaderOptionsPlugin (experimental)
303 *
304 * See: https://gist.github.com/sokra/27b24881210b56bbaff7
305 */
306 new LoaderOptionsPlugin({
307 options: {
308 sassLoader: {
383bfc83
C
309 precision: 10,
310 includePaths: [ helpers.root('src/sass') ]
4d19d2f1
C
311 }
312 }
c16ce1de
C
313 }),
314
c16ce1de
C
315 new ngcWebpack.NgcWebpackPlugin({
316 disabled: !AOT,
317 tsConfig: helpers.root('tsconfig.webpack.json'),
318 resourceOverride: helpers.root('config/resource-override.js')
b99290b1
C
319 }),
320
321 new BundleAnalyzerPlugin({
322 // Can be `server`, `static` or `disabled`.
323 // In `server` mode analyzer will start HTTP server to show bundle report.
324 // In `static` mode single HTML file with bundle report will be generated.
325 // In `disabled` mode you can use this plugin to just generate Webpack Stats JSON file by setting `generateStatsFile` to `true`.
326 analyzerMode: 'static',
327 // Path to bundle report file that will be generated in `static` mode.
328 // Relative to bundles output directory.
329 reportFilename: 'report.html',
330 // Automatically open report in default browser
331 openAnalyzer: false,
332 // If `true`, Webpack Stats JSON file will be generated in bundles output directory
333 generateStatsFile: true,
334 // Name of Webpack Stats JSON file that will be generated if `generateStatsFile` is `true`.
335 // Relative to bundles output directory.
336 statsFilename: 'stats.json',
337 // Options for `stats.toJson()` method.
338 // For example you can exclude sources of your modules from stats file with `source: false` option.
339 // See more options here: https://github.com/webpack/webpack/blob/webpack-1/lib/Stats.js#L21
340 statsOptions: null,
341 // Log level. Can be 'info', 'warn', 'error' or 'silent'.
342 logLevel: 'info'
4d19d2f1 343 })
2e92c10b 344 ],
4a6995be
C
345
346 /*
2e92c10b
C
347 * Include polyfills or mocks for various node stuff
348 * Description: Node configuration
4a6995be 349 *
2e92c10b 350 * See: https://webpack.github.io/docs/configuration.html#node
4a6995be 351 */
2e92c10b 352 node: {
cc3e2d9b 353 global: true,
2e92c10b 354 crypto: 'empty',
4d19d2f1 355 process: true,
2e92c10b
C
356 module: false,
357 clearImmediate: false,
c16ce1de
C
358 setImmediate: false,
359 setInterval: false,
360 setTimeout: false
2e92c10b 361 }
4a6995be 362 }
4a6995be 363}