# h5-template **Repository Path**: zybieku/h5-template ## Basic Information - **Project Name**: h5-template - **Description**: 手把手搭建h5框架,webpack+jq 与webpack +vue - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2019-04-03 - **Last Updated**: 2021-12-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 从零用webpack构建移动端项目(1) ### 1.初始化一个npm项目 ``` js npm init -y ``` 在项目根目录 就会多一个pakage.json文件,里面包含了项目的描述信息 - name :库名 - version :版本 - description :描述 - main :项目入口,也就是npm依赖入口 - scripts :项目指令集 - keywords :npm库 搜索关键词 - author :作者 - license :开源协议 ``` shell { "name": "demo", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" } ``` ### 2.安装pnpm pnpm 是新一代包管理工具,快速又节省磁盘空间 ```js npm install -g pnpm ``` ### 3.安装webpack webpack-cli - -D 或--save-dev 安装dev环境依赖 ``` js pnpm add webpack webpack-cli -D ``` ``` js "devDependencies": { "webpack": "^5.65.0", "webpack-cli": "^4.9.1" } ``` ### 4.新建webpack.config.js配置文件 - entry 入口配置,需要打包的文件 - output 出口配置,打包之后的文件信息 ``` js const path = require('path'); module.exports = { entry: './src/main.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'h5.min.js', }, }; ``` ### 5.安装babel ``` js pnpm add -D babel-loader @babel/core @babel/preset-env ``` 配置babel.config.json文件 ``` json { "presets": [ "@babel/preset-typescript", [ "@babel/preset-env", { "targets": { "edge": "17", "firefox": "60", "chrome": "67", "safari": "11.1" }, "useBuiltIns": "usage" } ] ] } ``` 配置webpack中的 babel-loader,resolve别名 ``` js resolve:{ extensions: ['.js', '.json', 'jsx'], }, module: { rules: [ { test: /\.js$/, exclude: /(node_modules|bower_components)/, use: { loader: 'babel-loader', } } ] } ```