@jay-framework/rollup-plugin 0.12.0 → 0.14.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 +65 -9
  2. package/package.json +8 -8
package/dist/index.js CHANGED
@@ -7,13 +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, parseJayModuleSpecifier, RuntimeMode, TS_EXTENSION, TSX_EXTENSION, JAY_QUERY_WORKER_TRUSTED_TS, CSS_EXTENSION, JAY_QUERY_MAIN_SANDBOX, JAY_QUERY_HYDRATE, 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
13
  import { getLogger } from "@jay-framework/logger";
14
- import { getJayHtmlImports, parseJayFile, JAY_IMPORT_RESOLVER, parseContract, compileContract, generateSandboxRootFile, generateElementBridgeFile } from "@jay-framework/compiler-jay-html";
14
+ import fs from "node:fs";
15
+ import { getJayHtmlImports, parseJayFile, JAY_IMPORT_RESOLVER, parseContract, compileContract, parseAction, compileAction, defaultContractResolver, generateSandboxRootFile, generateElementBridgeFile, generateElementHydrateFile } from "@jay-framework/compiler-jay-html";
15
16
  import { createRequire } from "module";
16
- import fs from "fs";
17
+ import fs$1 from "fs";
17
18
  function getFileContext(filename, extension = JAY_EXTENSION) {
18
19
  const basePath = getBasePath(filename);
19
20
  return {
@@ -32,7 +33,10 @@ async function writeDefinitionFile(dirname, filename, source, extension) {
32
33
  async function writeGeneratedFile(jayContext, context, id, code) {
33
34
  if (!jayContext.outputDir)
34
35
  return;
35
- const relativePath = path.dirname(path.relative(jayContext.projectRoot, id));
36
+ let relativePath = path.dirname(path.relative(jayContext.projectRoot, id));
37
+ if (relativePath.startsWith("build" + path.sep)) {
38
+ relativePath = relativePath.slice(("build" + path.sep).length);
39
+ }
36
40
  const filePath = path.resolve(jayContext.outputDir, relativePath, path.basename(id));
37
41
  await mkdir(path.dirname(filePath), { recursive: true });
38
42
  await writeFile(filePath, code, { encoding: "utf8", flag: "w" });
@@ -44,12 +48,40 @@ function checkCodeErrors(code) {
44
48
  throw new Error("Empty code file");
45
49
  return code;
46
50
  }
51
+ function createContractResolver(baseDir, importResolver) {
52
+ return (contractSubpath) => {
53
+ const { viewStateName } = defaultContractResolver(contractSubpath);
54
+ const subpathWithExt = contractSubpath.endsWith(JAY_CONTRACT_EXTENSION) ? contractSubpath : contractSubpath + JAY_CONTRACT_EXTENSION;
55
+ const candidates = [
56
+ `./${subpathWithExt}`,
57
+ `../contracts/${subpathWithExt}`,
58
+ `./contracts/${subpathWithExt}`,
59
+ `../${subpathWithExt}`
60
+ ];
61
+ for (const candidate of candidates) {
62
+ const absolutePath = importResolver.resolveLink(baseDir, candidate);
63
+ if (fs.existsSync(absolutePath)) {
64
+ const relativePath = path.relative(baseDir, absolutePath);
65
+ const importPath = relativePath.startsWith(".") ? relativePath : "./" + relativePath;
66
+ return { importPath, viewStateName };
67
+ }
68
+ }
69
+ try {
70
+ const absolutePath = importResolver.resolveLink(baseDir, subpathWithExt);
71
+ const relativePath = path.relative(baseDir, absolutePath);
72
+ const importPath = relativePath.startsWith(".") ? relativePath : "./" + relativePath;
73
+ return { importPath, viewStateName };
74
+ } catch {
75
+ return { importPath: "./" + subpathWithExt, viewStateName };
76
+ }
77
+ };
78
+ }
47
79
  function jayDefinitions(projectRoot) {
48
80
  return {
49
81
  name: "jay:definitions",
50
82
  // this name will show up in warnings and errors
51
83
  async load(id) {
52
- if (hasExtension(id, JAY_EXTENSION) || hasExtension(id, JAY_CONTRACT_EXTENSION)) {
84
+ if (hasExtension(id, JAY_EXTENSION) || hasExtension(id, JAY_CONTRACT_EXTENSION) || hasExtension(id, JAY_ACTION_EXTENSION)) {
53
85
  const code = await readFileAsString(id);
54
86
  checkCodeErrors(code);
55
87
  return { code };
@@ -108,6 +140,21 @@ function jayDefinitions(projectRoot) {
108
140
  );
109
141
  context.info(`[transform] generated ${generatedFilename}`);
110
142
  return { code: "", map: null };
143
+ } else if (hasExtension(id, JAY_ACTION_EXTENSION)) {
144
+ const context = this;
145
+ const { filename, dirname } = getFileContext(id, JAY_ACTION_EXTENSION);
146
+ const parsedFile = parseAction(code, filename);
147
+ const contractResolver = createContractResolver(dirname, JAY_IMPORT_RESOLVER);
148
+ const tsCode = compileAction(parsedFile, contractResolver);
149
+ const validatedTsCode = checkValidationErrors(tsCode);
150
+ const generatedFilename = await writeDefinitionFile(
151
+ dirname,
152
+ filename,
153
+ validatedTsCode,
154
+ JAY_ACTION_DTS_EXTENSION
155
+ );
156
+ context.info(`[transform] generated ${generatedFilename}`);
157
+ return { code: "", map: null };
111
158
  }
112
159
  return null;
113
160
  }
@@ -201,14 +248,23 @@ async function generateCodeFromStructure(jayContext, context, code, id, meta, ja
201
248
  const { format } = meta;
202
249
  const mode = getModeFromExtension(id);
203
250
  const generationTarget = jayContext.jayOptions.generationTarget || GenerateTarget.jay;
204
- const tsCode = format === SourceFileFormat.JayHtml ? generateCodeFromJayHtmlFile(mode, jayFile, generationTarget) : generateCodeFromTsFile(jayContext, mode, jayFile, id, code);
251
+ const isHydrate = parseJayModuleSpecifier(id).isHydrate === true;
252
+ const tsCode = format === SourceFileFormat.JayHtml ? generateCodeFromJayHtmlFile(
253
+ mode,
254
+ jayFile,
255
+ generationTarget,
256
+ isHydrate
257
+ ) : generateCodeFromTsFile(jayContext, mode, jayFile, id, code);
205
258
  await writeGeneratedFile(jayContext, context, id, tsCode);
206
259
  return tsCode;
207
260
  }
208
- function generateCodeFromJayHtmlFile(mode, jayFile, generationTarget) {
261
+ function generateCodeFromJayHtmlFile(mode, jayFile, generationTarget, isHydrate = false) {
209
262
  switch (mode) {
210
263
  case RuntimeMode.MainTrusted:
211
264
  case RuntimeMode.MainSandbox:
265
+ if (isHydrate) {
266
+ return checkValidationErrors(generateElementHydrateFile(jayFile, mode));
267
+ }
212
268
  return checkValidationErrors(generateElementFile(jayFile, mode, generationTarget));
213
269
  case RuntimeMode.WorkerSandbox:
214
270
  return generateElementBridgeFile(jayFile);
@@ -458,7 +514,7 @@ function getResolvedId(resolved, mode, originId) {
458
514
  return `${originId}?${mode}.${extension}`;
459
515
  }
460
516
  function hasCssImportedByJayHtml(source, importer) {
461
- return hasExtension(source, CSS_EXTENSION) && importer && (hasExtension(importer, JAY_EXTENSION, { withTs: true }) || hasExtension(importer, JAY_EXTENSION + JAY_QUERY_MAIN_SANDBOX, { withTs: true }));
517
+ return hasExtension(source, CSS_EXTENSION) && importer && (hasExtension(importer, JAY_EXTENSION, { withTs: true }) || hasExtension(importer, JAY_EXTENSION + JAY_QUERY_MAIN_SANDBOX, { withTs: true }) || hasExtension(importer, JAY_EXTENSION + JAY_QUERY_HYDRATE, { withTs: true }));
462
518
  }
463
519
  function resolveCssFileImportedByJayHtml(context, importer, root) {
464
520
  const originImporter = importer.split("?")[0];
@@ -490,7 +546,7 @@ class JayPluginContext {
490
546
  this.tsPrinter = createPrinter({ newLine: NewLineKind.LineFeed });
491
547
  let compilerPatternsParseResult = compileFunctionSplitPatternsBlock(
492
548
  (jayOptions.compilerPatternFiles || []).map((fileName) => {
493
- let fileContent = fs.readFileSync(fileName, { encoding: "utf8" });
549
+ let fileContent = fs$1.readFileSync(fileName, { encoding: "utf8" });
494
550
  return createTsSourceFileFromSource(fileName, fileContent);
495
551
  })
496
552
  );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jay-framework/rollup-plugin",
3
- "version": "0.12.0",
3
+ "version": "0.14.0",
4
4
  "license": "Apache-2.0",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
@@ -27,17 +27,17 @@
27
27
  "test:watch": "vitest"
28
28
  },
29
29
  "dependencies": {
30
- "@jay-framework/compiler": "^0.12.0",
31
- "@jay-framework/compiler-jay-html": "^0.12.0",
32
- "@jay-framework/logger": "^0.12.0",
30
+ "@jay-framework/compiler": "^0.14.0",
31
+ "@jay-framework/compiler-jay-html": "^0.14.0",
32
+ "@jay-framework/logger": "^0.14.0",
33
33
  "fast-glob": "^3.3.2",
34
34
  "typescript": "^5.3.3"
35
35
  },
36
36
  "devDependencies": {
37
- "@jay-framework/component": "^0.12.0",
38
- "@jay-framework/dev-environment": "^0.12.0",
39
- "@jay-framework/runtime": "^0.12.0",
40
- "@jay-framework/secure": "^0.12.0",
37
+ "@jay-framework/component": "^0.14.0",
38
+ "@jay-framework/dev-environment": "^0.14.0",
39
+ "@jay-framework/runtime": "^0.14.0",
40
+ "@jay-framework/secure": "^0.14.0",
41
41
  "@types/node": "^20.11.5",
42
42
  "rimraf": "^5.0.5",
43
43
  "rollup": "^4.9.5",