Concat and minify JS files in Node

前端 未结 9 1699
遥遥无期
遥遥无期 2020-12-04 09:40

Is there any module in NodeJS to concatenate and minify JavaScript files?

9条回答
  •  日久生厌
    2020-12-04 10:03

    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

    cat js/bootstrap-transition.js js/bootstrap-alert.js js/bootstrap-button.js js/bootstrap-carousel.js js/bootstrap-collapse.js js/bootstrap-dropdown.js js/bootstrap-modal.js js/bootstrap-tooltip.js js/bootstrap-popover.js js/bootstrap-scrollspy.js js/bootstrap-tab.js js/bootstrap-typeahead.js > docs/assets/js/bootstrap.js
    

    This is just a concatenation of files with an output to a js file

    Then you can install uglify-js to minify js:

    npm -g install uglify-js
    

    And perform this command with your path/file.js ofc:

    uglifyjs docs/assets/js/bootstrap.js -nc > docs/assets/js/bootstrap.min.js
    

    As mentioned in comments since uglifyjs 2 you could also do:

    uglifyjs --compress --mangle -- input.js
    

提交回复
热议问题