Client: responsive design
[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 2 async routes via .async.ts
89          *
90          * See: https://github.com/s-panferov/awesome-typescript-loader
91          */
92         {
93           test: /\.ts$/,
94           use: [
95             '@angularclass/hmr-loader?pretty=' + !isProd + '&prod=' + isProd,
96             'awesome-typescript-loader?{configFileName: "tsconfig.webpack.json"}',
97             'angular2-template-loader',
98             {
99               loader: 'ng-router-loader',
100               options: {
101                 loader: 'async-system',
102                 genDir: 'compiled',
103                 aot: AOT
104               }
105             }
106           ],
107           exclude: [/\.(spec|e2e)\.ts$/]
108         },
109
110         /*
111          * Json loader support for *.json files.
112          *
113          * See: https://github.com/webpack/json-loader
114          */
115         {
116           test: /\.json$/,
117           loader: 'json-loader'
118         },
119
120         {
121           test: /\.(sass|scss)$/,
122           use: [
123             'css-to-string-loader',
124             'css-loader?sourceMap',
125             'resolve-url-loader',
126             {
127               loader: 'sass-loader',
128               options: {
129                 sourceMap: true
130               }
131             },
132             {
133               loader: 'sass-resources-loader',
134               options: {
135                 resources: [
136                   helpers.root('src/sass/_variables.scss')
137                 ]
138               }
139             }
140           ]
141         },
142         { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, use: 'url-loader?limit=10000&minetype=application/font-woff' },
143         { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, use: 'file-loader' },
144
145         /* Raw loader support for *.html
146          * Returns file content as string
147          *
148          * See: https://github.com/webpack/raw-loader
149          */
150         {
151           test: /\.html$/,
152           loader: 'raw-loader',
153           exclude: [
154             helpers.root('src/index.html'),
155             helpers.root('src/standalone/videos/embed.html')
156           ]
157         }
158
159       ]
160
161     },
162
163     /*
164      * Add additional plugins to the compiler.
165      *
166      * See: http://webpack.github.io/docs/configuration.html#plugins
167      */
168     plugins: [
169       new AssetsPlugin({
170         path: helpers.root('dist'),
171         filename: 'webpack-assets.json',
172         prettyPrint: true
173       }),
174
175       /*
176        * Plugin: ForkCheckerPlugin
177        * Description: Do type checking in a separate process, so webpack don't need to wait.
178        *
179        * See: https://github.com/s-panferov/awesome-typescript-loader#forkchecker-boolean-defaultfalse
180        */
181       new CheckerPlugin(),
182
183       /*
184        * Plugin: CommonsChunkPlugin
185        * Description: Shares common code between the pages.
186        * It identifies common modules and put them into a commons chunk.
187        *
188        * See: https://webpack.github.io/docs/list-of-plugins.html#commonschunkplugin
189        * See: https://github.com/webpack/docs/wiki/optimization#multi-page-app
190        */
191       new CommonsChunkPlugin({
192         name: 'polyfills',
193         chunks: ['polyfills']
194       }),
195
196       // This enables tree shaking of the vendor modules
197       new CommonsChunkPlugin({
198         name: 'vendor',
199         chunks: ['main'],
200         minChunks: module => /node_modules\//.test(module.resource)
201       }),
202
203       // Specify the correct order the scripts will be injected in
204       new CommonsChunkPlugin({
205         name: ['polyfills', 'vendor'].reverse()
206       }),
207
208       /**
209        * Plugin: ContextReplacementPlugin
210        * Description: Provides context to Angular's use of System.import
211        *
212        * See: https://webpack.github.io/docs/list-of-plugins.html#contextreplacementplugin
213        * See: https://github.com/angular/angular/issues/11580
214        */
215       new ContextReplacementPlugin(
216         // The (\\|\/) piece accounts for path separators in *nix and Windows
217         /angular(\\|\/)core(\\|\/)src(\\|\/)linker/,
218         helpers.root('src'), // location of your src
219         {
220           // your Angular Async Route paths relative to this root directory
221         }
222       ),
223
224       /*
225        * Plugin: CopyWebpackPlugin
226        * Description: Copy files and directories in webpack.
227        *
228        * Copies project static assets.
229        *
230        * See: https://www.npmjs.com/package/copy-webpack-plugin
231        */
232       // Used by embed.html
233       new CopyWebpackPlugin([
234         {
235           from: 'src/assets',
236           to: 'assets'
237         },
238         {
239           from: 'node_modules/webtorrent/webtorrent.min.js',
240           to: 'assets/webtorrent'
241         },
242         {
243           from: 'src/standalone',
244           to: 'standalone'
245         }
246       ]),
247
248       /*
249        * Plugin: HtmlWebpackPlugin
250        * Description: Simplifies creation of HTML files to serve your webpack bundles.
251        * This is especially useful for webpack bundles that include a hash in the filename
252        * which changes every compilation.
253        *
254        * See: https://github.com/ampedandwired/html-webpack-plugin
255        */
256       new HtmlWebpackPlugin({
257         template: 'src/index.html',
258         title: METADATA.title,
259         chunksSortMode: 'dependency',
260         metadata: METADATA
261       }),
262
263       /*
264       * Plugin: ScriptExtHtmlWebpackPlugin
265       * Description: Enhances html-webpack-plugin functionality
266       * with different deployment options for your scripts including:
267       *
268       * See: https://github.com/numical/script-ext-html-webpack-plugin
269       */
270       new ScriptExtHtmlWebpackPlugin({
271         sync: [ 'webtorrent.min.js' ],
272         defaultAttribute: 'defer'
273       }),
274
275       new WebpackNotifierPlugin({ alwaysNotify: true }),
276
277       /**
278       * Plugin LoaderOptionsPlugin (experimental)
279       *
280       * See: https://gist.github.com/sokra/27b24881210b56bbaff7
281       */
282       new LoaderOptionsPlugin({
283         options: {
284           sassLoader: {
285             precision: 10,
286             includePaths: [ helpers.root('src/sass') ]
287           }
288         }
289       }),
290
291       new ngcWebpack.NgcWebpackPlugin({
292         disabled: !AOT,
293         tsConfig: helpers.root('tsconfig.webpack.json'),
294         resourceOverride: helpers.root('config/resource-override.js')
295       }),
296
297       new BundleAnalyzerPlugin({
298         // Can be `server`, `static` or `disabled`.
299         // In `server` mode analyzer will start HTTP server to show bundle report.
300         // In `static` mode single HTML file with bundle report will be generated.
301         // In `disabled` mode you can use this plugin to just generate Webpack Stats JSON file by setting `generateStatsFile` to `true`.
302         analyzerMode: 'static',
303         // Path to bundle report file that will be generated in `static` mode.
304         // Relative to bundles output directory.
305         reportFilename: 'report.html',
306         // Automatically open report in default browser
307         openAnalyzer: false,
308         // If `true`, Webpack Stats JSON file will be generated in bundles output directory
309         generateStatsFile: true,
310         // Name of Webpack Stats JSON file that will be generated if `generateStatsFile` is `true`.
311         // Relative to bundles output directory.
312         statsFilename: 'stats.json',
313         // Options for `stats.toJson()` method.
314         // For example you can exclude sources of your modules from stats file with `source: false` option.
315         // See more options here: https://github.com/webpack/webpack/blob/webpack-1/lib/Stats.js#L21
316         statsOptions: null,
317         // Log level. Can be 'info', 'warn', 'error' or 'silent'.
318         logLevel: 'info'
319       })
320     ],
321
322     /*
323      * Include polyfills or mocks for various node stuff
324      * Description: Node configuration
325      *
326      * See: https://webpack.github.io/docs/configuration.html#node
327      */
328     node: {
329       global: true,
330       crypto: 'empty',
331       process: true,
332       module: false,
333       clearImmediate: false,
334       setImmediate: false,
335       setInterval: false,
336       setTimeout: false
337     }
338   }
339 }