000699aa5396d4e157a34459170f846404e6c396
[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 ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin')
9 const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin')
10 const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin
11 const HtmlWebpackPlugin = require('html-webpack-plugin')
12 const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin')
13 const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin')
14 const ngcWebpack = require('ngc-webpack')
15
16 const WebpackNotifierPlugin = require('webpack-notifier')
17
18 const HMR = helpers.hasProcessFlag('hot')
19 const AOT = process.env.BUILD_AOT || helpers.hasNpmFlag('aot')
20 const METADATA = {
21   title: 'PeerTube',
22   baseUrl: '/',
23   isDevServer: helpers.isWebpackDevServer(),
24   HMR: HMR,
25   AOT: AOT
26 }
27
28 /*
29  * Webpack configuration
30  *
31  * See: http://webpack.github.io/docs/configuration.html#cli
32  */
33 module.exports = function (options) {
34   const isProd = options.env === 'production'
35   const AOT = isProd
36
37   return {
38
39     /*
40      * Cache generated modules and chunks to improve performance for multiple incremental builds.
41      * This is enabled by default in watch mode.
42      * You can pass false to disable it.
43      *
44      * See: http://webpack.github.io/docs/configuration.html#cache
45      */
46     // cache: false,
47
48     /*
49      * The entry point for the bundle
50      * Our Angular.js app
51      *
52      * See: http://webpack.github.io/docs/configuration.html#entry
53      */
54     entry: {
55       'polyfills': './src/polyfills.browser.ts',
56       'main': AOT
57               ? './src/main.browser.aot.ts'
58               : './src/main.browser.ts'
59     },
60
61     /*
62      * Options affecting the resolving of modules.
63      *
64      * See: http://webpack.github.io/docs/configuration.html#resolve
65      */
66     resolve: {
67       /*
68        * An array of extensions that should be used to resolve modules.
69        *
70        * See: http://webpack.github.io/docs/configuration.html#resolve-extensions
71        */
72       extensions: [ '.ts', '.js', '.json', '.scss' ],
73
74       modules: [ helpers.root('src'), helpers.root('node_modules') ]
75     },
76
77     /*
78      * Options affecting the normal modules.
79      *
80      * See: http://webpack.github.io/docs/configuration.html#module
81      */
82     module: {
83
84       rules: [
85
86         /*
87          * Typescript loader support for .ts and Angular async routes via .async.ts
88          *
89          * See: https://github.com/s-panferov/awesome-typescript-loader
90          */
91         {
92           test: /\.ts$/,
93           use: [
94             {
95               loader: 'ng-router-loader',
96               options: {
97                 loader: 'async-import',
98                 genDir: 'compiled',
99                 aot: AOT
100               }
101             },
102             {
103               loader: 'awesome-typescript-loader',
104               options: {
105                 configFileName: 'tsconfig.webpack.json',
106                 useCache: !isProd
107               }
108             },
109             {
110               loader: 'angular2-template-loader'
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           use: 'json-loader'
124         },
125
126         {
127           test: /\.(sass|scss)$/,
128           use: [
129             'css-to-string-loader',
130             {
131               loader: 'css-loader',
132               options: {
133                 sourceMap: true,
134                 importLoaders: 1
135               }
136             },
137             'resolve-url-loader',
138             {
139               loader: 'sass-loader',
140               options: {
141                 sourceMap: true
142               }
143             },
144             {
145               loader: 'sass-resources-loader',
146               options: {
147                 resources: [
148                   helpers.root('src/sass/_variables.scss')
149                 ]
150               }
151             }
152           ]
153         },
154         { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, use: 'url-loader?limit=10000&minetype=application/font-woff' },
155         { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, use: 'file-loader' },
156
157         /* Raw loader support for *.html
158          * Returns file content as string
159          *
160          * See: https://github.com/webpack/raw-loader
161          */
162         {
163           test: /\.html$/,
164           use: 'raw-loader',
165           exclude: [
166             helpers.root('src/index.html'),
167             helpers.root('src/standalone/videos/embed.html')
168           ]
169         },
170
171         /* File loader for supporting images, for example, in CSS files.
172          */
173         {
174           test: /\.(jpg|png|gif)$/,
175           use: 'url-loader'
176         }
177
178       ]
179
180     },
181
182     /*
183      * Add additional plugins to the compiler.
184      *
185      * See: http://webpack.github.io/docs/configuration.html#plugins
186      */
187     plugins: [
188       new AssetsPlugin({
189         path: helpers.root('dist'),
190         filename: 'webpack-assets.json',
191         prettyPrint: true
192       }),
193
194       /*
195        * Plugin: ForkCheckerPlugin
196        * Description: Do type checking in a separate process, so webpack don't need to wait.
197        *
198        * See: https://github.com/s-panferov/awesome-typescript-loader#forkchecker-boolean-defaultfalse
199        */
200       new CheckerPlugin(),
201
202       /*
203        * Plugin: CommonsChunkPlugin
204        * Description: Shares common code between the pages.
205        * It identifies common modules and put them into a commons chunk.
206        *
207        * See: https://webpack.github.io/docs/list-of-plugins.html#commonschunkplugin
208        * See: https://github.com/webpack/docs/wiki/optimization#multi-page-app
209        */
210       new CommonsChunkPlugin({
211         name: 'polyfills',
212         chunks: ['polyfills']
213       }),
214
215       // This enables tree shaking of the vendor modules
216       new CommonsChunkPlugin({
217         name: 'vendor',
218         chunks: ['main'],
219         minChunks: module => /node_modules\//.test(module.resource)
220       }),
221
222       // Specify the correct order the scripts will be injected in
223       new CommonsChunkPlugin({
224         name: ['polyfills', 'vendor'].reverse()
225       }),
226
227       /**
228        * Plugin: ContextReplacementPlugin
229        * Description: Provides context to Angular's use of System.import
230        *
231        * See: https://webpack.github.io/docs/list-of-plugins.html#contextreplacementplugin
232        * See: https://github.com/angular/angular/issues/11580
233        */
234       new ContextReplacementPlugin(
235         /**
236          * The (\\|\/) piece accounts for path separators in *nix and Windows
237          */
238         /(.+)?angular(\\|\/)core(.+)?/,
239         helpers.root('src'), // location of your src
240         {
241           /**
242            * Your Angular Async Route paths relative to this root directory
243            */
244         }
245       ),
246
247       /*
248        * Plugin: ScriptExtHtmlWebpackPlugin
249        * Description: Enhances html-webpack-plugin functionality
250        * with different deployment options for your scripts including:
251        *
252        * See: https://github.com/numical/script-ext-html-webpack-plugin
253        */
254       new ScriptExtHtmlWebpackPlugin({
255         sync: [ /polyfill|vendor/ ],
256         defaultAttribute: 'async',
257         preload: [/polyfill|vendor|main/],
258         prefetch: [/chunk/]
259       }),
260
261       /*
262        * Plugin: HtmlWebpackPlugin
263        * Description: Simplifies creation of HTML files to serve your webpack bundles.
264        * This is especially useful for webpack bundles that include a hash in the filename
265        * which changes every compilation.
266        *
267        * See: https://github.com/ampedandwired/html-webpack-plugin
268        */
269       new HtmlWebpackPlugin({
270         template: 'src/index.html',
271         title: METADATA.title,
272         chunksSortMode: function (a, b) {
273           const entryPoints = [ 'inline', 'polyfills', 'sw-register', 'styles', 'vendor', 'main' ]
274           return entryPoints.indexOf(a.names[0]) - entryPoints.indexOf(b.names[0])
275         },
276         metadata: METADATA,
277         inject: 'body'
278       }),
279
280       new WebpackNotifierPlugin({ alwaysNotify: true }),
281
282       /**
283       * Plugin LoaderOptionsPlugin (experimental)
284       *
285       * See: https://gist.github.com/sokra/27b24881210b56bbaff7
286       */
287       new LoaderOptionsPlugin({
288         options: {
289           sassLoader: {
290             precision: 10,
291             includePaths: [ helpers.root('src/sass') ]
292           }
293         }
294       }),
295
296       new ngcWebpack.NgcWebpackPlugin({
297         disabled: !AOT,
298         tsConfig: helpers.root('tsconfig.webpack.json')
299       })
300     ],
301
302     /*
303      * Include polyfills or mocks for various node stuff
304      * Description: Node configuration
305      *
306      * See: https://webpack.github.io/docs/configuration.html#node
307      */
308     node: {
309       global: true,
310       crypto: 'empty',
311       process: true,
312       module: false,
313       clearImmediate: false,
314       setImmediate: false,
315       setInterval: false,
316       setTimeout: false
317     }
318   }
319 }