【问题标题】:Reading bower dependencies into grunt file list将 bower 依赖项读入 grunt 文件列表
【发布时间】:2013-06-23 20:07:58
【问题描述】:

我正在使用 grunt,我想在创建生产发行版时复制我的 bower 依赖项

这些依赖已经存在于 ./components 中

我生成了一个包含 index.html 的生产目录,并且只想从 bower.json 文件中复制依赖项。

我认为这就像从 deps 生成列表一样简单:

prodComponents = Object.keys(grunt.file.readJSON('./bower.json').dependencies)

(从一个简单的 console.log(prodComponents) 产生

[ 'requirejs',
  'requirejs-text',
  'jquery',
  'underscore-amd',
  'backbone-amd',
  'backbone.wreqr',
  'backbone.babysitter',
  'marionette' ]

然后简单地复制匹配的文件:

    copy:
        deps:
            files: [
                expand: true
                cwd: './components'
                src: ['./<%= prodComponents %>/*']
                dest: './dev/components'
            ]

这可行,但会复制所有组件。即我的文件规范失败

Running "copy:deps" (copy) task
Created 15 directories

如果我删除 ./ 那么它会失败:

Warning: Unable to read "components/Applications" file (Error code: ENOENT). Use --force to continue.

忍不住想我要么太聪明了,要么就这样。

我对文件规范的规范做错了什么?

谢谢

【问题讨论】:

    标签: gruntjs bower


    【解决方案1】:

    我认为你很接近。我会将应用了通配模式的目录保存到prodComponents:

    prodComponents = Object.keys(grunt.file.readJSON('./bower.json').dependencies).map(
        function(prodComponent) {
            return prodComponent + "/**/*";
        }
    );
    

    所以prodComponents 将包含:

    ["requirejs/**/*",
     "requirejs-text/**/*",
     "jquery/**/*",
     "underscore-amd/**/*",
     "backbone-amd/**/*",
     "backbone.wreqr/**/*",
     "backbone.babysitter/**/*",
     "marionette/**/*" ]
    

    copy 配置为:

    copy:
        deps:
            files: [
                expand: true
                cwd: 'components'
                src: '<%= prodComponents %>'
                dest: 'dev/components'
            ]
    

    请注意,为了让您能够以这种方式在模板中使用prodComponents,需要在您的grunt config 中进行设置。

    【讨论】:

    • 不像我最初设想的那样简洁,但很有效。谢谢
    • 参考:prodComponent + "/*" 应该是 prodComponent + "/**/*" 来捕获子目录。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-22
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    相关资源
    最近更新 更多