Skip to content

Commit 665f3bf

Browse files
committed
set worker url explicit, add esm tests
1 parent a8b84bf commit 665f3bf

File tree

7 files changed

+111
-115
lines changed

7 files changed

+111
-115
lines changed

build/rollup/maplibregl.esm.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

rollup.config.csp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const configs = [
3030
config('src/source/worker.ts', `dist/maplibre-gl-csp-worker${outputPostfix}.js`, 'iife'),
3131

3232
// ESM builds for CSP
33-
config('src/index_esm.ts', `dist/maplibre-gl-csp${outputPostfix}.mjs`, 'es'),
33+
config('src/index.ts', `dist/maplibre-gl-csp${outputPostfix}.mjs`, 'es'),
3434
config('src/source/worker.ts', `dist/maplibre-gl-csp-worker${outputPostfix}.mjs`, 'es')
3535
];
3636

rollup.config.ts

Lines changed: 32 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -63,77 +63,39 @@ const config: RollupOptions[] = [{
6363
// only they get built, but not the merged dev build js
6464
...production ? [] : [watchStagingPlugin]
6565
],
66-
},
67-
// ESM build configuration
68-
{
69-
// First pass for ESM: Split into ES modules
70-
input: ['src/index_esm.ts', 'src/source/worker.ts'],
71-
output: {
72-
dir: 'staging/esm',
73-
format: 'es',
74-
sourcemap: 'inline',
75-
indent: false,
76-
chunkFileNames: '[name].js',
77-
entryFileNames: (chunkInfo) => {
78-
// Rename index_esm to index for consistency
79-
if (chunkInfo.name === 'index_esm') return 'index.js';
80-
return '[name].js';
66+
}];
67+
68+
// ESM builds (following CSP pattern - separate main and worker files)
69+
// Users must explicitly call setWorkerUrl() when using these builds
70+
const esmConfig: RollupOptions[] = [
71+
{
72+
// ESM main bundle
73+
input: 'src/index.ts',
74+
output: {
75+
file: production ? 'dist/maplibre-gl.mjs' : 'dist/maplibre-gl-dev.mjs',
76+
format: 'es',
77+
sourcemap: true,
78+
indent: false,
79+
banner
8180
},
82-
minifyInternalExports: production,
83-
manualChunks: (id) => {
84-
// Shared utilities go into a shared chunk
85-
if (id.includes('src/util/') ||
86-
id.includes('src/style-spec/') ||
87-
id.includes('src/data/')) {
88-
return 'shared';
89-
}
90-
}
91-
},
92-
onwarn: (message) => {
93-
console.error(message);
94-
throw message;
95-
},
96-
treeshake: production,
97-
plugins: plugins(production)
98-
}, {
99-
// Second pass for ESM: Bundle with module worker support
100-
input: 'build/rollup/maplibregl.esm.js',
101-
output: {
102-
file: esmOutputFile,
103-
format: 'es',
104-
sourcemap: true,
105-
indent: false,
106-
banner
81+
treeshake: production,
82+
plugins: plugins(production)
10783
},
108-
watch: {
109-
buildDelay: 1000
110-
},
111-
treeshake: false,
112-
plugins: [
113-
sourcemaps(),
114-
{
115-
name: 'watch-esm-staging',
116-
buildStart() {
117-
if (!production) {
118-
this.addWatchFile('staging/esm/index.js');
119-
this.addWatchFile('staging/esm/worker.js');
120-
this.addWatchFile('staging/esm/shared.js');
121-
}
122-
}
123-
}
124-
]
125-
}, {
126-
// Separate ESM worker bundle
127-
input: 'src/source/worker.ts',
128-
output: {
129-
file: production ? 'dist/maplibre-gl-worker.mjs' : 'dist/maplibre-gl-worker-dev.mjs',
130-
format: 'es',
131-
sourcemap: true,
132-
indent: false,
133-
banner
134-
},
135-
treeshake: production,
136-
plugins: plugins(production)
137-
}];
84+
{
85+
// ESM worker bundle
86+
input: 'src/source/worker.ts',
87+
output: {
88+
file: production ? 'dist/maplibre-gl-worker.mjs' : 'dist/maplibre-gl-worker-dev.mjs',
89+
format: 'es',
90+
sourcemap: true,
91+
indent: false,
92+
banner
93+
},
94+
treeshake: production,
95+
plugins: plugins(production)
96+
}
97+
];
98+
99+
config.push(...esmConfig);
138100

