How do I use grunt-sweet?

128 Views Asked by At

I would like to set up grunt sweet so that I can have it start watching my sweetjs-using files and compile them as they change.

However, I am new to making gruntfiles and there is simply not enough information in that link for me to know how to set it up. Searching for files that use grunt.loadNpmTasks('grunt-sweet'); I was able to find a file to use as a template but I cannot get it working.

This gruntfile

/*global module:false*/
module.exports = function(grunt) {
'use strict';

grunt.initConfig({
    sweet: {
        content_dir: 'src',
        publish_dir: 'build',
    },
    watch: {
        sweet: {
            files: [
                'src/**'
            ],
            tasks: 'sweet'
        }
    }
});

grunt.loadNpmTasks('grunt-sweet');

// Project tasks
grunt.registerTask('default', 'sweet');
};

Gives me

W:\sweetjs-playground> grunt sweet:watch
Running "sweet:watch" (sweet) task
Fatal error: Object #<Object> has no method 'done'
W:\sweetjs-playground> grunt sweet -v
Initializing
Command-line options: --verbose

Reading "Gruntfile.js" Gruntfile...OK

Registering Gruntfile tasks.
Initializing config...OK

Registering "grunt-sweet" local Npm module tasks.
Reading W:\sweetjs-playground\node_modules\grunt-sweet\package.json...OK
Parsing W:\sweetjs-playground\node_modules\grunt-sweet\package.json...OK
Loading "sweet.js" tasks...OK
+ sweet
Loading "Gruntfile.js" tasks...OK
+ default

Running tasks: sweet

Running "sweet" task
Verifying property sweet exists in config...OK
Fatal error: Object #<Object> has no method 'done'

Which seems like a success until it's not. What am I doing wrong here?

All I want is for it to automatically compile files in src into build

2

There are 2 best solutions below

0
On

I'm not an expert on this, but you can try out this snippet. I hope it can help some way.

 module.exports = function(grunt) {
        grunt.initConfig({
            sweetjs: {
                src: {
                    files: [{
                      expand: true,
                      cwd: 'src/',
                      src: ['**/*.js'],
                      dest: 'build/'
                    }]
                }
            },
            watch: {
                sweetjs: {
                    files: ['src/**/*.js'],
                    tasks: ['sweetjs:src']
                }
            }
        });

        grunt.event.on('watch', function(action, filepath, target) {
            if(action == 'changed' && target == 'sweetjs') {
                grunt.config.set('sweetjs.src.src', [filepath]);
                grunt.config.set('sweetjs.src.dest', filepath.replace(/^src/, 'build'));
            }
        });

        grunt.loadNpmTasks('grunt-contrib-watch');
        grunt.loadNpmTasks('grunt-sweet.js');

        grunt.registerTask('default', ['sweetjs']);
    };

If it wont work, maybe your files path is the problem. Try out adding an extension src: ['src/**/*{.php,.html}'],

0
On

i dont know if thats the reason for your error, but i think so. delete the comma after sweets last config-property:

publish_dir: 'build', <--