A framework for developing and distributing html components

OpenComponents is a framework to develop and deploy robust and distributed html components.
The goal is to own a system that allows big corporations (that may involve hundreds of engineers on a number of projects) to have tools to facilitate code sharing, reduce dependencies, and easily approach new features and experiments.
Disclaimer: This project is still under heavy development and the API is likely to change at any time. In case you would find any issues, check the troubleshooting page.
OpenComponents involves two parts:
components are small units of isomorphic code mainly consisting of html, javascript, css. They can optionally contain some logic, allowing a server-side node.js application to compose a model that is used to render the view. After rendering they are pieces of pure html to be injected in any html page.consumers are websites or microsites (small independently deployable web sites all connected by a front-door service or any routing mechanism) that need components for rendering partial contents in their web pages.The framework consists mainly of 3 parts.
cli allows developers to create, develop, test, and publish components.library is where the components are stored after the publishing. When components depend on static resources (such as images, css files, etc.) these are stored, during packaging and publishing, in a publicly-exposed part of the library that serves as cdn.registry is a rest api that is used to consume, retrieve, and publish components. Since they are immutable, the registry is the entity that handles the traffic between the library and the consumers.A component is a directory composed by
| File | Description |
|---|---|
package.json | The component definition, dependencies, and more |
view.html | The view containing the markup. Currently we support handlebars and jade. It can contain some css under the <style> tag and cliend-side javascript under the <script> tag |
server.js (optional) | If the component has some logic, including consuming services, this is the entity that will produce the view-model to compile the view. |
| static contents (optional) | Images, js, and files that will be referenced in the html markup. |
| * | Any other files that will be useful for the development such as tests, docs, etc. |
After publishing, components are immutable and semantic versioned.
Getting started with components
To create a folder containing the component:
npm install oc -goc init hello-world
To start a local test registry using a components' folder as a library with a watcher:
oc dev . 3030
To see how the component looks like when consuming it:
oc preview http://localhost:3030/hello-world 3031
As soon as you make changes on the component, you will be able to refresh this page and see how it looks.
You will need an online registry connected to a library. A component with the same name and version can't be already existing on that registry.
# you have to do the registry config first, just once oc registry add http://my-components-registry.mydomain.com # then, ship it oc publish hello-world/
Now, it should be available at http://my-components-registry.mydomain.com/hello-world.
From a consumer's perspective, a component is an html fragment. You can render components just on the client-side, just on the server-side, or use the client-side rendering just as failover strategy for when the server-side rendering fails because the registry is not responding quickly or is down.
You don't need node.js to consume components on the server-side. The registry can provide you rendered components so that you can consume them using any tech stack.
When published, components are immutable and semantic versioned. The registry allows consumers to get any version of the component: the latest patch, or minor version, etc.
You can get rendered components via the registry rest api.
curl http://my-components-registry.mydomain.com/hello-world
In case you would like to do the rendering yourself, try:
curl http://my-components-registry.mydomain.com/hello-world/~1.0.0 -H Accept:application/vnd.oc.prerendered+json
In this case you get the compiled view + the data, and you can do the rendering, eventually, interpolating the view-model data and rendering the compiled view with it.
First install the node.js client in your project:
npm install oc-client --save
Then, this is what you would do with a simple node.js http app:
var http = require'http' Client = require'oc-client' client = ; clientconfig = registries: 'http://my-components-registry.mydomain.com/' components: 'hello-world': '~1.0.0'; httpcreateServer clientrenderComponent'hello-world' reswriteHead200 'Content-Type': 'text/html'; resend'<html><head></head><body>' + html + '</body></html>'; ;listen4000;
Open http://localhost:4000/ and enjoy!
To make this happen, your components' registry has to be publicly available. This is all you need:
For more information about client-side operations, look at this page.
When the registry is slow or returns errors while doing server-side rendering, you may want to unblock the server-side rendering and postpone it to make it happen on the client-side after the DOM is loaded. If your registry is publicly available and you use the node.js client, this is done automatically. When on the client-side, a retry rendering attempt via javascript will happen every 10 seconds until the component is rendered.
If for some reasons you want to avoid client-side rendering when using the node.js client, you can do:
var http = require'http' oc = require'oc'; var client = disableFailoverRendering: true ; httpcreateServer clientrenderComponent'hello-world' reswriteHead200 'Content-Type': 'text/html'; resend'<html><head></head><body>' + html + '</body></html>'; ;listen4000;
npm install oc -g# to see available commands: oc
At the moment the only supported library is Amazon S3. Create an account and get the api credentials, you will need them while setting up the registry.
The registry is a node.js express app that serves the components. You can have multiple registries connected to a library, but you can't have multiple libraries connected to a registry. First, create a dir and install oc:
mkdir oc-registry && cd oc-registrynpm initnpm install oc --save
Then on the entry point, what you need on an index.js file is:
var oc = require'oc'; var configuration = verbosity: 0 baseUrl: 'https://my-components-registry.mydomain.com/' port: 3000 tempDir: './temp/' refreshInterval: 600 pollingInterval: 5 s3: key: 'your-s3-key' secret: 'your-s3-secret' bucket: 'your-s3-bucket' region: 'your-s3-region' path: '//s3.amazonaws.com/your-s3-bucket/' componentsDir: 'components' env: name: 'production' ; var registry = configuration; registrystart iferr console.log'Registry not started: ' err; processexit1; ;
For the registry configuration's documentation, look at this page.
MIT
If you wish to contribute, check the contributing guidelines.
Maintainer:
Contributors: