Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var webpack = require("webpack");
- var path = require("path");
- var clone = require("js.clone");
- var merge = require("webpack-merge");
- var MiniCssExtractPlugin = require("mini-css-extract-plugin");
- var ContextReplacementPlugin = require("webpack/lib/ContextReplacementPlugin");
- var CopyWebpackPlugin = require("copy-webpack-plugin");
- var AngularCompilerPlugin = require('@ngtools/webpack').AngularCompilerPlugin;
- var BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
- var OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin');
- var FilterWarningsPlugin = require('webpack-filter-warnings-plugin');
- var TerserPlugin = require('terser-webpack-plugin');
- var Terser = require("terser");
- var fs = require("fs");
- var csswring = require("csswring");
- var isDevBuild = process.env.ASPNETCORE_ENVIRONMENT === "Production" ? false : true;
- var isAotBuild = false;
- var isStatistics = false;
- var isDebug = false;
- module.exports = (env) => {
- if (env && env.prod) isDevBuild = false;
- if (env && env.aot) isAotBuild = true;
- if (env && env.analyze) isStatistics = true;
- if (isDebug) {
- console.log("Webpack entrypoint Main_client Env Development: " + isDevBuild);
- if (isAotBuild) console.log("****AoT Build Enabled****");
- if (isStatistics) console.log("****Webpack Statistics Enabled****");
- }
- var distDirectory = "./wwwroot/dist";
- var defaultLoaders = [
- { loader: "cache-loader" },
- { loader: "ts-loader", options: { transpileOnly: true, experimentalWatchApi: true } },
- { loader: "angular2-template-loader" },
- { loader: "angular-router-loader" }
- ];
- var FilterWarningPlugin = new FilterWarningsPlugin({ exclude: /System.import/ });
- var AnalysisPlugin = new BundleAnalyzerPlugin();
- var newTerserPlugin = new TerserPlugin({ cache: true, parallel: true, sourceMap: isDevBuild });
- var cssProcessPlugin = new OptimizeCssAssetsPlugin({ cssProcessorOptions: { discardComments: { removeAll: true } }, canPrint: false });
- var defineNODE_ENV_ProductionPlugin = new webpack.DefinePlugin({ "process.env.NODE_ENV": JSON.stringify("production") });
- var MiniCssPlugin = new MiniCssExtractPlugin({ filename: "initial.css" });
- var MomentOnlyEnItPlugin = new webpack.ContextReplacementPlugin(/moment[\/\\]locale$/, /it|en-gb/);
- var ProvidePlugin = new webpack.ProvidePlugin({
- jQuery: "jquery",
- $: "jquery",
- jquery: "jquery",
- 'window.jQuery': "jquery",
- moment: "moment"
- });
- var copyPlugin = new CopyWebpackPlugin([{
- from: "Client/assets", to: "assets", transform: (content, path) => {
- let cssToMinify = /[^\.][^m][^i][^n]\.css$/gi;
- let jsToMinify = /[^\.][^m][^i][^n]\.js$/gi;
- if (cssToMinify.test(path)) {
- if (isDebug) console.log("Minifying CSS asset: " + path);
- let res = csswring.wring(fs.readFileSync(path, "utf8")).css;
- return res;
- }
- else if (jsToMinify.test(path)) {
- if (isDebug) console.log("Minifying JS asset: " + path);
- let res = Terser.minify(content.toString());
- return res.code;
- }
- else
- return content;
- }
- }]);
- //*****************************************************END Plugins Declaration
- const vendor_treeShakableModules = [
- '@angular/animations',
- '@angular/common',
- '@angular/cdk',
- '@angular/material',
- '@angular/compiler',
- '@angular/core',
- '@angular/forms',
- '@angular/http',
- '@angular/platform-browser',
- '@angular/platform-browser-dynamic',
- '@angular/router',
- 'ngx-bootstrap',
- 'zone.js'
- ];
- const vendor_nonTreeShakableModules = [
- 'core-js',
- 'es6-promise',
- 'jquery'
- ];
- let vendorModules = isDevBuild ? vendor_treeShakableModules.concat(vendor_nonTreeShakableModules) : vendor_nonTreeShakableModules;
- var SHARED_CONFIG = {
- resolve: { extensions: [".js", ".ts", ".css", ".scss", ".json"] },
- stats: { modules: false, colors: true },
- mode: 'none',
- cache: isDevBuild,
- devtool: isDevBuild ? 'eval-source-map' : '',
- performance: { hints: isDevBuild ? false : 'warning' },
- optimization: {
- minimizer: [
- newTerserPlugin,
- cssProcessPlugin
- ],
- minimize: !isDevBuild,
- namedModules: isDevBuild,
- namedChunks: isDevBuild,
- nodeEnv: isDevBuild ? 'development' : 'production',
- flagIncludedChunks: !isDevBuild,
- occurrenceOrder: !isDevBuild,
- sideEffects: !isDevBuild,
- usedExports: !isDevBuild,
- concatenateModules: false,
- noEmitOnErrors: !isDevBuild,
- checkWasmTypes: !isDevBuild,
- splitChunks: {
- cacheGroups: {
- vendor: {
- test(module, chunks) {
- for (let i = 0; i < vendorModules.length; i++) {
- let t = vendorModules[i];
- if (module.resource && (module.resource.indexOf(t.replace("\\", "/")) !== -1 || module.resource.indexOf(t.replace("/", "\\")) !== -1) ) {
- if (isDebug) console.log("Adding Module to Vendor: " + module.resource);
- return true;
- }
- }
- return false;
- },
- name: "vendor",
- chunks: "all",
- enforce: true
- }
- },
- hidePathInfo: !isDevBuild,
- minSize: isDevBuild ? 10000 : 30000,
- maxAsyncRequests: isDevBuild ? Infinity : 5,
- maxInitialRequests: isDevBuild ? Infinity : 3
- }
- },
- context: __dirname,
- output: { filename: "[name].js", publicPath: "/dist/", pathinfo: isDevBuild },
- module: {
- exprContextCritical: false,
- rules: [
- { test: /(?:\.ngfactory\.js|\.ngstyle\.js|\.ts)$/, use: isAotBuild ? [{ loader: '@ngtools/webpack' }] : defaultLoaders },
- { test: /\.html$/, use: { loader: "html-loader", options: { attrs: false } } },
- { test: /\.css/, use: "raw-loader" },
- { test: /\.scss$/, use: ["raw-loader", "sass-loader"] },
- { test: /initial\.scss$/, use: [MiniCssExtractPlugin.loader, 'css-loader', "sass-loader"] },
- { test: /bootstrap\/dist\/js\/umd\//, use: "imports?jQuery=jquery" },
- { test: /\.woff(2)?(\?v=.+)?$/, use: "url-loader?limit=10000&mimetype=application/font-woff&name=fonts/[name].[ext]" },
- { test: /\.(ttf|eot|svg)(\?v=.+)?$/, use: "file-loader?&name=fonts/[name].[ext]" }
- ]
- },
- plugins: [FilterWarningPlugin, MiniCssPlugin, ProvidePlugin, copyPlugin, MomentOnlyEnItPlugin]
- .concat(isDevBuild ? [] : [defineNODE_ENV_ProductionPlugin])
- .concat(isStatistics ? [AnalysisPlugin] : [])
- };
- var AoTPlugin = new AngularCompilerPlugin({
- tsConfigPath: './tsconfig.json',
- entryModule: path.join(__dirname, 'Client/app/platform-modules/app.browser.module#AppBrowserModule'),
- sourceMap: isDevBuild
- });
- var contextReplacementPlugin = new ContextReplacementPlugin(
- /angular(\\|\/)core(\\|\/)(esm(\\|\/)src|src)(\\|\/)linker/,
- root("./Client")
- );
- var CLIENT_CONFIG = setTypeScriptAlias(require("./tsconfig.json"), {
- entry: { 'main-client': "./Client/bootstrap-client.ts" },
- output: { path: path.join(__dirname, distDirectory), filename: "[name].js", publicPath: "/dist/" },
- plugins: isAotBuild ? [AoTPlugin] : [contextReplacementPlugin]
- });
- return merge(SHARED_CONFIG, CLIENT_CONFIG);
- };
- function setTypeScriptAlias(tsConfig, config) {
- var newConfig = clone(config);
- newConfig = newConfig || {};
- newConfig.resolve = newConfig.resolve || {};
- newConfig.resolve.alias = newConfig.resolve.alias || {};
- var tsPaths = tsConfig.compilerOptions.paths;
- for (var prop in tsPaths)
- newConfig.resolve.alias[prop] = root(tsPaths[prop][0]);
- return newConfig;
- }
- function root() {
- args = Array.prototype.slice.call(arguments, 0);
- return path.join.apply(path, [__dirname].concat(args));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement