Skip to content

Commit 466b89c

Browse files
authored
Merge pull request #404 from OvertureMaps/style-bundle
feat(styles): export and publish reusable style bundles
2 parents a14135f + 4ed6354 commit 466b89c

9 files changed

Lines changed: 675 additions & 227 deletions

File tree

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
# Publish Styles Workflow
3+
#
4+
# Triggered on: push of a tag matching styles-v* (e.g. styles-v1.0.0)
5+
# Jobs:
6+
# - publish: Runs the tests, builds the style bundle, and attaches it with its sha256 to a GitHub Release
7+
8+
name: Publish Styles
9+
run-name: Publish style bundle ${{ github.ref_name }}
10+
11+
on:
12+
push:
13+
tags: ["styles-v*"]
14+
15+
permissions:
16+
contents: read # required to checkout the code from the repo
17+
18+
concurrency:
19+
group: publish-styles-${{ github.ref }}
20+
cancel-in-progress: false
21+
22+
jobs:
23+
publish:
24+
name: Publish
25+
runs-on: ubuntu-latest
26+
permissions:
27+
contents: write # required to create the release and upload its assets
28+
29+
steps:
30+
- name: Checkout
31+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
32+
with:
33+
fetch-depth: 0
34+
persist-credentials: false
35+
36+
- name: Ensure tag is on the default branch
37+
env:
38+
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
39+
run: |
40+
git merge-base --is-ancestor "$GITHUB_SHA" "origin/$DEFAULT_BRANCH" || {
41+
echo "Tag commit must be reachable from $DEFAULT_BRANCH" >&2
42+
exit 1
43+
}
44+
45+
- name: Set up Node.js
46+
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
47+
with:
48+
node-version-file: package.json
49+
package-manager-cache: false # a published artifact must not be built from a restored cache
50+
51+
- name: Configure sustainable npm
52+
uses: lowlydba/sustainable-npm@4cc1da285e5a589a7b3816122f791c21ec6ff3e6 # v4.0.0
53+
54+
- name: Install
55+
run: npm ci
56+
57+
- name: Test
58+
run: npm test
59+
60+
- name: Build style bundle
61+
run: npm run build:styles
62+
63+
- name: Write checksum
64+
working-directory: dist
65+
run: sha256sum overture-styles.json > overture-styles.json.sha256
66+
67+
- name: Create release
68+
run: |
69+
SHA256="$(cut -d ' ' -f 1 dist/overture-styles.json.sha256)"
70+
gh release create "$TAG" \
71+
dist/overture-styles.json \
72+
dist/overture-styles.json.sha256 \
73+
--verify-tag \
74+
--notes-file - <<EOF
75+
MapLibre styles for the Overture Maps PMTiles, built from the layers the site renders.
76+
Source URLs contain a \`{release}\` placeholder for the Overture release id.
77+
78+
sha256: \`${SHA256}\`
79+
EOF
80+
env:
81+
GH_TOKEN: ${{ github.token }}
82+
TAG: ${{ github.ref_name }}

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
node_modules
22
.next
33
out
4+
dist
45
.swc
56
public/maplibre-gl-worker.mjs
67
public/maplibre-gl-shared.mjs

‎README.md‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ Open [http://localhost:3000](http://localhost:3000) in your browser.
4040
| `npm start` | Start the Next.js production server |
4141
| `npm run preview` | Serve the static export from `out/` locally |
4242
| `npm run lint` | Run ESLint |
43+
| `npm run build:styles` | Build the publishable style bundle to `dist/overture-styles.json` |
4344
| `npm test` | Run unit tests |
4445
| `npm run test:ci` | Run tests in CI mode |
4546

@@ -140,6 +141,43 @@ At import time, `index.js` resolves all `$`-references to their actual values fr
140141

141142
Each layer's `id` matches its filename without `.json` (e.g., `base/land-cover-fill.json` has `"id": "land-cover-fill"`). The rendering order is defined in `layer-order.json`.
142143

144+
### Publishable style bundle
145+
146+
`npm run build:styles` exports standalone [MapLibre styles](https://maplibre.org/maplibre-style-spec/), one per Overture collection (`theme/type`). It uses the same resolver as the site (`components/map/resolver.js`), avoiding a separate implementation of the style tokens.
147+
148+
The bundle exports resolved collection layers, not every part of the site's map setup. In particular, it does not include the background layer or the visibility choices applied by the site at runtime, so some layers may appear differently in other clients.
149+
150+
```json
151+
{
152+
"format_version": 1,
153+
"release_placeholder": "{release}",
154+
"collections": {
155+
"base/water": {
156+
"default": "explore",
157+
"styles": { "explore": { "version": 8, ... }, "inspect": { "version": 8, ... } }
158+
}
159+
}
160+
}
161+
```
162+
163+
- Each collection has an `explore` and/or `inspect` style. `default` is `explore` where it exists, otherwise `inspect`.
164+
- Source URLs contain `{release}` (e.g. `pmtiles://https://tiles.overturemaps.org/{release}/base.pmtiles`). Consumers replace it with a release id.
165+
- Styles have no `glyphs` or `sprite` by default, because the site loads fonts and icons itself. Pass `--glyphs <url>` and `--sprite <url>` to set them.
166+
- The script validates every style and prints the bundle's sha256, so consumers can pin it.
167+
168+
Run `node scripts/build-style-bundle.mjs --help` for all options.
169+
170+
#### Releasing
171+
172+
Push a tag named `styles-vX.Y.Z` to publish a new version:
173+
174+
```sh
175+
git tag styles-v1.0.0
176+
git push origin styles-v1.0.0
177+
```
178+
179+
The [Publish Styles](.github/workflows/publish-styles.yml) workflow runs the tests, builds the bundle, and attaches `overture-styles.json` and `overture-styles.json.sha256` to a GitHub Release. Consumers should pin the asset URL and the sha256 from that release.
180+
143181
## License
144182

145183
See the [LICENSE.md](LICENSE.md) file for more details.

‎__tests__/styleBundle.test.js‎

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import { validateStyleMin } from '@maplibre/maplibre-gl-style-spec';
2+
import { defaultLayerSpecs, inspectLayerSpecs } from '@/components/map';
3+
import { buildStyleBundle, findUnresolvedRefs } from '@/lib/styleBundle';
4+
5+
const variants = { explore: defaultLayerSpecs, inspect: inspectLayerSpecs };
6+
const bundle = buildStyleBundle({ variants });
7+
const entries = Object.entries(bundle.collections);
8+
const styles = entries.flatMap(([key, c]) => Object.entries(c.styles).map(([variant, style]) => [key, variant, style]));
9+
10+
describe('style bundle', () => {
11+
it('has one collection per theme/type in the layer specs', () => {
12+
const expected = new Set(
13+
[...defaultLayerSpecs, ...inspectLayerSpecs]
14+
.filter((s) => s.metadata?.['overture:theme'])
15+
.map((s) => `${s.metadata['overture:theme']}/${s.metadata['overture:type']}`)
16+
);
17+
expect(Object.keys(bundle.collections).sort()).toEqual([...expected].sort());
18+
});
19+
20+
it('places every collection layer in exactly one style, in manifest order', () => {
21+
for (const [variant, specs] of Object.entries(variants)) {
22+
const collectionSpecs = specs.filter((s) => s.metadata?.['overture:theme']);
23+
const bundled = styles.filter(([, v]) => v === variant).flatMap(([, , style]) => style.layers.map((l) => l.id));
24+
expect(bundled.sort()).toEqual(collectionSpecs.map((s) => s.id).sort());
25+
}
26+
for (const [key, variant, style] of styles) {
27+
const order = style.layers.map((l) => l.id);
28+
const manifest = variants[variant].map((s) => s.id).filter((id) => order.includes(id));
29+
expect([key, order]).toEqual([key, manifest]);
30+
}
31+
});
32+
33+
it('only puts a collection\'s own layers in its styles', () => {
34+
for (const [key, , style] of styles) {
35+
for (const layer of style.layers) {
36+
expect(`${layer.metadata['overture:theme']}/${layer.metadata['overture:type']}`).toBe(key);
37+
}
38+
}
39+
});
40+
41+
it('produces valid standalone MapLibre styles', () => {
42+
for (const [key, variant, style] of styles) {
43+
const errors = validateStyleMin(style);
44+
expect([key, variant, errors]).toEqual([key, variant, []]);
45+
}
46+
});
47+
48+
it('leaves no unresolved token references', () => {
49+
expect(findUnresolvedRefs(bundle)).toEqual([]);
50+
expect(findUnresolvedRefs({ paint: { color: '$unresolved' } })).toEqual(['paint.color']);
51+
});
52+
53+
it('points every source at its theme PMTiles with the release placeholder', () => {
54+
for (const [, , style] of styles) {
55+
for (const [name, source] of Object.entries(style.sources)) {
56+
expect(source.url).toBe(`pmtiles://https://tiles.overturemaps.org/{release}/${name}.pmtiles`);
57+
}
58+
for (const layer of style.layers) {
59+
expect(style.sources).toHaveProperty(layer.source);
60+
}
61+
}
62+
});
63+
64+
it('defaults to the first variant that styles the collection', () => {
65+
for (const [key, c] of entries) {
66+
const first = Object.keys(variants).find((v) => c.styles[v]);
67+
expect([key, c.default]).toEqual([key, first]);
68+
}
69+
});
70+
71+
it('sets glyphs and sprite only when given', () => {
72+
const [, , style] = styles[0];
73+
expect(style).not.toHaveProperty('glyphs');
74+
expect(style).not.toHaveProperty('sprite');
75+
76+
const withAssets = buildStyleBundle({ variants, glyphs: 'https://g/{fontstack}/{range}.pbf', sprite: 'https://s/sprite' });
77+
for (const c of Object.values(withAssets.collections)) {
78+
for (const s of Object.values(c.styles)) {
79+
expect(s.glyphs).toBe('https://g/{fontstack}/{range}.pbf');
80+
expect(s.sprite).toBe('https://s/sprite');
81+
}
82+
}
83+
});
84+
85+
it('rejects a tiles URL without the release placeholder', () => {
86+
expect(() => buildStyleBundle({ variants, tilesUrl: 'pmtiles://x/{theme}.pmtiles' })).toThrow('{release}');
87+
});
88+
89+
it('rejects a tiles URL without the theme placeholder', () => {
90+
expect(() => buildStyleBundle({ variants, tilesUrl: 'pmtiles://x/{release}/tiles.pmtiles' })).toThrow('{theme}');
91+
});
92+
93+
it('does not mutate the app layer specs', () => {
94+
const before = JSON.stringify(defaultLayerSpecs);
95+
const b = buildStyleBundle({ variants });
96+
b.collections['base/water'].styles.explore.layers[0].paint = {};
97+
expect(JSON.stringify(defaultLayerSpecs)).toBe(before);
98+
});
99+
});

0 commit comments

Comments
 (0)