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