如何在react-map-gl中编写拆分Map组件的代码?

发布于 2025-01-18 12:57:20 字数 1907 浏览 3 评论 0原文

我正在尝试使用懒惰的悬念将React-Map-Gl映射组件编码为自己的块。

这是我的组成部分。

import 'mapbox-gl/dist/mapbox-gl.css'
import React, {lazy, Suspense} from 'react'
const Map = lazy(() => import('react-map-gl'))

const MAPBOX_TOKEN = process.env.REACT_APP_MAPBOX_TOKEN
const MAP_STYLE = process.env.REACT_APP_MAPBOX_STYLE_DARK

export default function MapboxMap() {

    const viewport = {
        latitude: 37.805,
        longitude: -122.447,
        zoom: 15.5
    }

    return <div style={{width: '500px', height: '500px', position: 'relative'}}>
        <Suspense fallback={<div>Loading...</div>}>
            <Map
                initialViewState={viewport}
                mapboxAccessToken={MAPBOX_TOKEN}
                mapStyle={MAP_STYLE}
                styleDiffing
            />
        </Suspense>
    </div>
}

这是我的软件包。JSON

{
  "name": "compression-test",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-map-gl": "^7.0.10",
    "react-scripts": "^5.0.0"
  },
  "scripts": {
    "start": "react-scripts --openssl-legacy-provider start",
    "build": "react-scripts --openssl-legacy-provider build",
    "analyze": "source-map-explorer 'build/static/js/*.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"
    ]
  },
  "devDependencies": {
    "source-map-explorer": "^2.5.2"
  }
}

,但是正如您可以从源地图示例屏幕截图中看到的那样,Mapbox仍在主块中。

“源地图Explorer”

我在这里做错了什么?是否有人成功尝试使用React-Map-Gl库和 设法将其分成自己的部分?

I'm trying to codesplit a react-map-gl Map component into its own chunk using Lazy,Suspense.

This is my component.

import 'mapbox-gl/dist/mapbox-gl.css'
import React, {lazy, Suspense} from 'react'
const Map = lazy(() => import('react-map-gl'))

const MAPBOX_TOKEN = process.env.REACT_APP_MAPBOX_TOKEN
const MAP_STYLE = process.env.REACT_APP_MAPBOX_STYLE_DARK

export default function MapboxMap() {

    const viewport = {
        latitude: 37.805,
        longitude: -122.447,
        zoom: 15.5
    }

    return <div style={{width: '500px', height: '500px', position: 'relative'}}>
        <Suspense fallback={<div>Loading...</div>}>
            <Map
                initialViewState={viewport}
                mapboxAccessToken={MAPBOX_TOKEN}
                mapStyle={MAP_STYLE}
                styleDiffing
            />
        </Suspense>
    </div>
}

and this is my package.json

{
  "name": "compression-test",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^17.0.2",
    "react-dom": "^17.0.2",
    "react-map-gl": "^7.0.10",
    "react-scripts": "^5.0.0"
  },
  "scripts": {
    "start": "react-scripts --openssl-legacy-provider start",
    "build": "react-scripts --openssl-legacy-provider build",
    "analyze": "source-map-explorer 'build/static/js/*.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"
    ]
  },
  "devDependencies": {
    "source-map-explorer": "^2.5.2"
  }
}

But as you can see from the source-map-explorer screenshot, Mapbox is still in the main chunk.

Source Map Explorer

What am I doing wrong here? Has anyone else successfully tried to use the react-map-gl library and
managed to split it into its own chunk?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文