为 webpack 安装 AoT 插件 ( https://www.npmjs.com/package/@ngtools/webpack ) 后,dynamic require
不再起作用:
// Example that used to work
public getJson<T>(fileName: String): T {
return require(`../../${fileName}_${this.lang}.json`);
}
使用标准的 ts-loader
或 awesome-typescript-loader
等,dynamic require
可以工作,并且 webpack 捆绑了 json
文件放入主 app
包中。但是,使用 AoT/Webpack 插件时,根本不会捆绑 json 文件。我什至认为 aot 加载器
不再迭代 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
最佳答案
解决方法是使用 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
关于json - AoT + Webpack + json动态require,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46706473/