我在尝试解决React-Leaflet / Leaflet问题时遇到了麻烦,因为它们需要定义window,这会导致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
我非常希望您能帮助解决这些构建错误。
非常感谢您。
步骤和代码
$ gatsby new leaflet
$ cd leaflet && npm install
- 安装包和依赖项
$ npm install react-leaflet leaflet react react-dom
- 创建
src/pages/map.js
- 从https://github.com/PaulLeCam/react-leaflet/blob/master/example/components/simple.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="&copy <a href="http://osm.org/copyright">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>
)
}
}