Client: add basic aot support
[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 NormalModuleReplacementPlugin = require('webpack/lib/NormalModuleReplacementPlugin')
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       alias: {
78         'video.js': 'video.js/dist/alt/video.novtt'
79       }
80     },
81
82     /*
83      * Options affecting the normal modules.
84      *
85      * See: http://webpack.github.io/docs/configuration.html#module
86      */
87     module: {
88
89       rules: [
90
91         /*
92          * Typescript loader support for .ts and Angular 2 async routes via .async.ts
93          *
94          * See: https://github.com/s-panferov/awesome-typescript-loader
95          */
96         {
97           test: /\.ts$/,
98           use: [
99             '@angularclass/hmr-loader?pretty=' + !isProd + '&prod=' + isProd,
100             'awesome-typescript-loader?{configFileName: "tsconfig.webpack.json"}',
101             'angular2-template-loader',
102             {
103               loader: 'ng-router-loader',
104               options: {
105                 loader: 'async-system',
106                 genDir: 'compiled',
107                 aot: AOT
108               }
109             }
110           ],
111           exclude: [/\.(spec|e2e)\.ts$/]
112         },
113
114         /*
115          * Json loader support for *.json files.
116          *
117          * See: https://github.com/webpack/json-loader
118          */
119         {
120           test: /\.json$/,
121           loader: 'json-loader'
122         },
123
124         {
125           test: /\.(sass|scss)$/,
126           use: ['css-to-string-loader', 'css-loader?sourceMap', 'resolve-url-loader', 'sass-loader?sourceMap'],
127           exclude: [ helpers.root('src', 'styles') ]
128         },
129         { test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, use: 'url-loader?limit=10000&minetype=application/font-woff' },
130         { test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, use: 'file-loader' },
131
132         /* Raw loader support for *.html
133          * Returns file content as string
134          *
135          * See: https://github.com/webpack/raw-loader
136          */
137         {
138           test: /\.html$/,
139           loader: 'raw-loader',
140           exclude: [ helpers.root('src/index.html'), helpers.root('src/standalone/videos/embed.html') ]
141         }
142
143       ]
144
145     },
146
147     /*
148      * Add additional plugins to the compiler.
149      *
150      * See: http://webpack.github.io/docs/configuration.html#plugins
151      */
152     plugins: [
153       new AssetsPlugin({
154         path: helpers.root('dist'),
155         filename: 'webpack-assets.json',
156         prettyPrint: true
157       }),
158
159       /*
160        * Plugin: ForkCheckerPlugin
161        * Description: Do type checking in a separate process, so webpack don't need to wait.
162        *
163        * See: https://github.com/s-panferov/awesome-typescript-loader#forkchecker-boolean-defaultfalse
164        */
165       new CheckerPlugin(),
166
167       /*
168        * Plugin: CommonsChunkPlugin
169        * Description: Shares common code between the pages.
170        * It identifies common modules and put them into a commons chunk.
171        *
172        * See: https://webpack.github.io/docs/list-of-plugins.html#commonschunkplugin
173        * See: https://github.com/webpack/docs/wiki/optimization#multi-page-app
174        */
175       new CommonsChunkPlugin({
176         name: 'polyfills',
177         chunks: ['polyfills']
178       }),
179
180       // This enables tree shaking of the vendor modules
181       new CommonsChunkPlugin({
182         name: 'vendor',
183         chunks: ['main'],
184         minChunks: module => /node_modules\//.test(module.resource)
185       }),
186
187       // Specify the correct order the scripts will be injected in
188       new CommonsChunkPlugin({
189         name: ['polyfills', 'vendor'].reverse()
190       }),
191
192       /**
193        * Plugin: ContextReplacementPlugin
194        * Description: Provides context to Angular's use of System.import
195        *
196        * See: https://webpack.github.io/docs/list-of-plugins.html#contextreplacementplugin
197        * See: https://github.com/angular/angular/issues/11580
198        */
199       new ContextReplacementPlugin(
200         // The (\\|\/) piece accounts for path separators in *nix and Windows
201         /angular(\\|\/)core(\\|\/)src(\\|\/)linker/,
202         helpers.root('src'), // location of your src
203         {
204           // your Angular Async Route paths relative to this root directory
205         }
206       ),
207
208       /*
209        * Plugin: CopyWebpackPlugin
210        * Description: Copy files and directories in webpack.
211        *
212        * Copies project static assets.
213        *
214        * See: https://www.npmjs.com/package/copy-webpack-plugin
215        */
216       // Used by embed.html
217       new CopyWebpackPlugin([
218         {
219           from: 'src/assets',
220           to: 'assets'
221         },
222         {
223           from: 'node_modules/webtorrent/webtorrent.min.js',
224           to: 'assets/webtorrent'
225         },
226         {
227           from: 'node_modules/video.js/dist/video.min.js',
228           to: 'assets/video-js'
229         },
230         {
231           from: 'node_modules/video.js/dist/video-js.min.css',
232           to: 'assets/video-js'
233         },
234         {
235           from: 'node_modules/videojs-dock/dist/videojs-dock.min.js',
236           to: 'assets/video-js'
237         },
238         {
239           from: 'node_modules/videojs-dock/dist/videojs-dock.css',
240           to: 'assets/video-js'
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           }
287         }
288       }),
289
290       // Fix Angular 2
291       new NormalModuleReplacementPlugin(
292         /facade(\\|\/)async/,
293         helpers.root('node_modules/@angular/core/src/facade/async.js')
294       ),
295       new NormalModuleReplacementPlugin(
296         /facade(\\|\/)collection/,
297         helpers.root('node_modules/@angular/core/src/facade/collection.js')
298       ),
299       new NormalModuleReplacementPlugin(
300         /facade(\\|\/)errors/,
301         helpers.root('node_modules/@angular/core/src/facade/errors.js')
302       ),
303       new NormalModuleReplacementPlugin(
304         /facade(\\|\/)lang/,
305         helpers.root('node_modules/@angular/core/src/facade/lang.js')
306       ),
307       new NormalModuleReplacementPlugin(
308         /facade(\\|\/)math/,
309         helpers.root('node_modules/@angular/core/src/facade/math.js')
310       ),
311
312       new ngcWebpack.NgcWebpackPlugin({
313         disabled: !AOT,
314         tsConfig: helpers.root('tsconfig.webpack.json'),
315         resourceOverride: helpers.root('config/resource-override.js')
316       })
317     ],
318
319     /*
320      * Include polyfills or mocks for various node stuff
321      * Description: Node configuration
322      *
323      * See: https://webpack.github.io/docs/configuration.html#node
324      */
325     node: {
326       global: 'true',
327       crypto: 'empty',
328       process: true,
329       module: false,
330       clearImmediate: false,
331       setImmediate: false,
332       setInterval: false,
333       setTimeout: false
334     }
335   }
336 }