webpack5項(xiàng)目之基本配置詳解
一.配置項(xiàng)目出入口
webpack.config.js(文檔)
const path = require('path');
module.exports = {
entry: {
//設(shè)置入口文件為src/main.js 將來會(huì)被打包到dist目錄下
main: './src/main.js',
//other: './src/other.js',//多入口文件,打包后會(huì)在dist目錄下生成相應(yīng)js
},
output: {
//打包后文件名
filename: '[name]-[contenthash:6].js',
//指定打包文件輸出目錄
path: path.resolve(__dirname, 'dist'),
//在每次構(gòu)建前清理 /dist 文件夾
clean: true,
},
};二.添加模式
webpack.config.js(文檔)
const path = require('path');
module.exports = {
mode: 'development',
entry: {
//設(shè)置入口文件為src/main.js 將來會(huì)被打包到dist目錄下
main: './src/main.js',
//other: './src/other.js',//多入口文件,打包后會(huì)在dist目錄下生成相應(yīng)js
},
output: {
//打包后文件名
filename: '[name]-[contenthash:6].js',
//指定打包文件輸出目錄
path: path.resolve(__dirname, 'dist'),
//在每次構(gòu)建前清理 /dist 文件夾
clean: true,
},
};如果沒有設(shè)置,webpack 會(huì)給 mode 的默認(rèn)值設(shè)置為 production
三.添加devtool
webpack.config.js(文檔)
const path = require('path');
module.exports = {
mode: 'development',
//將編譯后的代碼映射回原始源代碼
devtool: 'inline-source-map',
entry: {
//設(shè)置入口文件為src/main.js 將來會(huì)被打包到dist目錄下
main: './src/main.js',
//other: './src/other.js',//多入口文件,打包后會(huì)在dist目錄下生成相應(yīng)js
},
output: {
//打包后文件名
filename: '[name]-[contenthash:6].js',
//指定打包文件輸出目錄
path: path.resolve(__dirname, 'dist'),
//在每次構(gòu)建前清理 /dist 文件夾
clean: true,
},
};四.安裝本地環(huán)境和內(nèi)存html插件
npm install -D webpack webpack-cli html-webpack-plugin
webpack.config.js(文檔)
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = {
mode: 'development',
devtool: 'inline-source-map', //將編譯后的代碼映射回原始源代碼
entry: './src/main.js',
output: {
filename: '[name]-[contenthash:6].js',//打包后文件名
path: path.resolve(__dirname, 'dist'),
clean: true, //在每次構(gòu)建前清理 /dist 文件夾
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html', //打包的html模板文件
title: 'webpack-demo',
filename: "index.html", //打包后的html名稱
}),
],
};五.安裝開發(fā)工具
安裝開發(fā)工具webpack-dev-server(此處安裝3.11.2版本,最新版本4.0.0有兼容性問題)
npm install -D webpack-dev-server@3.11.2
在package.json中添加執(zhí)行腳本
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack serve --open --port 3000",
"build": "webpack"
},package.json
{
"name": "webpack-demo",
"version": "1.0.0",
"description": "",
"private": true,
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "webpack serve --open --port 3000",
"build": "webpack"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"html-webpack-plugin": "^5.3.2",
"webpack": "^5.51.1",
"webpack-cli": "^4.8.0",
"webpack-dev-server": "^3.11.2"
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于javascript實(shí)現(xiàn)listbox左右移動(dòng)
這篇文章主要介紹了基于javascript實(shí)現(xiàn)listbox左右移動(dòng)的相關(guān)資料,以一個(gè)完整的實(shí)例代碼分析了js實(shí)現(xiàn)listbox左右移動(dòng)的相關(guān)技巧,感興趣的小伙伴們可以參考一下2016-01-01
JavaScript設(shè)計(jì)模式--橋梁模式引入操作實(shí)例分析
這篇文章主要介紹了JavaScript設(shè)計(jì)模式--橋梁模式,結(jié)合實(shí)例形式分析了JavaScript設(shè)計(jì)模式中橋梁模式應(yīng)用操作技巧與注意事項(xiàng),需要的朋友可以參考下2020-05-05
js手動(dòng)播放圖片實(shí)現(xiàn)圖片輪播效果
這篇文章主要為大家詳細(xì)介紹了js手動(dòng)播放圖片實(shí)現(xiàn)圖片輪播效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
使用JavaScript實(shí)現(xiàn)隨機(jī)顏色生成器
這篇文章主要為大家詳細(xì)介紹了如何使用JavaScript+CSS實(shí)現(xiàn)一個(gè)隨機(jī)顏色生成器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以動(dòng)手嘗試一下2022-08-08
echarts學(xué)習(xí)筆記之圖表自適應(yīng)問題詳解
最近發(fā)現(xiàn)一個(gè)問題,echarts圖初始化后不能自適應(yīng)瀏覽器的縮放,所以下面這篇文章就來給大家介紹了關(guān)于echarts圖表自適應(yīng)問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
微信小程序搜索框樣式并實(shí)現(xiàn)跳轉(zhuǎn)到搜索頁面(小程序搜索功能)
這篇文章主要介紹了微信小程序搜索框樣式并實(shí)現(xiàn)跳轉(zhuǎn)到搜索頁面(小程序搜索功能),需要的朋友可以參考下2020-03-03

