Skip to content

Importers #1392

Closed
Closed

Description

I've just created a ticket on the Ruby bindings repo here cowboyd/less.rb#65 but thinking that the issue may come down to Less itself.

Less assumes that @import statements should look for a file on the filesystem, which is not always the case for example when their content is stored in a database.

Sass has the notion of 'Importers' (http://sass-lang.com/docs/yardoc/Sass/Importers.html), does Less have anything like this? ...otherwise it's a case of hacking up something to catch the filesystem calls and reroute them which is a horrible solution.

A much cleaner solution would be:

var parser = new(less.Parser)({
  importer: function(filename) {
    return db.findMyFile(filename);
  },
  filename: 'style.less' // Filename, for better error messages
});

parser.parse('@import("from-bd.less"); p { .a-mixin; }', function (e, tree) {
  tree.toCSS({ compress: true });
});

Activity

  1. matthew-dean commented on Jun 30, 2013

    @matthew-dean
    Member

    Yep. You can write a file importer exactly as you describe by overriding the less.Parser.importer function. You'll notice in the browser build that the browser environment does just that.

    However, that behavior is not currently consistent or centralized for all types of file operations. For instance, the data-uri handler assumes a Node file environment and does not have any of the same kinds of hooks as @import statements.

    Moving forward, LESS will (hopefully) eventually have a more graceful environment plugin model that allows you to handle file operations however you wish.

  2. matthew-dean commented on Dec 15, 2013

    @matthew-dean
    Member

    Sorry for the delay. As far as your specific issue, writing a file importer is possible today. But I agree, there are places where there is a file request that doesn't actually use the same file import method. Reopening.

  3. jonschlinkert commented on Dec 15, 2013

    @jonschlinkert
    Contributor

    I think this is a good call. agreed that this would be useful

  4. matthew-dean commented on Dec 21, 2013

    @matthew-dean
    Member

    Btw, if this helps, Crunch currently has an importer that looks like this:

    less.Parser.importer = function(path, paths, callback, env) {
            var entryPath = (paths.entryPath && paths.entryPath != "")
                ? paths.entryPath : env.rootpath;
            var file = Paths.project.resolvePath(entryPath).resolvePath(path);
    
            // Adopted from the Node.js implementation
            if(file.exists) {
                    var fileStream = new air.FileStream();
                    fileStream.open(file, air.FileMode.READ);
                    var fileData = fileStream.readUTFBytes(fileStream.bytesAvailable);
                    fileStream.close();
    
                    new (less.Parser)({
                        rootpath: env.rootpath,
                        relativeUrls: false,
                        filename : file.nativePath
                    }).parse(fileData, function(e, root) {
                            callback(e, root, fileData);
                    });
            } else {
                    if( typeof (env.errback) === "function") {
                            env.errback.call(null, path, paths, callback, env);
                    } else {
                            callback({
                                    type : 'File',
                                    message : "'" + file.nativePath + "' wasn't found.\n"
                            }, env.rootpath, file.nativePath);
                    }
            }
    
    };

    You can create your importer, and basically do whatever you want in that operation. And then you just return the fileData to the callback.

  5. matthew-dean commented on Dec 21, 2013

    @matthew-dean
    Member

    I should note that this importer was originally based on an older version of LESS when the API / callback / error-handling structure was all very different (although updated and somewhat tested with 1.5.1), so please don't take it as a best example. I'd love it if @lukeapage reviewed this.

  6. lukeapage commented on Jan 4, 2015

    @lukeapage
    Member
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions