react+cesium配置问题

网上很多教程,我没一个跑通了(大致就是各种库的版本之类的问题,但教程都没说版本,我会在最后附录说)。
而cesium、react具体是什么不多赘述,直接开始配置问题。

1 安装第三方库

npm install cesium -S //"cesium": "^1.104.0",
npm install copy-webpack-plugin -S //"copy-webpack-plugin": "^11.0.0",
npm install node-polyfill-webpack-plugin -S//"node-polyfill-webpack-plugin": "^2.0.1",

2 修改配置文件

首先需要把webpack配置文件暴露出来,执行下面代码后会出现config、scripts两个配置文件夹,我们会更改config中的webpack.config.js文件。

git add .
git commit –m “init”
npm run eject

3 引入插件

打开webpack.config.js,在众多引入插件的代码中添加下面代码。

const cesiumSource = 'node_modules/cesium/Source'
const dirCesiumSource = '../node_modules/cesium/Source'
const cesiumWorkers = '../Build/Cesium/Workers'
const CopyWebpackPlugin = require('copy-webpack-plugin') 
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin')

添加后如下图
在这里插入图片描述

4 添加amd对象

还是webpack.config.js,找到module.exports{......}中的return{......}中的output{......},与output后面添加如下代码(amd对象与output同级)

    amd: {
      // Enable webpack-friendly use of require in Cesium
      toUrlUndefined: true,
    },

添加后如下图
在这里插入图片描述

5 改动alias对象

还是webpack.config.js,找到reslove{......}中的alias:{}(与上一节中的output同级),添加如下代码

   cesium: path.resolve(cesiumSource),

添加后如下图
在这里插入图片描述

6 改动plugins

还是webpack.config.js,找到plugins:[......](与第四节中output同级,不是alias:{}同级的output),在plugins:[......]中添加如下代码

      new NodePolyfillPlugin(),
      new CopyWebpackPlugin({
        patterns: [
          { from: path.join(cesiumSource, cesiumWorkers), to: 'Workers' },
          { from: path.join(cesiumSource, 'Assets'), to: 'Assets' },
          { from: path.join(cesiumSource, 'Widgets'), to: 'Widgets' },
        ]
      }),

添加后,还是在plugins:[......]中找到new webpack.DefinePlugin({......}),改为如下代码

      new webpack.DefinePlugin({
        //env.stringified),
        CESIUM_BASE_URL: JSON.stringify('/'),
      }),

7 index.js

添加如下代码

import 'cesium/Widgets/widgets.css';//解决cesium页面混乱问题

