From 85410b502121cd9442cbc97ba86373ed1eb503ac Mon Sep 17 00:00:00 2001 From: Sebastian Date: Mon, 14 Nov 2016 00:04:42 +0100 Subject: [PATCH] css clean --- src/Squidex/app-config/clean-css-loader.js | 29 +++++++++++++++++++ src/Squidex/app-config/helpers.js | 18 +++++++++++- src/Squidex/app-config/webpack.run.prod.js | 10 +++++-- .../app-config/webpack.test.coverage.js | 19 ++---------- 4 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 src/Squidex/app-config/clean-css-loader.js diff --git a/src/Squidex/app-config/clean-css-loader.js b/src/Squidex/app-config/clean-css-loader.js new file mode 100644 index 000000000..0b6d8af16 --- /dev/null +++ b/src/Squidex/app-config/clean-css-loader.js @@ -0,0 +1,29 @@ +var loaderUtils = require('loader-utils'), + CleanCSS = require('clean-css'); + +function cleanCss(css) { + this.cacheable(); + + var loader = this; + var callback = this.async(); + + new CleanCSS().minify(css, function (err, minified) { + if (err) { + if (Array.isArray(err) && (err[0] != null)) { + return callback(err[0]); + } else { + return callback(err); + } + } + var warnings; + if (((warnings = minified.warnings) != null ? warnings.length : void 0) !== 0) { + warnings.forEach(function (msg) { + loader.emitWarning(msg.toString()); + }); + } + + return callback(null, minified.styles, minified.sourceMap); + }); +}; + +module.exports = cleanCss; \ No newline at end of file diff --git a/src/Squidex/app-config/helpers.js b/src/Squidex/app-config/helpers.js index 18af40621..fcf0f1160 100644 --- a/src/Squidex/app-config/helpers.js +++ b/src/Squidex/app-config/helpers.js @@ -10,4 +10,20 @@ exports.root = function () { var newArgs = Array.prototype.slice.call(arguments, 0); return path.join.apply(path, [appRoot].concat(newArgs)); -}; \ No newline at end of file +}; + +exports.removeLoaders = function (config, extensions) { + var loaders = config.module.loaders; + + for (var i = 0; i < loaders.length; i += 1) { + var loader = loaders[i]; + + for (var j = 0; j < extensions.length; j += 1) { + if (loader.test.source.indexOf(extensions[j]) >= 0) { + loaders.splice(i, 1); + i--; + break; + } + } + } +} \ 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 cf9a21566..5c65d3884 100644 --- a/src/Squidex/app-config/webpack.run.prod.js +++ b/src/Squidex/app-config/webpack.run.prod.js @@ -4,9 +4,11 @@ ExtractTextPlugin = require('extract-text-webpack-plugin'), runConfig = require('./webpack.run.base.js'), helpers = require('./helpers'); -const ENV = process.env.NODE_ENV = process.env.ENV = 'production'; +var ENV = process.env.NODE_ENV = process.env.ENV = 'production'; -module.exports = webpackMerge.smart(runConfig, { +helpers.removeLoaders(runConfig, ['scss', 'png']); + +module.exports = webpackMerge(runConfig, { devtool: 'source-map', output: { @@ -55,6 +57,10 @@ module.exports = webpackMerge.smart(runConfig, { test: /\.scss$/, include: helpers.root('app', 'theme'), loader: ExtractTextPlugin.extract({ fallbackLoader: 'style', loader: 'css!sass?sourceMap' }) + }, { + test: /\.scss$/, + exclude: helpers.root('app', 'theme'), + loaders: ['raw', helpers.root('app-config', 'clean-css-loader'), 'sass'] }, { test: /\.(png|jpe?g|gif|svg|ico)(\?.*$|$)/, loaders: ['file?hash=sha512&digest=hex&name=assets/[name].[hash].[ext]', 'image-webpack?bypassOnDebug&optimizationLevel=7&interlaced=false'] diff --git a/src/Squidex/app-config/webpack.test.coverage.js b/src/Squidex/app-config/webpack.test.coverage.js index ea510d58b..d0ca05d1e 100644 --- a/src/Squidex/app-config/webpack.test.coverage.js +++ b/src/Squidex/app-config/webpack.test.coverage.js @@ -4,7 +4,7 @@ var webpackMerge = require('webpack-merge'), helpers = require('./helpers'), testConfig = require('./webpack.test.js'); -removeTsLoader(); +helpers.removeLoaders(testConfig, ['ts']); module.exports = webpackMerge(testConfig, { module: { @@ -21,19 +21,4 @@ module.exports = webpackMerge(testConfig, { } ] } -}); - -function removeTsLoader() { - var tsModuleIndex = -1; - - for (var i = 0, len = testConfig.module.loaders.length; i < len; i += 1) { - if (testConfig.module.loaders[i].test.source.indexOf('.ts') > 0) { - tsModuleIndex = i; - break; - } - } - - if (tsModuleIndex >= 0) { - testConfig.module.loaders.splice(tsModuleIndex, 1); - } -} \ No newline at end of file +}); \ No newline at end of file