how to minify js files in order via grunt-contrib-uglify?

后端 未结 9 2029
执念已碎
执念已碎 2021-02-01 14:51

I have a directory like below:

/folder/b.js
/folder/jQuery.js
/folder/a.js
/folder/sub/c.js

I want to minify all these js files in one js file

相关标签:
9条回答
  • 2021-02-01 15:08

    Good questions!

    1) Uglify will reorder the functions in the destination file so that function definitions are on top and function execution on bottom but it seems that it will preserve the order of the function executions.

    This means that the function jQuery runs to define its global functions will be put first if you make sure jQuery is mentioned first in Uglify's config in the Gruntfile.

    I use this config:

    uglify: {
        options: {
            sourceMap: true
        },
        build: {
            files: {
                'public/all.min.js': ['public/js/vendor/jquery-1.10.2.min.js', 'public/js/*.js'],
            }
        }
    }
    

    2) I don't think there is one definite way to accomplish this. It depends on what web framework, templating framework and what kind of requirements you have. I use express + jade and in my main jade layout I have:

    if process.env.NODE_ENV === 'production'
      script(src='/all.min.js')
    else
      script(src='/js/vendor/jquery-1.10.2.min.js')
      script(src='/js/someScript.js')
      script(src='/js/otherScript.js')
    

    In my package.json I have:

    "scripts": {
      "postinstall": "grunt"
    },
    

    This means that when I run npm install on deploy (on Heroku) grunt is run to minify/concat files and when the app is started with NODE_ENV=production the minified client side javascript is used. Locally I get served the original client side javascripts for easy debugging.

    The two downsides are:

    • I have to keep the two lists of script files in sync (in the Gruntfile and in the layout.js) I solve this by using *.js in the Gruntfile but this may not suite everyone. You could put the list of javascripts in the Gruntfile and create a jade-template from this but it seems overkill for most projects.
    • If you don't trust your Grunt config you basically have to test running the application using NODE_ENV=production locally to verify that the minification worked the way you intended.
    0 讨论(0)
  • 2021-02-01 15:17

    If your problem was that you had vendors which needed to be loaded in order (let's say jquery before any jquery plugins). I solved it by putting jquery in its own folder called '!jquery', effectively putting it on top of the stack. Then I just used concat as you normally would:

    concat: {
      options: {
        separator: ';',
      },
      build: {
        files: [
          {
            src: ['js/vendor/**/*.js', 'js/main.min.js'],
            dest: 'js/global.min.js'
          }
        ]
      }
    },
    
    0 讨论(0)
  • 2021-02-01 15:21

    This might be only remotely related to your question but I wanted something similar. Only my order was important in the following way:

    I was loading all vendor files (angular, jquery, and their respective related plugins) with a wildcard (['vendor/**/*.js']). But some plugins had names that made them load before angular and jquery. A solution is to manually load them first.

    ['vendor/angular.js', 'vendor/jquery.js', 'vendor/**/*.js]
    

    Luckily angular and jquery handle being loaded twice well enough. Edit: Although it's not really the best practice to load such large libraries twice, causing your minified file unnecessary bloat. (thanks @Kano for pointing this out!)

    Another issue was client-js the order was important in a way that it required the main app file to be loaded last, after all its dependencies have been loaded. Solution to that was to exclude and then include:

    ['app/**/*.js', '!app/app.js', 'app/app.js']
    

    This prevents app.js from being loaded along with all the other files, and only then includes it at the end.

    0 讨论(0)
  • 2021-02-01 15:22

    I don't think you can do this with the uglify task alone, but you have a multitude of choices which might lead to your desired outcome.

    A possible workflow would be first concatenating (grunt-contrib-concat) the files in order into one single file, and put this concatenated file through uglify. You can either define the order for concat in your Gruntfile, or you use on of those plugins:

    First one would be https://github.com/yeoman/grunt-usemin, where you can specify the order in your HTML file, put some comments around your script block. The Google guys made it and it's pretty sweet to use.

    Second one would be https://github.com/trek/grunt-neuter, where you can define some dependencies with require, but without the bulk of require.js. It requires changes in your JS code, so might not like it. I'd go with option one.

    0 讨论(0)
  • 2021-02-01 15:28

    Looks like the second part of your question is still unanswered. But let me try one by one.

    Firstly you can join and uglify a large number of js files into one as explained by the concat answer earlier. It should also be possible to use https://github.com/gruntjs/grunt-contrib-uglify because it does seem to have wildcards. You may have to experiment with 'expand = true' option and wildcards. That takes care of your first question.

    For the second part, say you joined and uglified into big-ugly.js

    Now in your html you can add following directives:

    <!-- build:js:dist big-ugly.js -->
    <script src="js1.js"></script>
    <script src="js2.js"></script>
    <!-- etc etc --> 
    <script src="js100.js"></script>
    <!-- /build -->
    

    And then pass it through the grunt html preprocessor at https://www.npmjs.com/package/grunt-processhtml as part of your grunt jobs.

    This preprocessor will replace the entire block with

    <script src="big-ugly.js"></script>
    

    Which means that the html file with be semantically equivalent - before and after the grunt jobs; i.e. if the page works correctly in the native form (for debugging) - then the transformed page must work correctly after the grunt - without requiring you to manually change any tags.

    0 讨论(0)
  • This was @1469's answer but he didn't make it clear why this works. Use concat to put all js files into one, this module does this in the order of file names, so I put a prefix to the file names based on orders. I believe it even has other options for ordering.

    concat: {
    
            js: {
                options: {
                    block: true,
                    line: true,
                    stripBanners: true
                },
                files: {
                    'library/dist/js/scripts.js' : 'library/js/*.js',
                }
            }
        },
    

    Then use uglify to create the minified ugly version:

    uglify: {
          dist: {
            files: {
              'library/dist/js/scripts.min.js': [
                'library/js/scripts.js'
              ]
    
            },
            options: {
    
            }
          }
        },
    
    0 讨论(0)
提交回复
热议问题