bbc / VideoContext

An experimental HTML5 & WebGL video composition and rendering API.
http://bbc.github.io/VideoContext/
Apache License 2.0
1.33k stars 157 forks source link

Allow custom source nodes which extend videocontext nodes not to require videocontext #192

Closed Sacharified closed 5 years ago

Sacharified commented 5 years ago

Problem

I have a custom source node (https://github.com/Sacharified/videocontext-gif-source) which extends the canvas node, published as an NPM package. To extend the canvas node, I have to import the whole of VideoContext in my package. An application which consumes this package will also likely include VideoContext. If the version of VideoContext in the consumer application does not match the version in the package, the consumer's bundle will include two different versions of VideoContext, bloating its size unnecessarily.

Solutions

There are several ways this can be addressed:

  1. Do nothing and leave it up to package authors and users to manage their versions
  2. Require package authors to use a factory method to pass the required constructor to the package (a breaking API change)

    Example

    Package:

    function CustomNodeFactory(videocontext) {
    return class Node extends videocontext.NODES.CanvasNode {
    ...
    }
    }

Consumer:

import CustomNodeFactory from "my-custom-node"
import VideoContext from "videocontext"

const vc = new VideoContext(...)
const CustomNode = CustomNodeFactory(VideoContext)
const node = vc.customSourceNode(CustomNode, ...)
  1. Update the customSourceNode API to pass the node constructors from VideoContext to the custom node as shown in #191 (non-breaking)
germain-gg commented 5 years ago

Wouldn't the node.js peer dependency solve that specific issue? https://nodejs.org/en/blog/npm/peer-dependencies/

You would be able to define a new section in your package.json and your plugin will use the video context's version in the host package.

Is there a case that I am not considering here?

Sacharified commented 5 years ago

@gsouquet You're probably right, I didn't consider that! Thanks.