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