【问题标题】:How to copy files without the full path with grunt.js?如何使用 grunt.js 复制没有完整路径的文件?
【发布时间】:2013-10-10 13:25:55
【问题描述】:

我想用 grunt.js 将/pckg 的内容复制到/dist。 这是结构:

  |-- folder1
  |     |
  |     |-- folder2
  |           |
  |           |-- pckg
  |                 |
  |                 |--myfolder
  |                 |    |
  |                 |    |-- myfiles
  |                 |
  |                 |--myfiles
  |
  |
  |-- dist
        |
        |--myfolder
        |   |
        |   |-- myfiles
        |
        |--myfiles

这是我的Gruntfile.js

module.exports = function (grunt) {

  // Package configuration
  grunt.initConfig({

    // Metadata
    pkg: grunt.file.readJSON('package.json'),

    //Copy files
    copy: {
      main: {
        expand: true,
        src: 'folder1/folder2/pckg/**',
        dest: 'dest/'
      }
    }

  });

  // Load the plugin that provides the "copy" task.
  grunt.loadNpmTasks('grunt-contrib-copy');

  // Default task(s).
  grunt.registerTask('default', ['copy']);
};

当我运行 Grunt 时,它会保持路径。它会复制dit/folder1/folder2/pckg 中的所有内容。 怎么了?

感谢您的帮助!

【问题讨论】:

    标签: gruntjs grunt-contrib-copy


    【解决方案1】:

    这是我用过的:

    copy: {
      main: {
        expand: true,
        cwd: 'folder1/folder2/pckg/',
        src: ['**'],
        dest: 'dist/'
      }
    }
    

    【讨论】:

    • 这是正确答案。文档使用 cwd 参数,但从未解释过。
    • grunt 是一个如此“伟大”的工具.. 文档几乎处于同一水平
    【解决方案2】:

    使用 flatten:true

    copy: {
        main: {
            files: [ 
                {expand: true, src: ['components/xxx/*'], dest: 'dist/', flatten: true}
            ]
        }
    }
    

    【讨论】:

    • 我想将文件夹结构保留在pckg 中。这个配置可以吗?
    • @alienlebarge,faltten 不要将文件夹结构保留在 pckg 中。
    猜你喜欢
    • 1970-01-01
    • 2021-04-24
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 2021-02-23
    相关资源
    最近更新 更多