Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing Leaflet 1.5.0 fails #6645

Closed
marufkilic opened this issue May 8, 2019 · 6 comments · Fixed by #6647
Closed

Importing Leaflet 1.5.0 fails #6645

marufkilic opened this issue May 8, 2019 · 6 comments · Fixed by #6647
Labels
Milestone

Comments

@marufkilic
Copy link

marufkilic commented May 8, 2019

When using with react, leaflet 1.5.0 fails. However 1.4.0 works as expected

Code fails excatly at init function line "const map = L.map(mapName, { ..."

Class to render map

import React, { Component } from "react";

import L from "leaflet";
import { initMap } from "./init";

/**
 * @return {*} undefined
 */
export default class MapPanel extends Component {
  /**
   * @return {*} undefined
   */
  componentDidMount = () => {
    this.leftMap = initMap("lmap", "http://{s}.tile.osm.org/{z}/{x}/{y}.png");
    this.rightMap = initMap("rmap", "http://{s}.tile.osm.org/{z}/{x}/{y}.png");
  };

 render() {
    const props = this.props;
    const { job } = props;

    if (job && job.generated_polygon) this.moveWindow(job.generated_polygon);

    return (
      <div className="relative flex flex-col sm:flex-row flex-1">
        <div className="z-0 flex-1 sm:h-full" id="lmap" />
        <div className="h-1 sm:h-full sm:w-1 bg-theme-800" />
        <div className="z-0 flex-1 sm:h-full" id="rmap" />
      </div>
    );
  }
}

The init function

import L from "leaflet";

export const initMap = (mapName, url) => {
  // create map
  const map = L.map(mapName, {
    center: [49.8419, 24.0315],
    zoom: 16,
    layers: [
      L.tileLayer(url, {
        attribution:
          '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
      })
    ]
  });

  return map;
};

And the error

image

Webpack Config

const path = require("path");
const webpack = require("webpack");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CleanWebpackPlugin = require("clean-webpack-plugin");

const outputDirectory = "dist";

module.exports = {
  devtool: "source-map",
  entry: ["@babel/polyfill", "./src/client/index.js", "./src/client/index.css"],
  output: {
    path: path.join(__dirname, outputDirectory),
    filename: "bundle.js"
  },
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: {
          loader: "babel-loader"
        }
      },
      {
        test: /\.css$/,
        use: ["style-loader", "css-loader", "postcss-loader"]
      },
      {
        test: /\.(png|jpg|jpeg|woff|woff2|eot|ttf|svg)$/,
        loader: "url-loader?limit=100000"
      }
    ]
  },
  optimization: {
    splitChunks: {
      chunks: "all"
    }
  }
};
@cherniavskii
Copy link
Sponsor Collaborator

@marufkilic can you publish minimal example, which reproduces the issue?

@marufkilic
Copy link
Author

Above two classes can produce the error. It is enough to render the MapPanel component in a main React component.

@cherniavskii
Copy link
Sponsor Collaborator

Looks like there's no default export in leaflet@1.5.0

@cherniavskii
Copy link
Sponsor Collaborator

Caused by line:

Object.defineProperty(exports, '__esModule', { value: true });

at the end of leaflet-src.

This probably changed in rollup, which we updated in #6635

@marufkilic
Copy link
Author

marufkilic commented May 8, 2019

Also added webpack config

@cherniavskii
Copy link
Sponsor Collaborator

Quick workaround on users side:

- import L from 'leaflet';
+ import * as L from 'leaflet';

@cherniavskii cherniavskii pinned this issue May 8, 2019
@cherniavskii cherniavskii changed the title ReactJS and Leaflet 1.5.0 fails Importing Leaflet 1.5.0 fails May 8, 2019
@cherniavskii cherniavskii added this to the 1.5.1 milestone May 8, 2019
This was referenced May 8, 2019
@IvanSanchez IvanSanchez unpinned this issue May 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants