json - AoT + Webpack + json动态require

标签 json angular webpack loader angular2-aot

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

相关文章:

webpack - 如何同时使用 MiniCssExtractPlugin 和 vue-style-loader?

javascript - Webpack 多个 HTML - 我只需要为每个 HTML 注入(inject)一个 .js

Python-值错误: I/O Operation on Closed File Error

Angular 6 我是否必须在客户端生成 jwt 以匹配服务器或仅匹配服务器

javascript - 如何使用 Angular 6 触发 fadeInRight 效果

javascript - Webpack 找不到 jsx 文件

javascript - 显示从 url 中选择的子节点

javascript - 如何使用谷歌共享按钮包含小图像

json - 在 PowerShell 中调用 ExpandString 时如何转义字符串变量

angular - Angular 2/4 的无模态/非模态窗口