Skip to content
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

Improve exports #3601

Merged
merged 22 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 28 additions & 17 deletions build/rollup/bundle_prelude.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,32 @@
/* eslint-disable */

var shared, worker, maplibregl;
// define gets called three times: one for each chunk. we rely on the order
// they're imported to know which is which
function define(_, chunk) {
if (!shared) {
shared = chunk;
} else if (!worker) {
worker = chunk;
} else {
var workerBundleString = 'var sharedChunk = {}; (' + shared + ')(sharedChunk); (' + worker + ')(sharedChunk);'

var sharedChunk = {};
shared(sharedChunk);
maplibregl = chunk(sharedChunk);
var maplibregl = {};

var define = (function() {
HarelM marked this conversation as resolved.
Show resolved Hide resolved
var modules = {};

return function(moduleName, dependencies, moduleFactory) {
modules[moduleName] = moduleFactory;

// to get the list of modules see generated dist/maplibre-gl-dev.js file (look for `define(` calls)
if (moduleName !== 'index') {
return;
}

// we assume that when an index module is initializing then other modules are loaded already
var workerBundleString = 'var sharedModule = {}; (' + modules.shared + ')(sharedModule); (' + modules.worker + ')(sharedModule);'

var sharedModule = {};
// the order of arguments of a module factory depends on rollup (it decides who is whose dependency)
// to check the correct order, see dist/maplibre-gl-dev.js file (look for `define(` calls)
// we assume that for our 3 chunks it will generate 3 modules and their order is predefined like the following
modules.shared(sharedModule);
modules.index(maplibregl, sharedModule);

if (typeof window !== 'undefined') {
maplibregl.workerUrl = window.URL.createObjectURL(new Blob([workerBundleString], { type: 'text/javascript' }));
maplibregl.setWorkerUrl(window.URL.createObjectURL(new Blob([workerBundleString], { type: 'text/javascript' })));
}
}
}

return maplibregl;
};
})();
33 changes: 4 additions & 29 deletions build/rollup/maplibregl.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,42 +6,17 @@
// The three "chunks" imported here are produced by a first Rollup pass,
// which outputs them as AMD modules.

// Shared dependencies, i.e.:
/*
define(['exports'], function (exports) {
// Code for all common dependencies
// Each module's exports are attached attached to 'exports' (with
// names rewritten to avoid collisions, etc.)
})
*/
// Shared dependencies
import '../../staging/maplibregl/shared';

// Worker and its unique dependencies, i.e.:
/*
define(['./shared.js'], function (__shared__js) {
// Code for worker script and its unique dependencies.
// Expects the output of 'shared' module to be passed in as an argument,
// since all references to common deps look like, e.g.,
// __shared__js.shapeText().
});
*/
// When this wrapper function is passed to our custom define() above,
// Worker and its unique dependencies
// When this wrapper function is passed to our custom define() in build/rollup/bundle_prelude.js,
// it gets stringified, together with the shared wrapper (using
// Function.toString()), and the resulting string of code is made into a
// Blob URL that gets used by the main module to create the web workers.
import '../../staging/maplibregl/worker';

// Main module and its unique dependencies
/*
define(['./shared.js'], function (__shared__js) {
// Code for main GL JS module and its unique dependencies.
// Expects the output of 'shared' module to be passed in as an argument,
// since all references to common deps look like, e.g.,
// __shared__js.shapeText().
//
// Returns the actual maplibregl (i.e. src/index.js)
});
*/
// Main module and its dependencies
import '../../staging/maplibregl/index';

