javascript - 使用文件加载器 outputPath 的 AOT 构建将 img 标签转换为字符串

标签 javascript angular webpack angular2-aot webpack-file-loader

我在尝试使用文件加载器构建 AOT 构建时遇到问题 outputPath选项。结果输出是 <img>标记用引号 ( "<img src=images/world.png alt=world />") 括起来。

这只发生在我的 AOT 构建中,而不是在我的开发构建中。所以我猜文件加载器在它的编译周期中被切断并插入了结果字符串。

版本:

Angular: 4.4.4
@ngtools/webpack: 1.7.2,
file-loader: 1.1.5,
image-webpack-loader: 3.4.2,
webpack: 3.6.0,
webpack-dev-server: 2.9.1

webpack.common.js |规则部分

   module: {
        rules: [
            {
                test: /\.html$/,
                loader: 'html-loader',
                options: {
                    minimize: true,
                    caseSensitive: true
                }
            },
            {
                test: /\.scss$/,
                exclude: /node_modules/,
                loaders: ['raw-loader', 'sass-loader']
            },
            {
                test: /\.(gif|png|jpe?g|svg)$/i,
                use: [
                    {
                        loader: 'file-loader', // Image loader
                        options: {
                            name: '[name].[ext]',
                            outputPath: 'images/' // Fails with AOT build. <img> tag gets turned into a string
                        }
                    },
                    {
                        loader: 'image-webpack-loader'
                    }
                ]
            },
            {
                test: /\.(eot|woff2?|ttf)([?]?.*)$/, // Font loader
                use: 'file-loader'
            }
        ]
    },

webpack.prod.js

module.exports = merge(common, {
    module: {
        rules: [
            {
                test: /\.ts$/,
                loaders: ['@ngtools/webpack']
            }
        ]
    },
    plugins: [
        new webpack.optimize.UglifyJsPlugin({ // Uglyfy the JavaScript output | Still gives a small win with AOT build
            beautify: false,
            mangle: {
                screw_ie8: true,
                keep_fnames: true
            },
            compress: {
                warnings: false,
                screw_ie8: true
            },
            comments: false
        }),
        new AotPlugin({ // Create AOT build
            tsConfigPath: './tsconfig.json',
            entryModule: __dirname + '/src/app/app.module#AppModule'
        }),
        new webpack.DefinePlugin({ // Set the node env so that the project knows what to enable or disable
            'process.env': {
                'NODE_ENV': JSON.stringify('production')
            }
        })
    ]
});

这是一个错误还是我做错了什么?如果它是一个错误,它在哪一边是错误,@ngtools/webpackfile-loader

感谢任何帮助!


我的app.html 供引用

<div>
    <h1>Hello world</h1>
    <img src="../assets/world.png" alt="world"/>
</div>

输出:

The output html


更新

貌似是Angular的AOT编译器的bug。所以我做了一个 Bug report on GitHub .下面的评论中有一个解决方法,但如果能修复此错误,那就太好了。

最佳答案

我认为这是一个错误。它呈现为字符串的原因是因为 URL 周围没有引号。

解决方法是通过 html-loader 插件添加引号:

{
  test: /\.html$/, loader: 'html-loader', options: {
    removeAttributeQuotes: false
  }
}

有没有更好的解决方案?

关于javascript - 使用文件加载器 outputPath 的 AOT 构建将 img 标签转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46593269/

相关文章:

javascript - 如果填充了任何相关对象字段,如何使 ng-required 为真?

javascript - intro.js - 添加 Shift + ?键盘热键/快捷键支持启动(或触发)introJs().start()

angular - NativeScript 找不到 Angular 组件

angular - 如何测试 AngularDart 组件?

javascript - 有没有办法从 Gatsby 的构建失败中获取更多信息?

reactjs - 为什么 React 需要 Babel 和 Webpack 才能工作?

javascript - Webpack 构建但组件不渲染

Javascript : Pass String by value

Javascript 函数生成器参数

css - 如何更改 mat-button-toggle 的边界半径