Gathering detailed insights and metrics for express-minify
Gathering detailed insights and metrics for express-minify
Gathering detailed insights and metrics for express-minify
Gathering detailed insights and metrics for express-minify
@types/express-minify
TypeScript definitions for express-minify
express-minify-html
Express middleware around HTML minifier
express-minify-html-2
Express.js middleware wrapper around html-minifier-terser
html-minifier-terser
Highly configurable, well-tested, JavaScript-based HTML minifier.
npm install express-minify
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
86 Stars
106 Commits
18 Forks
6 Watching
1 Branches
3 Contributors
Updated on 07 Feb 2024
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
-15.1%
1,145
Compared to previous day
Last week
2.4%
6,427
Compared to previous week
Last month
-4%
29,210
Compared to previous month
Last year
-3.9%
1,044,144
Compared to previous year
3
9
Automatically minify (and cache) your JavaScript, CSS and JSON responses without pain. It also supports LESS/SASS/Stylus/CoffeeScript compiling and minifying.
npm install express-minify
express-minify takes care of all responses. You don't even need to pass a source directory as other minifying middlewares.
1var minify = require('express-minify'); 2app.use(minify());
It's very easy and elegant to integrate express-minify with express.static and compression:
1app.use(compression()); 2app.use(minify()); 3app.use(express.static(__dirname + '/static'));
Note that the order of the middlewares is important. In the example above, we want to: serve static files → for JS & CSS: minify → GZip → send to user, so we have such orders.
Default:
1app.use(minify({ 2 cache: false, 3 uglifyJsModule: null, 4 errorHandler: null, 5 jsMatch: /javascript/, 6 cssMatch: /css/, 7 jsonMatch: /json/, 8 sassMatch: /scss/, 9 lessMatch: /less/, 10 stylusMatch: /stylus/, 11 coffeeScriptMatch: /coffeescript/, 12}));
cache
: String | false
The directory for cache storage (must be writeable). Pass false
to cache in the memory (not recommended). If you want to disable cache for specific response, see Disable Minifying or Caching for Specific Response.
uglifyJsModule
: Object
Customize UglifyJS (>= 3) module. If not specified, it will be require('uglify-js')
. Example: Use Uglify-ES.
errorHandler
: Function(errorInfo, callback)
Function to handle compiling and minifying errors. You can determine what to respond for specific kind of error. See Customize Error Behavior.
jsMatch
: RegExp | false
Matches JavaScript content-type. Pass false
to disable handling this kind of content.
cssMatch
: RegExp | false
Matches CSS content-type. Pass false
to disable handling this kind of content.
jsonMatch
: RegExp | false
Matches JSON content-type. Pass false
to disable handling this kind of content.
sassMatch
: RegExp | false
Matches SASS content-type. Pass false
to disable handling this kind of content.
lessMatch
: RegExp | false
Matches LESS content-type. Pass false
to disable handling this kind of content.
stylusMatch
: RegExp | false
Matches Stylus content-type. Pass false
to disable handling this kind of content.
coffeeScriptMatch
: RegExp | false
Matches CoffeeScript content-type. Pass false
to disable handling this kind of content.
Options below can be supplied for specific response:
response.minifyOptions.enabled
: boolean
Pass false
to disable all kind of processing for this response: no compiling, no minifying.
response.minifyOptions.minify
: boolean
Pass false
to disable minifying (JS, CSS and JSON) for this response, suitable for already-minified contents: example.
response.minifyOptions.cache
: boolean
Pass false
to disable caching the processed response data, suitable for dynamic contents: example.
response.minifyOptions.js
: Object
Set UglifyJS options. You may want to disable mangling or compressing for specific response (e.g. AngularJS) via this option: example.
response.minifyOptions.css
: Object
response.minifyOptions.sass
: Object
response.minifyOptions.less
: Object
Set less render options.
By default, express-minify uses memory cache. You can change to file cache:
1app.use(minify({cache: __dirname + '/cache'}));
express-minify can automatically compile your files and minify it without the need of specifying a source file directory. Currently it supports CoffeeScript, SASS, LESS and Stylus.
To enable this feature, first of all you need to install those modules by yourself:
1# You needn't install all of these. Only choose what you need. 2npm install coffee-script less node-sass stylus --save
Then you need to define MIME for those files:
1// visit http://localhost/test.coffee 2 3express.static.mime.define( 4{ 5 'text/coffeescript': ['coffee'], 6 'text/less': ['less'], 7 'text/x-scss': ['scss'], 8 'text/stylus': ['styl'] 9}); 10 11app.use(minify());
Errors may thrown at the compiling stage (for CoffeeScript/LESS/SASS/Stylus) or at the minifying stage (for JSON/UglifyJS/CleanCSS). The default behavior is returning the error message for compiling errors and returning original content for minifying errors.
You can customize this behavior or get notified about the error by providing errorHandler
in options:
1var minify = require('express-minify'); 2 3var myErrorHandler = function (errorInfo, callback) { 4 console.log(errorInfo); 5 // below is the default implementation (minify.Minifier.defaultErrorHandler) 6 if (errorInfo.stage === 'compile') { 7 callback(errorInfo.error, JSON.stringify(errorInfo.error)); 8 return; 9 } 10 callback(errorInfo.error, errorInfo.body); 11}; 12 13app.use(minify({ errorHandler: myErrorHandler }));
The structure of errorInfo
is:
stage
: One of ["compile", "minify"]
The stage when error is thrown.
body
: String
The content to compile or minify, which causes the error of course.
If there are errors when minifying the compiled source, body
will be the compiled source.
assetType
: One of ["js", "css", "json", "coffee", "sass", "less", "stylus"]
The type of the original content.
options
: Object
The options you supplied via response.minifyOptions
.
error
: Error
The error thrown by the corresponding processor.
You can pass the uglify-es module in options to replace the built-in UglifyJS 3 module.
1var uglifyEs = require('uglify-es'); 2app.use(minify({ 3 uglifyJsModule: uglifyEs, 4}));
Remember to invalidate file caches after switching a UglifyJS module. They won't be invalidated automatically.
1app.use(function(req, res, next) 2{ 3 // do not mangle -angular.js files 4 if (/-angular\.js$/.test(req.url)) { 5 res.minifyOptions = res.minifyOptions || {}; 6 res.minifyOptions.js = { mangle: true }; 7 } 8 next(); 9}); 10app.use(minify());
1app.use(function(req, res, next) 2{ 3 if (/\.(user|meta)\.js$/.test(req.url)) { 4 res.minifyOptions = res.minifyOptions || {}; 5 res.minifyOptions.js = { output: { comments: true } }; 6 } 7 next(); 8});
express-minify is able to handle all kind of responses, including dynamic responses.
1var responseJS = 2 "(function(window, undefined)\n" + 3 "{\n" + 4 "\n" + 5 " var hello = 'hello';\n" + 6 "\n" + 7 " var world = 'world';\n" + 8 "\n" + 9 " alert(hello + world);\n" + 10 "\n" + 11 "})(window);" 12app.use(minify()); 13app.get('/response.js', function(req, res) 14{ 15 res.setHeader('Content-Type', 'application/javascript'); 16 res.end(responseJS); 17});
If you don't want to minify a specific response, just set response.minifyOptions.minify = false
.
If you want to minify a response but don't want to cache it (for example, dynamic response data), set response.minifyOptions.cache = false
.
1app.use(function(req, res, next) 2{ 3 if (/\.min\.(css|js)$/.test(req.url)) { 4 res.minifyOptions = res.minifyOptions || {}; 5 res.minifyOptions.minify = false; 6 } 7 next(); 8}); 9app.use(minify());
1app.use(minify()); 2app.get('/server_time_min.jsonp', function(req, res) 3{ 4 var obj = { 5 'ok': true, 6 'data': { 7 'timestamp': new Date().getTime(), 8 }, 9 }; 10 11 // minify this response, but do not cache it 12 res.minifyOptions = res.minifyOptions || {}; 13 res.minifyOptions.cache = false; 14 res.setHeader('Content-Type', 'application/javascript'); 15 res.send("callback(" + JSON.stringify(obj, null, 4) + ");"); 16}); 17 18app.get('/server_time.jsonp', function(req, res) 19{ 20 var obj = { 21 'ok': true, 22 'data': { 23 'timestamp': new Date().getTime(), 24 }, 25 }; 26 27 // do not minify (and do not cache) this response 28 res.minifyOptions = res.minifyOptions || {}; 29 res.minifyOptions.minify = false; 30 res.setHeader('Content-Type', 'application/javascript'); 31 res.send("callback(" + JSON.stringify(obj, null, 4) + ");"); 32});
1.0.0
4.0.0
for Object.assign
0.2.0
onerror
0.12.0
for fs.access
0.1.7
0.1.6
node-sass
, stylus
, less
, coffee-script
dependency optional, now developers need to manually install those modules to enable compiling0.1.5
0.1.4
0.1.3
0.1.2
res._skip
.res._no_minify
. Now it will only disable minifying and won't cause precompiling not working. #170.1.1
res._no_mangle = true
(Please use res._uglifyMangle = false
)0.1.0
res._no_mangle = true
=> res._uglifyMangle = false
res._uglifyCompress
, res._uglifyOutput
#150.0.11
0.0.10
Added tests
Fixed SASS compiling
Fixed express-compression compatibility
0.0.9
res._no_mangle
#100.0.8
Removed options of whitelist
and blacklist
Added support for res._no_cache
#5
Node v0.10 compatible
0.0.7
Changed options
's default blacklist
to [/\.min\.(css|js)$/]
Replaced uglifycss
with cssmin
Dropped support for .sass
(https://github.com/andrew/node-sass/issues/12)
Fixed #3
0.0.6
0.0.5
Added support for res._no_minify
Fixed #1
0.0.4
0.0.3
Support for file cache
Fixed the bug of non-string path
0.0.2
The MIT License (MIT)
Copyright (c) 2017 Breezewish
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 5/28 approved changesets -- score normalized to 1
Reason
project is archived
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More