export default maplibregl;
5 changes: 4 additions & 1 deletion rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const config: RollupOptions[] = [{
sourcemap: 'inline',
indent: false,
chunkFileNames: 'shared.js',
amd: {
autoId: true,
},
minifyInternalExports: production
},
onwarn: (message) => {
Expand All @@ -39,7 +42,7 @@ const config: RollupOptions[] = [{
output: {
name: 'maplibregl',
file: outputFile,
format: 'umd',
format: 'iife',
HarelM marked this conversation as resolved.
Show resolved Hide resolved
sourcemap: true,
indent: false,
intro: fs.readFileSync('build/rollup/bundle_prelude.js', 'utf8'),
Expand Down
26 changes: 13 additions & 13 deletions src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {config} from './util/config';
import maplibre from './index';
import {addProtocol, getWorkerCount, removeProtocol, getVersion} from './index';
import {getJSON, getArrayBuffer} from './util/ajax';
import {ImageRequest} from './util/image_request';
import {isAbortError} from './util/abort_error';
Expand All @@ -13,31 +13,31 @@ describe('maplibre', () => {
});

test('workerCount', () => {
expect(typeof maplibre.workerCount === 'number').toBeTruthy();
expect(typeof getWorkerCount() === 'number').toBeTruthy();
});

test('addProtocol', () => {
const protocolName = 'custom';
expect(Object.keys(config.REGISTERED_PROTOCOLS)).toHaveLength(0);

maplibre.addProtocol(protocolName, async () => Promise.resolve({} as any));
addProtocol(protocolName, async () => Promise.resolve({} as any));
expect(Object.keys(config.REGISTERED_PROTOCOLS)[0]).toBe(protocolName);
});

test('removeProtocol', () => {
const protocolName = 'custom';
expect(Object.keys(config.REGISTERED_PROTOCOLS)).toHaveLength(0);

maplibre.addProtocol(protocolName, () => Promise.resolve({} as any));
addProtocol(protocolName, () => Promise.resolve({} as any));
expect(Object.keys(config.REGISTERED_PROTOCOLS)[0]).toBe(protocolName);

maplibre.removeProtocol(protocolName);
removeProtocol(protocolName);
expect(Object.keys(config.REGISTERED_PROTOCOLS)).toHaveLength(0);
});

test('#addProtocol - getJSON', async () => {
let protocolCallbackCalled = false;
maplibre.addProtocol('custom', () => {
addProtocol('custom', () => {
protocolCallbackCalled = true;
return Promise.resolve({data: {'foo': 'bar'}});
});
Expand All @@ -48,7 +48,7 @@ describe('maplibre', () => {

test('#addProtocol - getArrayBuffer', async () => {
let protocolCallbackCalled = false;
maplibre.addProtocol('custom', () => {
addProtocol('custom', () => {
protocolCallbackCalled = true;
return Promise.resolve({data: new ArrayBuffer(1), cacheControl: 'cache-control', expires: 'expires'});
});
Expand All @@ -61,7 +61,7 @@ describe('maplibre', () => {

test('#addProtocol - returning ImageBitmap for getImage', async () => {
let protocolCallbackCalled = false;
maplibre.addProtocol('custom', () => {
addProtocol('custom', () => {
protocolCallbackCalled = true;
return Promise.resolve({data: new ImageBitmap()});
});
Expand All @@ -73,7 +73,7 @@ describe('maplibre', () => {

test('#addProtocol - returning HTMLImageElement for getImage', async () => {
let protocolCallbackCalled = false;
maplibre.addProtocol('custom', () => {
addProtocol('custom', () => {
protocolCallbackCalled = true;
return Promise.resolve({data: new Image()});
});
Expand All @@ -83,7 +83,7 @@ describe('maplibre', () => {
});

test('#addProtocol - error', () => {
maplibre.addProtocol('custom', () => Promise.reject(new Error('test error')));
addProtocol('custom', () => Promise.reject(new Error('test error')));

getJSON({url: 'custom://test/url/json'}, new AbortController()).catch((error) => {
expect(error).toBeTruthy();
Expand All @@ -92,7 +92,7 @@ describe('maplibre', () => {

test('#addProtocol - Cancel request', async () => {
let cancelCalled = false;
maplibre.addProtocol('custom', (_req, abortController) => {
addProtocol('custom', (_req, abortController) => {
abortController.signal.addEventListener('abort', () => {
cancelCalled = true;
});
Expand All @@ -111,11 +111,11 @@ describe('maplibre', () => {
});

test('version', () => {
expect(typeof maplibre.version === 'string').toBeTruthy();
expect(typeof getVersion() === 'string').toBeTruthy();

// Semver regex: https://gist.github.com/jhorsman/62eeea161a13b80e39f5249281e17c39
// Backslashes are doubled to escape them
const regexp = new RegExp('^([0-9]+)\\.([0-9]+)\\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\\.[0-9A-Za-z-]+)*))?(?:\\+[0-9A-Za-z-]+)?$');
expect(regexp.test(maplibre.version)).toBeTruthy();
expect(regexp.test(getVersion())).toBeTruthy();
});
});
Loading