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