Dynamic Mapping and Concat with Grunt Uglify

蓝咒 提交于 2019-12-31 04:29:32

问题


I'm trying to use dynamic mapping AND concat Javascript files with Grunt Uglify.

I have the following which is not working correctly.

Here is my folder structure:

javascript
    |- account
        |- custom.js
    |- bills
        |- billing-one.js
        |- billing-two.js
    |- test (output folder)

Here is what I'm expecting:

javascript
    |- account
        |- custom.js
    |- bills
        |- billing-one.js
        |- billing-two.js
    |- test
        |- billing-one.min.js (this file includes billing-one.js AND custom.js)
        |- billing-two.min.js (this file includes billing-two.js AND custom.js)

This is what I'm currently getting:

javascript
    |- account
        |- custom.js
    |- bills
        |- billing-one.js
        |- billing-two.js
    |- test
        |- bills
            |- billing-one.min.js (this file includes just billing-one.js)
            |- billing-two.min.js (this file includes just billing-two.js)
        |- account 
            |- custom.min.js (this file includes just custom.js)

It is not including the custom.js file but instead creates a 2 folders test/account/custom.min.js 'test/bills/billing-one.js' - see above

options: {
    beautify: true,
    mangle: false,
    compress: false,
    preserveComments: 'all'
},
files: [
  {
    expand: true,     // Enable dynamic expansion.
    cwd: 'javascript/',      // Src matches are relative to this path.
    src: [[bills/*.js'], 'account/custom.js'], // Actual pattern(s) to match.
    dest: 'test/',   // Destination path prefix.
    ext: '.min.js',   // Dest filepaths will have this extension.
    extDot: 'first'   // Extensions in filenames begin after the first dot
  },
],

I want all the Javascript files within the bills/ folder to contain custom.js

So if there are 2 files: bills/billing-one.js bills/billing-two.js

I would expect test/ folder to include

test/billing-one.min.js (this file would contain billing-one + custom.js) test/billing-two.min.js (this file would contain billing-two + custom.js)

I don't want to hard code the file names in. If more files are added tobills/ folder the should be concat and output to the test/ folder.

Any help much appreciated.

UPDATE SINCE ANSWER ACCEPTED:

Use the follow updated code to make sure this works as intended - otherwise you'll run into errors when running GRUNT.

I did try adding this to the answer by submitting an edit for review. But it got rejected, twice, by the all knowing all superior mods... when in fact it's a valid input and improves on the answer given. Note the [], cwd and src changes.

files: [{
    expand: true,
    cwd: 'javascript/bills/',
    src: ['*.js'],
    dest: 'test/',
    ext: '.min.js',
    extDot: 'first'
}],

回答1:


You can use the banner property of grunt-contrib-uglify to append the content. https://github.com/gruntjs/grunt-contrib-uglify#banner

Here is the grunt configuration:

grunt.initConfig({
    uglify: {
      options: {
        banner: grunt.file.read('./javascript/account/custom.js'),
        beautify: true,
        mangle: false,
        compress: false,
        preserveComments: 'all'
      },
      files: {
        expand: true,
        cwd: 'javascript/',
        src: ['bills/*.js'],
        dest: 'test/',
        ext: '.min.js',
        extDot: 'first'
      },
    }
  });

In the above configuration every file in the bills folder will get the content of custom.js configured in the banner property.

I hope it helps you.



来源:https://stackoverflow.com/questions/38854998/dynamic-mapping-and-concat-with-grunt-uglify

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!