安装指定版本的webpack

npm install webpack@3.6 -g

安装live-server    运行项目插件   输入live-server  运行后自动打开网页

npm install -g live-server

webpack.config.js

const path = require('path');
module.exports = {
entry:{
entry:'./src/entry.js',
entry2:'./src/entry2.js'
},
output:{
path: path.resolve(__dirname, 'dist'),
//filename:'bundle.js'
//[name] 自动匹配js名称
filename:'[name].js'
},
module:{},
plugins:[],
//热更新
devServer:{
//文件地址
contentBase: path.resolve(__dirname, 'dist'),
//IP
host: '174.16.10.160',
//文件压缩
compress: true,
//端口
port:
}
}
module.exports={
//入口文件的配置项
entry:{},
//出口文件的配置项
output:{},
//模块:例如解读CSS,图片如何转换,压缩
module:{},
//插件,用于生产模版和各项功能
plugins:[],
//配置webpack开发服务功能
devServer:{}
}

  

  • entry:配置入口文件的地址,可以是单一入口,也可以是多入口。
  • output:配置出口文件的地址,在webpack2.X版本后,支持多出口配置。
  • module:配置模块,主要是解析CSS和图片转换压缩等功能。
  • plugins:配置插件,根据你的需要配置不同功能的插件。
  • devServer:配置开发服务功能,后期我们会详细讲解。

webpack配置了  热更新  需要安装webpack-dev-server

安装 webpack-dev-server       webpack是3.6版本  对应   webpack-dev-server  2.9.4版本

npm install webpack-dev-server@2.9.4 --save-dev

然后修改package.json

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

css打包

在./src/css/index.css  简历index.css文件

body{
background-color:#f00;
color:#fff;
}

  

在entry.js引入css文件

import css from './css/index.css';

document.getElementById('title').innerHTML='Hello jason';

  

style-loader:

它是用来处理css文件中的url()等,npm中的网址:https://www.npmjs.com/package/style-loader

用npm install 进行项目安装:

npm install style-loader --save-dev

css-loader:

它是用来将css插入到页面的style标签。npm中的网址:https://www.npmjs.com/package/css-loader

用npm install 进行项目安装:

npm n install --save-dev css-loader

两个loader都下载安装好后,我们就可以配置我们loaders了。

修改webpack.config.js中module属性中的配置代码如下:

webpack.config.js

第一种:

module:{
rules: [
{
test: /\.css$/,
use: [ 'style-loader', 'css-loader' ]
}
]
},

第二种:

    module:{
rules: [
{
test: /\.css$/,
loader: ['style-loader','css-loader'] }
]
},

  

第三种:   常用方式,  可以扩展

    module:{
rules: [
{
test: /\.css$/,
use: [{
loader: 'style-loader'
},{
loader: 'css-loader'
}] }
]
},

  

JS压缩  (插件配置:配置JS压缩)

webpack.config.js  引入  uglify.js

const path = require('path');
const uglify = require('uglifyjs-webpack-plugin');
module.exports = {
entry:{
entry:'./src/entry.js',
entry2:'./src/entry2.js'
},
output:{
path: path.resolve(__dirname, 'dist'),
//filename:'bundle.js'
//[name] 自动匹配js名称
filename:'[name].js'
},
module:{
rules: [
{
test: /\.css$/,
//use: ['style-loader','css-loader']
//loader: ['style-loader','css-loader']
use: [{
loader: 'style-loader'
},{
loader: 'css-loader'
}] }
]
},
plugins:[
new uglify
],
//热更新 需要安装 webpack-dev-server2.9.4 对应 webpack3.6版本
//然后修改 package.json
/*
*"scripts": {
* "server": "webpack-dev-server"
*}
*/
devServer:{
//文件地址
contentBase: path.resolve(__dirname, 'dist'),
//IP
host: '174.16.10.160',
//文件压缩
compress: true,
//端口
port: 8008
} }

  

然后webpack  打包  就可以看到js文件压缩了

Html 打包

1. 先安装html 打包压缩插件

介绍网址: https://www.npmjs.com/package/extract-text-webpack-plugin

npm install --save-dev html-webpack-plugin

2. 然后配置webpack.config.js文件,  先引入 html-webpack-plugin插件

const htmlPlugin = request('html-webpack-plugin');

3. 最后插件配置代码

const path = require('path');
//引入js打包插件
const uglify = require('uglifyjs-webpack-plugin');
//引入html打包插件
const htmlPlugin = require('html-webpack-plugin'); module.exports = {
entry:{
entry:'./src/entry.js',
entry2:'./src/entry2.js'
},
output:{
path: path.resolve(__dirname, 'dist'),
//filename:'bundle.js'
//[name] 自动匹配js名称
filename:'[name].js'
},
module:{
rules: [
{
test: /\.css$/,
//use: ['style-loader','css-loader']
//loader: ['style-loader','css-loader']
use: [{
loader: 'style-loader'
},{
loader: 'css-loader'
}] }
]
},
plugins:[
//new uglify,
new htmlPlugin({
minify: {
removeAttributeQuotes: true
},
hash: true,
template: './src/index.html'
})
],
//热更新 需要安装 webpack-dev-server2.9.4 对应 webpack3.6版本
//然后修改 package.json
/*
*"scripts": {
* "server": "webpack-dev-server"
*}
*/
devServer:{
//文件地址
contentBase: path.resolve(__dirname, 'dist'),
//IP
host: '174.16.10.160',
//文件压缩
compress: true,
//端口
port: 8008
} }  
  • minify:是对html文件进行压缩,removeAttrubuteQuotes是却掉属性的双引号。
  • hash:为了开发中js有缓存效果,所以加入hash,这样可以有效避免缓存JS。
  • template:是要打包的html模版路径和文件名称。

