0

我正在尝试构建我的资产,以便多个项目可以共享构建脚本的“阶段”。例如,如果我有 3 个都使用 Bootstrap 和 jQuery 的基于 Web 的产品,我只想从共享的“库”中获取库。

建议文件夹的结构如下:

shared
  -sharedGrunt.js (file)
  -lib (folder)
    -bootstrap (folder)
    -jQuery.js (file)
app1
  -Gruntfile.js (file)
  -src (folder)
    -images (folder)
    -js (folder)
    -etc
app2
  -Gruntfile.js (file)
  -src (folder)
    -images (folder)
    -js (folder)
    -etc

我希望每个“app”文件夹中的 Gruntfile.js 能够“导入”或以其他方式执行sharedGrunt.js. 我还没有跨过那座桥,因为我被困在第一个概念验证测试中:一个简单的副本。

因此,在应用程序的一个Gruntfile.js文件中,我有一个如下所示的复制任务:

copy: {
  externalTest: {
    expand: true,
    src: '../shared/lib/jQuery.js',
    dest: 'dev/js/jQuery.js',
    flatten: true
  }
}

如您所见,我尝试从 Gruntfile 上一层。这是包含“shared”、“app1”和“app2”的目录。然后向下导航到共享库文件夹以获取 jQuery。

任务“成功”(没有抛出实际错误),但没有文件被复制。

我怎样才能实现我的目标?我是否需要将“包罗万象”的 gruntFile 放入包含所有项目的“父”文件夹中?我不希望开发人员要求检查整个父母。我希望开发人员能够检查“app1”和“shared”并使用它运行。

4

2 回答 2

1

这很奇怪——Grunt 提升任何级别都没有问题,而在我的机器上,你的代码确实复制了 1 个文件。

However, your use of expand cause grunt to use the dest property as a directory, so your file ends up being dev/js/jQuery.js/jQuery.js (note the repetition).

Fixed by doing:

copy: {
  externalTest: {
    expand: true,
    src: '../shared/lib/*.js',
    dest: 'dev/js',
    flatten: true
  }
}
于 2015-07-18T15:10:14.033 回答
1

Prompted by observations in Xavier's answer, I forged ahead and determined that I had two flaws:

  1. I should simply provide the destination directory. If I try to provide a filename, it will make an additional directory with the name of what I thought would be the filename; this is per Xavier's observation.

  2. If you don't issue a "cwd" for the copy, it reproduces the entire directory tree from the source side.

So, the following:

share: {
  expand: true,
  src: '../common/lib/jquery.js',
  dest: 'dev/js/'
}

results in a file found in the destination as '"dev/common/lib/jquery.js"'. This is not intended behaviour.


SOLUTION:

  1. Supply the destination as a directory, not a filename. It will use the original filename.
  2. Issue a CWD before the copy so that the source path is not merged into the destination path.

Working task:

share: {
  expand: true,
  cwd: '../common/lib',
  src: 'jquery.js',
  dest: 'dev/js/'
}

Results in a file sourced at "../common/lib/jquery.js" and ending up at destination "dev/js/jquery.js" as intended.

于 2015-07-24T15:10:30.343 回答