oc-server 2.1.2 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Server.d.ts CHANGED
@@ -43,6 +43,15 @@ interface ServerOptions<S extends boolean> {
43
43
  * @default false
44
44
  */
45
45
  stream?: S;
46
+ development?: {
47
+ /**
48
+ * If true, the server will log the browser console.
49
+ * Useful for AI agents to debug on the terminal.
50
+ *
51
+ * @default false
52
+ */
53
+ console?: boolean;
54
+ };
46
55
  }
47
56
  export declare class Server<E = {
48
57
  name: string;
package/dist/Server.js CHANGED
@@ -1,6 +1,14 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ServerError = exports.getSettings = exports.getInitialData = exports.Server = void 0;
4
+ // Simple ANSI color utility function
5
+ const styleText = (color, text) => {
6
+ const colors = {
7
+ gray: '\x1b[90m',
8
+ reset: '\x1b[0m',
9
+ };
10
+ return `${colors[color] || ''}${text}${colors.reset}`;
11
+ };
4
12
  class Server {
5
13
  _options;
6
14
  _parameters = {};
@@ -38,6 +46,11 @@ class HandledServer {
38
46
  this._middleware = _middleware;
39
47
  this._parameters = _parameters;
40
48
  this._options = _options;
49
+ if (this._options.development?.console) {
50
+ this.actions['$$__oc__server___console__$$'] = (params) => {
51
+ console[params.level]?.(`${styleText('gray', '[browser]')} ${params.message}`);
52
+ };
53
+ }
41
54
  }
42
55
  action(name, action) {
43
56
  this.actions[name] = action;
@@ -67,7 +80,7 @@ class HandledServer {
67
80
  if (res && context.streamSymbol) {
68
81
  res[context.streamSymbol] = stream;
69
82
  }
70
- cb(null, res);
83
+ cb(null, res, { console: !!this._options.development?.console });
71
84
  };
72
85
  }
73
86
  }
@@ -45,7 +45,7 @@ function getDataProvider() {
45
45
  parametersSchema: backend.server?._parameters,
46
46
  };
47
47
  }
48
- function getBaseTemplate(appBlock) {
48
+ function getBaseTemplate(appBlock, imports = {}) {
49
49
  const baseTemplate = `
50
50
  <!DOCTYPE html>
51
51
  <html>
@@ -60,6 +60,9 @@ function getBaseTemplate(appBlock) {
60
60
  margin: 0;
61
61
  }
62
62
  </style>
63
+ ${imports
64
+ ? `<script type="importmap">{"imports": ${JSON.stringify(imports)}}</script>`
65
+ : ''}
63
66
  </head>
64
67
  <body>
65
68
  <script src="/oc-client/client.js"></script>
@@ -69,7 +72,7 @@ function getBaseTemplate(appBlock) {
69
72
  `;
70
73
  return baseTemplate;
71
74
  }
72
- async function getHtmlTemplate(appBlock, filePath = 'index.html') {
75
+ async function getHtmlTemplate(appBlock, imports = {}, filePath = 'index.html') {
73
76
  let template;
74
77
  try {
75
78
  template = node_fs_1.default.readFileSync(node_path_1.default.resolve(process.cwd(), filePath), 'utf-8');
@@ -90,7 +93,7 @@ async function getHtmlTemplate(appBlock, filePath = 'index.html') {
90
93
  })}</body>`);
91
94
  }
92
95
  catch (error) {
93
- template = getBaseTemplate(appBlock);
96
+ template = getBaseTemplate(appBlock, imports);
94
97
  }
95
98
  return template;
96
99
  }
@@ -104,6 +107,11 @@ async function createServer({ template, port, }) {
104
107
  plugin.register.register(null, null, () => { });
105
108
  }
106
109
  const defaultParams = (0, oc_1.getDefaultParams)(pk.oc?.parameters ?? parametersSchema);
110
+ const baseConfig = await (0, vite_1.loadConfigFromFile)({
111
+ command: 'build',
112
+ mode: 'development',
113
+ }, undefined, process.cwd()).catch(() => null);
114
+ let imports = baseConfig?.config?.resolve?.alias;
107
115
  const vite = await (0, vite_1.createServer)({
108
116
  server: { middlewareMode: true },
109
117
  plugins: [(0, cssModulesPlugin_1.default)(), template.plugin?.()].filter(Boolean),
@@ -151,7 +159,9 @@ async function createServer({ template, port, }) {
151
159
  try {
152
160
  const params = defaultParams;
153
161
  const { context, responseHeaders } = (0, oc_1.getContext)(plugins, request, params);
154
- let htmlTemplate = await getHtmlTemplate(template.appBlock);
162
+ let htmlTemplate = await getHtmlTemplate(template.appBlock, typeof imports === 'object' && !!imports
163
+ ? imports
164
+ : {});
155
165
  const data = await dataProvider(context);
156
166
  htmlTemplate = htmlTemplate.replace('__INITIAL_DATA__', JSON.stringify(data));
157
167
  htmlTemplate = await vite.transformIndexHtml(url, htmlTemplate);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "oc-server",
3
- "version": "2.1.2",
3
+ "version": "3.0.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",