AoT + Webpack + json dynamic require



在为 webpack 安装 AoT 插件 (https://www.npmjs.com/package/@ngtools/webpack) 后,dynamic requires不再工作:

// 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

解决方法是使用 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

相关内容

最新更新