aurelia-useable-style-loader
Version:
Automatically loads/unloads styles using webpack's style-loader/useable
53 lines • 2.46 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { Loader } from 'aurelia-loader';
import { getLogger } from 'aurelia-logging';
export class CSSResource {
constructor(address) {
this._logger = getLogger('aurelia-useable-style-loader');
this._logger.debug(`creating css resource for '${address}'`);
this.address = address;
}
initialize(_container, Hooks) {
this._logger.debug(`initializing css view engine hooks`);
this.scoped = new Hooks(this);
}
register(registry, _name) {
this._logger.debug(`registering css view engine hooks`);
registry.registerViewEngineHooks(this.scoped);
}
load(container) {
return __awaiter(this, void 0, void 0, function* () {
this._logger.debug(`loading css module`);
const loader = container.get(Loader);
const module = yield this.getModule(loader);
this.scoped.css = module;
return this;
});
}
getModule(loader) {
return __awaiter(this, void 0, void 0, function* () {
let styleModule;
try {
styleModule = yield loader.loadModule(this.address);
if (!('use' in styleModule) || !('unuse' in styleModule)) {
// tslint:disable-next-line:max-line-length
throw new Error(`the css module does not have use/unuse methods available, are you missing the style-loader 'lazyStyleTag' injectType?`);
}
}
catch (e) {
this._logger.error(`an error occurred while loading the css module`, e);
styleModule = null;
}
return styleModule;
});
}
}
//# sourceMappingURL=css-resource.js.map