一、环境搭建
1. 基础环境搭建
- 新建初始化项目shell
mkdir threejs-map && cd threejs-map # 初始化package.json 文件 npm init -y
- 此时的
package.json
文件内容如下:shell{ "name": "threejs-map", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC" }
- 使用
IDEA
or
VsCode
打开项目工程 - 安装
webpack
andwebpack-cli
shell# 采用 yarn 进行包管理 yarn add webpack webpack-cli -D
- 安装完成之后,在
package.json
文件中,会多出一个devDependencies
节点,对应的就是开发环境需要用到的插件。json{ "devDependencies": { "webpack": "^5.72.1", "webpack-cli": "^4.9.2" } }
- 新建
public
目录,用来存放静态资源 - 新建
src/js
和src/view
,分别存放js
和页面
- 在
src/js
下新建index.js
,入口jsjsfunction test() { console.log('3D地图示例'); } test();
- 在
src/view
下新建index.html
,入口视图html<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>3D地图</title> </head> <body> <div>3D地图示例</div> </body> </html>
- 在
- 配置
webpack
- 新建
build
目录,将webpack
配置大致分为:通用配置
、开发环境配置
、生产环境配置
- 新建通用配置
webpack-common.js
- 设置入口js
module.exports = { entry: "./src/js/index.js", }
- 设置入口
- 新建开发环境配置
webpack-dev.js
- 设置开发环境js
const devConfig = { mode: "development" } module.exports = devConfig
- 设置开发环境
- 新建生产环境配置
webpack-prod.js
- 设置生产环境js
const prodConfig = { mode: "production" } module.exports = prodConfig
- 设置生产环境
- 新建通用配置
- 新建
2. 配置合并
- 安装
webpack-merge
shellyarn add webpack-merge -D
- 安装完成json5
{ "devDependencies": { "webpack": "^5.72.1", "webpack-cli": "^4.9.2", "webpack-merge": "^5.8.0", } }
- 针对
webpack.dev.js
和webpack.prod.js
配置如下:webpack.dev.js
jsconst commonConfig = require("./webpack.common"); const { merge } = require("webpack-merge"); const devConfig = { mode: "development" } module.exports = merge(commonConfig, devConfig);
webpack.prod.js
jsconst commonConfig = require("./webpack.common"); const { merge } = require("webpack-merge"); const prodConfig = { mode: "production" } module.exports = merge(commonConfig, prodConfig)
3. 视图入口配置
- 安装
html-webpack-plugin
html-webpack-plugin
会在打包结束后,⾃动⽣成⼀个 html⽂件,并把打包⽣成的 js 模块引⼊到该 html 中。shellyarn add html-webpack-plugin -D
- 安装完成json5
{ "devDependencies": { "html-webpack-plugin": "^5.5.0", "webpack": "^5.72.1", "webpack-cli": "^4.9.2", "webpack-merge": "^5.8.0", } }
- 配置
HtmlWebpackPlugin
- 在
build/webpack.common.js
配置如下:jsconst HtmlWebpackPlugin = require('html-webpack-plugin'); module.exports = { entry: "./src/js/index.js", plugins: [ new HtmlWebpackPlugin({ template: "src/view/index.html", filename: "index.html" }), ], }
- 在
4. 配置启动脚本
- 在
package.json
文件中的script
节点下配置json5{ "scripts": { "dev": "webpack-dev-server --config ./build/webpack.dev.js", } }
5. 配置webpack-dev-server
- 安装插件shell
yarn add webpack-dev-server -D
- 安装完成的
devDependencies
json5{ "devDependencies": { "html-webpack-plugin": "^5.5.0", "webpack": "^5.72.1", "webpack-cli": "^4.9.2", "webpack-merge": "^5.8.0", "webpack-dev-server": "^4.9.0" } }
- 在
build/webpack-dev.js
文件中配置jsconst path = require('path'); const devConfig = { devServer: { static: { directory: path.join(__dirname, './public'), }, open: true, port: 8089 } }
static
是webpack5的静态资源配置目录
- 运行配置脚本,启动项目shell
yarn run dev
- 效果