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