How to use the sass.js.importer function in sass

To help you get started, we’ve selected a few sass examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dougludlow / plugin-sass / src / sass-builder.js View on Github external
if (err) {
        reject(err);
      } else {
        resolve(data);
      }
    });
  });
}

function fromFileURL(url) {
  const address = decodeURIComponent(url.replace(/^file:(\/+)?/i, ''));
  return path.sep === '/' ? `/${address}` : address.replace(/\//g, '\\');
}

// intercept file loading requests (@import directive) from libsass
sass.importer(async (request, done) => {
  // Currently only supporting scss imports due to
  // https://github.com/sass/libsass/issues/1695
  let content;
  let resolved;
  let readImportPath;
  let readPartialPath;
  try {
    resolved = await resolvePath(request);
    const partialUrl = resolved.replace(/\/([^/]*)$/, '/_$1');
    readImportPath = fromFileURL(resolved);
    readPartialPath = fromFileURL(partialUrl);
    content = await loadFile(readPartialPath);
  } catch (e) {
    try {
      content = await loadFile(readImportPath);
    } catch (er) {
github vlocityinc / vlocity_build / lib / datapacksbuilder.js View on Github external
DataPacksBuilder.prototype.compile = function (lang, source, options, cb) {
  // This function contains the core code to execute compilation of source data
  // add addtional languages here to support more compilation types
  switch (lang) {
    case 'scss':
      // intercept file loading requests from libsass
      sass.importer((request, done) => {
        // (object) request
        // (string) request.current path libsass wants to load (content of »@import "
github NickHeiner / sassjs-loader / index.js View on Github external
module.exports = function(content) {
    var callback = this.async();

    sassJs.importer(function(request, done) {
        // Adapted from
        // eslint-disable-next-line max-len 
        // https://github.com/amiramw/grunt-contrib-sassjs/blob/a65f869df967a4e417c4260fd93239e4f0bc55ee/tasks/sass.js#L11
		if (request.path) {
			done();
		} else if (request.resolved) {
			var realPath = request.resolved.replace(/^\/sass\//, ''),
                pathVariations = sassJs.getPathVariations(realPath);

            q.all(_.map(pathVariations, function(pathVariation) {
                return qFs.read(pathVariation)
                    .then(function(fileContents) {
                        return {
                            path: pathVariation,
                            content: fileContents
                        };
github NickHeiner / sassjs-loader / sassjs-demo.js View on Github external
var sassJs = require('sass.js'),
    path = require('path');

sassJs.importer(function(request, done) {
    console.log(request);
    done();
});

sassJs.compile(
    '@import "posix"', 
    {
        inputPath: path.posix.join('nested', 'directory', 'file.scss')
    }, function() {});

sassJs.compile(
    '@import "win32"', 
    {
        inputPath: path.win32.join('nested', 'directory', 'file.scss')
    }, function() {});
github IgniteUI / igniteui-angular-samples / live-editing / services / SassCompiler.ts View on Github external
private static _initSassJsImporter(shouldLogStatus: boolean) {
        sassJs.importer(function(request, done) {
            let basePath = path.join(__dirname, "../../");
            if (request.current.indexOf("~igniteui-angular") !== -1) {
                let fullImportPath = request.current.replace("~", "node_modules/");
                let importFilePath = path.join(basePath, fullImportPath);
                importFilePath = SassCompiler._convertSassImportToFilePath(importFilePath);
                let importFileContent = fs.readFileSync(importFilePath, "utf8");
                if (shouldLogStatus) {
                    console.log("Compiling " + importFilePath);
                }

                done({
                    content: importFileContent,
                    path: importFilePath
                });
            } else if (request.previous) {
                let importFilePath = SassCompiler._getFilePathFromImport(request.current, request.previous);
github amiramw / grunt-contrib-sassjs / tasks / sass.js View on Github external
module.exports = function (grunt) {
	Sass.importer(function (request, done) {
		if (request.path) {
			done();
		} else if (request.resolved) {
			var realPath = request.resolved.replace(/^\/sass\//, "");
			done(Sass.getPathVariations(realPath).reduce(function (found, path) {
				if (found) {
					return found;
				}
				if (grunt.file.exists(path) && !grunt.file.isDir(path)) {
					return {
						path: request.resolved.substr(0, request.resolved.lastIndexOf('/') + 1) + PATH.basename(path),
						content: grunt.file.read(path)
					};
				}
				return null;
			}, null));
github imdreamrunner / browser-vue-loader / src / processors / sass / index.js View on Github external
const compileSass = (key, source, indentedSyntax = false) => {
  const parsedKey = urlParse(key)
  const {origin, pathname} = parsedKey

  const options = {}

  options.indentedSyntax = indentedSyntax
  options.inputPath = pathname

  sass.importer(async (request, done) => {
    const possiblePaths = sass.getPathVariations(request.resolved)
    for (let path of possiblePaths) {
      const fullpath = origin + path
      if (await checkResourceByUrl(fullpath)) {
        const content = await fetchContent(fullpath)
        done({content})
        break
      }
    }
  })

  return new Promise(resolve => {
    sass.compile(source, options, result => {
      resolve(result)
    })
  })