-
-
Notifications
You must be signed in to change notification settings - Fork 932
ESM build #6254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
ESM build #6254
Changes from 21 commits
82beef4
e3a7755
63c12a0
0336d73
e9e573f
22a073c
bbb18d6
265daf6
5bdcb4e
ad9077e
0c1f6be
9b181a1
71af865
5e108f3
2b730cb
92b2b85
48c11d3
9ad23a8
f349838
b739a96
4902b64
3a4e14f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import {describe, test, expect} from 'vitest'; | ||
| import fs from 'fs'; | ||
| import path from 'path'; | ||
|
|
||
| describe('ESM build', () => { | ||
| test('ESM main bundle exists and exports expected API', () => { | ||
| const esmPath = path.join(process.cwd(), 'dist/maplibre-gl-dev.mjs'); | ||
| expect(fs.existsSync(esmPath)).toBe(true); | ||
|
|
||
| const content = fs.readFileSync(esmPath, 'utf8'); | ||
|
|
||
| // Check for ES module exports at the end of the file | ||
| expect(content).toMatch(/export\s+\{[^}]+\};\s*$/m); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not super thrilled with checking the string content of the file, is there a better way to check this? |
||
| expect(content).toContain('Map'); | ||
| expect(content).toContain('Marker'); | ||
| expect(content).toContain('Popup'); | ||
| expect(content).toContain('setWorkerUrl'); | ||
| expect(content).toContain('getWorkerUrl'); | ||
|
|
||
| // The bundle should use ES module format (export statements) | ||
| // Note: Some dependencies may contain module.exports internally, | ||
| // but the bundle itself should export using ES module syntax | ||
| expect(content).toMatch(/^export\s+\{/m); | ||
| }); | ||
|
|
||
| test('ESM worker bundle exists', () => { | ||
| const workerPath = path.join(process.cwd(), 'dist/maplibre-gl-worker-dev.mjs'); | ||
| expect(fs.existsSync(workerPath)).toBe(true); | ||
|
|
||
| const content = fs.readFileSync(workerPath, 'utf8'); | ||
|
|
||
| // Worker should be an ES module | ||
| // The worker doesn't export anything but should not use AMD | ||
| expect(content).not.toContain('define.amd'); | ||
|
|
||
| // Should contain worker-specific code | ||
| expect(content).toContain('Actor'); | ||
| }); | ||
|
|
||
| test('Production ESM builds exist', () => { | ||
| // These might not exist if only dev build was run | ||
| const mainProd = path.join(process.cwd(), 'dist/maplibre-gl.mjs'); | ||
| const workerProd = path.join(process.cwd(), 'dist/maplibre-gl-worker.mjs'); | ||
|
|
||
| if (fs.existsSync(mainProd)) { | ||
| const content = fs.readFileSync(mainProd, 'utf8'); | ||
| // Production build is minified, just check for export statement | ||
| expect(content).toContain('export'); | ||
| } | ||
|
|
||
| if (fs.existsSync(workerProd)) { | ||
| const content = fs.readFileSync(workerProd, 'utf8'); | ||
| // Should be ES module format with export statement | ||
| expect(content).toContain('export'); | ||
| } | ||
| }); | ||
|
|
||
| test('CSP ESM builds follow same pattern', () => { | ||
| const cspMain = path.join(process.cwd(), 'dist/maplibre-gl-csp-dev.mjs'); | ||
| const cspWorker = path.join(process.cwd(), 'dist/maplibre-gl-csp-worker-dev.mjs'); | ||
|
|
||
| if (fs.existsSync(cspMain)) { | ||
| const content = fs.readFileSync(cspMain, 'utf8'); | ||
| expect(content).toContain('export {'); | ||
| // Should be ES module format with export statement | ||
| expect(content).toContain('export'); | ||
| } | ||
|
|
||
| if (fs.existsSync(cspWorker)) { | ||
| const content = fs.readFileSync(cspWorker, 'utf8'); | ||
| // Should be ES module format with export statement | ||
| expect(content).toContain('export'); | ||
| } | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,33 @@ | ||||
| <!DOCTYPE html> | ||||
| <html lang="en"> | ||||
| <head> | ||||
| <title>Display a map with ESM</title> | ||||
| <meta property="og:description" content="Initialize a map in an HTML element with MapLibre GL JS using EcmaScript Module (ESM)." /> | ||||
| <meta charset='utf-8'> | ||||
| <meta name="viewport" content="width=device-width, initial-scale=1"> | ||||
| <link rel='stylesheet' href='../../dist/maplibre-gl.css' /> | ||||
| </head> | ||||
| <style> | ||||
| body { margin: 0; padding: 0; } | ||||
| html, body, #map { height: 100%; } | ||||
| </style> | ||||
| </head> | ||||
| <body> | ||||
| <div id="map"></div> | ||||
| <script type="module"> | ||||
| import * as maplibregl from '../../dist/maplibre-gl.mjs'; | ||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this will work in the docs' examples. maplibre-gl-js/build/generate-docs.ts Line 101 in 05067bd
|
||||
|
|
||||
| // Set the worker URL for ESM build (CSP-style pattern) | ||||
| maplibregl.setWorkerUrl('../../dist/maplibre-gl-worker-dev.mjs'); | ||||
|
|
||||
|
|
||||
| const map = new maplibregl.Map({ | ||||
| container: 'map', // container id | ||||
| style: 'https://demotiles.maplibre.org/style.json', // style URL | ||||
| center: [0, 0], // starting position [lng, lat] | ||||
| zoom: 1, // starting zoom | ||||
| maplibreLogo: true | ||||
| }); | ||||
| </script> | ||||
| </body> | ||||
| </html> | ||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,4 +103,4 @@ | |
| }); | ||
| </script> | ||
|
|
||
| </html> | ||
| </html> | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a better name to the file and method is probably needed here.