博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(10/24) 图片跳坑大战--处理html中的图片
阅读量:7086 次
发布时间:2019-06-28

本文共 3114 字,大约阅读时间需要 10 分钟。

补充,在前面的服务启动执行命令中,我们在package.json中的配置信息为:

"scripts": {    "server": "webpack-dev-server",  },

该种方式在启动服务后还需自己访问相关url,这很不友好。此处我们新增一些配置,保证在我们启动服务后自动访问url并渲染,配置如下:

"scripts": {    "server": "webpack-dev-server --open",  },

这样就实现了服务启动浏览器也会自动打开。

正文:

在前端开发中,我们常用img标签来引入图片,这样webpack在打包时又得做一些特殊处理,此处我们通过一个插件 来处理我们在html 中引入图片的问题。

 处理html中的图片

 

1.新增一张图片

在src/images目录下新增一张图片,作为等会引入到html文件中图片,此处我的图片为wfbin.png。

2.引入图片

在src/index.html中引入:

3. 插件安装

使用npm进行安装,若失败则采用cnpm进行安装。

npm install html-withimg-loader --save-dev

4.配置loader

在webpack.config.js文件中的module属性中进行配置:

{    test: /\.(htm|html)$/i,     use:[ 'html-withimg-loader'] }

5.打包

使用webpack进行打包,我们的图片被进行了很好的打包。

6.启动服务

运行命令npm run server命令,服务被启动,浏览器自动打开,并进行了渲染,如下:

npm run server

渲染效果:

记:到此完成了webpack打包过程中图片的相关处理。

本节源码:

index.html:

1  2  3  4     
5
6
7 webpack 8 9 10
11
12
13
14 15
View Code

src/index.css:

body{    background-color: #018eea;    color: red;    font-size: 32px;    text-align: center;}#img{    background-image: url(../images/webapck.jpg);    width:271px;    height:285px;}
View Code

webpack.config.js:

const path = require('path');const uglify = require('uglifyjs-webpack-plugin');const htmlPlugin= require('html-webpack-plugin');const extractTextPlugin = require("extract-text-webpack-plugin");var website ={    publicPath:"http://localhost:1818/"}module.exports={    //入口文件的配置项    entry:{        entry:'./src/entry.js',        //这里我们又引入了一个入口文件        entry2:'./src/entry2.js',    },    //出口文件的配置项    output:{        //输出的路径,用了Node语法        path:path.resolve(__dirname,'dist'),        //输出的文件名称        filename:'[name].js',        publicPath: website.publicPath    },    //模块:例如解读CSS,图片如何转换,压缩    module:{        rules: [            {                test: /\.css$/,                use: extractTextPlugin.extract({                    fallback: "style-loader",                    use: "css-loader"                })            },            {                test:/\.(png|jpg|gif)/,                use:[{                    loader:'url-loader',                    options:{                        limit:50,                        outputPath:'images/'//图片打包到images下                    }                }                ]            },            {                test: /\.(htm|html)$/i,                use:[ 'html-withimg-loader']            }        ]    },    //插件,用于生产模版和各项功能    plugins:[        // new uglify(),        new htmlPlugin({            minify:{                removeAttributeQuotes:true            },            hash:true,            template:'./src/index.html'        }),        new extractTextPlugin("css/index.css")    ],    //配置webpack开发服务功能    devServer:{        contentBase:path.resolve(__dirname,'dist'), //绝对路径        host:'localhost',        compress:true,        port:1818    }}
View Code

entry.js://入口文件

import css from './css/index.css'document.getElementById('title').innerHTML='Hello Webpack';
View Code

 

posted on
2018-12-15 11:32 阅读(
...) 评论(
...)

转载于:https://www.cnblogs.com/wfaceboss/p/10122803.html

你可能感兴趣的文章
python使用pipeline读写redis
查看>>
怎样通过信息化提高工厂工业化效率?
查看>>
Redis设计与实现 第二部分
查看>>
xtrabackup介绍及相关操作流程
查看>>
CentOS 6.10安装配置WebLogic 11g双机集群
查看>>
layoutInflater 获取布局对象
查看>>
3000套IOS android源码分享 7.2G UI素材
查看>>
Android中visibility属性VISIBLE、INVISIBLE、GONE的区别
查看>>
Fragment和FragmentActivity的使用方法 .
查看>>
datagrid增加提示功能tooltip
查看>>
闲诗一首:《莫追梦》
查看>>
windows hash注入
查看>>
kubernetes1.5 statefulset搭建zk集群
查看>>
Python高级:细说Python浅拷贝和深拷贝
查看>>
小技巧:利用QUIC提升Chrome浏览器网页打开速度
查看>>
windows redis 集群搭建(一)
查看>>
Sql优化器究竟帮你做了哪些工作?
查看>>
CentOS 5.6下pptpd *** 服务器搭建
查看>>
Android 生成keystore的两种方式
查看>>
spring 的事务回滚 异常exception 和 编译期异常和运行期异常
查看>>