-
Notifications
You must be signed in to change notification settings - Fork 23
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
"this" is undefined when running via grunt. #4
Comments
Hmm... after a nights rest I've come up with a better solution. I also had issues with grunt-watch. Since the "localObj" cache was set on the first run, it would simply not include any files on subsequent runs, since the files were already in the cache. So, my solution for getting this to work with grunt and grunt-contrib-watch is:
Simplified example of Gruntfile.js: var importOnce = require("node-sass-import-once");
grunt.initConfig({
importOnce: { options: {} },
sass: {
options: {
...,
importer: function (uri, prev, done) {
importOnce.call(grunt.config.data.importOnce, uri, prev, done);
},
...
},
...
},
watch: {
scss: {
files: ["/scss/**/*.scss"],
tasks: ["sass"],
options: {
...
}
}
}
grunt.event.on("watch", function (action, filepath) {
grunt.config(["importOnce"], { options: {} });
});
}); I hope this will help others who are having trouble getting this to work with grunt. Cheers. |
I'm closing this as it can be resolved externally. Please consider updating the readme with some of my findings (or something better?). |
The error you're getting is most likely because you're not using the Node Sass >= v3.0.0-alpha.5, which is explicitly required in order to use the importer. |
That, or |
I'm using the latest version of grunt-sass which requires "node-sass": "^3.0.0-", so that's probably it. Other grunt-users will likely be in the same situation until it's updated though. Thanks. EDIT: Hmm... just checked the version of node-sass that grunt-sass installs and it's @2.1.1, but the node-sass repo says: "importer (>= v2.0.0) - experimental" So, node-sass 2.1.1 still supports this feature (experimentally) which, I suppose, is why I can still get it to work. |
EDIT: See my updated solution below
Hi,
I've tried implementing this module in a project, but ran into issues. The error is: "Fatal error: Cannot read property 'options' of undefined".
Maybe I've missed something obvious, but I looked at index.js and it's "this" that's undefined. It fails at line 275 "if (!this.options.importOnce) {".
I haven't fiddled much with node or indeed grunt, so I don't know the "correct" way to fix it. Maybe it needs to be wrapped in some grunt-specific code (all the grunt versions of node-modules seem to suggest it).
I've temporarily made it work, by copying the module and requiring that instead. Inside I've modified all instances of "this" to "localObj", which is defined alongside the other top-level vars:
This works for me (I also removed the bower stuff), but it would be ideal if the module supported grunt better, or a grunt version of it could be made.
The text was updated successfully, but these errors were encountered: