Load default marker icons error while use Webpack
See original GitHub issue- I’ve looked at the documentation to make sure the behavior is documented and expected
- I’m sure this is a Leaflet code issue, not an issue with my own code nor with the framework I’m using (Cordova, Ionic, Angular, React…)
- I’ve searched through the issues to make sure it’s not yet reported
Steps to reproduce Steps to reproduce the behavior:
L.marker([30.683628, 103.955644]).addTo(map)
Expected behavior
Just show the marker icons.
Current behavior
Cannot load the icon images and the following error occured:
Environment
- Leaflet version: 1.7.1
- Browser (with version): Chrome 87.0.4280.141 (x64)
- OS/Platform (with version): Windows10
Additional context
I use leaflet in webpack environment, and I load them async via import("leaflet")
.
The images are loaded as data url.
The issue
I reviewed the codes of leaflet, and found the following code:
The code always append the icon file name.
However, webpack may load it as data url ( with prefix data:image/png;base64,
),
The code resolve image path by detecting marker-icon.png exactly and cannot process data url.
Solution
Replace the declaration of IconDefault:
export var IconDefault = Icon.extend({
options: {
iconUrl: 'marker-icon.png',
iconRetinaUrl: 'marker-icon-2x.png',
shadowUrl: 'marker-shadow.png',
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
tooltipAnchor: [16, -28],
shadowSize: [41, 41]
},
_getIconUrl: function (name) {
if (!IconDefault.imagePath) { // Deprecated, backwards-compatibility only
var path = this._detectIconPath(name);
// Compatible with webpack
// Don't attach data url onto IconDefault.imagePath.
// Just return it as it is.
if (path.indexOf('data:image/') === 0) {
return path;
}
IconDefault.imagePath = path
}
// @option imagePath: String
// `Icon.Default` will try to auto-detect the location of the
// blue icon images. If you are placing these images in a non-standard
// way, set this option to point to the right path.
return (this.options.imagePath || IconDefault.imagePath) + Icon.prototype._getIconUrl.call(this, name);
},
_detectIconPath: function (name) {
var el = create$1('div', 'leaflet-default-marker-' + name, document.body);
var path = getStyle(el, 'background-image') ||
getStyle(el, 'backgroundImage'); // IE8
document.body.removeChild(el);
if (path === null || path.indexOf('url') !== 0) {
path = '';
} else {
// Compatible with webpack
path = path.replace(/^url\((["']?)(.+?)(marker-(icon|shadow)\.png)?\1\)/, '$2');
}
return path;
}
});
What’s more, some CSS should be touch:
Instead CSS content:
.leaflet-default-icon-path {
background-image: url(images/marker-icon.png);
}
With following content:
.leaflet-default-marker-icon {
background-image: url(images/marker-icon.png);
}
.leaflet-default-marker-icon-2x {
background-image: url(images/marker-icon-2x.png);
}
.leaflet-default-marker-shadow {
background-image: url(images/marker-shadow.png);
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:7
Top Results From Across the Web
webpack not able to resolve node_modules material-icons
1 Answer 1 · delete the node_modules folder · run yarn/npm install whatever you're using to re-install packages · Then add the required...
Read more >To v5 from v4 - webpack
This guide aims to help you migrating to webpack 5 when using webpack directly. If you are using a higher level tool to...
Read more >css-loader | webpack - JS.ORG
The default export is array of modules with specific API which is used in style-loader or other. webpack.config.js module.exports = { module: {...
Read more >MiniCssExtractPlugin - webpack
It creates a CSS file per JS file which contains CSS. It supports On-Demand-Loading of CSS and SourceMaps. It builds on top of...
Read more >Tree Shaking - webpack
Tree shaking is a term commonly used in the JavaScript context for dead-code elimination. It relies on the static structure of ES2015 module...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Here is a patch function for the issue ( Webpack ENV ):
Thanks, it seems the PR has not been merged. I need an easy way to fix it rather than waiting for the next release.