eyeglass
Version:
Sass modules for npm.
88 lines • 4.07 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const URI_1 = require("../util/URI");
const ImportUtilities_1 = __importDefault(require("./ImportUtilities"));
const EyeglassModules_1 = require("../modules/EyeglassModules");
const typescriptUtils_1 = require("../util/typescriptUtils");
// import pattern matches `assets` and `foo/assets`, but not `foo/bar/assets`
const rAssetsImport = /^(?:([^/]+)\/)?assets$/;
const AssetImporter = function (eyeglass, sass, options, fallbackImporter) {
return ImportUtilities_1.default.createImporter("assets", function (uri, prev, done) {
let importUtils = new ImportUtilities_1.default(eyeglass, sass, options, fallbackImporter, this);
let isRealFile = fs_1.existsSync(prev);
let mod;
function importAssetsFor(mod) {
let file = "autoGenerated:" + URI_1.URI.join(mod.name || EyeglassModules_1.ROOT_NAME, "assets");
let contents;
function getAssetImport() {
// XXX what is the correct behavior for when mod.name isn't found?
return mod.assets ? mod.assets.asAssetImport(mod.name) : "";
}
// allow build tools to specify a function to cache the imports
if (typescriptUtils_1.isPresent(mod.assets) && typescriptUtils_1.isPresent(options.assetsCache)) {
contents = options.assetsCache.bind(options.assetsCache, mod.assets.cacheKey(mod.name || EyeglassModules_1.ROOT_NAME), getAssetImport);
}
else {
contents = getAssetImport;
}
importUtils.importOnce({ file, contents }, done);
}
let isRelativeImport = URI_1.URI.isRelative(uri);
let assetsImport = !isRelativeImport && rAssetsImport.exec(uri);
// the first fragment of the match is the module name
let moduleName = assetsImport && assetsImport[1];
// if it's not an assets import, or it's `eyeglass/assets`,
// just use the fallback importer
if (!assetsImport) {
importUtils.fallback(uri, prev, done, function () {
done(null);
});
}
else {
// if the module name wasn't specified in the import,
// infer it from the origin
if (!moduleName && isRealFile) {
mod = eyeglass.modules.findByPath(prev);
// if it's an eyeglass module...
if (mod && mod.isEyeglassModule) {
// use the module's name
moduleName = mod.name;
}
}
// if we have a module name...
if (moduleName) {
// try to access the module
mod = eyeglass.modules.access(moduleName, isRealFile ? prev : options.eyeglass.root);
// if we can access it...
if (mod) {
/* eslint max-depth:0 */
// if it has assets...
if (mod.assets) {
// import them
importAssetsFor(mod);
}
else {
// otherwise throw an error
done(new Error("No assets specified for eyeglass plugin " + mod.name));
}
}
else {
// if we can't find/access it, throw an error
done(new Error("No eyeglass plugin named: " + moduleName));
}
}
else {
// otherwise, import the app assets
// TODO: Move app assets to the root module and pass the root eyeglass
// module here
importAssetsFor(eyeglass);
}
}
});
};
exports.default = AssetImporter;
//# sourceMappingURL=AssetImporter.js.map