基于Threejs的3D疫情地球,环境采用上篇博客里的前端准备环境。
webpack搭建前端代码框架,搭建好的代码结构:
没什么好说的,搭建可以参考webpack官网,这里采用类库的方式搭建,附上webpack的配置
webpack.base.js 基础配置
1 const path = require('path'); 2 let HtmlWebpackPlugin = require('html-webpack-plugin'); 3 const { CleanWebpackPlugin } = require('clean-webpack-plugin'); 4 const MiniCssExtractPlugin = require('mini-css-extract-plugin'); 5 const OptimizeCssAssetsPlugin = require('optimize-css-assets-webpack-plugin'); 6 const copyWebpackPlugin = require('copy-webpack-plugin'); 7 module.exports = { 8 entry: './src/index.js', 9 output: { 10 filename: 'VDEarth.js', 11 path: path.resolve(__dirname, 'dist'), 12 library: 'VDEarth', 13 libraryTarget: 'umd', 14 umdNamedDefine: true, 15 }, 16 plugins: [ 17 new HtmlWebpackPlugin({ 18 template: './src/public/index.html', 19 }), 20 new CleanWebpackPlugin(), 21 new MiniCssExtractPlugin(), 22 new OptimizeCssAssetsPlugin(), 23 new copyWebpackPlugin([ 24 { 25 from: __dirname + '/src/fonts', //打包的静态资源目录地址 26 to: './fonts', //打包到dist下面的public 27 }, 28 ]), 29 // { 30 // from: __dirname + '/src/data', //打包的静态资源目录地址 31 // to: './data', //打包到dist下面的public 32 // }, 33 // { 34 // from: __dirname + '/src/icons', //打包的静态资源目录地址 35 // to: './icons', //打包到dist下面的public 36 // }, 37 // ]), 38 ], 39 module: { 40 rules: [ 41 { 42 test: /\.(woff|woff2|eot|ttf|otf|json)$/, 43 use: { 44 loader: 'file-loader', 45 options: { 46 name: 'fonts/[name].[ext]', 47 publicPath: './', 48 }, 49 }, 50 }, 51 { 52 test: /\.(svg|gif|png|jpg)$/, 53 use: { 54 loader: 'file-loader', 55 options: { 56 name: 'images/[name].[ext]', 57 publicPath: './', 58 }, 59 }, 60 }, 61 { 62 test: /\.(css|less)$/, 63 use: [MiniCssExtractPlugin.loader, 'css-loader', 'less-loader'], 64 }, 65 66 // { 67 // test: /\.(jsx|js)$/, 68 // enforce: 'pre', 69 // exclude: /node_modules/, 70 // loader: 'eslint-loader' 71 // }, 72 { 73 test: /\.(jsx|js)$/, 74 exclude: /node_modules/, 75 loader: 'babel-loader', 76 }, 77 ], 78 }, 79 externals: { 80 lodash: { 81 commonjs: 'lodash', 82 commonjs2: 'lodash', 83 amd: 'lodash', 84 root: '_', 85 }, 86 }, 87 };
package.json
{ "name": "vdearth", "version": "1.0.0", "description": "", "private": true, "dependencies": { "lodash": "^4.17.15", "three": "^0.115.0" }, "devDependencies": { "@babel/cli": "^7.8.4", "@babel/core": "^7.9.0", "@babel/preset-env": "^7.9.0", "babel-loader": "^8.1.0", "clean-webpack-plugin": "^3.0.0", "copy-webpack-plugin": "^5.1.1", "css-loader": "^3.5.2", "file-loader": "^6.0.0", "html-webpack-plugin": "^4.0.3", "json-loader": "^0.5.7", "less": "^3.11.1", "less-loader": "^5.0.0", "mini-css-extract-plugin": "^0.9.0", "optimize-css-assets-webpack-plugin": "^5.0.3", "style-loader": "^1.1.3", "webpack": "^4.42.1", "webpack-cli": "^3.3.11", "webpack-dev-server": "^3.10.3", "webpack-merge": "^4.2.2" }, "scripts": { "build": "webpack --config webpack.prod.js", "watch": "webpack --watch", "start": "webpack-dev-server --config webpack.dev.js" }, "keywords": [], "author": "", "license": "ISC" }
分层文件
在src文件内分别创建fonts,image,public,style文件夹,存放3D可视化的分类素材
新增VDEarth.js 作为主要地球逻辑的实现类,indexjs作为组件入口文件
安装组件
npm安装应用到的组件
npm install three 安装threejs组件
npm install lodash 安装lodash组件