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