【问题标题】:Using Grunt.js to copy all HTML files from one directory structure to another使用 Grunt.js 将所有 HTML 文件从一个目录结构复制到另一个目录结构
【发布时间】:2015-01-26 09:20:02
【问题描述】:

我有一个大型目录结构,这是大多数应用程序的典型结构。

例如,像这样:

theprojectroot
|- src
|     |- app
|     |     |- index.html
|     |     |- index.js
|     |     |- userhome
|     |     |     |- userhome.html
|     |     |     |- userhome.js
|     |     |- management
|     |     |     |- management.html
|     |     |     |- management.js
|     |     |- social
|     |     |     |- social.html
|     |     |     |- social.js
|     |- assets
|- vendor
|- package.json

我想将所有目录中的所有 HTML 文件 - 和 HTML 文件 - 复制到另一个文件夹中。

我目前正在使用 Grunt copy 复制所有文件,但现在我只想为 HTML 这样做。 In the docs,似乎没有选择文件类型的选项。

有没有人可以建议这样做的 hack?

【问题讨论】:

  • 经过一些调整,我发现src: '**/**.html' 可以满足我的需求。我只是不明白为什么。所以如果有人可以解释,我会接受他们的回答

标签: build gruntjs grunt-contrib-copy


【解决方案1】:

以下代码将起作用

copy: {
    files: {
        cwd: 'path/to/files',  // set working folder / root to copy
        src: '**/*.html',      // copy all files and subfolders **with ending .html**
        dest: 'dist/files',    // destination folder
        expand: true           // required when using cwd
      }
    }

【讨论】:

    【解决方案2】:

    此答案中的 flatten: true 选项可能适用于某些情况,但在我看来,更常见的要求(如我的情况)是按原样复制文件夹及其子文件夹结构,到目的地。似乎在大多数情况下,如果您有子文件夹,它们可能会在代码中以这种方式被引用。这样做的关键是 cwd 选项,它将保留相对于指定工作目录的文件夹结构:

    copy: {
      files: {
        cwd: 'path/to/files',  // set working folder / root to copy
        src: '**/*.html',      // copy only html files
        dest: 'dist/files',    // destination folder
        expand: true           // required when using cwd
      }
    }
    

    【讨论】:

    • 我想你错过了我的要求。我只想复制html 文件而不是目录中的其他文件。也许我应该更新问题
    猜你喜欢
    • 2013-09-28
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    相关资源
    最近更新 更多