139101
export default config;

src/index_esm.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/util/setup_esm_worker.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

test/build/esm.test.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import {describe, test, expect} from 'vitest';
2+
import fs from 'fs';
3+
import path from 'path';
4+
5+
describe('ESM build', () => {
6+
test('ESM main bundle exists and exports expected API', () => {
7+
const esmPath = path.join(process.cwd(), 'dist/maplibre-gl-dev.mjs');
8+
expect(fs.existsSync(esmPath)).toBe(true);
9+
10+
const content = fs.readFileSync(esmPath, 'utf8');
11+
12+
// Check for ES module exports at the end of the file
13+
expect(content).toMatch(/export\s+\{[^}]+\};\s*$/m);
14+
expect(content).toContain('Map');
15+
expect(content).toContain('Marker');
16+
expect(content).toContain('Popup');
17+
expect(content).toContain('setWorkerUrl');
18+
expect(content).toContain('getWorkerUrl');
19+
20+
// The bundle should use ES module format (export statements)
21+
// Note: Some dependencies may contain module.exports internally,
22+
// but the bundle itself should export using ES module syntax
23+
expect(content).toMatch(/^export\s+\{/m);
24+
});
25+
26+
test('ESM worker bundle exists', () => {
27+
const workerPath = path.join(process.cwd(), 'dist/maplibre-gl-worker-dev.mjs');
28+
expect(fs.existsSync(workerPath)).toBe(true);
29+
30+
const content = fs.readFileSync(workerPath, 'utf8');
31+
32+
// Worker should be an ES module
33+
// The worker doesn't export anything but should not use AMD
34+
expect(content).not.toContain('define.amd');
35+
36+
// Should contain worker-specific code
37+
expect(content).toContain('Actor');
38+
});
39+
40+
test('Production ESM builds exist', () => {
41+
// These might not exist if only dev build was run
42+
const mainProd = path.join(process.cwd(), 'dist/maplibre-gl.mjs');
43+
const workerProd = path.join(process.cwd(), 'dist/maplibre-gl-worker.mjs');
44+
45+
if (fs.existsSync(mainProd)) {
46+
const content = fs.readFileSync(mainProd, 'utf8');
47+
expect(content).toContain('export {');
48+
}
49+
50+
if (fs.existsSync(workerProd)) {
51+
const content = fs.readFileSync(workerProd, 'utf8');
52+
// Should be ES module format with export statement
53+
expect(content).toContain('export');
54+
}
55+
});
56+
57+
test('CSP ESM builds follow same pattern', () => {
58+
const cspMain = path.join(process.cwd(), 'dist/maplibre-gl-csp-dev.mjs');
59+
const cspWorker = path.join(process.cwd(), 'dist/maplibre-gl-csp-worker-dev.mjs');
60+
61+
if (fs.existsSync(cspMain)) {
62+
const content = fs.readFileSync(cspMain, 'utf8');
63+
expect(content).toContain('export {');
64+
// Should be ES module format with export statement
65+
expect(content).toContain('export');
66+
}
67+
68+
if (fs.existsSync(cspWorker)) {
69+
const content = fs.readFileSync(cspWorker, 'utf8');
70+
// Should be ES module format with export statement
71+
expect(content).toContain('export');
72+
}
73+
});
74+
});

test/examples/filter-layer-symbols-using-global-state.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@
4141
</body>
4242

4343
<script type="module">
44-
import maplibregl from '../../dist/maplibre-gl-dev.mjs';
44+
import * as maplibregl from '../../dist/maplibre-gl-dev.mjs';
45+
46+
// Set the worker URL for ESM build (CSP-style pattern)
47+
maplibregl.setWorkerUrl('../../dist/maplibre-gl-worker-dev.mjs');
4548

4649
const map = new maplibregl.Map({
4750
container: 'map',

0 commit comments

Comments
 (0)