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

"this" is undefined when running via grunt. #4

Closed
mcsagittarius opened this issue Apr 21, 2015 · 5 comments
Closed

"this" is undefined when running via grunt. #4

mcsagittarius opened this issue Apr 21, 2015 · 5 comments

Comments

@mcsagittarius
Copy link

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:

    var fs = require('fs'),
    yaml = require('js-yaml'),
    path = require('path'),
    localObj = {
        options: {
            index: false,
            css: false
        }
    };

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.

@mcsagittarius
Copy link
Author

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:

  1. Require module as normal.
  2. Setup an options object e.g. importOnce: { options: {} } somewhere in the grunt.config tree.
  3. Create anonymous function as importer in your "node-sass" task options.
  4. Call the required importOnce with your options object passed as scope.
  5. Setup an event listener for the "watch" event and reset the options object to empty (or whatever options you initially had).

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.

@mcsagittarius
Copy link
Author

I'm closing this as it can be resolved externally. Please consider updating the readme with some of my findings (or something better?).

@Snugug
Copy link
Member

Snugug commented Apr 22, 2015

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.

@Snugug
Copy link
Member

Snugug commented Apr 22, 2015

That, or grunt-sass is doing something super crazy weird with the way it passes options to Node Sass

@mcsagittarius
Copy link
Author

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.

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

2 participants