Is there any module in NodeJS to concatenate and minify JavaScript files?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fs
- Keeping track of variable instances
Maybe not exactly what you're looking for but Enderjs could work.
You'll be better using something like gulp / webpack to concat/organize/bundle your assets.
In order to join js file you can do as its done in twitter bootstrap makefile
This is just a concatenation of files with an output to a js file
Then you can install uglify-js to minify js:
And perform this command with your path/file.js ofc:
As mentioned in comments since uglifyjs 2 you could also do:
Try these two plugins for Grunt
https://www.npmjs.org/package/grunt-contrib-concat
https://www.npmjs.org/package/grunt-contrib-uglify
You can install everything you need like so:
Then create your grunt file, like so:
Gruntfile.js
Finally, type
grunt
in the terminal and Grunt will start watching your JavaScript files for changes and automatically concat and uglify them (whenever you save a change to your js files in ../src/I'd definitely suggest the Closure Compiler's simple mode.
If you already have
uglify-js
, your code uses some of the latest ES6 features (ECMAScript 2015) and it just gave you back parse errors on the first run, then install uglify-es:Or:
The explanation is in uglify-js-es6 package:
You can still run it normally with the
uglifyjs
command. A compress and mangle example:Which will produce a single file with all the JS files of a folder. The double dash (
--
) just prevents input files being used as option arguments.I recommend using UglifyJS which is a JavaScript parser / mangler / compressor / beautifier library for NodeJS.
If you are interested in automation tools that do more than simply concatenate and minify files, there are the following solutions:
GruntJS is a task-based command line build tool for JavaScript projects. The current version has the following built-in tasks:
Besides this tasks there's a lot of plugins available.
Gulp is a toolkit that will help you automate painful or time-consuming tasks in your development workflow. For web development (if that's your thing) it can help you by doing CSS preprocessing, JS transpiling, minification, live reloading, and much more. Integrations are built into all major IDEs and people are loving gulp across PHP, .NET, Node.js, Java, and more. With over 1700 plugins (and plenty you can do without plugins), gulp let's you quit messing with build systems and get back to work.
Yeoman is a robust and opinionated set of tools, libraries, and a workflow that can help developers quickly build beautiful, compelling web apps.