首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么Grunt Copy会给我一个'undefined is not a function‘的警告?

为什么Grunt Copy会给我一个'undefined is not a function‘的警告?
EN

Stack Overflow用户
提问于 2015-04-03 06:28:26
回答 2查看 2.2K关注 0票数 0

我使用Grunt作为构建工具(令人惊讶),使用grunt-contrib-copy作为调试和早期开发工具。我已经检查过我的代码很多次了,我不明白为什么我会得到一个错误。这是我的代码。

Gruntfile.js

代码语言:javascript
复制
module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),
        concurrent: {
            watch: {
                options: {
                    logConcurrentOutput: true
                },
                tasks: ['watch:css', 'watch:html', 'watch:js', 'watch:img']
            }
        },
        watch: {
            css: {
                files: ['./src/scss/*.scss'],
                tasks: ['sass', 'autoprefixer']
            },
            js: {
                files: ['./src/js/*.js'],
                tasks: ['copy:js']
            },
            html: {
                files: ['./src/html/*.html'],
                tasks: ['copy:html']
            },
            img: {
                files: ['./src/img/*'],
                tasks: ['copy:img']
            }
        },
        sass: {
            files: {
                expand: true,
                cwd: './src/scss/',
                src: ['*.scss'],
                dest: './src/no-prefix-css',
                ext: '.css'
            }
        },
        autoprefixer: {
            options: {
                browsers: ['all']
            },
            files: {
                expand: true,
                cwd: './src/no-prefix-css',
                src: ['*.css'],
                dest: './build/css/',
            }
        },





        // Affected code
        copy: {
            html: {
                files: {
                    expand: true,
                    cwd: './src/html/',
                    src: ['*.html'],
                    dest: './build/html/'
                }
            },
            js: {
                files: {
                    expand: true,
                    cwd: './src/js/',
                    src: ['*.js'],
                    dest: './build/js/'
                }
            },
            img: {
                files: {
                    expand: true,
                    cwd: './src/img/',
                    src: ['*.bmp', '*.jpg', '*.jpeg', '*.svg', '*.png'],
                    dest: './build/img/'
                }
            }
        }
    });

    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks('grunt-contrib-sass');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-autoprefixer');
    grunt.loadNpmTasks('grunt-concurrent');

    grunt.registerTask('default', 'Run build tools', ['concurrent:watch']);
}

以下是我运行grunt copy:html -v时的输出

代码语言:javascript
复制
Initializing
Command-line options: --verbose

Reading "Gruntfile.js" Gruntfile...OK

Registering Gruntfile tasks.
Reading package.json...OK
Parsing package.json...OK
Initializing config...OK

Registering "grunt-contrib-watch" local Npm module tasks.
Reading C:\Projects\tonal-music-visualizer\node_modules\grunt-contrib-watch\pack
age.json...OK
Parsing C:\Projects\tonal-music-visualizer\node_modules\grunt-contrib-watch\pack
age.json...OK
Loading "watch.js" tasks...OK
+ watch

Registering "grunt-contrib-sass" local Npm module tasks.
Reading C:\Projects\tonal-music-visualizer\node_modules\grunt-contrib-sass\packa
ge.json...OK
Parsing C:\Projects\tonal-music-visualizer\node_modules\grunt-contrib-sass\packa
ge.json...OK
Loading "sass.js" tasks...OK
+ sass

Registering "grunt-contrib-copy" local Npm module tasks.
Reading C:\Projects\tonal-music-visualizer\node_modules\grunt-contrib-copy\packa
ge.json...OK
Parsing C:\Projects\tonal-music-visualizer\node_modules\grunt-contrib-copy\packa
ge.json...OK
Loading "copy.js" tasks...OK
+ copy

Registering "grunt-autoprefixer" local Npm module tasks.
Reading C:\Projects\tonal-music-visualizer\node_modules\grunt-autoprefixer\packa
ge.json...OK
Parsing C:\Projects\tonal-music-visualizer\node_modules\grunt-autoprefixer\packa
ge.json...OK
Loading "autoprefixer.js" tasks...OK
+ autoprefixer

Registering "grunt-concurrent" local Npm module tasks.
Reading C:\Projects\tonal-music-visualizer\node_modules\grunt-concurrent\package
.json...OK
Parsing C:\Projects\tonal-music-visualizer\node_modules\grunt-concurrent\package
.json...OK
Loading "concurrent.js" tasks...OK
+ concurrent
Loading "Gruntfile.js" tasks...OK
+ default

Running tasks: copy:html

Running "copy:html" (copy) task
Verifying property copy.html exists in config...OK
Warning: undefined is not a function Use --force to continue.

Aborted due to warnings.
EN

回答 2

Stack Overflow用户

发布于 2015-04-08 22:22:36

尽管这不起作用:

代码语言:javascript
复制
    copy: {
        buildImages: {
            files: {
                    expand: true,
                    cwd: paths.dev.cssImages,
                    src: '**/*',
                    dest: paths.prod.cssImages,
                    filter: 'isFile'
                }
        },
        buildBlub: {
            files: {
                    expand: true,
                    cwd: paths.dev.cssImages,
                    src: '**/*',
                    dest: paths.prod.cssImages,
                    filter: 'isFile'
                }
        }
    }

..this执行以下操作:

代码语言:javascript
复制
    copy: {
        buildImages: {
            files: [
                {
                    expand: true,
                    cwd: paths.dev.cssImages,
                    src: '**/*',
                    dest: paths.prod.cssImages,
                    filter: 'isFile'
                }
            ]
        },
        buildBlub: {
            files: [
                {
                    expand: true,
                    cwd: paths.dev.cssImages,
                    src: '**/*',
                    dest: paths.prod.cssImages,
                    filter: 'isFile'
                }
            ]
        }

    }

"files“只需要一个对象数组,如果只给出一个复制任务,为什么它只处理一个对象。没有线索:)

票数 2
EN

Stack Overflow用户

发布于 2015-04-03 09:01:31

我不确定为什么会这样,但对于那些有类似问题的人来说,这就是我的代码的错误所在:

Copy不允许我在其中创建子对象,就像我认为所有Grunt命令都允许的那样。为了解决我的问题,我修改了我的代码,如下所示:

代码语言:javascript
复制
copy: {
    default: {
        files: [
        {
            expand: true,
            cwd: './src/html/',
            src: ['*.html'],
            dest: './build/html/'
        },
        {
            expand: true,
            cwd: './src/js/',
            src: ['*.js'],
            dest: './build/js/'
        },
        {
            expand: true,
            cwd: './src/img/',
            src: ['*'],
            dest: './build/img/'
        }]
    }
}

显然,copy需要一个子对象,但它不能处理多个子对象。如果有人能为这个功能提供一个理由,我会非常高兴。似乎这可能被认为是grunt-contrib-copy中的一个错误,或者我的发现是不正确的。我都不喜欢。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29423535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档