]>
Commit | Line | Data |
---|---|---|
1 | const helpers = require('./helpers') | |
2 | ||
3 | /* | |
4 | * Webpack Plugins | |
5 | */ | |
6 | ||
7 | const AssetsPlugin = require('assets-webpack-plugin') | |
8 | const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin | |
9 | const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin') | |
10 | const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin') | |
11 | const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin | |
12 | const HtmlWebpackPlugin = require('html-webpack-plugin') | |
13 | const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin') | |
14 | const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin') | |
15 | const ngcWebpack = require('ngc-webpack') | |
16 | ||
17 | const WebpackNotifierPlugin = require('webpack-notifier') | |
18 | ||
19 | /* | |
20 | * Webpack Constants | |
21 | */ | |
22 | const METADATA = { | |
23 | title: 'PeerTube', | |
24 | baseUrl: '/', | |
25 | isDevServer: helpers.isWebpackDevServer() | |
26 | } | |
27 | ||
28 | /* | |
29 | * Webpack configuration | |
30 | * | |
31 | * See: http://webpack.github.io/docs/configuration.html#cli | |
32 | */ | |
33 | module.exports = function (options) { | |
34 | const isProd = options.env === 'production' | |
35 | const AOT = isProd | |
36 | ||
37 | return { | |
38 | ||
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, | |
47 | ||
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: { | |
55 | 'polyfills': './src/polyfills.browser.ts', | |
56 | 'main': AOT | |
57 | ? './src/main.browser.aot.ts' | |
58 | : './src/main.browser.ts' | |
59 | }, | |
60 | ||
61 | /* | |
62 | * Options affecting the resolving of modules. | |
63 | * | |
64 | * See: http://webpack.github.io/docs/configuration.html#resolve | |
65 | */ | |
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 | */ | |
72 | extensions: [ '.ts', '.js', '.json', '.scss' ], | |
73 | ||
74 | modules: [ helpers.root('src'), helpers.root('node_modules') ] | |
75 | }, | |
76 | ||
77 | /* | |
78 | * Options affecting the normal modules. | |
79 | * | |
80 | * See: http://webpack.github.io/docs/configuration.html#module | |
81 | */ | |
82 | module: { | |
83 | ||
84 | rules: [ | |
85 | ||
86 | /* | |
87 | * Typescript loader support for .ts and Angular async routes via .async.ts | |
88 | * | |
89 | * See: https://github.com/s-panferov/awesome-typescript-loader | |
90 | */ | |
91 | { | |
92 | test: /\.ts$/, | |
93 | use: [ | |
94 | { | |
95 | loader: '@angularclass/hmr-loader', | |
96 | options: { | |
97 | pretty: !isProd, | |
98 | prod: isProd | |
99 | } | |
100 | }, | |
101 | { | |
102 | loader: 'ng-router-loader', | |
103 | options: { | |
104 | loader: 'async-import', | |
105 | genDir: 'compiled', | |
106 | aot: AOT | |
107 | } | |
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' | |
118 | } | |
119 | ], | |
120 | exclude: [/\.(spec|e2e)\.ts$/] | |
121 | }, | |
122 | ||
123 | /* | |
124 | * Json loader support for *.json files. | |
125 | * | |
126 | * See: https://github.com/webpack/json-loader | |
127 | */ | |
128 | { | |
129 | test: /\.json$/, | |
130 | use: 'json-loader' | |
131 | }, | |
132 | ||
133 | { | |
134 | test: /\.(sass|scss)$/, | |
135 | use: [ | |
136 | 'css-to-string-loader', | |
137 | { | |
138 | loader: 'css-loader', | |
139 | options: { | |
140 | sourceMap: true, | |
141 | importLoaders: 1 | |
142 | } | |
143 | }, | |
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 | ] | |
160 | }, | |
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' }, | |
163 | ||
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$/, | |
171 | use: 'raw-loader', | |
172 | exclude: [ | |
173 | helpers.root('src/index.html'), | |
174 | helpers.root('src/standalone/videos/embed.html') | |
175 | ] | |
176 | }, | |
177 | ||
178 | /* File loader for supporting images, for example, in CSS files. | |
179 | */ | |
180 | { | |
181 | test: /\.(jpg|png|gif)$/, | |
182 | use: 'url-loader' | |
183 | } | |
184 | ||
185 | ] | |
186 | ||
187 | }, | |
188 | ||
189 | /* | |
190 | * Add additional plugins to the compiler. | |
191 | * | |
192 | * See: http://webpack.github.io/docs/configuration.html#plugins | |
193 | */ | |
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 | */ | |
207 | new CheckerPlugin(), | |
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 | */ | |
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() | |
232 | }), | |
233 | ||
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 | |
240 | */ | |
241 | new ContextReplacementPlugin( | |
242 | /** | |
243 | * The (\\|\/) piece accounts for path separators in *nix and Windows | |
244 | */ | |
245 | /angular(\\|\/)core(\\|\/)@angular/, | |
246 | helpers.root('src'), // location of your src | |
247 | { | |
248 | /** | |
249 | * Your Angular Async Route paths relative to this root directory | |
250 | */ | |
251 | } | |
252 | ), | |
253 | ||
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({ | |
262 | sync: [ /polyfill|vendor/ ], | |
263 | defaultAttribute: 'async', | |
264 | preload: [/polyfill|vendor|main/], | |
265 | prefetch: [/chunk/] | |
266 | }), | |
267 | ||
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', | |
278 | title: METADATA.title, | |
279 | chunksSortMode: 'dependency', | |
280 | metadata: METADATA, | |
281 | inject: 'body' | |
282 | }), | |
283 | ||
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: { | |
294 | precision: 10, | |
295 | includePaths: [ helpers.root('src/sass') ] | |
296 | } | |
297 | } | |
298 | }), | |
299 | ||
300 | new ngcWebpack.NgcWebpackPlugin({ | |
301 | disabled: !AOT, | |
302 | tsConfig: helpers.root('tsconfig.webpack.json') | |
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' | |
327 | }) | |
328 | ], | |
329 | ||
330 | /* | |
331 | * Include polyfills or mocks for various node stuff | |
332 | * Description: Node configuration | |
333 | * | |
334 | * See: https://webpack.github.io/docs/configuration.html#node | |
335 | */ | |
336 | node: { | |
337 | global: true, | |
338 | crypto: 'empty', | |
339 | process: true, | |
340 | module: false, | |
341 | clearImmediate: false, | |
342 | setImmediate: false, | |
343 | setInterval: false, | |
344 | setTimeout: false | |
345 | } | |
346 | } | |
347 | } |