Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
13 changes: 13 additions & 0 deletions src/cli/domain/ocConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export interface OpenComponentsConfig {
registries?: string[];
/** Development-specific configuration settings */
development?: {
/** Importmap to add to the preview HTML's <head> section. */
importmap?: {
imports?: Record<string, string>;
};
/** Additional Express routes to mount on the registry application */
routes?: Array<{
route: string;
Expand Down Expand Up @@ -48,9 +52,13 @@ export interface OpenComponentsConfig {
}

type ParsedConfig = {
$schema?: string | null;
sourcePath?: string;
registries: string[];
development: {
importmap?: {
imports?: Record<string, string>;
};
routes?: Array<{
route: string;
method: string;
Expand Down Expand Up @@ -100,6 +108,7 @@ function parseConfig(config: OpenComponentsConfig): ParsedConfig {
...config,
registries: config.registries || [],
development: {
importmap: config.development?.importmap,
preload: config.development?.preload,
plugins,
fallback: config.development?.fallback,
Expand Down Expand Up @@ -131,6 +140,10 @@ export function getOcConfig(folder?: string): ParsedConfig {

export function setOcConfig(config: ParsedConfig, path?: string) {
const { sourcePath, ...rest } = config;
if (!rest.$schema) {
rest.$schema = 'https://opencomponents.github.io/schema.json';
}

fs.writeFileSync(
path || sourcePath || settings.configFile.src,
JSON.stringify(rest, null, 2)
Expand Down
2 changes: 2 additions & 0 deletions src/registry/routes/component-preview.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Request, Response } from 'express';
import { fromPromise } from 'universalify';
import { getOcConfig } from '../../cli/domain/ocConfig';
import type { Component, Config, TemplateInfo } from '../../types';
import type { Repository } from '../domain/repository';
import * as urlBuilder from '../domain/url-builder';
Expand Down Expand Up @@ -32,6 +33,7 @@ function componentPreview(
res.send(
previewView({
component,
importmap: getOcConfig(res.conf.path)?.development?.importmap,
fallbackClient: res.conf.fallbackClient
? `${res.conf.fallbackRegistryUrl.replace(/\/$/, '')}/oc-client/client.dev.js`
: undefined,
Expand Down
5 changes: 4 additions & 1 deletion src/registry/views/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ export default function preview(vm: {
qs: string;
liveReload: string;
templates: TemplateInfo[];
importmap?: {
imports?: Record<string, string>;
};
preload?: string;
}): string {
const baseUrl = vm.href.replace('http://', '//').replace('https://', '//');
const { name, version } = vm.component;
const imports = vm.component.oc.files.imports;
const imports = vm.importmap?.imports || vm.component.oc.files.imports;
const componentHref = `${baseUrl}${name}/${version}/${vm.qs}`;
const clientHref = vm.fallbackClient || `${baseUrl}oc-client/client.js`;
const id = `oc-${name}@${version}`;
Expand Down