Skip to content

Commit

Permalink
Simplify headless context handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tsherif committed Nov 26, 2019
1 parent 71e8131 commit 3f94e68
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 82 deletions.
30 changes: 28 additions & 2 deletions modules/webgl/src/context/create-headless-context.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import {headlessGL} from '../webgl-utils/webgl-types';

const ERR_HEADLESSGL_NOT_AVAILABLE =
'Failed to create WebGL context in Node.js, headless gl not available';

const ERR_HEADLESSGL_FAILED =
'Failed to create WebGL context in Node.js, headless gl returned null';

const ERR_HEADLESSGL_LOAD = `\
luma.gl: loaded under Node.js without headless gl installed, meaning that WebGL \
contexts can not be created. This may not be an error. For example, this is a \
typical configuration for isorender applications running on the server.`;

// Create headless gl context (for running under Node.js)
export function createHeadlessContext(options) {
const {width, height, webgl1, webgl2, onError} = options;
Expand All @@ -21,3 +24,26 @@ export function createHeadlessContext(options) {
}
return gl;
}

// TODO(Tarek): OOGLY HACK to avoid webpack requiring headless
// browser bundles. Will be removed in 8.0 when we
// remove automatic headless context creation
// NOTE: Rollup does not process the line `const m = module;`
// and writes it out verbatim in its final output, which ends
// up falling over in browser environments at runtime. Added
// a `try/catch` block to fix usage in rollup builds.
let m;
try {
m = module;
} catch (e) {
m = null;
}

// Load headless gl dynamically, if available
function headlessGL(...args) {
const headless = m.require('gl');
if (!headless) {
throw new Error(ERR_HEADLESSGL_LOAD);
}
return headless(...args);
}
10 changes: 10 additions & 0 deletions modules/webgl/src/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ if (!global.luma) {
};
}

// Ensures that WebGL2RenderingContext is defined in non-WebGL2 environments
// so that apps can test their gl contexts with instanceof
// E.g. if (gl instanceof WebGL2RenderingContext) { }
class WebGL2RenderingContextNotSupported {}
global.WebGL2RenderingContext = global.WebGL2RenderingContext || WebGL2RenderingContextNotSupported;

// Ensure that Image is defined under Node.js
class ImageNotSupported {}
global.Image = global.Image || ImageNotSupported;

export {global};
export {lumaStats};
export default global.luma;
80 changes: 0 additions & 80 deletions modules/webgl/src/webgl-utils/webgl-types.js

This file was deleted.

0 comments on commit 3f94e68

Please sign in to comment.