2017-10-12 90 views
2

安裝了的WebPack的AOT插件(https://www.npmjs.com/package/@ngtools/webpack)後,dynamic requires不再起作用:AOT +的WebPack + JSON動態需要

// Example that used to work 
public getJson<T>(fileName: String): T { 
    return require(`../../${fileName}_${this.lang}.json`); 
} 

隨着標準ts-loaderawesome-typescript-loader等,dynamic requires工作,的WebPack捆綁json文件到主要app捆綁。但是,使用AoT/Webpack插件時,json文件根本就沒有捆綁。我甚至覺得aot loader不再重複json文件。

任何想法如何讓這個工作再次?謝謝。

信息:

https://github.com/angular/angular-cli/issues/3306

https://github.com/angular/angular-cli/pull/4153

更新:

作品有些與SystemJS -> System.import()但不穩定 https://github.com/angular/angular-cli/issues/6629#issuecomment-336411537

回答

2

解決方法是使用System.import()建立負載和捆綁的動態文件,然後使用標準的WebPack機制加載的實際文件:

public getLazyFiles<T>(somePath: string): T { 
     /* AoT Hack - causes the AoT to find and prepare the dynamically loaded files */ 
     System.import(`../../${somePath}_${this.someSuffix}.json`); 
     /* ------- */ 
     // This is then used by webpack to actually load the files 
     return require(`../../${somePath}_${this.someSuffix}.json`); 
    } 

爲什麼需要這個解決辦法在此說明:https://github.com/angular/angular-cli/issues/6629#issuecomment-336478854