From a23519a761beacbdbb21cfa8dde3a77989607428 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sat, 2 Jun 2018 11:51:08 +0200 Subject: [PATCH] Minor improvements to logging and webpack. --- .../Log/ConsoleLogChannel.cs | 21 +++++++--- .../Log/Internal/AnsiLogConsole.cs | 4 ++ .../Log/Internal/ConsoleLogProcessor.cs | 4 ++ .../Log/Internal/IConsole.cs | 2 + .../Log/Internal/WindowsLogConsole.cs | 5 +++ src/Squidex/Config/Domain/LoggingServices.cs | 7 ++++ src/Squidex/app-config/karma.coverage.conf.js | 2 +- src/Squidex/app-config/webpack.config.js | 27 +++++++++---- src/Squidex/app-config/webpack.run.base.js | 24 +++++------- src/Squidex/app-config/webpack.run.dev.js | 22 +++-------- src/Squidex/app-config/webpack.run.prod.js | 36 +++++++++--------- .../app-config/webpack.test.coverage.js | 38 +++++++++++++++++++ src/Squidex/appsettings.json | 6 ++- src/Squidex/package.json | 1 + 14 files changed, 135 insertions(+), 64 deletions(-) create mode 100644 src/Squidex/app-config/webpack.test.coverage.js diff --git a/src/Squidex.Infrastructure/Log/ConsoleLogChannel.cs b/src/Squidex.Infrastructure/Log/ConsoleLogChannel.cs index 77ba0448e..bfc924469 100644 --- a/src/Squidex.Infrastructure/Log/ConsoleLogChannel.cs +++ b/src/Squidex.Infrastructure/Log/ConsoleLogChannel.cs @@ -13,6 +13,12 @@ namespace Squidex.Infrastructure.Log public sealed class ConsoleLogChannel : ILogChannel, IDisposable { private readonly ConsoleLogProcessor processor = new ConsoleLogProcessor(); + private readonly bool useColors; + + public ConsoleLogChannel(bool useColors = false) + { + this.useColors = useColors; + } public void Dispose() { @@ -23,13 +29,16 @@ namespace Squidex.Infrastructure.Log { var color = 0; - if (logLevel == SemanticLogLevel.Warning) - { - color = 0xffff00; - } - else if (logLevel >= SemanticLogLevel.Error) + if (useColors) { - color = 0xff0000; + if (logLevel == SemanticLogLevel.Warning) + { + color = 0xffff00; + } + else if (logLevel >= SemanticLogLevel.Error) + { + color = 0xff0000; + } } processor.EnqueueMessage(new LogMessageEntry { Message = message, Color = color }); diff --git a/src/Squidex.Infrastructure/Log/Internal/AnsiLogConsole.cs b/src/Squidex.Infrastructure/Log/Internal/AnsiLogConsole.cs index fffabfcd4..cb8ec5acc 100644 --- a/src/Squidex.Infrastructure/Log/Internal/AnsiLogConsole.cs +++ b/src/Squidex.Infrastructure/Log/Internal/AnsiLogConsole.cs @@ -18,6 +18,10 @@ namespace Squidex.Infrastructure.Log.Internal this.logToStdError = logToStdError; } + public void Reset() + { + } + public void WriteLine(int color, string message) { if (color != 0 && logToStdError) diff --git a/src/Squidex.Infrastructure/Log/Internal/ConsoleLogProcessor.cs b/src/Squidex.Infrastructure/Log/Internal/ConsoleLogProcessor.cs index b536f509f..06c76e40e 100644 --- a/src/Squidex.Infrastructure/Log/Internal/ConsoleLogProcessor.cs +++ b/src/Squidex.Infrastructure/Log/Internal/ConsoleLogProcessor.cs @@ -86,6 +86,10 @@ namespace Squidex.Infrastructure.Log.Internal { Debug.WriteLine($"Failed to shutdown log queue grateful: {ex}."); } + finally + { + console.Reset(); + } } } } diff --git a/src/Squidex.Infrastructure/Log/Internal/IConsole.cs b/src/Squidex.Infrastructure/Log/Internal/IConsole.cs index d5c45b274..c996fe108 100644 --- a/src/Squidex.Infrastructure/Log/Internal/IConsole.cs +++ b/src/Squidex.Infrastructure/Log/Internal/IConsole.cs @@ -10,5 +10,7 @@ namespace Squidex.Infrastructure.Log.Internal public interface IConsole { void WriteLine(int color, string message); + + void Reset(); } } diff --git a/src/Squidex.Infrastructure/Log/Internal/WindowsLogConsole.cs b/src/Squidex.Infrastructure/Log/Internal/WindowsLogConsole.cs index 7275761e2..3a0b136f6 100644 --- a/src/Squidex.Infrastructure/Log/Internal/WindowsLogConsole.cs +++ b/src/Squidex.Infrastructure/Log/Internal/WindowsLogConsole.cs @@ -18,6 +18,11 @@ namespace Squidex.Infrastructure.Log.Internal this.logToStdError = logToStdError; } + public void Reset() + { + Console.ResetColor(); + } + public void WriteLine(int color, string message) { if (color != 0) diff --git a/src/Squidex/Config/Domain/LoggingServices.cs b/src/Squidex/Config/Domain/LoggingServices.cs index e4ee51850..486c29dff 100644 --- a/src/Squidex/Config/Domain/LoggingServices.cs +++ b/src/Squidex/Config/Domain/LoggingServices.cs @@ -42,6 +42,13 @@ namespace Squidex.Config.Domain .As(); } + var useColors = config.GetValue("logging:colors"); + + if (console == null) + { + console = new ConsoleLogChannel(useColors); + } + services.AddSingletonAs(console) .As(); diff --git a/src/Squidex/app-config/karma.coverage.conf.js b/src/Squidex/app-config/karma.coverage.conf.js index 3c68d229e..6ee5490b3 100644 --- a/src/Squidex/app-config/karma.coverage.conf.js +++ b/src/Squidex/app-config/karma.coverage.conf.js @@ -1,4 +1,4 @@ -var webpackConfig = require('./webpack.test'); +var webpackConfig = require('./webpack.test.coverage'); module.exports = function (config) { var _config = { diff --git a/src/Squidex/app-config/webpack.config.js b/src/Squidex/app-config/webpack.config.js index 43d3b0ac6..8497259b1 100644 --- a/src/Squidex/app-config/webpack.config.js +++ b/src/Squidex/app-config/webpack.config.js @@ -1,9 +1,13 @@ -var webpack = require('webpack'), - path = require('path'), - HtmlWebpackPlugin = require('html-webpack-plugin'), - MiniCssExtractPlugin = require('mini-css-extract-plugin'), - TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin'), - helpers = require('./helpers'); +const webpack = require('webpack'), + path = require('path'), + helpers = require('./helpers'); + +const plugins = { + // https://github.com/webpack-contrib/mini-css-extract-plugin + MiniCssExtractPlugin: require('mini-css-extract-plugin'), + // https://github.com/dividab/tsconfig-paths-webpack-plugin + TsconfigPathsPlugin: require('tsconfig-paths-webpack-plugin') +}; module.exports = { /** @@ -25,7 +29,7 @@ module.exports = { ], plugins: [ - new TsconfigPathsPlugin() + new plugins.TsconfigPathsPlugin() ] }, @@ -83,7 +87,7 @@ module.exports = { }, { test: /\.css$/, use: [ - MiniCssExtractPlugin.loader, + plugins.MiniCssExtractPlugin.loader, { loader: 'css-loader' }] @@ -100,6 +104,13 @@ module.exports = { }, plugins: [ + /* + * Puts each bundle into a file and appends the hash of the file to the path. + * + * See: https://github.com/webpack-contrib/mini-css-extract-plugin + */ + new plugins.MiniCssExtractPlugin('[name].css'), + new webpack.LoaderOptionsPlugin({ options: { tslint: { diff --git a/src/Squidex/app-config/webpack.run.base.js b/src/Squidex/app-config/webpack.run.base.js index 6e02fbf5b..094710da3 100644 --- a/src/Squidex/app-config/webpack.run.base.js +++ b/src/Squidex/app-config/webpack.run.base.js @@ -1,8 +1,11 @@ - var webpack = require('webpack'), - webpackMerge = require('webpack-merge'), -HtmlWebpackPlugin = require('html-webpack-plugin'), - commonConfig = require('./webpack.config.js'), - helpers = require('./helpers'); +const webpack = require('webpack'), + webpackMerge = require('webpack-merge'), + commonConfig = require('./webpack.config.js'), + helpers = require('./helpers'); + +const plugins = { + HtmlWebpackPlugin: require('html-webpack-plugin') +}; module.exports = webpackMerge(commonConfig, { /** @@ -17,21 +20,14 @@ module.exports = webpackMerge(commonConfig, { }, plugins: [ - /** - * Simplifies creation of HTML files to serve your webpack bundles. - * This is especially useful for webpack bundles that include a hash in the filename - * which changes every compilation. - * - * See: https://github.com/ampedandwired/html-webpack-plugin - */ - new HtmlWebpackPlugin({ + new plugins.HtmlWebpackPlugin({ hash: true, chunks: ['shims', 'app'], chunksSortMode: 'manual', template: 'wwwroot/index.html' }), - new HtmlWebpackPlugin({ + new plugins.HtmlWebpackPlugin({ template: 'wwwroot/theme.html', hash: true, chunksSortMode: 'none', filename: 'theme.html' }) ] diff --git a/src/Squidex/app-config/webpack.run.dev.js b/src/Squidex/app-config/webpack.run.dev.js index e1bbae6de..f969dfe84 100644 --- a/src/Squidex/app-config/webpack.run.dev.js +++ b/src/Squidex/app-config/webpack.run.dev.js @@ -1,13 +1,13 @@ - var webpackMerge = require('webpack-merge'), -MiniCssExtractPlugin = require('mini-css-extract-plugin'), - runConfig = require('./webpack.run.base.js'), - helpers = require('./helpers'); +const webpackMerge = require('webpack-merge'), + runConfig = require('./webpack.run.base.js'), + helpers = require('./helpers'); module.exports = webpackMerge(runConfig, { mode: 'development', output: { filename: '[name].js', + // Set the public path, because we are running the website from another port (5000) publicPath: 'http://localhost:3000/' }, @@ -37,20 +37,10 @@ module.exports = webpackMerge(runConfig, { } ] }, - - plugins: [ - /* - * Puts each bundle into a file and appends the hash of the file to the path. - * - * See: https://github.com/webpack-contrib/mini-css-extract-plugin - */ - new MiniCssExtractPlugin('[name].css'), - ], - devServer: { - historyApiFallback: true, stats: 'minimal', headers: { 'Access-Control-Allow-Origin': '*' - } + }, + historyApiFallback: true } }); \ No newline at end of file diff --git a/src/Squidex/app-config/webpack.run.prod.js b/src/Squidex/app-config/webpack.run.prod.js index 141282e52..44b2dbc38 100644 --- a/src/Squidex/app-config/webpack.run.prod.js +++ b/src/Squidex/app-config/webpack.run.prod.js @@ -1,10 +1,15 @@ - var webpack = require('webpack'), - webpackMerge = require('webpack-merge'), -MiniCssExtractPlugin = require('mini-css-extract-plugin'), - ngToolsWebpack = require('@ngtools/webpack'), - runConfig = require('./webpack.run.base.js'), - helpers = require('./helpers'); +const webpack = require('webpack'), + webpackMerge = require('webpack-merge'), + runConfig = require('./webpack.run.base.js'), + helpers = require('./helpers'); +const plugins = { + // https://www.npmjs.com/package/@ngtools/webpack + NgToolsWebpack: require('@ngtools/webpack'), + // https://github.com/webpack-contrib/mini-css-extract-plugin + MiniCssExtractPlugin: require('mini-css-extract-plugin'), +}; + helpers.removeLoaders(runConfig, ['scss', 'ts']); module.exports = webpackMerge(runConfig, { @@ -57,9 +62,9 @@ module.exports = webpackMerge(runConfig, { * See: https://github.com/webpack-contrib/extract-text-webpack-plugin */ use: [ - MiniCssExtractPlugin.loader, + plugins.MiniCssExtractPlugin.loader, { - loader: 'css-loader' + loader: 'css-loader', options: { minimize: true }, }, { loader: 'sass-loader' }], @@ -85,16 +90,11 @@ module.exports = webpackMerge(runConfig, { }, plugins: [ - /* - * Puts each bundle into a file and appends the hash of the file to the path. - * - * See: https://github.com/webpack-contrib/mini-css-extract-plugin - */ - new MiniCssExtractPlugin('[name].css'), - - new ngToolsWebpack.AngularCompilerPlugin({ - tsConfigPath: './tsconfig.json', - entryModule: 'app/app.module#AppModule' + new plugins.NgToolsWebpack.AngularCompilerPlugin({ + entryModule: 'app/app.module#AppModule', + sourceMap: false, + skipSourceGeneration: false, + tsConfigPath: './tsconfig.json' }) ] }); \ No newline at end of file diff --git a/src/Squidex/app-config/webpack.test.coverage.js b/src/Squidex/app-config/webpack.test.coverage.js new file mode 100644 index 000000000..78124c65e --- /dev/null +++ b/src/Squidex/app-config/webpack.test.coverage.js @@ -0,0 +1,38 @@ +var webpackMerge = require('webpack-merge'), + path = require('path'), + helpers = require('./helpers'), + testConfig = require('./webpack.test.js'); + +helpers.removeLoaders(testConfig, ['ts']); + +module.exports = webpackMerge(testConfig, { + module: { + /** + * An array of Rules which are matched to requests when modules are created. + * + * See: https://webpack.js.org/configuration/module/#module-rules + */ + rules: [ + { + test: /\.ts$/, + use: [{ + loader: 'awesome-typescript-loader' + }], + include: [/\.(e2e|spec)\.ts$/], + + }, { + test: /\.ts$/, + use: [{ + loader: 'istanbul-instrumenter-loader' + },{ + loader: 'awesome-typescript-loader' + }, { + loader: 'angular2-router-loader' + }, { + loader: 'angular2-template-loader' + }], + exclude: [/\.(e2e|spec)\.ts$/] + } + ] + } +}); \ No newline at end of file diff --git a/src/Squidex/appsettings.json b/src/Squidex/appsettings.json index b72f5ec5c..b23f0e36c 100644 --- a/src/Squidex/appsettings.json +++ b/src/Squidex/appsettings.json @@ -53,7 +53,11 @@ /* * Setting the flag to true, enables well formatteds json logs. */ - "human": true + "human": true, + /* + * Set to true, to use colors. + */ + "colors": true }, "assetStore": { diff --git a/src/Squidex/package.json b/src/Squidex/package.json index 395f6975d..b57c27fc2 100644 --- a/src/Squidex/package.json +++ b/src/Squidex/package.json @@ -9,6 +9,7 @@ "test:coverage": "karma start karma.coverage.conf.js", "test:clean": "rimraf _test-output", "dev": "cpx node_modules/oidc-client/dist/oidc-client.min.js wwwroot/scripts/ && webpack-dev-server --config app-config/webpack.run.dev.js --inline --port 3000", + "start": "cpx node_modules/oidc-client/dist/oidc-client.min.js wwwroot/scripts/ && webpack-dev-server --config app-config/webpack.run.dev.js --inline --port 3000", "build": "webpack --config app-config/webpack.run.prod.js", "build:copy": "cpx node_modules/oidc-client/dist/oidc-client.min.js wwwroot/scripts/", "build:clean": "rimraf wwwroot/build",