上边的都配置完成后,我们就可以在终端中使用webpack,进行打包。你会看到index.html文件已经被打包到我们的dist目录下了,并且自动为我们引入了路口的JS文件。

总结:

html文件的打包可以有效的区分开发目录和生产目录,在webpack的配置中也要搞清楚哪些配置用于生产环境,哪些配置用于开发环境,避免两种环境的配置冲突。

webpack webpack.config.js配置的更多相关文章

  1. webpack.config.js配置信息的说明

    module.exports = { entry: "./src/main.js", output: { filename: "build/build.js" ...

  2. 解决运行webpack --config webpack.dev.config.js 报错ReferenceError: _dirname is not defined

    控制台报错信息如下 将webpack.dev.config.js中的_dirname 改为 __dirname 注意这里是两个下划线谢谢!感觉好坑

  3. vue-cli & webpack & vue.config.js

    vue-cli & webpack & vue.config.js configureWebpack // vue.config.js module.exports = { confi ...

  4. @vue/cl构建得项目下,postcss.config.js配置,将px转化成rem

    依赖包: postcss-pxtorem 配置: 在项目根目录下创建 postcss.config.js 配置如下: module.exports = () => ({ plugins: [ r ...

  5. vue-cli的webpack模版,相关配置文件dev-server.js与webpack.config.js配置解析

    1.下载vue-cli npm install vue-cli -g vue-cli的使用与详细介绍,可以到github上获取https://github.com/vuejs/vue-cli 2.安装 ...

  6. webpack.config.js配置遇到Error: Cannot find module '@babel/core'&&Cannot find module '@babel/plugin-transform-react-jsx' 问题

    下文是网上找到的方法,是因为版本冲突的原因,参照后安装7版本解决 cnpm install -D babel-loader@ babel-core babel-preset-env 一. 问题描述 在 ...

  7. webpack.config.js====配置babel

    参考:https://www.jianshu.com/p/9808f550c6a91. 安装依赖babel-loader: 负责 es6 语法转化babel-preset-env: 包含 es6.7 ...

  8. webpack.config.js配置入口出口文件

    目录结构: 新建webpack.config.js配置文件 const path = require('path') //导出 path是node内置的包 通过npm init初始化得到package ...

  9. webpack(4)webpack.config.js配置和package.json配置

    前言 上一篇文章我们使用webpack打包成功了,但是每次都要自己手动输入打包的文件地址和打包到哪里去的地址,非常麻烦,所以这里介绍使用配置文件进行打包 webpack.config.js 首先我们创 ...

随机推荐

  1. (10)python学习笔记一

    学习参考博客:http://blog.csdn.net/a359680405/article/details/42486689  深表感谢 1.单行注释  #    多行注释 "" ...

  2. Behaviour Tree Service 中的几个函数

    Service中可以override的函数有8个,因为每个函数都有个AI版本的,所以实际上是4组函数,AI版本的和非AI版本基本一样, 他们分别是: Receive Search Start (AI) ...

  3. day02-Python基础

    >>> if a > b:... c = a+b... else:... c = a-b...>>> c-1 三元运算: >>> c = a ...

  4. JS-Proxy

    Proxy 对象用于定义基本操作的自定义行为(如属性查找,赋值,枚举,函数调用等). 参考:Proxy - JavaScript | MDN 语法: let p = new Proxy(target, ...

  5. 001-spring boot概述与课程概要

    一.Spring Boot介绍 Spring Boot的目的在于创建和启动新的基于spring框架的项目.Spring boot会选择最适合的Spring 子项目和第三方开源库进行整合.大部分Spri ...

  6. 012-elasticsearch5.4.3【五】-搜索API【一】搜索匹配所有matchAllQuery、全文查询[matchQuery、multiMatchQuery、commonTermsQuery、queryStringQuery、simpleQueryStringQuery]

    一.概述 查询所使用的 QueryBuilders来源于以下 import static org.elasticsearch.index.query.QueryBuilders.*; 请注意,您可以使 ...

  7. Delphi XE2 之 FireMonkey 入门(10) - 常用结构 TPoint、TPointF、TSmallPoint、TSize、TRect、TRectF 及相关方法

    它们都是结构, TPointF.TRectF 属新增, 其它也都有升级; 现在都拥有丰富的方法和方便的运算符重载; 且有一组相关的公共函数. 这组内容重要的是它们都来自 System.Types 单元 ...

  8. 阶段1 语言基础+高级_1-3-Java语言高级_06-File类与IO流_04 IO字节流_2_一切皆为字节

    这里的视频就是字节的形式,为了看着方便转换成了MB.一个字节就是8个二进制 包括文本,都是以字节的形式存储的

  9. Nginx-Lua模块的执行顺序(转)

    一.nginx执行步骤 nginx在处理每一个用户请求时,都是按照若干个不同的阶段依次处理的,与配置文件上的顺序没有关系,详细内容可以阅读<深入理解nginx:模块开发与架构解析>这本书, ...

  10. spring boot 启动之后404

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...