@jay-framework/rollup-plugin 0.11.0 → 0.13.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.
Files changed (2) hide show
  1. package/dist/index.js +71 -26
  2. package/package.json +8 -7
package/dist/index.js CHANGED
@@ -7,12 +7,14 @@ var __publicField = (obj, key, value) => {
7
7
  import { generateElementDefinitionFile, parseGenericTypescriptFile, generateImportsFileFromJayFile, generateElementFile, transformComponent, transformComponentBridge, compileFunctionSplitPatternsBlock, createTsSourceFileFromSource, FunctionRepositoryBuilder } from "@jay-framework/compiler";
8
8
  export * from "@jay-framework/compiler";
9
9
  import path from "node:path";
10
- import { getBasePath, JAY_EXTENSION, hasExtension, JAY_CONTRACT_EXTENSION, checkValidationErrors, JAY_DTS_EXTENSION, JAY_CONTRACT_DTS_EXTENSION, SourceFileFormat, getModeFromExtension, GenerateTarget, RuntimeMode, TS_EXTENSION, TSX_EXTENSION, parseJayModuleSpecifier, JAY_QUERY_WORKER_TRUSTED_TS, CSS_EXTENSION, JAY_QUERY_MAIN_SANDBOX, hasJayExtension, hasJayModeExtension, Import } from "@jay-framework/compiler-shared";
10
+ import { getBasePath, JAY_EXTENSION, hasExtension, JAY_CONTRACT_EXTENSION, JAY_ACTION_EXTENSION, checkValidationErrors, JAY_DTS_EXTENSION, JAY_CONTRACT_DTS_EXTENSION, JAY_ACTION_DTS_EXTENSION, SourceFileFormat, getModeFromExtension, GenerateTarget, RuntimeMode, TS_EXTENSION, TSX_EXTENSION, parseJayModuleSpecifier, JAY_QUERY_WORKER_TRUSTED_TS, CSS_EXTENSION, JAY_QUERY_MAIN_SANDBOX, hasJayExtension, hasJayModeExtension, Import } from "@jay-framework/compiler-shared";
11
11
  import { readFile, mkdir } from "node:fs/promises";
12
12
  import { writeFile } from "fs/promises";
13
- import { getJayHtmlImports, parseJayFile, JAY_IMPORT_RESOLVER, parseContract, compileContract, generateSandboxRootFile, generateElementBridgeFile } from "@jay-framework/compiler-jay-html";
13
+ import { getLogger } from "@jay-framework/logger";
14
+ import fs from "node:fs";
15
+ import { getJayHtmlImports, parseJayFile, JAY_IMPORT_RESOLVER, parseContract, compileContract, parseAction, compileAction, defaultContractResolver, generateSandboxRootFile, generateElementBridgeFile } from "@jay-framework/compiler-jay-html";
14
16
  import { createRequire } from "module";
15
- import fs from "fs";
17
+ import fs$1 from "fs";
16
18
  function getFileContext(filename, extension = JAY_EXTENSION) {
17
19
  const basePath = getBasePath(filename);
18
20
  return {
@@ -35,7 +37,7 @@ async function writeGeneratedFile(jayContext, context, id, code) {
35
37
  const filePath = path.resolve(jayContext.outputDir, relativePath, path.basename(id));
36
38
  await mkdir(path.dirname(filePath), { recursive: true });
37
39
  await writeFile(filePath, code, { encoding: "utf8", flag: "w" });
38
- console.info(["[transform] written", filePath].join(" "));
40
+ getLogger().info(["[transform] written", filePath].join(" "));
39
41
  return filePath;
40
42
  }
41
43
  function checkCodeErrors(code) {
@@ -43,12 +45,40 @@ function checkCodeErrors(code) {
43
45
  throw new Error("Empty code file");
44
46
  return code;
45
47
  }
48
+ function createContractResolver(baseDir, importResolver) {
49
+ return (contractSubpath) => {
50
+ const { viewStateName } = defaultContractResolver(contractSubpath);
51
+ const subpathWithExt = contractSubpath.endsWith(JAY_CONTRACT_EXTENSION) ? contractSubpath : contractSubpath + JAY_CONTRACT_EXTENSION;
52
+ const candidates = [
53
+ `./${subpathWithExt}`,
54
+ `../contracts/${subpathWithExt}`,
55
+ `./contracts/${subpathWithExt}`,
56
+ `../${subpathWithExt}`
57
+ ];
58
+ for (const candidate of candidates) {
59
+ const absolutePath = importResolver.resolveLink(baseDir, candidate);
60
+ if (fs.existsSync(absolutePath)) {
61
+ const relativePath = path.relative(baseDir, absolutePath);
62
+ const importPath = relativePath.startsWith(".") ? relativePath : "./" + relativePath;
63
+ return { importPath, viewStateName };
64
+ }
65
+ }
66
+ try {
67
+ const absolutePath = importResolver.resolveLink(baseDir, subpathWithExt);
68
+ const relativePath = path.relative(baseDir, absolutePath);
69
+ const importPath = relativePath.startsWith(".") ? relativePath : "./" + relativePath;
70
+ return { importPath, viewStateName };
71
+ } catch {
72
+ return { importPath: "./" + subpathWithExt, viewStateName };
73
+ }
74
+ };
75
+ }
46
76
  function jayDefinitions(projectRoot) {
47
77
  return {
48
78
  name: "jay:definitions",
49
79
  // this name will show up in warnings and errors
50
80
  async load(id) {
51
- if (hasExtension(id, JAY_EXTENSION) || hasExtension(id, JAY_CONTRACT_EXTENSION)) {
81
+ if (hasExtension(id, JAY_EXTENSION) || hasExtension(id, JAY_CONTRACT_EXTENSION) || hasExtension(id, JAY_ACTION_EXTENSION)) {
52
82
  const code = await readFileAsString(id);
53
83
  checkCodeErrors(code);
54
84
  return { code };
@@ -107,6 +137,21 @@ function jayDefinitions(projectRoot) {
107
137
  );
108
138
  context.info(`[transform] generated ${generatedFilename}`);
109
139
  return { code: "", map: null };
140
+ } else if (hasExtension(id, JAY_ACTION_EXTENSION)) {
141
+ const context = this;
142
+ const { filename, dirname } = getFileContext(id, JAY_ACTION_EXTENSION);
143
+ const parsedFile = parseAction(code, filename);
144
+ const contractResolver = createContractResolver(dirname, JAY_IMPORT_RESOLVER);
145
+ const tsCode = compileAction(parsedFile, contractResolver);
146
+ const validatedTsCode = checkValidationErrors(tsCode);
147
+ const generatedFilename = await writeDefinitionFile(
148
+ dirname,
149
+ filename,
150
+ validatedTsCode,
151
+ JAY_ACTION_DTS_EXTENSION
152
+ );
153
+ context.info(`[transform] generated ${generatedFilename}`);
154
+ return { code: "", map: null };
110
155
  }
111
156
  return null;
112
157
  }
@@ -269,35 +314,35 @@ async function transformJayFile(jayContext, context, code, id) {
269
314
  if (!Boolean(getJayMetadata(context, id).originId))
270
315
  return null;
271
316
  const mode = getModeFromExtension(id);
272
- console.info(`[transform] start ${mode} ${id}`);
317
+ getLogger().info(`[transform] start ${mode} ${id}`);
273
318
  const { meta, jayFile } = await getJayFileStructure(jayContext, context, code, id);
274
319
  const tsCode = await generateCodeFromStructure(jayContext, context, code, id, meta, jayFile);
275
- console.info(`[transform] end ${mode} ${id}`);
320
+ getLogger().info(`[transform] end ${mode} ${id}`);
276
321
  return { code: tsCode };
277
322
  }
278
323
  function watchChangesFor(context, sourcePath) {
279
324
  if (context.getWatchFiles().includes(sourcePath))
280
325
  return;
281
326
  context.addWatchFile(sourcePath);
282
- console.info(`[watch] add ${sourcePath}`);
327
+ getLogger().info(`[watch] add ${sourcePath}`);
283
328
  }
284
329
  function stripTSExtension(id) {
285
330
  const basePath = getBasePath(id);
286
331
  return basePath.replace(TS_EXTENSION, "").replace(TSX_EXTENSION, "");
287
332
  }
288
333
  async function loadJayFile(context, id) {
289
- console.info(`[load] start ${id}`);
334
+ getLogger().info(`[load] start ${id}`);
290
335
  const metadata = getJayMetadata(context, id);
291
336
  let originId = metadata.originId;
292
337
  if (!Boolean(originId)) {
293
338
  originId = stripTSExtension(id);
294
339
  }
295
340
  const code = checkCodeErrors(await readFileAsString(originId));
296
- console.info(`[load] end ${id}, code length: ${code.length}`);
341
+ getLogger().info(`[load] end ${id}, code length: ${code.length}`);
297
342
  return { code };
298
343
  }
299
344
  async function loadContractFile(context, id) {
300
- console.info(`[load] start ${id}`);
345
+ getLogger().info(`[load] start ${id}`);
301
346
  let { originId } = getJayMetadata(context, id);
302
347
  if (!Boolean(originId))
303
348
  originId = stripTSExtension(id);
@@ -308,12 +353,12 @@ async function loadContractFile(context, id) {
308
353
  if (!tsCode.val) {
309
354
  throw new Error(`Failed to compile contract ${id}: ${JSON.stringify(tsCode.validations)}`);
310
355
  }
311
- console.info(`[load] end ${id}`);
356
+ getLogger().info(`[load] end ${id}`);
312
357
  return { code: tsCode.val };
313
358
  }
314
359
  async function loadCssFile(context, jayContext, id, isVite) {
315
360
  if (isVite) {
316
- console.info(`[load] start ${id}`);
361
+ getLogger().info(`[load] start ${id}`);
317
362
  const { originId } = getJayMetadata(context, id);
318
363
  const code = checkCodeErrors(await readFileAsString(originId));
319
364
  const fileName = path.basename(originId);
@@ -333,10 +378,10 @@ async function loadCssFile(context, jayContext, id, isVite) {
333
378
  watchChangesFor(context, cssFile);
334
379
  }
335
380
  }
336
- console.info(`[load] end ${id}`);
381
+ getLogger().info(`[load] end ${id}`);
337
382
  return { code: jayHtml.val?.css };
338
383
  } else {
339
- console.info(`[load] rollup environment - css not supported - ignoring css ${id}`);
384
+ getLogger().info(`[load] rollup environment - css not supported - ignoring css ${id}`);
340
385
  return { code: "" };
341
386
  }
342
387
  }
@@ -346,7 +391,7 @@ async function resolveJayHtml(context, source, importer, options, root, generati
346
391
  const sourceBasePath = sourceParsed.basePath;
347
392
  if (source.endsWith(TS_EXTENSION) || source.endsWith(TSX_EXTENSION)) {
348
393
  const originId2 = sourceBasePath.replace(TS_EXTENSION, "").replace(TSX_EXTENSION, "");
349
- console.info(`[resolveId] already resolved ${source}, originId: ${originId2}`);
394
+ getLogger().info(`[resolveId] already resolved ${source}, originId: ${originId2}`);
350
395
  return {
351
396
  id: source,
352
397
  meta: appendJayMetadata(context, source, {
@@ -376,7 +421,7 @@ async function resolveJayHtml(context, source, importer, options, root, generati
376
421
  }
377
422
  const baseWithQuery = sourceParsed.fullQueryString ? `${originId}${sourceParsed.fullQueryString}` : originId;
378
423
  const id = context["ssr"] && originId.startsWith(root) ? `${baseWithQuery}${extension}`.slice(root.length) : `${baseWithQuery}${extension}`;
379
- console.info(`[resolveId] resolved ${id} as ${format}`);
424
+ getLogger().info(`[resolveId] resolved ${id} as ${format}`);
380
425
  return { id, meta: appendJayMetadata(context, id, { format, originId }) };
381
426
  }
382
427
  async function resolveJayContract(context, source, importer, options, root) {
@@ -384,7 +429,7 @@ async function resolveJayContract(context, source, importer, options, root) {
384
429
  const sourceBasePath = sourceParsed.basePath;
385
430
  if (source.endsWith(TS_EXTENSION) || source.endsWith(TSX_EXTENSION)) {
386
431
  const originId2 = sourceBasePath.replace(TS_EXTENSION, "").replace(TSX_EXTENSION, "");
387
- console.info(`[resolveId] already resolved contract ${source}, originId: ${originId2}`);
432
+ getLogger().info(`[resolveId] already resolved contract ${source}, originId: ${originId2}`);
388
433
  return {
389
434
  id: source,
390
435
  meta: appendJayMetadata(context, source, {
@@ -403,7 +448,7 @@ async function resolveJayContract(context, source, importer, options, root) {
403
448
  const originId = resolvedParsed.basePath;
404
449
  const baseWithQuery = sourceParsed.fullQueryString ? `${originId}${sourceParsed.fullQueryString}` : originId;
405
450
  const id = context["ssr"] && originId.startsWith(root) ? `${baseWithQuery}${TS_EXTENSION}`.slice(root.length) : `${baseWithQuery}${TS_EXTENSION}`;
406
- console.info(
451
+ getLogger().info(
407
452
  `[resolveId] contract - id: ${id}, originId: ${originId}, ssr: ${context["ssr"]}`
408
453
  );
409
454
  return {
@@ -429,7 +474,7 @@ async function resolveJayModeFile(context, source, importer, options) {
429
474
  const format = resolvedJayMeta.format || SourceFileFormat.TypeScript;
430
475
  const originId = resolvedJayMeta.originId || resolved.id;
431
476
  const id = getResolvedId(resolved, mode, originId);
432
- console.info(`[resolveId] resolved ${id} as ${format}`);
477
+ getLogger().info(`[resolveId] resolved ${id} as ${format}`);
433
478
  return { id, meta: appendJayMetadata(context, id, { format, originId }, resolvedJayMeta) };
434
479
  }
435
480
  async function removeSandboxPrefixForWorkerRoot(context, source, importer, options) {
@@ -442,7 +487,7 @@ async function removeSandboxPrefixForWorkerRoot(context, source, importer, optio
442
487
  return null;
443
488
  const id = `${resolved.id}${JAY_QUERY_WORKER_TRUSTED_TS}`;
444
489
  const originId = id.split("?")[0];
445
- console.info(`[resolveId] resolved sandbox root ${id}`);
490
+ getLogger().info(`[resolveId] resolved sandbox root ${id}`);
446
491
  return {
447
492
  id,
448
493
  meta: appendJayMetadata(context, id, {
@@ -489,7 +534,7 @@ class JayPluginContext {
489
534
  this.tsPrinter = createPrinter({ newLine: NewLineKind.LineFeed });
490
535
  let compilerPatternsParseResult = compileFunctionSplitPatternsBlock(
491
536
  (jayOptions.compilerPatternFiles || []).map((fileName) => {
492
- let fileContent = fs.readFileSync(fileName, { encoding: "utf8" });
537
+ let fileContent = fs$1.readFileSync(fileName, { encoding: "utf8" });
493
538
  return createTsSourceFileFromSource(fileName, fileContent);
494
539
  })
495
540
  );
@@ -501,14 +546,14 @@ class JayPluginContext {
501
546
  this.globalFunctionsRepository = new FunctionRepositoryBuilder();
502
547
  }
503
548
  cacheJayFile(id, jayFile) {
504
- console.info("[cache] set", id);
549
+ getLogger().info(`[cache] set ${id}`);
505
550
  this.jayFileCache.set(id, jayFile);
506
551
  return jayFile;
507
552
  }
508
553
  getCachedJayFile(id) {
509
554
  const jayFile = this.jayFileCache.get(id);
510
555
  if (Boolean(jayFile)) {
511
- console.info("[cache] hit", id);
556
+ getLogger().info(`[cache] hit ${id}`);
512
557
  }
513
558
  return jayFile;
514
559
  }
@@ -534,7 +579,7 @@ function jayRuntime(jayOptions = {}, givenJayContext) {
534
579
  (plugin) => plugin.name === "vite:build-metadata" || plugin.name?.startsWith("vite:")
535
580
  )
536
581
  );
537
- console.log("[buildStart] Vite detected:", isVite);
582
+ getLogger().info("[buildStart] Vite detected: " + isVite);
538
583
  },
539
584
  configureServer(_server) {
540
585
  server = _server;
@@ -582,7 +627,7 @@ function jayRuntime(jayOptions = {}, givenJayContext) {
582
627
  return null;
583
628
  },
584
629
  watchChange(id, change) {
585
- console.log(`[watchChange] ${id} ${change.event}`);
630
+ getLogger().info(`[watchChange] ${id} ${change.event}`);
586
631
  jayContext.deleteCachedJayFile(id);
587
632
  if (server) {
588
633
  const module = server.moduleGraph.getModuleById(id + TS_EXTENSION);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jay-framework/rollup-plugin",
3
- "version": "0.11.0",
3
+ "version": "0.13.0",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -27,16 +27,17 @@
27
27
  "test:watch": "vitest"
28
28
  },
29
29
  "dependencies": {
30
- "@jay-framework/compiler": "^0.11.0",
31
- "@jay-framework/compiler-jay-html": "^0.11.0",
30
+ "@jay-framework/compiler": "^0.13.0",
31
+ "@jay-framework/compiler-jay-html": "^0.13.0",
32
+ "@jay-framework/logger": "^0.13.0",
32
33
  "fast-glob": "^3.3.2",
33
34
  "typescript": "^5.3.3"
34
35
  },
35
36
  "devDependencies": {
36
- "@jay-framework/component": "^0.11.0",
37
- "@jay-framework/dev-environment": "^0.11.0",
38
- "@jay-framework/runtime": "^0.11.0",
39
- "@jay-framework/secure": "^0.11.0",
37
+ "@jay-framework/component": "^0.13.0",
38
+ "@jay-framework/dev-environment": "^0.13.0",
39
+ "@jay-framework/runtime": "^0.13.0",
40
+ "@jay-framework/secure": "^0.13.0",
40
41
  "@types/node": "^20.11.5",
41
42
  "rimraf": "^5.0.5",
42
43
  "rollup": "^4.9.5",