### HTML Page for Component Preview
Source: https://opencomponents.github.io/docs/components/getting-started
An example HTML structure to embed and preview an OpenComponent. It includes the custom element `` and the OC client script for rendering.
```html
Optionally, some failover text here
```
--------------------------------
### Preview OpenComponent Directly
Source: https://opencomponents.github.io/docs/components/getting-started
Command to preview a specific OpenComponent directly from the command line, useful for quick checks without setting up a full HTML page.
```bash
$ oc preview http://localhost:3030/hello-world
```
--------------------------------
### Local Development Server Command
Source: https://opencomponents.github.io/docs/components/getting-started
Command to start a local development server for OpenComponents, allowing you to test and debug components in real-time.
```bash
$ oc dev . 3030
```
--------------------------------
### Initialize OpenComponent
Source: https://opencomponents.github.io/docs/components/getting-started
Command to initialize a new OpenComponent project. It creates the basic directory structure and configuration files. You can optionally specify a template type.
```bash
$ oc init hello-world
```
```bash
$ oc init hello-world oc-template-es6
```
--------------------------------
### OpenComponent Directory Structure
Source: https://opencomponents.github.io/docs/components/getting-started
Illustrates the basic file and directory layout for an OpenComponent, including configuration, view, server logic, and static assets.
```tree
├── hello-world/
├── package.json
├── src/view.ts
├── src/server.ts
├── public/
├── logo.png
```
--------------------------------
### Basic OpenComponent Server Logic
Source: https://opencomponents.github.io/docs/components/getting-started
Provides a basic example of the server.js file, which handles the data logic for a component. It exports a data function that receives context and parameters to generate a view model.
```javascript
exportconstdata=(context, callback)=>{
const{ name }= context.params;
const{ staticPath }= context;
callback(null,{
name,
staticPath,
});
};
```
--------------------------------
### Set Up OpenComponents Registry
Source: https://opencomponents.github.io/docs/quick-start-tutorial
Details the steps to set up an OpenComponents registry for production, including creating a directory, initializing npm, installing the `oc` package, and creating a configuration file.
```bash
mkdir my-oc-registry
cd my-oc-registry
npm init -y
npm install oc --save
```
--------------------------------
### Verify OpenComponents CLI Installation
Source: https://opencomponents.github.io/docs/quick-start-tutorial
Checks the installed version of the OpenComponents CLI. This helps confirm that the installation was successful and provides the version number for reference.
```bash
oc --version
```
--------------------------------
### Install OpenComponents CLI
Source: https://opencomponents.github.io/docs/quick-start-tutorial
Installs the OpenComponents Command Line Interface globally using npm. This command is essential for creating and managing OpenComponents projects.
```bash
npm install -g oc
```
--------------------------------
### Install OpenComponents CLI
Source: https://opencomponents.github.io/docs/quick-start-tutorial
This section details the steps required to install the OpenComponents Command Line Interface (CLI). The CLI is essential for generating and managing OpenComponents projects.
```shell
# Step 0: Install the CLI
# (Command to install the CLI would typically go here, e.g., npm install -g opencomponents-cli)
```
--------------------------------
### Install OpenComponents CLI and Initialize a Component
Source: https://opencomponents.github.io/docs/intro
This snippet shows how to install the OpenComponents command-line interface globally using npm and then initialize a new component project. It's the first step to getting started with building components.
```bash
npm install -g oc
oc init my-component
```
--------------------------------
### Start Local Development
Source: https://opencomponents.github.io/docs/quick-start-tutorial
Guidance on how to start a local development server for testing OpenComponents. This includes checking component information and understanding different component URLs.
```shell
# Step 3: Start Local Development
# Commands to start the local server and test components would go here.
```
--------------------------------
### OpenComponent package.json Structure
Source: https://opencomponents.github.io/docs/components/getting-started
Defines the structure of the package.json file for an OpenComponent, including component metadata, OC-specific configurations for files and parameters, and development dependencies.
```json
{
"name":"base-component-es6",
"description":"",
"version":"1.0.0",
"oc":{
"files":{
"data":"server.js",
"static":["img"],
"template":{
"src":"src/view.ts",
"type":"oc-template-es6"
}
},
"parameters":{
"name":{
"default":"Jane Doe",
"description":"Your name",
"example":"Jane Doe",
"mandatory":false,
"type":"string"
}
}
},
"devDependencies":{
"oc-template-handlebars-compiler":"6.0.8"
}
}
```
--------------------------------
### Test Client-Side Rendering with HTML
Source: https://opencomponents.github.io/docs/quick-start-tutorial
Provides an example HTML file to test client-side rendering of an OpenComponent. It includes the `oc-component` tag and the OpenComponents client script.
```html
Testing My Component
My Website
Loading component...
```
--------------------------------
### Set Up OpenComponents Registry
Source: https://opencomponents.github.io/docs/quick-start-tutorial
Instructions for setting up a registry to manage and serve OpenComponents. This involves creating a directory and configuration files for the registry.
```shell
# Step 7: Set Up a Registry (Production)
# Commands to create registry directory and configuration files would go here.
```
--------------------------------
### Create OpenComponents Project Directory
Source: https://opencomponents.github.io/docs/quick-start-tutorial
Creates a new directory for your OpenComponents project and navigates into it. This is the first step in setting up a new component.
```bash
mkdir oc-tutorial && cd oc-tutorial
```
--------------------------------
### Configure OC Client Global Settings
Source: https://opencomponents.github.io/docs/consumers/client-setup
This example shows how to configure global settings for the OC Client by defining the `oc.conf` object before loading the client script. It includes options like enabling debug mode, setting retry intervals, and defining global parameters.
```JavaScript
var oc ={
conf:{
debug:true,
retryInterval:2000,
globalParameters:{userId:"123"},
},
};
```
--------------------------------
### Start Dev Server with Verbose Output
Source: https://opencomponents.github.io/docs/building/debugging
Starts the OpenComponents development server on a specified port with verbose logging enabled for detailed output during development.
```bash
oc dev . 3030 --verbose
```
--------------------------------
### Start Local OpenComponents Development Registry
Source: https://opencomponents.github.io/docs/quick-start-tutorial
Starts a local development registry for OpenComponents on port 3030. This command watches for file changes and automatically recompiles components, facilitating local development and testing.
```bash
cd .. # Go back to oc-tutorial directory
oc dev . 3030
```
--------------------------------
### Generate OpenComponents Component
Source: https://opencomponents.github.io/docs/quick-start-tutorial
Initializes a new OpenComponents component named 'hello-world'. This command scaffolds the basic file structure and configuration for a new component.
```bash
oc init hello-world
```
--------------------------------
### Create OpenComponents Project
Source: https://opencomponents.github.io/docs/quick-start-tutorial
Instructions on how to create a new OpenComponents project using the CLI. This involves generating the initial project structure and understanding the generated files.
```shell
# Step 1: Create Your First Component
# Commands to create a new component would go here, e.g., oc new-component my-component
```
--------------------------------
### Component View Template Example
Source: https://opencomponents.github.io/docs/quick-start-tutorial
An example of a TypeScript view template for an OpenComponents component. It renders a simple HTML structure with dynamic content and an image.
```typescript
export default function(model){
return`
Hello ${model.name}!
`;
}
```
--------------------------------
### Safe OC Client Initialization
Source: https://opencomponents.github.io/docs/consumers/client-setup
This JavaScript pattern demonstrates a safe way to initialize the OC Client, especially in dynamic environments like single-page applications. It uses a command queue (`oc.cmd`) to ensure that functions are executed only after the client is fully ready.
```JavaScript
window.oc=window.oc||{};
oc.cmd= oc.cmd||[];
oc.cmd.push(function(oc){
// This runs after the client is fully ready
console.log("OC version:", oc.clientVersion);
});
```
--------------------------------
### Publish OpenComponents Component
Source: https://opencomponents.github.io/docs/quick-start-tutorial
Details on how to publish your created OpenComponents component to a registry. This includes configuring the registry and verifying the publication.
```shell
# Step 8: Publish Your Component
# Commands for configuring registry and publishing would go here.
```
--------------------------------
### OpenComponents Registry Configuration and Start
Source: https://opencomponents.github.io/docs/registry/registry-configuration
Example JavaScript code for configuring and starting an OpenComponents registry. It defines settings for verbosity, base URL, port, temporary directory, refresh intervals, S3 credentials, and environment, then starts the registry.
```javascript
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 =new oc.Registry(configuration);
registry.start(function(err, app){
if(err){
console.log("Registry not started: ", err);
process.exit(1);
}
});
```
--------------------------------
### OpenComponents Registry with Google Storage (Production)
Source: https://opencomponents.github.io/docs/registry/registry-using-google-storage
Configures and starts an OpenComponents registry using Google Cloud Storage as the storage adapter. This example demonstrates a typical production setup with specified project ID, bucket, and component directory.
```javascript
var oc =require("oc");
var gs =require("oc-gs-storage-adapter");
var configuration ={
verbosity:0,
baseUrl:"https://my-components-registry.mydomain.com/",
port:3000,
tempDir:"./temp/",
refreshInterval:600,
pollingInterval:5,
storage:{
adapter: gs,
options:{
projectId:"myproject-12345",
bucket:"my-components-bucket",
path:"//storage.googleapis.com/my-components-bucket/",
componentsDir:"components",
maxAge:3600,
},
},
env:{name:"production"},
};
const registry =new oc.Registry(configuration);
registry.start(function(err, app){
if(err){
console.log("Registry not started: ", err);
process.exit(1);
}
console.log("Registry started successfully on port", configuration.port);
});
```
--------------------------------
### Setup OpenComponents Registry
Source: https://opencomponents.github.io/docs/registry/registry-configuration
Commands to set up a new OpenComponents registry directory, initialize npm, install the 'oc' package, and create an index.js file.
```bash
mkdir oc-registry && cd oc-registry
npm init
npm install oc --save
touch index.js
```
--------------------------------
### Start Local OpenComponents Development Server
Source: https://opencomponents.github.io/docs/intro
This command starts a local development server for your OpenComponents project, allowing you to preview and test your components. It specifies the current directory as the component library and the port to run on.
```bash
oc dev . 3030
```
--------------------------------
### Include OC Client Script
Source: https://opencomponents.github.io/docs/consumers/client-setup
This snippet shows how to include the OC Client script in your HTML. It uses a protocol-relative URL to ensure compatibility with both HTTP and HTTPS. Best practice is to place this tag just before the closing