【问题标题】:Webpack does not replace images in HTML filesWebpack 不会替换 HTML 文件中的图像
【发布时间】:2017-03-03 02:37:11
【问题描述】:

我将 Webpack 与 Angular 2 应用程序一起使用。 My Webpack 配置文件中的加载器如下所示。我删除了插件和导入以避免冗长的示例代码。 Webpack 正在用散列名称而不是 html 文件替换 css 文件中的图像。不知道我错过了什么。将不胜感激任何指针。

module.exports = function (options) {
    const DATAS = {
        VERSION: JSON.stringify(require("../package.json").version),
        DEBUG_INFO_ENABLED: options.env === 'dev'
    };
    return {
        entry: {
            'polyfills': './src/main/webapp/app/polyfills',
            'global': './src/main/webapp/content/css/global.css',
            'main': './src/main/webapp/app/app.main'
        },
        resolve: {
            extensions: ['.ts', '.js'],
            modules: ['node_modules']
        },
        module: {
            rules: [
                { test: /bootstrap\/dist\/js\/umd\//, loader: 'imports-loader?jQuery=jquery' },
                {
                    test: /\.ts$/,
                    loaders: [
                        'angular2-template-loader',
                        'awesome-typescript-loader'
                    ],
                    exclude: ['node_modules/generator-jhipster']
                },
                {
                    test: /\.html$/,
                    loader: 'raw-loader',
                    exclude: ['./src/main/webapp/index.html']
                },
                {
                    test: /\.css$/,
                    loaders: ['to-string-loader', 'css-loader'],
                    exclude: /(vendor\.css|global\.css)/
                },
                {
                    test: /(vendor\.css|global\.css)/,
                    loaders: ['style-loader', 'css-loader']
                },
                {
                    test: /\.(jpe?g|png|gif|svg|woff|woff2|ttf|eot)$/i,
                    loaders: [
                        'file-loader?hash=sha512&digest=hex&name=[hash].[ext]',
                        'image-webpack-loader?bypassOnDebug&optimizationLevel=7&interlaced=false'
                    ]
                },
                {
                    test: /app.constants.ts$/,
                    loader: StringReplacePlugin.replace({
                        replacements: [{
                            pattern: /\/\* @toreplace (\w*?) \*\//ig,
                            replacement: function (match, p1, offset, string) {
                                return `_${p1} = ${DATAS[p1]};`;
                            }
                        }]
                    })
                }
            ]
        }

【问题讨论】:

    标签: webpack


    【解决方案1】:

    您将raw-loader 用于HTML 文件,它只会为您提供一个字符串,而无需任何处理。相反,您想使用 html-loader ,这正是您想要的。将.html 规则更改为:

    {
      test: /\.html$/,
      loader: 'html-loader',
      exclude: ['./src/main/webapp/index.html']
    },
    

    【讨论】:

    • 我根据另一个 SO 答案做了完全相同的事情。当我使用 html-loader ERROR in ./src/main/webapp/app/account/password-reset/init/password-reset-init.component.ts Module not found: Error: Can't resolve 'html-loader' in '/Users/company/folder/myapp' @ ./src/main/webapp/app/account/password-reset/init/password-reset-init.component.ts 46:18-65 @ ./src/main/webapp/app/account/index.ts @ ./src/main/webapp/app/account/account.module.ts @ ./src/main/webapp/app/app.module.ts @ ./src/main/webapp/app/app.main.ts 和更多代码时,出现如下错误。
    • 你需要安装html-loadernpm install --save-dev html-loader
    • 我是第一次使用 webpack,并假设 wepack 会从其库中加载模块。我真的浪费了至少 2 天的时间来尝试几件事。非常感谢。
    猜你喜欢
    • 2017-02-26
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 2013-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多