Answer the question
In order to leave comments, you need to log in
Is it possible to move Promise & require.ensure into a separate function and include files through it?
Hello others.
Lazy loading modules in angular2 in conjunction with webpack looks something like this:
loadChildren: () => new Promise(resolve => {
(require as any).ensure([], (require: any) => {
resolve(require("./module_path")["ModuleName"]);
}, "chunkName")
})
function loadModule(path: string){
// обработка строки path
let p = new Promise(resolve => {
(require as any).ensure([], (require: any) => {
resolve(require(path)[ModuleName]);
}, chunkName)
});
return p;
}
loadChildren: () => loadModule('./module_path#ModuleName')
Critical dependency: require function is used in a way in which dependencies cannot be statically extracted
var p = new Promise(function (resolve) {
!(function webpackMissingModule() { var e = new Error("Cannot find module \".\""); e.code = 'MODULE_NOT_FOUND'; throw e; }()).ensure([], function (require) {
resolve(require(path)[moduleName]);
}, chunkName);
});
Answer the question
In order to leave comments, you need to log in
If still relevant, then the second example does not work due to the fact that path is passed through a parameter, and not passed as an explicit line to require. I'll show you with an example, this is how it works:
export const loadFoo = () => new Promise<any>((resolve, reject) => {
require.ensure(["Foo/path"], require => {
resolve(require("Foo/path"));
}, error => reject(error));
});
export const load = (path: string) => new Promise<any>((resolve, reject) => {
require.ensure([path], require => {
resolve(require(path));
}, error => reject(error));
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question