简体   繁体   中英

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

My package.json file looks like this:

{
  "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, ...

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. The whole project is not public, just everything within /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 . Otherwise I glue them together with a custom grunt task.

Currently grunt puts the dependencies into the node_modules folder within the project root. How can I say grunt to put those certain dependencies into the public /var/static/lib/ folder?

Meanwhile I have solved this by using: 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. This is way more comfortable but I like the flexibility of just copying what I need.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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