Grunt cwd option issue

2019-08-12 16:50发布

I am trying to configure grunt-svg-sprite plugin in my project and as per documentation here http://gruntjs.com/configuring-tasks:

cwd All src matches are relative to (but don't include) this path.

src Pattern(s) to match, relative to the cwd.

And as per https://www.npmjs.com/package/grunt-svg-sprite

However, as the path/to/assets would become part of the shape IDs, you will most likely want to add a working directory in most cases:

your_target: {
    expand      : true,
    cwd         : 'path/to/assets',
    src         : ['**/*.svg'],
    dest        : 'path/to/css/dir',
    options     : {
        // Target-specific options 
    }
},

Now, my grunt file has section:

    svg_sprites: {
        options: {

        },
        cw_sprites: {
            expand: true,
            cwd: 'public/assets/dist/svgs/',
            src: ['**/*.svg'],
            dest: 'public/assets/stylesheets/svg-sprites',
            options: {
                shape : {
                    dimension: {
                        maxWidth: 32,
                        maxHeight: 32,
                    },
                    spacing: {
                        padding:0,
                    }

                },
                mode: {
                    css : {
                        render: {
                            css: true
                        },
                        example:true
                    },
                    less: true,
                }
            }


        }
    },

when I run the above task with the --verbose option, I see the following in the output:

....
Reading public/assets/dist/svgs/uncollapsed-triangle.svg...OK
Reading public/assets/fonts/glyphicons-halflings-regular.svg...OK
Reading public/assets/fonts/myraid-pro/MyriadPro-Regular.svg...OK
Warning: Cannot read property 'replace' of undefined Use --force to continue.

As per the log above, it is trying to read other directories as well that are outside the CWD path public/assets/dist/svgs. In fact, it goes on to read the root directory of project containing bower_components etc. and scans the entire project root.

I thought using the cwd option with src option would restrict it to sub-directories of 'cwd' or directories relative to 'cwd'. Why are other directories being scanned?

Am I missing something? Thank you for your help.

1条回答
叛逆
2楼-- · 2019-08-12 17:20

Okay, I think I finally figured it out. There are actually two projects with very similar names:

  1. grunt-svg-sprite (https://www.npmjs.com/package/grunt-svg-sprite) and
  2. grunt-svg-sprites (https://www.npmjs.com/package/grunt-svg-sprites)

I wanted to use the first project but installed the second one by mistake!!!!! Obviously it wouldn't have worked :-)

I removed the erroneous module (2) and installed the first one - it works like a charm. Makes me wonder if the naming convention needs some thinking for npm modules.

查看更多
登录 后发表回答