-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
232 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters