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