Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var webpack = require("webpack");
  2. var path = require("path");
  3. var clone = require("js.clone");
  4. var merge = require("webpack-merge");
  5. var MiniCssExtractPlugin = require("mini-css-extract-plugin");
  6. var ContextReplacementPlugin = require("webpack/lib/ContextReplacementPlugin");
  7. var CopyWebpackPlugin = require("copy-webpack-plugin");
  8. var AngularCompilerPlugin = require('@ngtools/webpack').AngularCompilerPlugin;
  9. var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
  10. var OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
  11. var FilterWarningsPlugin = require('webpack-filter-warnings-plugin');
  12. var TerserPlugin = require('terser-webpack-plugin');
  13. var Terser = require("terser");
  14. var fs = require("fs");
  15. var csswring = require("csswring");
  16. var isDevBuild = process.env.ASPNETCORE_ENVIRONMENT === "Production" ? false : true;
  17. var isAotBuild = false;
  18. var isStatistics = false;
  19.  
  20. var isDebug = false;
  21.  
  22. module.exports = (env) => {
  23.     if (env && env.prod) isDevBuild = false;
  24.     if (env && env.aot) isAotBuild = true;
  25.     if (env && env.analyze) isStatistics = true;
  26.  
  27.     if (isDebug) {
  28.         console.log("Webpack entrypoint Main_client Env Development: " + isDevBuild);
  29.         if (isAotBuild) console.log("****AoT Build Enabled****");
  30.         if (isStatistics) console.log("****Webpack Statistics Enabled****");
  31.     }
  32.  
  33.     var distDirectory = "./wwwroot/dist";
  34.  
  35.     var defaultLoaders = [
  36.         { loader: "cache-loader" },
  37.         { loader: "ts-loader", options: { transpileOnly: true, experimentalWatchApi: true } },
  38.         { loader: "angular2-template-loader" },
  39.         { loader: "angular-router-loader" }
  40.     ];
  41.  
  42.     var FilterWarningPlugin = new FilterWarningsPlugin({ exclude: /System.import/ });
  43.     var AnalysisPlugin = new BundleAnalyzerPlugin();
  44.     var newTerserPlugin = new TerserPlugin({ cache: true, parallel: true, sourceMap: isDevBuild });
  45.     var cssProcessPlugin = new OptimizeCssAssetsPlugin({ cssProcessorOptions: { discardComments: { removeAll: true } }, canPrint: false });
  46.     var defineNODE_ENV_ProductionPlugin = new webpack.DefinePlugin({ "process.env.NODE_ENV": JSON.stringify("production") });
  47.     var MiniCssPlugin = new MiniCssExtractPlugin({ filename: "initial.css" });
  48.     var MomentOnlyEnItPlugin = new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /it|en-gb/);
  49.  
  50.     var ProvidePlugin = new webpack.ProvidePlugin({
  51.         jQuery: "jquery",
  52.         $: "jquery",
  53.         jquery: "jquery",
  54.         'window.jQuery': "jquery",
  55.         moment: "moment"
  56.     });
  57.    
  58.     var copyPlugin = new CopyWebpackPlugin([{
  59.         from: "Client/assets", to: "assets", transform: (content, path) => {
  60.             let cssToMinify = /[^\.][^m][^i][^n]\.css$/gi;
  61.             let jsToMinify = /[^\.][^m][^i][^n]\.js$/gi;
  62.             if (cssToMinify.test(path)) {
  63.                 if (isDebug) console.log("Minifying CSS asset: " + path);
  64.                 let res = csswring.wring(fs.readFileSync(path, "utf8")).css;
  65.                 return res;
  66.             }
  67.             else if (jsToMinify.test(path)) {
  68.                 if (isDebug) console.log("Minifying JS asset: " + path);
  69.                 let res = Terser.minify(content.toString());
  70.                 return res.code;
  71.             }
  72.             else
  73.                 return content;
  74.         }
  75.     }]);
  76.  
  77.     //*****************************************************END Plugins Declaration
  78.     const vendor_treeShakableModules = [
  79.         '@angular/animations',
  80.         '@angular/common',
  81.         '@angular/cdk',
  82.         '@angular/material',
  83.         '@angular/compiler',
  84.         '@angular/core',
  85.         '@angular/forms',
  86.         '@angular/http',
  87.         '@angular/platform-browser',
  88.         '@angular/platform-browser-dynamic',
  89.         '@angular/router',
  90.         'ngx-bootstrap',
  91.         'zone.js'
  92.     ];
  93.  
  94.     const vendor_nonTreeShakableModules = [
  95.         'core-js',
  96.         'es6-promise',
  97.         'jquery'
  98.     ];
  99.  
  100.     let vendorModules = isDevBuild ? vendor_treeShakableModules.concat(vendor_nonTreeShakableModules) : vendor_nonTreeShakableModules;
  101.  
  102.     var SHARED_CONFIG = {
  103.         resolve: { extensions: [".js", ".ts", ".css", ".scss", ".json"] },
  104.         stats: { modules: false, colors: true },
  105.         mode: 'none',
  106.         cache: isDevBuild,
  107.         devtool: isDevBuild ? 'eval-source-map' : '',
  108.         performance: { hints: isDevBuild ? false : 'warning' },
  109.         optimization: {
  110.             minimizer: [
  111.                 newTerserPlugin,
  112.                 cssProcessPlugin
  113.             ],
  114.             minimize: !isDevBuild,
  115.             namedModules: isDevBuild,
  116.             namedChunks: isDevBuild,
  117.             nodeEnv: isDevBuild ? 'development' : 'production',
  118.             flagIncludedChunks: !isDevBuild,
  119.             occurrenceOrder: !isDevBuild,
  120.             sideEffects: !isDevBuild,
  121.             usedExports: !isDevBuild,
  122.             concatenateModules: false,
  123.             noEmitOnErrors: !isDevBuild,
  124.             checkWasmTypes: !isDevBuild,
  125.             splitChunks: {
  126.                 cacheGroups: {
  127.                     vendor: {
  128.                         test(module, chunks) {
  129.                             for (let i = 0; i < vendorModules.length; i++) {
  130.                                 let t = vendorModules[i];
  131.  
  132.                                 if (module.resource && (module.resource.indexOf(t.replace("\\", "/")) !== -1 || module.resource.indexOf(t.replace("/", "\\")) !== -1) ) {
  133.                                     if (isDebug) console.log("Adding Module to Vendor: " + module.resource);
  134.                                     return true;
  135.                                 }
  136.                             }
  137.  
  138.                             return false;
  139.                         },
  140.                         name: "vendor",
  141.                         chunks: "all",
  142.                         enforce: true
  143.                     }
  144.                 },
  145.                 hidePathInfo: !isDevBuild,
  146.                 minSize: isDevBuild ? 10000 : 30000,
  147.                 maxAsyncRequests: isDevBuild ? Infinity : 5,
  148.                 maxInitialRequests: isDevBuild ? Infinity : 3
  149.             }
  150.         },
  151.         context: __dirname,
  152.         output: { filename: "[name].js", publicPath: "/dist/", pathinfo: isDevBuild },
  153.         module: {
  154.             exprContextCritical: false,
  155.             rules: [
  156.                 { test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/, use: isAotBuild ? [{ loader: '@ngtools/webpack' }] : defaultLoaders },
  157.                 { test: /\.html$/, use: { loader: "html-loader", options: { attrs: false } } },
  158.                 { test: /\.css/, use: "raw-loader" },
  159.                 { test: /\.scss$/, use: ["raw-loader", "sass-loader"] },
  160.                 { test: /initial\.scss$/, use: [MiniCssExtractPlugin.loader, 'css-loader', "sass-loader"] },
  161.                 { test: /bootstrap\/dist\/js\/umd\//, use: "imports?jQuery=jquery" },
  162.                 { test: /\.woff(2)?(\?v=.+)?$/, use: "url-loader?limit=10000&mimetype=application/font-woff&name=fonts/[name].[ext]" },
  163.                 { test: /\.(ttf|eot|svg)(\?v=.+)?$/, use: "file-loader?&name=fonts/[name].[ext]" }
  164.             ]
  165.         },
  166.  
  167.         plugins: [FilterWarningPlugin, MiniCssPlugin, ProvidePlugin, copyPlugin, MomentOnlyEnItPlugin]
  168.             .concat(isDevBuild ? [] : [defineNODE_ENV_ProductionPlugin])
  169.             .concat(isStatistics ? [AnalysisPlugin] : [])
  170.     };
  171.  
  172.     var AoTPlugin = new AngularCompilerPlugin({
  173.         tsConfigPath: './tsconfig.json',
  174.         entryModule: path.join(__dirname, 'Client/app/platform-modules/app.browser.module#AppBrowserModule'),
  175.         sourceMap: isDevBuild
  176.     });
  177.  
  178.     var contextReplacementPlugin = new ContextReplacementPlugin(
  179.         /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
  180.         root("./Client")
  181.     );
  182.  
  183.     var CLIENT_CONFIG = setTypeScriptAlias(require("./tsconfig.json"), {
  184.         entry: { 'main-client': "./Client/bootstrap-client.ts" },
  185.         output: { path: path.join(__dirname, distDirectory), filename: "[name].js", publicPath: "/dist/" },
  186.         plugins: isAotBuild ? [AoTPlugin] : [contextReplacementPlugin]
  187.     });
  188.  
  189.     return merge(SHARED_CONFIG, CLIENT_CONFIG);
  190. };
  191.  
  192. function setTypeScriptAlias(tsConfig, config) {
  193.     var newConfig = clone(config);
  194.     newConfig = newConfig || {};
  195.     newConfig.resolve = newConfig.resolve || {};
  196.     newConfig.resolve.alias = newConfig.resolve.alias || {};
  197.  
  198.     var tsPaths = tsConfig.compilerOptions.paths;
  199.     for (var prop in tsPaths)
  200.         newConfig.resolve.alias[prop] = root(tsPaths[prop][0]);
  201.  
  202.     return newConfig;
  203. }
  204.  
  205. function root() {
  206.     args = Array.prototype.slice.call(arguments, 0);
  207.     return path.join.apply(path, [__dirname].concat(args));
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement