简体   繁体   English

从模板中获取图像时出错 webpack 5

[英]Error taking images from the template in webpack 5

I am developing a basic application so instead of using some framework I am just using Typescript with the help of Webpack.我正在开发一个基本的应用程序,所以我没有使用一些框架,而是在 Webpack 的帮助下使用 Typescript。

The problem is that I can't get webpack to take the images from the html template correctly.问题是我无法让 webpack 正确地从 html 模板中获取图像。 The only way I could do it is by manipulating the DOM with Typescript, but the goal is to make it transparent and take it directly from the template without having to write any typescript for it.我能做到的唯一方法是用 Typescript 操作 DOM,但目标是使其透明并直接从模板中获取它,而不必为其编写任何 typescript。

Next I add the rules in the config and a basic html接下来我在配置中添加规则和一个基本的 html

The webpack it's separated in 3 files webpack 它分为 3 个文件

  1. webpack.common.js webpack.common.js
  2. webpack.dev.js webpack.dev.js
  3. webpack.prod.js webpack.prod.js

webpack.common.js webpack.common.js

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './src/index.ts',

  module: {
    rules: [
      {
        test: /\.ts$/,
        include: [path.resolve(__dirname, 'src')],
        use: 'ts-loader',
      },
      {
        test: /\.html$/,
        use: ["html-loader"]
      },
      {
        test : /\.(png|jp(e*)g|svg)$/,
        use : [{
          loader : 'url-loader',
          options : {
            limit : 800,
            name : 'images/[hash]-[name].[ext]'
          }
        }]
      }
    ]
  },
  resolve: {
    extensions: ['.ts', '.js']
  },
  plugins: [new HtmlWebpackPlugin(
    {
      template: "./src/template.html"
    }
  )],
};

webpack.dev.js webpack.dev.js

const path = require('path');
const common = require("./webpack.common");
const {merge} = require("webpack-merge");

module.exports = merge(common,{
    mode: "development",
    devtool: 'eval-source-map',
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    "style-loader",'css-loader'
                ]
            }
        ]
    },
    devServer: {
        proxy: {
          '/api': 'url of my api',
        },
      }
  }) ;

webpack.prod.js webpack.prod.js

const common = require("./webpack.common");
const { merge } = require("webpack-merge");

const MiniCssExtractPlugin = require('mini-css-extract-plugin');


module.exports = merge(common, {
    mode: "production",
    plugins: [
        new MiniCssExtractPlugin({ filename: "[name].[contenthash].css" })
    ],
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    'css-loader'
                ]
            }
        ]
    }
});

template.html模板.html

........
....
<div>
            <h2 class="text-danger mb-0">Algo salió mal</h2>
            <picture class="qr">
                <img class="error" src="assets/img/error.svg" id="error">
            </picture>
</div>

The problem is that after deploying de code generated is the following:问题是部署生成的代码如下:

<picture class="qr">
                <img class="error" src="6cf31722af2d86e1084b.svg" id="error">
            </picture>

when 6cf31722af2d86e1084b.svg include6cf31722af2d86e1084b.svg包括

export default webpack_public_path + "images/a062cdc3b40c269bca1f81936119073c-error.svg";导出默认webpack_public_path + "images/a062cdc3b40c269bca1f81936119073c-error.svg";

url-loader is deprecated for webpack v5. webpack v5 已弃用 url-loader。 Maybe this will help?也许这会有所帮助? module:{rules:[{test:/\.(png|jp(e*)g|svg)$/i,type:'asset/resource'}]}, . module:{rules:[{test:/\.(png|jp(e*)g|svg)$/i,type:'asset/resource'}]}, . See url-loader deprecated for v5请参阅 v5 弃用的 url-loader

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM