Skip to content

Commit 71e351e

Browse files
lesbaaLes Moffat
andauthored
RD-902 Fixes MapLibre direct import due to CJS issues (#190)
* RD-902 Fixes MapLibre direct import due to CJS issues * 3.2.1 * RD-902 Add RC suffix for testing * RD-902 Update changelog * RD-902 - Add eslint config to check for non-default import from maplibre-js - version bump * RD-902 Update eslint regex condition to only catch maplibre-gl lib --------- Co-authored-by: Les Moffat <[email protected]>
1 parent 7c59f89 commit 71e351e

File tree

5 files changed

+87
-4
lines changed

5 files changed

+87
-4
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# MapTiler SDK Changelog
22

3+
## 3.2.1
4+
## ✨ Features and improvements
5+
None
6+
7+
## 🐛 Bug fixes
8+
- [RD-902](https://maptiler.atlassian.net/browse/RD-902?atlOrigin=eyJpIjoiNGM2NGQxNzg0ZjEzNGJlMGI3M2Y1YTM3YTIyNjdkMDkiLCJwIjoiaiJ9) Changes to use default import for maplibre-gl as it uses commonjs modules under the hood.
9+
10+
## 🔧 Others
11+
- Adds linting config to check for non default maplibre defaults. Named imports from CJS modules fail on some build pipelines.
12+
313
## 3.2.0
414
## ✨ Features and improvements
515
- Updates Maplibre-gl to 5.3.1

eslint.config.mjs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,77 @@ export default tseslint.config(
88
tseslint.configs.strictTypeChecked,
99
tseslint.configs.stylisticTypeChecked,
1010
tseslint.configs.recommendedTypeChecked,
11+
{
12+
// forked from https://www.npmjs.com/package/eslint-plugin-restrict-imports
13+
plugins: {
14+
import: {
15+
rules: {
16+
"default-imports-only": {
17+
meta: {
18+
type: "suggestion",
19+
docs: {},
20+
schema: [
21+
{
22+
bannedImport: {
23+
locations: ["filePaths"],
24+
message: "string",
25+
fixedLocation: "string",
26+
},
27+
},
28+
],
29+
},
30+
create: function (context) {
31+
const filePath = context.getFilename();
32+
const options = context.options[0] || {
33+
"^/(.*)": {
34+
locations: ["(.*)"],
35+
},
36+
};
37+
38+
return {
39+
ImportDeclaration: (node) => {
40+
Object.entries(options).forEach(([bannedImport, config]) => {
41+
const importLocationRegex = new RegExp(bannedImport);
42+
43+
if (config.ignoreTypeImports && node.importKind === "type") return;
44+
45+
if (importLocationRegex.test(node.source.value)) {
46+
config.locations.forEach((fp) => {
47+
const bannedLocationRegex = new RegExp(fp);
48+
49+
if (bannedLocationRegex.test(filePath)) {
50+
node.specifiers.forEach((specifier) => {
51+
if (specifier.type !== "ImportDefaultSpecifier") {
52+
context.report({
53+
message: config.message ?? `Importing from '${bannedImport}' is banned in '${fp}'`,
54+
node,
55+
});
56+
}
57+
});
58+
}
59+
});
60+
}
61+
});
62+
},
63+
};
64+
},
65+
},
66+
},
67+
},
68+
},
69+
rules: {
70+
"import/default-imports-only": [
71+
"error",
72+
{
73+
"maplibre-gl$": {
74+
locations: ["^(?!.*\.d\.ts$).*\.((ts|js))$"],
75+
message: `Maplibre-gl uses CJS modules, only default imports are supported, named imports may fail on some setups.`,
76+
ignoreTypeImports: true,
77+
},
78+
},
79+
],
80+
},
81+
},
1182
{
1283
languageOptions: {
1384
parserOptions: {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@maptiler/sdk",
3-
"version": "3.2.0",
3+
"version": "3.2.1",
44
"description": "The Javascript & TypeScript map SDK tailored for MapTiler Cloud",
55
"author": "MapTiler",
66
"module": "dist/maptiler-sdk.mjs",

src/caching.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { GetResourceResponse, RequestParameters, ResourceType } from "mapli
22

33
import { config } from "./config";
44

5-
import { addProtocol } from "maplibre-gl";
5+
import maplibregl from "maplibre-gl";
66

77
import { defaults } from "./constants/defaults";
88

@@ -14,6 +14,8 @@ const CACHE_LIMIT_ITEMS = 1000;
1414
const CACHE_LIMIT_CHECK_INTERVAL = 100;
1515
export const CACHE_API_AVAILABLE = typeof caches !== "undefined";
1616

17+
const { addProtocol } = maplibregl;
18+
1719
export function localCacheTransformRequest(reqUrl: URL, resourceType?: ResourceType): string {
1820
if (CACHE_API_AVAILABLE && config.caching && config.session && reqUrl.host === defaults.maptilerApiHost) {
1921
if (resourceType === "Source" && reqUrl.href.includes("tiles.json")) {

0 commit comments

Comments
 (0)