配置webpack(一)

webpack官网链接https://www.webpackjs.com

全局安装webpack

npm i webpack webpack-cli -g
npm init 
接下来按照提示回车就可

开发局部安装

npm i webpack webpack-cli -d

配置webpack

  • 新建一个webpack.config.js,将package.json中的scripts下的"build":“webpack"改为"build”:“webpack --mode production --config webpack.config.js”.

  • 在src下新建一个index.js文件,新建public/index.html,新建index.css,自行写样式等测试即可

  • 配置webpack的entry指向src/index.js,配置output,配置插件html-webpack-plugin处理html,配置安装插件css-loader和min-css-extract-plugin,插件安装npm i 插件名 -g

    webpack.config.js代码如下

    const path = require('path')
    const HtmlWebpackPlugin = reqiure("html-webpack-plugin")
    const MinCssExtractPlugin = require("min-css-extract-plugin")
    module.export = {
    	entry: {
    		index: './src/index.js'
    	},
    	output: {
    		path: path.resolve(process.cwd(), "dist"),	//打包后文件位置
    		filename: '[name].[chunkHash].js' //打包后文件+hash值
    	},
    	module: {
    		rules: [{ //配置css
    			test: /\.css$/,
    			use: [
    				MinCssExtractPlugin,
    				'css-loader'
    			]
    		}]
    	}.
    	plugins: [
    			new HtmlWebpackPlugin({ //配置html
    				title: 'webpack', //可在html中获取
    				template: 'public/index.html'
    			}),
    			new MinCssExtractPlugin({
    				filename: '[name].[chunkHash].css'
    			})
    	]
    }
    

配置webpack开发环境

在这里插入图片描述
webpack.config.js中配置deserver:

const path = require('path')
const HtmlWebpackPlugin = reqiure("html-webpack-plugin")
const MinCssExtractPlugin = require("min-css-extract-plugin")
module.export = {
	entry: {
		index: './src/index.js'
	},
	output: {
		path: path.resolve(process.cwd(), "dist"),	//打包后文件位置
		filename: '[name].[chunkHash].js' //打包后文件+hash值
	},
	module: {
		rules: [{ //配置css
			test: /\.css$/,
			use: [
				MinCssExtractPlugin,
				'css-loader'
			]
		}]
	}.
	plugins: [
			new HtmlWebpackPlugin({ //配置html
				title: 'webpack', //可在html中获取
				template: 'public/index.html'
			}),
			new MinCssExtractPlugin({
				filename: '[name].[chunkHash].css'
			})
	],
	devServer: {
		port: 3000, //开发环境下,服务端口3000
		open:true //开发环境自动打开,还可配置其他
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值