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