feat vite feature

This commit is contained in:
2024-07-30 14:49:42 +08:00
parent ed728f66d3
commit 4f92af3360
21 changed files with 8090 additions and 1446 deletions

114
webpackTest/README.md Normal file
View File

@@ -0,0 +1,114 @@
**初始化项目**
```shell
npm init -y
```
**安装webpack依赖开发依赖**
```shell
npm add -D webpack webpack-cli
```
**打包项目**
```shell
npx webpack
```
### entery 入口文件
**入口文件是webpack的起点通过入口文件来找到项目中的所有依赖模块。**
```shell
module.exports = {
entry: './src/index.js', //默认值
entry"./hello/hello.js", // 修改默认值
entry: ["./src/a.js", "./src/b.js"], // 多个入口文件
entry: {
hello: "./src/a.js",
b: "./src/b.js"
} // 多个入口文件,使用对象形式
}
```
### output 出口文件
**出口文件是webpack打包后输出的文件。**
```shell
module.exports = {
output: {
filename: "bundle.js", // 默认值
clean: true, // 清除打包文件所在目录
path: `${__dirname}/dist/dist` // 指定打包后的文件路径
}
}
```
### loader
**需要安装css-loader、style-loader**
```shell
module.exports = {
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
'css-loader', // loader 执行顺序是从右到左
]
},
{
test: /\.(png|jpg|jpeg|gif)$/,
type: 'asset/resource' // 图片资源类型
}
]
}
}
```
### babel
**安装babel依赖开发依赖**
```shell
npm add -D @babel/core @babel/preset-env babel-loader
```
**配置babel**
```shell
module: {
rules: [
{
test: /\.m?js$/,
exclude: /(node_modules | bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
}
```
**plugin 插件**
例如:`html-webpack-plugin`
```shell
npm add -D html-webpack-plugin
```
```shell
const HtmlWebpackPlugin = require('html-webpack-plugin')
module: {
plugins: [
new HtmlWebpackPlugin({
// template: './src/index.html'
// filename: 'index.html',
// template: './src/index.html'
title: 'webpack-demo',
})
]
}
```
**Webpack 开发服务器**
```shell
npm add -D webpack-dev-server
```
```shell
module.exports = {
devServer: {
port: 7000,
}
}
```

6997
webpackTest/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

28
webpackTest/package.json Normal file
View File

@@ -0,0 +1,28 @@
{
"name": "webpackTest",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack --config webpack.config.js",
"dev": "webpack-dev-server --config webpack.config.js --open"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.25.2",
"@babel/preset-env": "^7.25.2",
"babel-loader": "^9.1.3",
"css-loader": "^7.1.2",
"html-webpack-plugin": "^5.6.0",
"style-loader": "^4.0.0",
"webpack": "^5.93.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4"
},
"dependencies": {
"jquery": "^3.7.1"
}
}

BIN
webpackTest/src/img/img.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

View File

@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>hello world shenjianZ</h1>
</body>
</html>

11
webpackTest/src/index.js Normal file
View File

@@ -0,0 +1,11 @@
import m1 from './m1'
import m2 from './m2'
import './style/index.css'
// 引入图片并使用
import img from './img/img.png'
const imgElement = document.createElement('img')
imgElement.src = img
const a = 2;
console.log(a,'@@')
m1.m1Method()
m2.m2Method()

6
webpackTest/src/m1.js Normal file
View File

@@ -0,0 +1,6 @@
import $ from 'jquery'
export default {
m1Method() {
console.log('m1Method')
}
}

5
webpackTest/src/m2.js Normal file
View File

@@ -0,0 +1,5 @@
export default {
m2Method() {
console.log('m2Method')
}
}

View File

@@ -0,0 +1,3 @@
h1{
margin:10px;
}

View File

@@ -0,0 +1,47 @@
const webpack = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
module.exports = {
mode: "development",
entry: './src/index.js',
output: {
filename: "main.js", // 默认值
clean: true,
path: `${__dirname}/dist/dist` // 指定打包后的文件路径
},
module: {
rules: [
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
},
{
test: /\.(png|jpg|jpeg|gif)$/,
type: 'asset/resource'
},
{
test: /\.m?js$/,
exclude: /(node_modules | bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env']
}
}
}
]
},
plugins: [
new HtmlWebpackPlugin({
template: './src/index.html',
// filename: 'index.html',
// template: './src/index.html'
title: 'webpack-demo',
})
],
devServer: {
port: 7000,
}
}