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