Created
December 1, 2016 15:08
-
-
Save sis0k0/86b4eb1bf979fd5d17e776109409cd2e to your computer and use it in GitHub Desktop.
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
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