效果图
webpack.config.js
const webpack = require("webpack");const path = require("path");const HtmlWebpackPlugin = require('html-webpack-plugin');const {CleanWebpackPlugin} = require("clean-webpack-plugin")const {VueLoaderPlugin} = require("vue-loader")const config = { entry:path.resolve(__dirname,'src/main.js'), output:{ path:path.resolve(__dirname,'dist'), filename:'[hash]-bundle.js' }, module:{ rules:[ { test:/\.css$/i, use:['style-loader','css-loader'] }, { test:/\.vue$/, loader:'vue-loader' }, { test:/\.(png|jpg|svg|gif|jpeg)$/i, type:'asset/resource' }, ] }, plugins:[ new HtmlWebpackPlugin({ title:"webpack-vue", template:path.join(__dirname,"public/index.html") }), new CleanWebpackPlugin(), new VueLoaderPlugin() ], devServer:{ contentBase:"./dist", port:8000,//改端口 host:"localhost", overlay:{ errors:true } }, devtool:"inline-source-map", mode:'development'}module.exports = config
src/assets/index.css
div{ color: red;}
src/main.js
import './assets/index.css';//console.log("hello world");import App from './App.vue';import Vue from 'vue';new Vue({ render:h=>h(App)}).$mount("#app");
src/App.vue
<template> <div> <p>hello world</p> <img src="./assets/dog2.jpg" alt=""> </div></template><script> export default { data(){ return { msg:"test" } } };</script><style scoped></style>
public/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div id="app">
</div>
</body>
</html>
package.json
{
"name": "web-package",
"version": "1.0.0",
"description": "{**When you're done, you can delete the content in this README and update the file with details for others getting started with your repository**}",
"private": true,
"scripts": {
"build": "webpack --config webpack.config.js",
"serve": "webpack serve --open"
},
"repository": {
"type": "git",
"url": "git@gitee.com:dingmochou/web-package.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"clean-webpack-plugin": "^3.0.0",
"css-loader": "^5.0.1",
"html-webpack-plugin": "^4.5.1",
"style-loader": "^2.0.0",
"vue": "^2.6.12",
"vue-loader": "^15.9.6",
"vue-template-compiler": "^2.6.12",
"webpack": "^5.17.0",
"webpack-cli": "^4.4.0",
"webpack-dev-server": "^3.11.2"
}
}
.gitignore
node_modules