eyeglass
Version:
Sass modules for npm.
177 lines • 7.59 kB
JavaScript
"use strict";
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const EyeglassModules_1 = __importStar(require("./modules/EyeglassModules"));
const ModuleFunctions_1 = __importDefault(require("./modules/ModuleFunctions"));
const ModuleImporter_1 = __importDefault(require("./importers/ModuleImporter"));
const AssetImporter_1 = __importDefault(require("./importers/AssetImporter"));
const FSImporter_1 = __importDefault(require("./importers/FSImporter"));
const Options_1 = __importDefault(require("./util/Options"));
const Assets_1 = __importDefault(require("./assets/Assets"));
const deprecator_1 = __importDefault(require("./util/deprecator"));
const semverChecker_1 = __importDefault(require("./util/semverChecker"));
const fs = __importStar(require("fs-extra"));
const SassImplementation_1 = require("./util/SassImplementation");
const heimdall = require("heimdalljs");
const SimpleCache_1 = require("./util/SimpleCache");
const perf_1 = require("./util/perf");
const version_1 = __importDefault(require("./util/version"));
const debug_1 = __importDefault(require("debug"));
const debug = debug_1.default("eyeglass:initialization");
function resetGlobalCaches() {
EyeglassModules_1.resetGlobalCaches();
perf_1.resetGlobalCaches();
}
exports.resetGlobalCaches = resetGlobalCaches;
class Eyeglass {
constructor(options) {
if (arguments.length === 2) {
_forbidNodeSassArg(arguments[1]);
}
let timer = heimdall.start("eyeglass:instantiation");
this.onceCache = new SimpleCache_1.SimpleCache();
try {
// an interface for deprecation warnings
this.deprecate = deprecator_1.default(options);
this.options = new Options_1.default(options);
this.assets = new Assets_1.default(this, this.options.eyeglass.engines.sass);
this.modules = new EyeglassModules_1.default(this.options.eyeglass.root, this.options, this.options.eyeglass.modules);
fs.mkdirpSync(this.options.eyeglass.cacheDir);
semverChecker_1.default(this, this.options.eyeglass.engines.sass, this.options.eyeglass, Eyeglass.VERSION);
checkMissingDependencies.call(this);
checkDependencyConflicts.call(this);
// initialize all the modules
this.modules.init(this, this.options.eyeglass.engines.sass);
// add importers and functions
addImporters.call(this);
addFunctions.call(this);
forbidProperties.call(this, ["enableImportOnce"]);
// auto-add asset paths specified via options
if (this.options.eyeglass.assets.sources) {
for (let assetSource of this.options.eyeglass.assets.sources) {
this.assets.addSource(assetSource.directory, assetSource);
}
}
}
catch (e) {
// typescript needs this catch & throw to convince it that the instance properties are initialized.
throw e;
}
finally {
timer.stop();
}
}
static helpers(sass) {
return SassImplementation_1.helpers(sass);
}
// eslint-disable-next-line no-dupe-class-members
once(key, firstTime, otherwise) {
if (this.onceCache.has(key)) {
if (otherwise) {
return otherwise();
}
else {
return;
}
}
else {
this.onceCache.set(key, true);
return firstTime();
}
}
}
exports.default = Eyeglass;
Eyeglass.VERSION = version_1.default.string;
function _forbidNodeSassArg(arg) {
if (SassImplementation_1.isSassImplementation(arg)) {
// throw an error
throw new Error([
"You may no longer pass `sass` directly to Eyeglass. Instead pass it as an option:",
"var options = eyeglass({",
" /* sassOptions */",
" ...",
" eyeglass: {",
" engines: {",
" sass: require('node-sass')",
" }",
" }",
"});"
].join("\n "));
}
}
exports._forbidNodeSassArg = _forbidNodeSassArg;
const VERSION_WARNINGS_ISSUED = {};
function checkDependencyConflicts() {
let conflicts = this.modules.issues.dependencies.versions;
let strictMode = this.options.eyeglass.strictModuleVersions;
if (typeof strictMode === "undefined") {
strictMode = "warn";
}
for (let conflict of conflicts) {
let message = `Version conflict for eyeglass module '${conflict.name}': ${conflict.requested.version} was requested but it was globally resolved to ${conflict.resolved.version}.`;
if (strictMode === false) {
debug(message);
}
else if (strictMode === "warn") {
if (!VERSION_WARNINGS_ISSUED[message]) {
console.error(`WARNING: ${message}`);
VERSION_WARNINGS_ISSUED[message] = true;
}
}
else if (strictMode === true) {
throw new Error(message);
}
}
}
function checkMissingDependencies() {
let missing = this.modules.issues.dependencies.missing;
if (missing.length) {
let warning = ["The following dependencies were not found:"];
warning.push.apply(warning, missing.map(function (dep) {
return " " + dep;
}));
warning.push("You might need to `npm install` the above.");
// eslint-disable-next-line no-console
console.warn(warning.join("\n"));
}
}
function addImporters() {
let fsImporter = FSImporter_1.default(this, this.options.eyeglass.engines.sass, this.options, this.options.importer);
let assetImporter = AssetImporter_1.default(this, this.options.eyeglass.engines.sass, this.options, fsImporter);
this.options.importer = ModuleImporter_1.default(this, this.options.eyeglass.engines.sass, this.options, assetImporter);
}
function addFunctions() {
this.options.functions = ModuleFunctions_1.default(this, this.options.eyeglass.engines.sass, this.options, this.options.functions // The type of @types/node-sass/Options["functions"] is bad.
);
}
function forbidProperties(properties) {
for (let prop of properties) {
Object.defineProperty(this, prop, {
get: function () {
throw new Error("The property `" + prop + "` may no longer be accessed directly on eyeglass. " +
"Instead, you'll find the value on `eyeglass.options.eyeglass." + prop + "`");
},
set: function (_value) {
throw new Error("The property `" + prop + "` may no longer be set directly on eyeglass. " +
"Instead, you should pass this as an option to eyeglass:" +
"\n var options = eyeglass({" +
"\n /* sassOptions */" +
"\n ..." +
"\n eyeglass: {" +
"\n " + prop + ": ..." +
"\n }" +
"\n });");
}
});
}
}
//# sourceMappingURL=Eyeglass.js.map