简体   繁体   English

Grunt package.json:如何将依赖项安装到公共子文件夹?

[英]Grunt package.json: How to install dependencies to public subfolder?

My package.json file looks like this: 我的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"
  }
}

I have a Gruntfile.js which performs certain tasks like file watching, SASS compiling, autoprefixing, minification, ... 我有一个Gruntfile.js,它执行某些任务,例如文件监视,SASS编译,自动前缀,缩小,...

Sample task: 示例任务:

 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
            }
        }
    },

My node_modules folder is within the project root. 我的node_modules文件夹在项目根目录下。 The whole project is not public, just everything within /web/var/static/ . 整个项目不是公开的,只是/web/var/static/

The required libraries like bootstrap, jquery and popper.js should be publicly accessible because they are partly directly included within the project view like /var/static/lib/jquery/dist/jquery.min.js . 所需的库(例如bootstrap,jquery和popper.js)应该可以公开访问,因为它们部分直接包含在项目视图中,例如/var/static/lib/jquery/dist/jquery.min.js Otherwise I glue them together with a custom grunt task. 否则,我将它们与自定义的咕unt声任务粘合在一起。

Currently grunt puts the dependencies into the node_modules folder within the project root. 当前,grunt将依赖项放入项目根目录下的node_modules文件夹中。 How can I say grunt to put those certain dependencies into the public /var/static/lib/ folder? 我该怎么说呢,把那些特定的依赖项放到公共的/var/static/lib/文件夹中?

Meanwhile I have solved this by using: https://www.npmjs.com/package/grunt-contrib-copy 同时,我已经通过使用以下方法解决了此问题: https : //www.npmjs.com/package/grunt-contrib-copy

With this module you can copy required files to your project directory: 使用此模块,您可以将所需文件复制到项目目录:

    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'}],
        },
     }

There are some alternatives like https://www.npmjs.com/package/grunt-copy-deps which only copy the concrete library files. 有一些替代方法,例如https://www.npmjs.com/package/grunt-copy-deps ,它们仅复制具体的库文件。 This is way more comfortable but I like the flexibility of just copying what I need. 这样比较舒适,但是我喜欢仅复制所需内容的灵活性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM