Skip to content

Instantly share code, notes, and snippets.

@sis0k0
Created December 1, 2016 15:08
Show Gist options
  • Save sis0k0/86b4eb1bf979fd5d17e776109409cd2e to your computer and use it in GitHub Desktop.
Save sis0k0/86b4eb1bf979fd5d17e776109409cd2e to your computer and use it in GitHub Desktop.
import {
Injectable,
Compiler,
NgModuleFactory,
NgModuleFactoryLoader
} from "@angular/core";
import { path, knownFolders } from "file-system";
const SEPARATOR = "#";
@Injectable()
export class NinjaModuleFactoryLoader implements NgModuleFactoryLoader {
constructor(private compiler: Compiler) {
}
load(path: string): Promise<NgModuleFactory<any>> {
let {modulePath, exportName} = this.splitPath(path);
let loadedModule = require(modulePath)[exportName];
if (!loadedModule) {
throw new Error(`Cannot find "${exportName}" in "${modulePath}"`);
}
return this.compiler.compileModuleAsync(loadedModule);
}
private splitPath(path: string): {modulePath: string, exportName: string} {
let [modulePath, exportName] = path.split(SEPARATOR);
modulePath = getAbsolutePath(modulePath);
if (typeof exportName === "undefined") {
exportName = "default";
}
return {modulePath, exportName};
}
}
function getAbsolutePath(relativePath: string) {
return path.normalize(path.join(knownFolders.currentApp().path, relativePath));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment