Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using grunt.config to access JSON object #542

Open
johnruane opened this issue Jun 8, 2017 · 1 comment
Open

Using grunt.config to access JSON object #542

johnruane opened this issue Jun 8, 2017 · 1 comment

Comments

@johnruane
Copy link

Can anyone help me access a JSON object using the grunt.config command?

The filepath i want to replace on my 'watch' event is formatted as so:

render: {
  files: [{
    expand: true,
    src: "**/*.nunjucks",
    dest: "build/",
    ext: ".html"
  }]
}

I have tried every combination to access the 'src' property but none work. The documentation only gives the following format as an example: grunt.config('render.files.src', filepath);

@johnruane johnruane changed the title grunt.config JSON format Using grunt.config to access JSON object Jun 8, 2017
@johnruane
Copy link
Author

I have figured out the correct syntax, but the tasks is running against all files and not just the changed ones. Can confirm the 'src' is being updated, but the task runs against all files.

module.exports = function(grunt) {
  var changedFiles = Object.create(null);
  var onChange = grunt.util._.debounce(function() {
    grunt.config('nunjucks.render.files.src', Object.keys(changedFiles));
    grunt.log.writeln(grunt.config('nunjucks.render.files.src'));
  }, 200);

  grunt.initConfig({
      nunjucks: {
        options: {
          data: grunt.file.readJSON('data.json'),
          paths: 'html'
        },
      render: {
        files: [ {
	  expand: true,
	  src: ['**/*.nunjucks'],
	  dest: "build/",
	  ext: ".html"
	}]
      }
   },
   watch: {
     nunjucks: {
       files: ['html/**/*'],
       tasks: ['nunjucks'],
       options: {
       spawn: false
     }
  }
}
});

grunt.event.on('watch', function(action, filepath) {
  changedFiles[filepath] = action;
  onChange();
});

grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-nunjucks-2-html');
  grunt.registerTask('default', ['nunjucks', 'watch']);
};

@johnruane johnruane reopened this Jun 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant