【问题标题】:Move files to root when my NPM package is installed安装我的 NPM 包时将文件移动到根目录
【发布时间】:2016-11-27 10:51:12
【问题描述】:

我目前正在处理我的回购https://github.com/Aotik/Blossom。它是一个 NPM 发布的包,名为 blossom-ui

我的问题是,有没有办法在安装软件包时将文件从node_modules/blossom-ui 移到node_modules 之外的文件夹的根目录中?

所以它看起来像

blossom-ui

  • css/

  • styl/

  • fonts/

  • js/

node_modules

  • ...

【问题讨论】:

  • node_modules 目录的重点是依赖项所在的位置。为什么你的包裹需要放在别处?
  • 我想将已编译的文件(例如 CSS 和 JS)移出 node_modules,以便轻松定位它们
  • 我仍然希望将源文件保存在node_modules 中,以便以后构建build 脚本。

标签: node.js npm node-modules package.json


【解决方案1】:

这可以在 npm 中的 postinstall 脚本中完成。

postinstall 会在每次 npm install 完成时由 npm 自动执行。

    "scripts": {
            "test": "echo \"Error: no test specified\" && exit 1",
            "postinstall": "cp node_modules/blossom-ui ."
    },

更多信息:npm site scripts page

【讨论】:

    【解决方案2】:

    如果你使用grunt,一个简单的copy 任务就会变成这样:

    copy: {
        vendor: {
            files: [{
                expand: true,
                cwd: 'node_modules/bootstrap/',
                src: ['js/**', 'less/**'],
                dest: 'public/vendor/bootstrap/'
            }]
        }
    }
    .....
    grunt.registerTask('build', ['copy:vendor']);
    

    例如Drywall project 使用它将引导程序和主干复制到/public/vendor 就像上面一样。如果你检查它的gruntfile.js

    请记住,如果您从 node_modules 复制,您的目标文件夹必须存在于您的 .gitignore

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      • 2015-08-29
      • 1970-01-01
      • 2013-04-09
      • 2019-05-07
      • 1970-01-01
      相关资源
      最近更新 更多