Skip to content

Commit 8bb3517

Browse files
Merge pull request #1441 from opencomponents/preload-option
add preload option
2 parents a8557ff + 7433af3 commit 8bb3517

File tree

6 files changed

+45
-2
lines changed

6 files changed

+45
-2
lines changed

src/cli/domain/ocConfig.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ export interface OpenComponentsConfig {
99
registries?: string[];
1010
/** Development-specific configuration settings */
1111
development?: {
12+
/** JavaScript code to be included in the preview HTML's <head> section.
13+
* Can be either a filepath to a JS script or inline JavaScript code.
14+
*/
15+
preload?: string;
1216
/** Fallback configuration for when components cannot be found locally */
1317
fallback?: {
1418
/** URL of the fallback registry to use when components cannot be found locally */
@@ -41,6 +45,7 @@ type ParsedConfig = {
4145
sourcePath?: string;
4246
registries: string[];
4347
development: {
48+
preload?: string;
4449
plugins: {
4550
dynamic?: Record<string, string>;
4651
static?: Record<string, string>;
@@ -84,6 +89,7 @@ function parseConfig(config: OpenComponentsConfig): ParsedConfig {
8489
...config,
8590
registries: config.registries || [],
8691
development: {
92+
preload: config.development?.preload,
8793
plugins,
8894
fallback: config.development?.fallback
8995
}

src/cli/facade/dev.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ const dev = ({ local, logger }: { logger: Logger; local: Local }) =>
4343

4444
let fallbackRegistryUrl = opts.fallbackRegistryUrl;
4545
let fallbackClient = false;
46+
const localConfig = getOcConfig(componentsDir);
4647
if (!fallbackRegistryUrl) {
4748
try {
48-
const localConfig = getOcConfig(componentsDir);
4949
if (
5050
!fallbackRegistryUrl &&
5151
typeof localConfig.development?.fallback?.url === 'string'
@@ -207,7 +207,8 @@ const dev = ({ local, logger }: { logger: Logger; local: Local }) =>
207207
path: path.resolve(componentsDir),
208208
port,
209209
templates: dependencies.templates,
210-
verbosity: 1
210+
verbosity: 1,
211+
preload: localConfig.development?.preload
211212
});
212213

213214
registerPlugins(registry);

src/registry/domain/options-sanitiser.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,31 @@ export default function optionsSanitiser(input: RegistryOptions): Config {
113113
options.templates = [];
114114
}
115115

116+
// Handle preload script - if it's a filepath, read the file content
117+
if (
118+
options.preload &&
119+
!options.preload.includes(';') &&
120+
!options.preload.includes('{')
121+
) {
122+
try {
123+
const fs = require('node:fs');
124+
const path = require('node:path');
125+
const preloadPath = path.isAbsolute(options.preload)
126+
? options.preload
127+
: path.resolve(process.cwd(), options.preload);
128+
129+
if (fs.existsSync(preloadPath)) {
130+
options.preload = fs.readFileSync(preloadPath, 'utf8');
131+
}
132+
} catch (error) {
133+
// If file reading fails, keep the original value (might be inline JS)
134+
console.warn(
135+
`Warning: Could not read preload file "${options.preload}":`,
136+
(error as Error).message
137+
);
138+
}
139+
}
140+
116141
if (options.compileClient || options.compileClient !== false) {
117142
const clientOptions =
118143
typeof options.compileClient === 'boolean'

src/registry/routes/component-preview.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ function componentPreview(
3737
: undefined,
3838
href: res.conf.baseUrl,
3939
liveReload,
40+
preload: res.conf.preload,
4041
qs: urlBuilder.queryString(req.query as any),
4142
templates
4243
})

src/registry/views/preview.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default function preview(vm: {
77
qs: string;
88
liveReload: string;
99
templates: TemplateInfo[];
10+
preload?: string;
1011
}): string {
1112
const baseUrl = vm.href.replace('http://', '//').replace('https://', '//');
1213
const { name, version } = vm.component;
@@ -18,6 +19,7 @@ export default function preview(vm: {
1819
return `<!DOCTYPE html>
1920
<html>
2021
<head>
22+
${vm.preload ? `<script>${vm.preload}</script>` : ''}
2123
<style>
2224
html, body {
2325
width: 100%;

src/types.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@ export interface Config<T = any> {
242242
* If omitted there is no execution timeout.
243243
*/
244244
executionTimeout?: number;
245+
/**
246+
* JavaScript code to be included in the preview HTML's <head> section.
247+
* Can be either a filepath to a JS script or inline JavaScript code.
248+
*
249+
* @example "path/to/script.js"
250+
* @example "console.log('Hello from preload script');"
251+
*/
252+
preload?: string;
245253
/**
246254
* URL of a secondary registry that will be queried if a component cannot
247255
* be found on this instance. A trailing slash is appended automatically.

0 commit comments

Comments
 (0)