【问题标题】:React-Leaflet with GatsbyReact-Leaflet 与 Gatsby
【发布时间】:2018-12-20 14:23:22
【问题描述】:

我在尝试修复 React-Leaflet / Leaflet 需要定义窗口并导致 Gatsby 构建 npm run build 失败时遇到了麻烦。

错误信息:

WebpackError: window is not defined

  - leaflet-src.js:230
    ~/leaflet/dist/leaflet-src.js:230:1

  - leaflet-src.js:7 version
    ~/leaflet/dist/leaflet-src.js:7:1

  - leaflet-src.js:10 Object.exports.__esModule
    ~/leaflet/dist/leaflet-src.js:10:2

  - AttributionControl.js:5 Object.<anonymous>
    ~/react-leaflet/lib/AttributionControl.js:5:1

  - index.js:26 Object.exports.__esModule
    ~/react-leaflet/lib/index.js:26:1

  - map.js:2 Object.exports.__esModule
    src/pages/map.js:2:1

Gatsby 文档列出了一些可能的场景或解决方案。 https://www.gatsbyjs.org/docs/debugging-html-builds/#fixing-third-party-modules

因此,通过尝试遵循列出的示例,我尝试添加 gatsby-node.js

gatsby-node.js

exports.modifyWebpackConfig = ({ config, stage }) => {
  if (stage === 'build-html') {
    config.loader('null', {
      test: /react-leaflet/,
      loader: 'null-loader',
    })
  }
}

test: /leaflet/test: /react-leaflet/

But that spits out a WebpackError: Minified React error #130

对于摆脱这些构建错误的任何帮助,我将不胜感激。

非常感谢。


步骤和代码

  1. $ gatsby new leaflet
  2. $ cd leaflet &amp;&amp; npm install
  3. 安装包和依赖$ npm install react-leaflet leaflet react react-dom
  4. 已创建src/pages/map.js
  5. https://github.com/PaulLeCam/react-leaflet/blob/master/example/components/simple.js复制/粘贴简单示例

ma​​p.js

import React, { Component } from 'react'
import { Map, TileLayer, Marker, Popup } from 'react-leaflet'

export default class SimpleExample extends Component {
  state = {
    lat: 51.505,
    lng: -0.09,
    zoom: 13,
  }

  render() {
    const position = [this.state.lat, this.state.lng]
    return (
      <Map center={position} zoom={this.state.zoom}>
        <TileLayer
          attribution="&amp;copy <a href=&quot;http://osm.org/copyright&quot;>OpenStreetMap</a> contributors"
          url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
        />
        <Marker position={position}>
          <Popup>
            A pretty CSS3 popup. <br /> Easily customizable.
          </Popup>
        </Marker>
      </Map>
    )
  }
}

【问题讨论】:

    标签: webpack gatsby react-leaflet


    【解决方案1】:

    我会使用您拥有的 modifyWebpackConfig 代码。缩小错误是因为现在 react-leaflet 是一个空模块,当它尝试使用 react-leaflet 组件构建任何页面时,它会导致错误。您必须将地图包装在检查中以查看窗口是否可用。这样它会在服务器端渲染中被跳过。

    在 React 中它看起来像这样。

    render() {
        if (typeof window !== 'undefined') {
            return (
                <Map {...options}>
    
                </Map>
            )
        }
        return null
    }
    

    【讨论】:

    • 谢谢。完全有道理。我不知道为什么我无法从错误消息中弄清楚。
    【解决方案2】:

    根据 2019 年 10 月的 Gatsby 版本提醒未来的读者; API 与 OPs 版本略有不同。

    gatsby-node.js

    
    exports.onCreateWebpackConfig = ({ stage, loaders, actions }) => {
      if (stage === 'build-html') {
        actions.setWebpackConfig({
          module: {
            rules: [
              {
                test: /react-leaflet|leaflet/,
                use: loaders.null(),
              },
            ],
          },
        })
      }
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多