I am trying to get the Leaflet library to play well with my Typescript project.
My project follows the commonJs pattern (imports\exports in my source files), but when I build with webpack, the compiler gives me the following errors from my files where I try to access an L
global exposed by the leaflet module
'L' refers to a UMD global, but the current file is a module. Consider adding an import instead.
Now I can get around this if I import in leaflet into my source files as follows:
import * as L from 'leaflet'
But I was wondering if I could access t globally across all my files without having to have imports in each of my files.
I had a dig around and found some workarounds where you can declare your own globals like so:
globals.d.ts
import * as L from "leaflet";
declare global {
const L: L
}
export {};
I'm not sure if this is recommended, it feels a bit of a dirty hack, is there a proper way with webpack config to provide globals exposed by UMD modules for a commonJs project, or am I talking nonsense? I admit I get confused trying to understand the differences between the module types and their interchangeability, so go easy on me!
*.min.js
UMD library files manually via a Gulp task, but we have changed all of our TypeScript code to modules. We compile to SystemJS modules and do not have Webpack incorporated in the project and it would take significant effort to rewrite the project structure. Thus, declaring in the global namespace is necessary to be able to use UMD globals without TypeScript complaining. – Dibucaine