【问题标题】:Dynamic Mapping and Concat with Grunt Uglify使用 Grunt Uglify 进行动态映射和 Concat
【发布时间】:2016-08-09 15:36:29
【问题描述】:

我正在尝试将 dynamic mapping 和 concat Javascript 文件与 Grunt Uglify 一起使用。

我有以下工作不正常。

这是我的文件夹结构:

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

这是我所期待的:

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)

这是我目前得到的:

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)

它不包括 custom.js 文件,而是创建了 2 个文件夹 test/account/custom.min.js 'test/bills/billing-one.js' - 见上文

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
  },
],

我希望bills/ 文件夹中的所有 Javascript 文件都包含 custom.js

所以如果有 2 个文件: bills/billing-one.js bills/billing-two.js

我希望 test/ 文件夹包含

test/billing-one.min.js(此文件将包含 billing-one + custom.js) test/billing-two.min.js(此文件将包含 billing-two + custom.js)

我不想硬编码文件名。如果将更多文件添加到bills/ 文件夹,则应该连接并输出到test/ 文件夹。

非常感谢任何帮助。

接受答复后的更新:

使用以下更新的代码确保它按预期工作 - 否则在运行 GRUNT 时会遇到错误。

我确实尝试通过提交编辑以将其添加到答案中以供审核。但是它被所有的高级模组拒绝了两次……实际上它是一个有效的输入并且改进了给出的答案。注意 []cwdsrc 的变化。

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

【问题讨论】:

    标签: gruntjs grunt-contrib-uglify


    【解决方案1】:

    您可以使用 grunt-contrib-uglify 的 banner 属性来附加内容。 https://github.com/gruntjs/grunt-contrib-uglify#banner

    这里是 grunt 配置:

    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'
          },
        }
      });

    在上面的配置中, bills 文件夹中的每个文件都会获取到横幅属性中配置的 custom.js 的内容。

    希望对你有帮助。

    【讨论】:

    • 谢谢 - 效果很好。我搜索了文档并试图找出一种在横幅中包含多个文件的方法,例如:banner: grunt.file.read([ ./javascript/account/custom.js', ./javascript/account-2/custom.js ]), 但这不起作用。
    • 通过在多个文件上使用 concat 然后在横幅中包含 concat 找到上述评论的解决方法
    猜你喜欢
    • 2023-03-28
    • 2018-11-27
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    • 2014-06-19
    相关资源
    最近更新 更多