【问题标题】:Grunt package.json: How to install dependencies to public subfolder?Grunt package.json:如何将依赖项安装到公共子文件夹?
【发布时间】:2020-01-11 04:56:14
【问题描述】:

我的 package.json 文件如下所示:

{
  "name": "xxxx",
  "version": "1.0.0",
  "private": true,
  "devDependencies": {
    "grunt": "^1.0.4",
    "grunt-autoprefixer": "^3.0.4",
    "grunt-contrib-cssmin": "^3.0.0",
    "grunt-contrib-jshint": "~0.10.0",
    "grunt-contrib-nodeunit": "~0.4.1",
    "grunt-contrib-uglify": "~0.5.0",
    "grunt-contrib-watch": "^1.1.0",
    "grunt-package-modules": "^1.0.0",
    "grunt-sass": "^3.1.0",
    "node-sass": "^4.12.0"
  },
  "dependencies": {
    "bootstrap": "^4.2.1",
    "jquery": "^3.3.1",
    "popper.js": "^1.14.6"
  }
}

我有一个 Gruntfile.js,它执行某些任务,例如文件监视、SASS 编译、自动前缀、缩小,...

示例任务:

 sass: { // sass tasks
        dist: {
            options: {
                compass: true, // enable the combass lib, more on this later
                style: 'expanded', // we don't want to compress it
                implementation: sass,
                sourceMap: true
            },
            files: {
                'web/var/static/css/main.css': 'web/var/static/sass/main.scss' // this is our main scss file
            }
        }
    },

我的 node_modules 文件夹位于项目根目录中。整个项目不公开,只公开/web/var/static/内的所有内容。

所需的库(如 bootstrap、jquery 和 popper.js)应该可以公开访问,因为它们部分直接包含在项目视图中,如 /var/static/lib/jquery/dist/jquery.min.js。否则,我会使用自定义的 grunt 任务将它们粘合在一起。

目前 grunt 将依赖项放入项目根目录下的 node_modules 文件夹中。我怎么说 grunt 把那些特定的依赖放到公共的/var/static/lib/ 文件夹中?

【问题讨论】:

    标签: javascript dependencies gruntjs node-modules package.json


    【解决方案1】:

    同时我已经解决了这个问题:https://www.npmjs.com/package/grunt-contrib-copy

    使用此模块,您可以将所需文件复制到项目目录:

        copy: {
            jquery: {
                files: [{src: ['**'], dest: 'web/var/static/lib/jquery',expand: true, cwd: 'node_modules/jquery/dist'}],
            },
            popper: {
                files: [{src: ['**'], dest: 'web/var/static/lib/popper.js',expand: true, cwd: 'node_modules/popper.js/dist'}],
            },
         }
    

    有一些替代方案,例如 https://www.npmjs.com/package/grunt-copy-deps,它只复制具体的库文件。这更舒服,但我喜欢只复制我需要的东西的灵活性。

    【讨论】:

      猜你喜欢
      • 2017-04-18
      • 1970-01-01
      • 1970-01-01
      • 2014-10-16
      • 1970-01-01
      • 2016-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多