]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/config/webpack.common.js
Client: fix lint
[github/Chocobozzz/PeerTube.git] / client / config / webpack.common.js
CommitLineData
4a6995be
C
1const helpers = require('./helpers')
2
3/*
4 * Webpack Plugins
5 */
6
2e92c10b 7const AssetsPlugin = require('assets-webpack-plugin')
b99290b1 8const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
c16ce1de 9const NormalModuleReplacementPlugin = require('webpack/lib/NormalModuleReplacementPlugin')
d268c551 10const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin')
c16ce1de
C
11const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin')
12const CopyWebpackPlugin = require('copy-webpack-plugin')
13const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin
14const HtmlWebpackPlugin = require('html-webpack-plugin')
4d19d2f1
C
15const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin')
16const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin')
c16ce1de
C
17const ngcWebpack = require('ngc-webpack')
18
66698b83 19const WebpackNotifierPlugin = require('webpack-notifier')
4a6995be
C
20
21/*
22 * Webpack Constants
23 */
24const METADATA = {
25 title: 'PeerTube',
ab32b0fc
C
26 baseUrl: '/',
27 isDevServer: helpers.isWebpackDevServer()
4a6995be
C
28}
29
30/*
31 * Webpack configuration
32 *
33 * See: http://webpack.github.io/docs/configuration.html#cli
34 */
2e92c10b 35module.exports = function (options) {
c16ce1de
C
36 const isProd = options.env === 'production'
37 const AOT = isProd
2e92c10b
C
38
39 return {
4a6995be 40
2e92c10b
C
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,
4a6995be 49
2e92c10b
C
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: {
c16ce1de
C
57 'polyfills': './src/polyfills.browser.ts',
58 'main': AOT
59 ? './src/main.browser.aot.ts'
60 : './src/main.browser.ts'
2e92c10b 61 },
b20b5fed 62
4a6995be 63 /*
2e92c10b 64 * Options affecting the resolving of modules.
4a6995be 65 *
2e92c10b 66 * See: http://webpack.github.io/docs/configuration.html#resolve
4a6995be 67 */
2e92c10b
C
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 */
4d19d2f1 74 extensions: [ '.ts', '.js', '.json', '.scss' ],
2e92c10b 75
c16ce1de 76 modules: [ helpers.root('src'), helpers.root('node_modules') ],
e903c2f3
C
77
78 alias: {
79 'video.js': 'video.js/dist/alt/video.novtt'
80 }
2e92c10b 81 },
4a6995be
C
82
83 /*
2e92c10b 84 * Options affecting the normal modules.
4a6995be 85 *
2e92c10b 86 * See: http://webpack.github.io/docs/configuration.html#module
4a6995be 87 */
2e92c10b 88 module: {
4a6995be 89
4d19d2f1 90 rules: [
4a6995be 91
2e92c10b
C
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$/,
c16ce1de 99 use: [
2e92c10b 100 '@angularclass/hmr-loader?pretty=' + !isProd + '&prod=' + isProd,
c16ce1de
C
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 }
2e92c10b
C
111 ],
112 exclude: [/\.(spec|e2e)\.ts$/]
113 },
4a6995be 114
2e92c10b
C
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 },
4a6995be 124
2e92c10b 125 {
ad22074a 126 test: /\.(sass|scss)$/,
c16ce1de
C
127 use: ['css-to-string-loader', 'css-loader?sourceMap', 'resolve-url-loader', 'sass-loader?sourceMap'],
128 exclude: [ helpers.root('src', 'styles') ]
2e92c10b 129 },
c16ce1de
C
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' },
4a6995be 132
2e92c10b
C
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',
3bb2c7f9 141 exclude: [ helpers.root('src/index.html'), helpers.root('src/standalone/videos/embed.html') ]
2e92c10b 142 }
4a6995be 143
2e92c10b
C
144 ]
145
146 },
147
4a6995be 148 /*
2e92c10b 149 * Add additional plugins to the compiler.
4a6995be 150 *
2e92c10b 151 * See: http://webpack.github.io/docs/configuration.html#plugins
4a6995be 152 */
2e92c10b
C
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 */
c16ce1de 166 new CheckerPlugin(),
2e92c10b
C
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 */
c16ce1de
C
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()
2e92c10b
C
191 }),
192
d268c551
C
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
4d19d2f1 199 */
d268c551
C
200 new ContextReplacementPlugin(
201 // The (\\|\/) piece accounts for path separators in *nix and Windows
c16ce1de
C
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 }
d268c551
C
207 ),
208
2e92c10b
C
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 */
a8644408 217 // Used by embed.html
2e92c10b
C
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'
3bb2c7f9
C
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'
2e92c10b
C
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',
4d19d2f1
C
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({
3bb2c7f9 272 sync: [ 'webtorrent.min.js' ],
4d19d2f1 273 defaultAttribute: 'defer'
2e92c10b
C
274 }),
275
4d19d2f1
C
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 }
c16ce1de
C
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')
b99290b1
C
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'
4d19d2f1 341 })
2e92c10b 342 ],
4a6995be
C
343
344 /*
2e92c10b
C
345 * Include polyfills or mocks for various node stuff
346 * Description: Node configuration
4a6995be 347 *
2e92c10b 348 * See: https://webpack.github.io/docs/configuration.html#node
4a6995be 349 */
2e92c10b 350 node: {
cc3e2d9b 351 global: true,
2e92c10b 352 crypto: 'empty',
4d19d2f1 353 process: true,
2e92c10b
C
354 module: false,
355 clearImmediate: false,
c16ce1de
C
356 setImmediate: false,
357 setInterval: false,
358 setTimeout: false
2e92c10b 359 }
4a6995be 360 }
4a6995be 361}