]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - client/config/webpack.common.js
Client: Fix Javascript OOM error during webpack compilation (#89)
[github/Chocobozzz/PeerTube.git] / client / config / webpack.common.js
index 6067d94e7757c6bad07f0f7bb67a7eaecb1852ea..7392c58d395ae026cef5ab756707621688c46517 100644 (file)
@@ -8,7 +8,6 @@ const AssetsPlugin = require('assets-webpack-plugin')
 const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin
 const ContextReplacementPlugin = require('webpack/lib/ContextReplacementPlugin')
 const CommonsChunkPlugin = require('webpack/lib/optimize/CommonsChunkPlugin')
-const CopyWebpackPlugin = require('copy-webpack-plugin')
 const CheckerPlugin = require('awesome-typescript-loader').CheckerPlugin
 const HtmlWebpackPlugin = require('html-webpack-plugin')
 const LoaderOptionsPlugin = require('webpack/lib/LoaderOptionsPlugin')
@@ -17,13 +16,14 @@ const ngcWebpack = require('ngc-webpack')
 
 const WebpackNotifierPlugin = require('webpack-notifier')
 
-/*
- * Webpack Constants
- */
+const HMR = helpers.hasProcessFlag('hot')
+const AOT = process.env.BUILD_AOT || helpers.hasNpmFlag('aot')
 const METADATA = {
   title: 'PeerTube',
   baseUrl: '/',
-  isDevServer: helpers.isWebpackDevServer()
+  isDevServer: helpers.isWebpackDevServer(),
+  HMR: HMR,
+  AOT: AOT
 }
 
 /*
@@ -85,23 +85,30 @@ module.exports = function (options) {
       rules: [
 
         /*
-         * Typescript loader support for .ts and Angular async routes via .async.ts
+         * Typescript loader support for .ts and Angular async routes via .async.ts
          *
          * See: https://github.com/s-panferov/awesome-typescript-loader
          */
         {
           test: /\.ts$/,
           use: [
-            '@angularclass/hmr-loader?pretty=' + !isProd + '&prod=' + isProd,
-            'awesome-typescript-loader?{configFileName: "tsconfig.webpack.json"}',
-            'angular2-template-loader',
             {
               loader: 'ng-router-loader',
               options: {
-                loader: 'async-system',
+                loader: 'async-import',
                 genDir: 'compiled',
                 aot: AOT
               }
+            },
+            {
+              loader: 'awesome-typescript-loader',
+              options: {
+                configFileName: 'tsconfig.webpack.json',
+                useCache: !isProd
+              }
+            },
+            {
+              loader: 'angular2-template-loader'
             }
           ],
           exclude: [/\.(spec|e2e)\.ts$/]
@@ -114,14 +121,20 @@ module.exports = function (options) {
          */
         {
           test: /\.json$/,
-          loader: 'json-loader'
+          use: 'json-loader'
         },
 
         {
           test: /\.(sass|scss)$/,
           use: [
             'css-to-string-loader',
-            'css-loader?sourceMap',
+            {
+              loader: 'css-loader',
+              options: {
+                sourceMap: true,
+                importLoaders: 1
+              }
+            },
             'resolve-url-loader',
             {
               loader: 'sass-loader',
@@ -149,11 +162,18 @@ module.exports = function (options) {
          */
         {
           test: /\.html$/,
-          loader: 'raw-loader',
+          use: 'raw-loader',
           exclude: [
             helpers.root('src/index.html'),
             helpers.root('src/standalone/videos/embed.html')
           ]
+        },
+
+        /* File loader for supporting images, for example, in CSS files.
+         */
+        {
+          test: /\.(jpg|png|gif)$/,
+          use: 'url-loader'
         }
 
       ]
@@ -213,37 +233,31 @@ module.exports = function (options) {
        * See: https://github.com/angular/angular/issues/11580
        */
       new ContextReplacementPlugin(
-        // The (\\|\/) piece accounts for path separators in *nix and Windows
-        /angular(\\|\/)core(\\|\/)src(\\|\/)linker/,
+        /**
+         * The (\\|\/) piece accounts for path separators in *nix and Windows
+         */
+        /(.+)?angular(\\|\/)core(.+)?/,
         helpers.root('src'), // location of your src
         {
-          // your Angular Async Route paths relative to this root directory
+          /**
+           * Your Angular Async Route paths relative to this root directory
+           */
         }
       ),
 
       /*
-       * Plugin: CopyWebpackPlugin
-       * Description: Copy files and directories in webpack.
+       * Plugin: ScriptExtHtmlWebpackPlugin
+       * Description: Enhances html-webpack-plugin functionality
+       * with different deployment options for your scripts including:
        *
-       * Copies project static assets.
-       *
-       * See: https://www.npmjs.com/package/copy-webpack-plugin
+       * See: https://github.com/numical/script-ext-html-webpack-plugin
        */
-      // Used by embed.html
-      new CopyWebpackPlugin([
-        {
-          from: 'src/assets',
-          to: 'assets'
-        },
-        {
-          from: 'node_modules/webtorrent/webtorrent.min.js',
-          to: 'assets/webtorrent'
-        },
-        {
-          from: 'src/standalone',
-          to: 'standalone'
-        }
-      ]),
+      new ScriptExtHtmlWebpackPlugin({
+        sync: [ /polyfill|vendor/ ],
+        defaultAttribute: 'async',
+        preload: [/polyfill|vendor|main/],
+        prefetch: [/chunk/]
+      }),
 
       /*
        * Plugin: HtmlWebpackPlugin
@@ -256,20 +270,12 @@ module.exports = function (options) {
       new HtmlWebpackPlugin({
         template: 'src/index.html',
         title: METADATA.title,
-        chunksSortMode: 'dependency',
-        metadata: METADATA
-      }),
-
-      /*
-      * Plugin: ScriptExtHtmlWebpackPlugin
-      * Description: Enhances html-webpack-plugin functionality
-      * with different deployment options for your scripts including:
-      *
-      * See: https://github.com/numical/script-ext-html-webpack-plugin
-      */
-      new ScriptExtHtmlWebpackPlugin({
-        sync: [ 'webtorrent.min.js' ],
-        defaultAttribute: 'defer'
+        chunksSortMode: function (a, b) {
+          const entryPoints = [ 'inline', 'polyfills', 'sw-register', 'styles', 'vendor', 'main' ]
+          return entryPoints.indexOf(a.names[0]) - entryPoints.indexOf(b.names[0])
+        },
+        metadata: METADATA,
+        inject: 'body'
       }),
 
       new WebpackNotifierPlugin({ alwaysNotify: true }),
@@ -290,8 +296,7 @@ module.exports = function (options) {
 
       new ngcWebpack.NgcWebpackPlugin({
         disabled: !AOT,
-        tsConfig: helpers.root('tsconfig.webpack.json'),
-        resourceOverride: helpers.root('config/resource-override.js')
+        tsConfig: helpers.root('tsconfig.webpack.json')
       }),
 
       new BundleAnalyzerPlugin({