Skip to content

Commit

Permalink
Merge branch 'release/2.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
maoberlehner committed Oct 31, 2016
2 parents bd0db3f + f24429d commit 3f0a647
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ sass.render({

### CLI
```bash
node-sass --importer node_modules/node-sass-magic-importer -o dist src/index.scss
node-sass --importer node_modules/node-sass-magic-importer/dist/cli.js -o dist src/index.scss
```

## About
Expand Down
208 changes: 208 additions & 0 deletions dist/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
'use strict';

function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }

var path = _interopDefault(require('path'));
var GlobImporter = _interopDefault(require('node-sass-glob-importer/dist/GlobImporter.js'));
var PackageImporter = _interopDefault(require('node-sass-package-importer/dist/PackageImporter.js'));
var SelectorImporter = _interopDefault(require('node-sass-selector-importer/dist/SelectorImporter.js'));
var fs = _interopDefault(require('fs'));
var uniqueConcat = _interopDefault(require('unique-concat'));

/**
* Selector specific imports, module importing,
* globbing support, import files only once.
*/
var MagicImporter = function MagicImporter(options) {
if ( options === void 0 ) options = {};

var defaultOptions = {
cwd: process.cwd(),
includePaths: [process.cwd()],
extensions: [
".scss",
".sass"
],
packageKeys: [
"sass",
"scss",
"style",
"css",
"main.sass",
"main.scss",
"main.style",
"main.css",
"main"
]
};
/** @type {Object} */
this.options = Object.assign({}, defaultOptions, options);
/** @type {Object} */
this.onceStore = {};
};

/**
* Find the absolute URL for a given relative URL.
* @param {string} url - Import url from node-sass.
* @return {string} Absolute import url.
*/
MagicImporter.prototype.getAbsoluteUrl = function getAbsoluteUrl (url) {
var absoluteUrl = url;
if (!path.isAbsolute(url)) {
this.options.includePaths.some(function (includePath) {
try {
absoluteUrl = path.normalize(path.join(includePath, absoluteUrl));
return fs.statSync(absoluteUrl).isFile();
} catch (e) {}
return false;
});
}
return absoluteUrl;
};

/**
* Store the given URL and selector filters
* and determine if the URL should be imported.
* @param {string} url - Import url from node-sass.
* @param {Array} selectorFilters - CSS selectors and replacement selectors.
* @return {boolean|Object} - Absolute URL and selector filters or false.
*/
MagicImporter.prototype.store = function store (url, selectorFilters) {
var this$1 = this;
if ( selectorFilters === void 0 ) selectorFilters = null;

var absoluteUrl = this.getAbsoluteUrl(url);

// URL is not in store: store and load the URL.
if (this.onceStore[absoluteUrl] === undefined) {
this.onceStore[absoluteUrl] = selectorFilters;
return { url: absoluteUrl, selectorFilters: selectorFilters };
}

// URL is in store without filters, filters given: load the URL.
if (this.onceStore[absoluteUrl] === null && selectorFilters) {
// eslint-disable-next-line no-console
console.warn(("Warning: double import of file \"" + url + "\""));
return { url: absoluteUrl, selectorFilters: selectorFilters };
}

// URL and filters in store, URL without filters given:
// load and remove filters from store.
if (this.onceStore[absoluteUrl] && !selectorFilters) {
// eslint-disable-next-line no-console
console.warn(("Warning: double import of file \"" + url + "\""));
this.onceStore[absoluteUrl] = null;
return { url: absoluteUrl, selectorFilters: selectorFilters };
}

// URL and filters in store, URL with same and other filters given:
// only load other filters that not already are stored.
if (this.onceStore[absoluteUrl] && selectorFilters) {
var concatSelectorFilters = uniqueConcat(
this.onceStore[absoluteUrl],
selectorFilters
);
// If stored and given selector filters are identically, do not load.
if (JSON.stringify(concatSelectorFilters) !== JSON.stringify(this.onceStore[absoluteUrl])) {
var selectorFiltersDiff = selectorFilters.filter(function (x) { return !this$1.onceStore[absoluteUrl].some(function (y) { return JSON.stringify(x) === JSON.stringify(y); }); }
);
this.onceStore[absoluteUrl] = concatSelectorFilters;
return { url: absoluteUrl, selectorFilters: selectorFiltersDiff };
}
}
return false;
};

/**
* Synchronously resolve the path to a node-sass import url.
* @param {string} url - Import url from node-sass.
* @return {string} Fully resolved import url or null.
*/
MagicImporter.prototype.resolveSync = function resolveSync (url) {
var this$1 = this;

var data = null;
var resolvedUrl = url;

// Parse url to eventually extract selector filters.
var selectorImporter = new SelectorImporter();
selectorImporter.options = this.options;
var urlData = selectorImporter.parseUrl(resolvedUrl);
resolvedUrl = urlData.url;
var selectorFilters = urlData.selectorFilters;

// Try to resolve glob pattern url.
var globImporter = new GlobImporter();
var globFiles = globImporter.resolveSync(resolvedUrl, this.options.includePaths);
if (globFiles) {
return { contents: globFiles.map(function (x) {
this$1.store(x);
return fs.readFileSync(x, { encoding: "utf8" });
}).join("\n") };
}

// Try to resolve a module url.
var packageImporter = new PackageImporter();
packageImporter.options = this.options;
var packageFile = packageImporter.resolveSync(resolvedUrl);
if (packageFile) {
resolvedUrl = packageFile;
data = { file: resolvedUrl };
}

var storedData = this.store(resolvedUrl, selectorFilters);

// If the file is already stored and should not be loaded,
// prevent node-sass from importing the file again.
if (!storedData) {
return {
file: "",
contents: ""
};
}

resolvedUrl = storedData.url;
selectorFilters = storedData.selectorFilters;

// Filter selectors.
var filteredContents = selectorImporter.extractSelectors(resolvedUrl, selectorFilters);
if (filteredContents) {
data = { contents: filteredContents };
}

return data;
};

/**
* Asynchronously resolve the path to a node-sass import url.
* @param {string} url - Import url from node-sass.
* @return {Promise} Promise for a fully resolved import url.
*/
MagicImporter.prototype.resolve = function resolve (url) {
var this$1 = this;

return new Promise(function (promiseResolve) {
promiseResolve(this$1.resolveSync(url));
});
};

var magicImporter = new MagicImporter();

/**
* Magic importer for node-sass
* @param {string} url - The path in import as-is, which LibSass encountered.
* @param {string} prev - The previously resolved path.
*/
var cli = function (url, prev) {
// Create an array of include paths to search for files.
var includePaths = [];
if (path.isAbsolute(prev)) {
includePaths.push(path.dirname(prev));
}
magicImporter.options.includePaths = includePaths
.concat(this.options.includePaths.split(path.delimiter));

return magicImporter.resolveSync(url);
};

module.exports = cli;
21 changes: 21 additions & 0 deletions js/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import path from 'path';
import MagicImporter from './lib/MagicImporter.js';

const magicImporter = new MagicImporter();

/**
* Magic importer for node-sass
* @param {string} url - The path in import as-is, which LibSass encountered.
* @param {string} prev - The previously resolved path.
*/
export default function (url, prev) {
// Create an array of include paths to search for files.
const includePaths = [];
if (path.isAbsolute(prev)) {
includePaths.push(path.dirname(prev));
}
magicImporter.options.includePaths = includePaths
.concat(this.options.includePaths.split(path.delimiter));

return magicImporter.resolveSync(url);
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"sass",
"selector-importer"
],
"version": "2.0.0",
"version": "2.1.0",
"author": "Markus Oberlehner",
"homepage": "https://github.com/maoberlehner/node-sass-magic-importer",
"license": "MIT",
Expand Down Expand Up @@ -45,7 +45,7 @@
"main": "dist/index.js",
"jsnext:main": "js/index.js",
"scripts": {
"scripts:bundle": "rollup --config --format cjs --output dist/index.js js/index.js && rollup --config --format cjs --output dist/MagicImporter.js js/lib/MagicImporter.js",
"scripts:bundle": "rollup --config --format cjs --output dist/index.js js/index.js && rollup --config --format cjs --output dist/cli.js js/cli.js && rollup --config --format cjs --output dist/MagicImporter.js js/lib/MagicImporter.js",
"scripts:lint": "eslint js/* test/*",
"scripts:lint-no-error": "eslint js/* test/* || exit 0",
"scripts:doc": "esdoc -c .esdocrc",
Expand Down

0 comments on commit 3f0a647

Please sign in to comment.