于此同时,如果你的react是18+,并且会使用到生命周期函数,那么删掉两个</React.StrictMode>,解决地图加载两次的问题(就是页面中有两张地图,具体见链接生命周期函数执行了两次的问题

8 App.js

代码如下

import React, { Component } from 'react';
import * as Cesium  from "cesium/Cesium";
 
class App extends Component {
  componentDidMount() {
		const viewer = new Cesium.Viewer("cesiumContainer");
	}
  render() {
    return (
      <div id="cesiumContainer" />
    );
  }
}
export default App;

9 结语

改动完成后,保存,然后npm start,效果如下图(会报一些错,我确实不知道怎么改,我目前要求能用就行)
在这里插入图片描述

10 附录

版本如下

{
  "name": "myweb",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@babel/core": "^7.16.0",
    "@pmmmwh/react-refresh-webpack-plugin": "^0.5.3",
    "@svgr/webpack": "^5.5.0",
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@testing-library/user-event": "^13.5.0",
    "babel-jest": "^27.4.2",
    "babel-loader": "^8.2.3",
    "babel-plugin-named-asset-import": "^0.3.8",
    "babel-preset-react-app": "^10.0.1",
    "bfj": "^7.0.2",
    "browserslist": "^4.18.1",
    "camelcase": "^6.2.1",
    "case-sensitive-paths-webpack-plugin": "^2.4.0",
    "cesium": "^1.104.0",
    "copy-webpack-plugin": "^11.0.0",
    "css-loader": "^6.5.1",
    "css-minimizer-webpack-plugin": "^3.2.0",
    "dotenv": "^10.0.0",
    "dotenv-expand": "^5.1.0",
    "eslint": "^8.3.0",
    "eslint-config-react-app": "^7.0.1",
    "eslint-webpack-plugin": "^3.1.1",
    "file-loader": "^6.2.0",
    "fs-extra": "^10.0.0",
    "html-webpack-plugin": "^5.5.0",
    "identity-obj-proxy": "^3.0.0",
    "jest": "^27.4.3",
    "jest-resolve": "^27.4.2",
    "jest-watch-typeahead": "^1.0.0",
    "mini-css-extract-plugin": "^2.4.5",
    "node-polyfill-webpack-plugin": "^2.0.1",
    "postcss": "^8.4.4",
    "postcss-flexbugs-fixes": "^5.0.2",
    "postcss-loader": "^6.2.1",
    "postcss-normalize": "^10.0.1",
    "postcss-preset-env": "^7.0.1",
    "prompts": "^2.4.2",
    "react": "^18.2.0",
    "react-app-polyfill": "^3.0.0",
    "react-dev-utils": "^12.0.1",
    "react-dom": "^18.2.0",
    "react-refresh": "^0.11.0",
    "resolve": "^1.20.0",
    "resolve-url-loader": "^4.0.0",
    "sass-loader": "^12.3.0",
    "semver": "^7.3.5",
    "source-map-loader": "^3.0.0",
    "style-loader": "^3.3.1",
    "tailwindcss": "^3.0.2",
    "terser-webpack-plugin": "^5.2.5",
    "web-vitals": "^2.1.4",
    "webpack": "^5.64.4",
    "webpack-dev-server": "^4.6.0",
    "webpack-manifest-plugin": "^4.0.2",
    "workbox-webpack-plugin": "^6.4.1"
  },
  "scripts": {
    "start": "node scripts/start.js",
    "build": "node scripts/build.js",
    "test": "node scripts/test.js"
  },
  "eslintConfig": {
    "extends": [
      "react-app",
      "react-app/jest"
    ]
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "jest": {
    "roots": [
      "<rootDir>/src"
    ],
    "collectCoverageFrom": [
      "src/**/*.{js,jsx,ts,tsx}",
      "!src/**/*.d.ts"
    ],
    "setupFiles": [
      "react-app-polyfill/jsdom"
    ],
    "setupFilesAfterEnv": [
      "<rootDir>/src/setupTests.js"
    ],
    "testMatch": [
      "<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}",
      "<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}"
    ],
    "testEnvironment": "jsdom",
    "transform": {
      "^.+\\.(js|jsx|mjs|cjs|ts|tsx)$": "<rootDir>/config/jest/babelTransform.js",
      "^.+\\.css$": "<rootDir>/config/jest/cssTransform.js",
      "^(?!.*\\.(js|jsx|mjs|cjs|ts|tsx|css|json)$)": "<rootDir>/config/jest/fileTransform.js"
    },
    "transformIgnorePatterns": [
      "[/\\\\]node_modules[/\\\\].+\\.(js|jsx|mjs|cjs|ts|tsx)$",
      "^.+\\.module\\.(css|sass|scss)$"
    ],
    "modulePaths": [],
    "moduleNameMapper": {
      "^react-native$": "react-native-web",
      "^.+\\.module\\.(css|sass|scss)$": "identity-obj-proxy"
    },
    "moduleFileExtensions": [
      "web.js",
      "js",
      "web.ts",
      "ts",
      "web.tsx",
      "tsx",
      "json",
      "web.jsx",
      "jsx",
      "node"
    ],
    "watchPlugins": [
      "jest-watch-typeahead/filename",
      "jest-watch-typeahead/testname"
    ],
    "resetMocks": true
  },
  "babel": {
    "presets": [
      "react-app"
    ]
  }
}

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值