'use strict';
var fs = require('fs'),
    path = require('path'),
    mkdirp = require('mkdirp'),
    regex = /@import[^'"]+?['"](.+?)['"];?/g;

function ImportResolver (opts) {
    this.aliases = opts.aliases || {};
    this.cwd = '';
    this.dist = '';
    this.output = opts.output;
    this.ext = ('.' + opts.ext).replace(/\.{2}/, '.');
    this.root = path.resolve(process.cwd(), opts.pathToMain).split(path.sep);
    this.resolveImportStatements = function () {
        var oldFile = '',
            resolved = false,
            filename = this.root.pop();
        filename = this.trimExtension(filename);

        this.root = this.root.join(path.sep) + path.sep;

        this.dist = this.read(filename);

        while (!resolved) {
            oldFile = this.dist;
            this.dist = this.resolve(oldFile);
            if (this.dist === oldFile) {
                resolved = true;
            }
        }

        console.log('\x1b[32m', 'stylesheets concatenated!');

        return this.dist;
    };
}

ImportResolver.prototype.writeToFile = function (dist, fn) {
    fs.writeFile(this.output, dist, function () {
        console.log('\x1b[36m', 'to ', this.output);
        if (fn) {
            fn();
        }
    }.bind(this));
};

ImportResolver.prototype.trimExtension = function (filename) {
    if (filename.indexOf(this.ext) === -1) return filename;
    var prefix = filename.match(/^\.*/);
    prefix = prefix && prefix.length ? prefix[0] : '';
    filename = filename.slice(prefix.length).split('.');
    filename = filename.length > 1 ? filename.slice(0, -1).join('.') : filename[0];
    return prefix + filename;
};

ImportResolver.prototype.getFile = function (filename) {
    var str = '';
    try {
        str = fs.readFileSync(this.cwd + filename, {"encoding": "utf8"});
    } catch (e) {
        try {
            str = fs.readFileSync(this.cwd + '_' + filename, {"encoding": "utf8"});
        } catch (er) {
            console.log('\x1b[36m', 'to ', 'Cannot read file "' + filename + '"');
        }
    }
    return str;
};

ImportResolver.prototype.normalizeImport = function (str) {
    if (regex.test(str)) {
        str = str.replace(regex, function (m, capture) {
            var p,
                pathToImport,
                aliasKeys = Object.keys(this.aliases),
                index = aliasKeys && aliasKeys.length ? aliasKeys.indexOf(capture) : -1;
            if (index !== -1) {
                pathToImport = this.aliases[aliasKeys[index]];
            } else {
                pathToImport = capture;
            }
            p = path.resolve(this.cwd, pathToImport);
            return m && m.replace(capture, p);
        }.bind(this));
    }
    return str;
};

ImportResolver.prototype.read = function (filename) {
    var stylesheet = '',
        dir = filename.split(path.sep);

    filename = dir.pop();
    console.log('\x1b[34m', 'Reading ' + filename);
    filename = this.trimExtension(filename) + this.ext;
    dir = dir.join(path.sep);

    this.cwd = path.resolve(this.root, this.cwd, dir) + path.sep;

    stylesheet = this.getFile(filename);
    stylesheet = this.normalizeImport(stylesheet);

    return stylesheet;
};

ImportResolver.prototype.resolve = function (oldFile) {
    return oldFile.replace(regex, function (m, capture) {
        return this.read(capture);
    }.bind(this));
};

ImportResolver.prototype.write = function (dist, fn) {
    if (this.output) {
        var output = this.output.split(path.sep),
            outPath;
        output.reverse();
        outPath = /\./.test(output[0]) ? output.reverse().slice(0, -1).join(path.sep) : output.reverse().join(path.sep);
        fs.stat(outPath, function (err, stats) {
            if (err) {
                mkdirp.sync(outPath);
            }
            this.writeToFile(dist, fn);
        }.bind(this));
    } else if (fn) {
        fn();
    }
};

module.exports = ImportResolver;


@@ -3230,17 +3230,16 @@
 function
- 
 (oldFile
@@ -3243,83 +3243,481 @@
 ile)
- %7B%0A    return oldFile.replace(regex, function (m, capture) %7B%0A        return
+%7B%0A    %0A    const commentRegex = /((?:%5C/%5C*(?:.%7C%5Cs)*?%5C*%5C/)%7C(?:%5C/%5C/.*))/g;%0A%0A    // remove comments and make a copy of oldFile to keep comments later%0A    let oldFileCopy = oldFile.replace(commentRegex, '');%0A%0A    //get and store imports ..%0A    let matches=%5B%5D;%0A    let tmpResult;%0A    while ((tmpResult = regex.exec(oldFileCopy)) !== null) %7B%0A        matches.push(tmpResult)%0A    %7D%0A    %0A    %0A    matches.forEach( function(match)%7B%0A        %0A        oldFile = oldFile.replace(match%5B0%5D,
  thi
@@ -3727,17 +3727,18 @@
 ead(
-capture);
+match%5B1%5D))
 %0A   
@@ -3743,36 +3743,56 @@
    %7D.bind(this))
+%0A%0A    return oldFile
 ;%0A%7D
-;
+%0A
 %0A%0AImportResolver

Fix for #5