【问题标题】:How to copy from node_modules just the files needed for distribution using grunt and grunt-contrib-copy如何从 node_modules 复制使用 grunt 和 grunt-contrib-copy 分发所需的文件
【发布时间】:2013-08-17 05:32:04
【问题描述】:

虽然我在某处读到 package.json 的“main”属性可用于复制“dist”或部署所需的文件。我以为它有一个繁重的任务,但我没有看到任何可以帮助我或指导我的东西。我现在复制 node_modules 下的所有内容,但我当然不需要分发库示例代码,例如。

是否有一个 grunt 任务或任何关于如何正确使用 grunt-contrib-copy 来从 node_module 依赖项中复制文件的说明,希望从配置对象的标准 pkg 对象(已解析的 package.json 文件)中复制?

【问题讨论】:

    标签: copy dependencies npm gruntjs


    【解决方案1】:

    package.json 没有包含足够的信息让您知道要包含什么。您将不得不解析出所有的 require 语句,但即便如此,您也无法检测到某些情况,例如模块加载资源等。

    这样做的正确方法是包作者使用.npmignore file 忽略不需要的文件,或者更好的是,使用package.json 中的files property 明确定义哪些文件应该包含在包。

    但不幸的是,大多数包作者都很懒惰,不关心这些......

    我鼓励您使用 files 属性打开相关模块的 PR。

    【讨论】:

      【解决方案2】:

      你可以:

      1) 使用复制任务将每个相关文件复制到dest目录:

      copy:
        js:
          files: [
            { 
               expand: true, 
               cwd: 'node_modules/jquery', 
               src: 'jquery.min.js', 
               dest: 'www/js' 
            },
            { 
               expand: true, 
               cwd: 'node_modules/jquery-mobile-bower/js', 
               src: 'jquery.mobile-*.min.js', 
               dest: 'www/js' 
            }
          ]
      

      jquery.min.js 和 jquery.mobile-x.y.z.min.js 都将被复制到 www/js 目录。

      2) 使用 concat 任务将所有文件连接到单个 dest 文件(用于生成唯一的 javascript/样式表文件)

      concat:
        options:
          separator: ';'
        js:
          dest: 'www/js/lib.js'
          src: [
            'node_modules/jquery/jquery.min.js',
            'node_modules/jquery-mobile-bower/js/jquery.mobile-*.min.js'
          ]
      

      jquery.min.js 和 jquery.mobile-x.y.z.min.js 将合并为一个 www/js/lib.js 文件,用分号分隔。

      【讨论】:

      • 我认为现在做#2的“正确”方法是使用Browserify或Webpack而不是手动连接js文件。
      猜你喜欢
      • 2017-10-25
      • 1970-01-01
      • 2018-02-13
      • 2015-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-21
      • 2014-06-13
      相关资源
      最近更新 更多