@jay-framework/rollup-plugin 0.5.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/index.d.ts +54 -0
- package/dist/index.js +487 -0
- package/package.json +48 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { CompiledPattern, FunctionRepositoryBuilder } from '@jay-framework/compiler';
|
|
2
|
+
export * from '@jay-framework/compiler';
|
|
3
|
+
import { LoadResult, TransformResult, CustomPluginOptions, ResolveIdResult } from 'rollup';
|
|
4
|
+
import * as ts from 'typescript';
|
|
5
|
+
import { CompilerOptions } from 'typescript';
|
|
6
|
+
import { GenerateTarget, CompilerSourceFile } from '@jay-framework/compiler-shared';
|
|
7
|
+
|
|
8
|
+
declare function jayDefinitions(): {
|
|
9
|
+
name: string;
|
|
10
|
+
load(id: string): Promise<LoadResult>;
|
|
11
|
+
transform(code: string, id: string): Promise<TransformResult>;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
interface ResolveIdOptions {
|
|
15
|
+
attributes: Record<string, string>;
|
|
16
|
+
custom?: CustomPluginOptions;
|
|
17
|
+
isEntry: boolean;
|
|
18
|
+
skipSelf?: boolean;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface JayRollupConfig {
|
|
22
|
+
tsConfigFilePath?: string;
|
|
23
|
+
tsCompilerOptionsOverrides?: CompilerOptions;
|
|
24
|
+
outputDir?: string;
|
|
25
|
+
isWorker?: boolean;
|
|
26
|
+
compilerPatternFiles?: string[];
|
|
27
|
+
generationTarget?: GenerateTarget;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
declare class JayPluginContext {
|
|
31
|
+
readonly jayOptions: JayRollupConfig;
|
|
32
|
+
readonly projectRoot: string;
|
|
33
|
+
readonly outputDir: string;
|
|
34
|
+
readonly tsPrinter: ts.Printer;
|
|
35
|
+
readonly compilerPatterns: CompiledPattern[];
|
|
36
|
+
readonly jayFileCache: Map<string, CompilerSourceFile>;
|
|
37
|
+
readonly globalFunctionsRepository: FunctionRepositoryBuilder;
|
|
38
|
+
constructor(jayOptions?: JayRollupConfig);
|
|
39
|
+
cacheJayFile(id: string, jayFile: CompilerSourceFile): CompilerSourceFile;
|
|
40
|
+
getCachedJayFile(id: string): CompilerSourceFile;
|
|
41
|
+
deleteCachedJayFile(id: string): boolean;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
declare function jayRuntime(jayOptions?: JayRollupConfig, givenJayContext?: JayPluginContext): {
|
|
45
|
+
name: string;
|
|
46
|
+
resolveId(source: string, importer: string | undefined, options: ResolveIdOptions): Promise<ResolveIdResult>;
|
|
47
|
+
load(id: string): Promise<LoadResult>;
|
|
48
|
+
transform(code: string, id: string): Promise<TransformResult>;
|
|
49
|
+
watchChange(id: string, change: {
|
|
50
|
+
event: 'create' | 'update' | 'delete';
|
|
51
|
+
}): void;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export { JayPluginContext, type JayRollupConfig, jayDefinitions, jayRuntime };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
4
|
+
var __publicField = (obj, key, value) => {
|
|
5
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
6
|
+
return value;
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
9
|
+
const compiler = require("@jay-framework/compiler");
|
|
10
|
+
const path = require("node:path");
|
|
11
|
+
const compilerShared = require("@jay-framework/compiler-shared");
|
|
12
|
+
const promises = require("node:fs/promises");
|
|
13
|
+
const promises$1 = require("fs/promises");
|
|
14
|
+
const compilerJayHtml = require("@jay-framework/compiler-jay-html");
|
|
15
|
+
const ts = require("typescript");
|
|
16
|
+
const fs = require("fs");
|
|
17
|
+
function _interopNamespaceDefault(e) {
|
|
18
|
+
const n = Object.create(null, { [Symbol.toStringTag]: { value: "Module" } });
|
|
19
|
+
if (e) {
|
|
20
|
+
for (const k in e) {
|
|
21
|
+
if (k !== "default") {
|
|
22
|
+
const d = Object.getOwnPropertyDescriptor(e, k);
|
|
23
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
get: () => e[k]
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
n.default = e;
|
|
31
|
+
return Object.freeze(n);
|
|
32
|
+
}
|
|
33
|
+
const ts__namespace = /* @__PURE__ */ _interopNamespaceDefault(ts);
|
|
34
|
+
function getFileContext(filename, extension = compilerShared.JAY_EXTENSION) {
|
|
35
|
+
return {
|
|
36
|
+
filename: path.basename(filename).replace(extension, ""),
|
|
37
|
+
dirname: path.dirname(filename)
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
async function readFileAsString(filePath) {
|
|
41
|
+
return (await promises.readFile(filePath)).toString();
|
|
42
|
+
}
|
|
43
|
+
async function writeDefinitionFile(dirname, filename, source, extension) {
|
|
44
|
+
const name = path.resolve(dirname, `${filename}${extension}`);
|
|
45
|
+
await promises$1.writeFile(name, source, { encoding: "utf8", flag: "w" });
|
|
46
|
+
return name;
|
|
47
|
+
}
|
|
48
|
+
async function writeGeneratedFile(jayContext, context, id, code) {
|
|
49
|
+
if (!jayContext.outputDir)
|
|
50
|
+
return;
|
|
51
|
+
const relativePath = path.dirname(path.relative(jayContext.projectRoot, id));
|
|
52
|
+
const filePath = path.resolve(jayContext.outputDir, relativePath, path.basename(id));
|
|
53
|
+
await promises.mkdir(path.dirname(filePath), { recursive: true });
|
|
54
|
+
await promises$1.writeFile(filePath, code, { encoding: "utf8", flag: "w" });
|
|
55
|
+
console.info(["[transform] written", filePath].join(" "));
|
|
56
|
+
return filePath;
|
|
57
|
+
}
|
|
58
|
+
function checkCodeErrors(code) {
|
|
59
|
+
if (code.length === 0)
|
|
60
|
+
throw new Error("Empty code file");
|
|
61
|
+
return code;
|
|
62
|
+
}
|
|
63
|
+
function jayDefinitions() {
|
|
64
|
+
return {
|
|
65
|
+
name: "jay:definitions",
|
|
66
|
+
// this name will show up in warnings and errors
|
|
67
|
+
async load(id) {
|
|
68
|
+
if (compilerShared.hasExtension(id, compilerShared.JAY_EXTENSION) || compilerShared.hasExtension(id, compilerShared.JAY_CONTRACT_EXTENSION)) {
|
|
69
|
+
const code = await readFileAsString(id);
|
|
70
|
+
checkCodeErrors(code);
|
|
71
|
+
return { code };
|
|
72
|
+
}
|
|
73
|
+
return null;
|
|
74
|
+
},
|
|
75
|
+
async transform(code, id) {
|
|
76
|
+
if (compilerShared.hasExtension(id, compilerShared.JAY_EXTENSION)) {
|
|
77
|
+
const context = this;
|
|
78
|
+
const { filename, dirname } = getFileContext(id);
|
|
79
|
+
const imports = compilerJayHtml.getJayHtmlImports(code).filter(
|
|
80
|
+
(module2) => module2.endsWith("jay-html.d")
|
|
81
|
+
);
|
|
82
|
+
await Promise.all(
|
|
83
|
+
imports.map(
|
|
84
|
+
(imported) => context.load({
|
|
85
|
+
id: path.resolve(dirname, imported.slice(0, -2)),
|
|
86
|
+
resolveDependencies: true
|
|
87
|
+
})
|
|
88
|
+
)
|
|
89
|
+
);
|
|
90
|
+
const parsedFile = await compilerJayHtml.parseJayFile(
|
|
91
|
+
code,
|
|
92
|
+
filename,
|
|
93
|
+
dirname,
|
|
94
|
+
{},
|
|
95
|
+
compilerJayHtml.JAY_IMPORT_RESOLVER
|
|
96
|
+
);
|
|
97
|
+
const tsCode = compilerShared.checkValidationErrors(compiler.generateElementDefinitionFile(parsedFile));
|
|
98
|
+
const generatedFilename = await writeDefinitionFile(
|
|
99
|
+
dirname,
|
|
100
|
+
filename,
|
|
101
|
+
tsCode,
|
|
102
|
+
compilerShared.JAY_DTS_EXTENSION
|
|
103
|
+
);
|
|
104
|
+
context.info(`[transform] generated ${generatedFilename}`);
|
|
105
|
+
return { code: "", map: null };
|
|
106
|
+
} else if (compilerShared.hasExtension(id, compilerShared.JAY_CONTRACT_EXTENSION)) {
|
|
107
|
+
const context = this;
|
|
108
|
+
const { filename, dirname } = getFileContext(id, compilerShared.JAY_CONTRACT_EXTENSION);
|
|
109
|
+
const parsedFile = compilerJayHtml.parseContract(code, filename);
|
|
110
|
+
const tsCode = await compilerJayHtml.compileContract(
|
|
111
|
+
parsedFile,
|
|
112
|
+
`${dirname}/${filename}`,
|
|
113
|
+
compilerJayHtml.JAY_IMPORT_RESOLVER
|
|
114
|
+
);
|
|
115
|
+
const generatedFilename = await writeDefinitionFile(
|
|
116
|
+
dirname,
|
|
117
|
+
filename,
|
|
118
|
+
tsCode.val,
|
|
119
|
+
compilerShared.JAY_CONTRACT_DTS_EXTENSION
|
|
120
|
+
);
|
|
121
|
+
context.info(`[transform] generated ${generatedFilename}`);
|
|
122
|
+
return { code: "", map: null };
|
|
123
|
+
}
|
|
124
|
+
return null;
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
const SANDBOX_ROOT_PREFIX = "jay-sandbox:";
|
|
129
|
+
function getJayMetadata(context, id, { checkPresent = false } = {}) {
|
|
130
|
+
return jayMetadataFromModuleMetadata(id, context.getModuleInfo(id)?.meta, { checkPresent });
|
|
131
|
+
}
|
|
132
|
+
function jayMetadataFromModuleMetadata(id, meta, { checkPresent = false } = {}) {
|
|
133
|
+
const metadata = meta?.jay ?? {};
|
|
134
|
+
if (checkPresent) {
|
|
135
|
+
if (!metadata.originId)
|
|
136
|
+
throw new Error(`Unknown Jay originId for ${id}`);
|
|
137
|
+
if (!metadata.format)
|
|
138
|
+
throw new Error(`Unknown Jay format for ${id}`);
|
|
139
|
+
}
|
|
140
|
+
return metadata;
|
|
141
|
+
}
|
|
142
|
+
function appendJayMetadata(context, id, metadata, moduleMetadata) {
|
|
143
|
+
return { jay: { ...moduleMetadata ?? getJayMetadata(context, id), ...metadata } };
|
|
144
|
+
}
|
|
145
|
+
function getSourceJayMetadata(context, id) {
|
|
146
|
+
const metadata = getJayMetadata(context, id, { checkPresent: true });
|
|
147
|
+
const sourcePath = metadata.originId;
|
|
148
|
+
if (!sourcePath)
|
|
149
|
+
throw new Error(`Unknown Jay originId for ${id}`);
|
|
150
|
+
const sourceMetadata = getJayMetadata(context, sourcePath);
|
|
151
|
+
return sourceMetadata.originId ? sourceMetadata : metadata;
|
|
152
|
+
}
|
|
153
|
+
async function getJayFileStructure(jayContext, context, code, id) {
|
|
154
|
+
const meta = getSourceJayMetadata(context, id);
|
|
155
|
+
const sourceJayFile = jayContext.getCachedJayFile(meta.originId);
|
|
156
|
+
if (Boolean(sourceJayFile))
|
|
157
|
+
return { meta, jayFile: sourceJayFile };
|
|
158
|
+
const jayFile = compilerShared.checkValidationErrors(await getJayFile(jayContext, meta, code));
|
|
159
|
+
jayContext.cacheJayFile(meta.originId, jayFile);
|
|
160
|
+
return { meta, jayFile };
|
|
161
|
+
}
|
|
162
|
+
async function getJayFile(jayContext, meta, code) {
|
|
163
|
+
const { originId: id, format } = meta;
|
|
164
|
+
switch (format) {
|
|
165
|
+
case compilerShared.SourceFileFormat.JayHtml:
|
|
166
|
+
return await getJayStructureFromJayHtmlSource(jayContext, code, id);
|
|
167
|
+
case compilerShared.SourceFileFormat.TypeScript:
|
|
168
|
+
return await getJayStructureFromTypeScriptSource(code, id);
|
|
169
|
+
default:
|
|
170
|
+
throw new Error(`Unknown Jay format ${format}`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
async function getJayStructureFromJayHtmlSource(jayContext, code, id) {
|
|
174
|
+
const { filename, dirname } = getFileContext(id);
|
|
175
|
+
return await compilerJayHtml.parseJayFile(
|
|
176
|
+
code,
|
|
177
|
+
filename,
|
|
178
|
+
dirname,
|
|
179
|
+
{
|
|
180
|
+
relativePath: jayContext.jayOptions.tsConfigFilePath
|
|
181
|
+
},
|
|
182
|
+
compilerJayHtml.JAY_IMPORT_RESOLVER
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
async function getJayStructureFromTypeScriptSource(code, id) {
|
|
186
|
+
return await compiler.parseGenericTypescriptFile(id, code);
|
|
187
|
+
}
|
|
188
|
+
function checkDiagnosticsErrors(tsCode) {
|
|
189
|
+
if (tsCode.diagnostics.length > 0) {
|
|
190
|
+
throw new Error(
|
|
191
|
+
`typescript transpilation failed ${tsCode.diagnostics.map((diagnostic) => diagnostic.toString()).join("\n")}`
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
async function generateCodeFromStructure(jayContext, context, code, id, meta, jayFile) {
|
|
196
|
+
const { format } = meta;
|
|
197
|
+
const mode = compilerShared.getModeFromExtension(id);
|
|
198
|
+
const generationTarget = jayContext.jayOptions.generationTarget || compilerShared.GenerateTarget.jay;
|
|
199
|
+
const tsCode = format === compilerShared.SourceFileFormat.JayHtml ? generateCodeFromJayHtmlFile(mode, jayFile, generationTarget) : generateCodeFromTsFile(jayContext, mode, jayFile, id, code);
|
|
200
|
+
await writeGeneratedFile(jayContext, context, id, tsCode);
|
|
201
|
+
return tsCode;
|
|
202
|
+
}
|
|
203
|
+
function generateCodeFromJayHtmlFile(mode, jayFile, generationTarget) {
|
|
204
|
+
switch (mode) {
|
|
205
|
+
case compilerShared.RuntimeMode.MainTrusted:
|
|
206
|
+
case compilerShared.RuntimeMode.MainSandbox:
|
|
207
|
+
return compilerShared.checkValidationErrors(compiler.generateElementFile(jayFile, mode, generationTarget));
|
|
208
|
+
case compilerShared.RuntimeMode.WorkerSandbox:
|
|
209
|
+
return compilerJayHtml.generateElementBridgeFile(jayFile);
|
|
210
|
+
case compilerShared.RuntimeMode.WorkerTrusted:
|
|
211
|
+
return hasSandboxImport(jayFile) ? compilerJayHtml.generateSandboxRootFile(jayFile) : compiler.generateImportsFileFromJayFile(jayFile);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function hasSandboxImport(jayFile) {
|
|
215
|
+
return jayFile.imports.some((link) => link.sandbox);
|
|
216
|
+
}
|
|
217
|
+
function generateCodeFromTsFile(jayContext, mode, jayFile, id, code) {
|
|
218
|
+
switch (mode) {
|
|
219
|
+
case compilerShared.RuntimeMode.MainTrusted:
|
|
220
|
+
return code;
|
|
221
|
+
case compilerShared.RuntimeMode.MainSandbox: {
|
|
222
|
+
if (!code.includes("makeJayComponent"))
|
|
223
|
+
return code;
|
|
224
|
+
return transformTsCode(
|
|
225
|
+
jayContext,
|
|
226
|
+
[
|
|
227
|
+
compiler.transformComponentBridge(
|
|
228
|
+
mode,
|
|
229
|
+
jayContext.compilerPatterns,
|
|
230
|
+
jayContext.globalFunctionsRepository
|
|
231
|
+
)
|
|
232
|
+
],
|
|
233
|
+
id,
|
|
234
|
+
code
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
case compilerShared.RuntimeMode.WorkerTrusted:
|
|
238
|
+
return compiler.generateImportsFileFromJayFile(jayFile);
|
|
239
|
+
case compilerShared.RuntimeMode.WorkerSandbox:
|
|
240
|
+
return transformTsCode(
|
|
241
|
+
jayContext,
|
|
242
|
+
[
|
|
243
|
+
compiler.transformComponent(
|
|
244
|
+
jayContext.compilerPatterns,
|
|
245
|
+
jayContext.globalFunctionsRepository
|
|
246
|
+
)
|
|
247
|
+
],
|
|
248
|
+
id,
|
|
249
|
+
code
|
|
250
|
+
);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
function transformTsCode(jayContext, transformers, id, code) {
|
|
254
|
+
const tsSource = ts__namespace.createSourceFile(id, code, ts__namespace.ScriptTarget.Latest, true, ts__namespace.ScriptKind.TS);
|
|
255
|
+
const tsCode = ts.transform(tsSource, transformers);
|
|
256
|
+
checkDiagnosticsErrors(tsCode);
|
|
257
|
+
const outputCode = jayContext.tsPrinter.printNode(
|
|
258
|
+
ts__namespace.EmitHint.Unspecified,
|
|
259
|
+
tsCode.transformed[0],
|
|
260
|
+
tsSource
|
|
261
|
+
);
|
|
262
|
+
return outputCode;
|
|
263
|
+
}
|
|
264
|
+
async function transformJayFile(jayContext, context, code, id) {
|
|
265
|
+
if (!Boolean(getJayMetadata(context, id).originId))
|
|
266
|
+
return null;
|
|
267
|
+
const mode = compilerShared.getModeFromExtension(id);
|
|
268
|
+
console.info(`[transform] start ${mode} ${id}`);
|
|
269
|
+
const { meta, jayFile } = await getJayFileStructure(jayContext, context, code, id);
|
|
270
|
+
const tsCode = await generateCodeFromStructure(jayContext, context, code, id, meta, jayFile);
|
|
271
|
+
console.info(`[transform] end ${mode} ${id}`);
|
|
272
|
+
return { code: tsCode };
|
|
273
|
+
}
|
|
274
|
+
function watchChangesFor(context, sourcePath) {
|
|
275
|
+
if (context.getWatchFiles().includes(sourcePath))
|
|
276
|
+
return;
|
|
277
|
+
context.addWatchFile(sourcePath);
|
|
278
|
+
console.info(`[watch] add ${sourcePath}`);
|
|
279
|
+
}
|
|
280
|
+
async function resolveJayHtml(context, source, importer, options, generationTarget = compilerShared.GenerateTarget.jay) {
|
|
281
|
+
const resolved = await context.resolve(source, importer, { ...options, skipSelf: true });
|
|
282
|
+
if (!resolved || compilerShared.hasExtension(resolved.id, compilerShared.TS_EXTENSION) || compilerShared.hasExtension(resolved.id, compilerShared.TSX_EXTENSION))
|
|
283
|
+
return null;
|
|
284
|
+
const resolvedJayMeta = jayMetadataFromModuleMetadata(resolved.id, resolved.meta);
|
|
285
|
+
const extension = generationTarget === compilerShared.GenerateTarget.react ? compilerShared.TSX_EXTENSION : compilerShared.TS_EXTENSION;
|
|
286
|
+
if (resolvedJayMeta.originId) {
|
|
287
|
+
const { format, originId } = resolvedJayMeta;
|
|
288
|
+
const id = `${originId}${extension}`;
|
|
289
|
+
console.info(`[resolveId] resolved ${id} as ${format}`);
|
|
290
|
+
return { id, meta: appendJayMetadata(context, id, { format, originId }) };
|
|
291
|
+
} else {
|
|
292
|
+
watchChangesFor(context, resolved.id);
|
|
293
|
+
const format = compilerShared.SourceFileFormat.JayHtml;
|
|
294
|
+
const originId = resolved.id;
|
|
295
|
+
const id = `${originId}${extension}`;
|
|
296
|
+
console.info(`[resolveId] resolved ${id} as ${format}`);
|
|
297
|
+
return { id, meta: appendJayMetadata(context, id, { format, originId }) };
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
async function resolveJayContract(context, source, importer, options) {
|
|
301
|
+
const resolved = await context.resolve(source, importer, { ...options, skipSelf: true });
|
|
302
|
+
const id = `${resolved.id}${compilerShared.TS_EXTENSION}`;
|
|
303
|
+
console.info(`[resolveId] resolved ${id}`);
|
|
304
|
+
return {
|
|
305
|
+
id,
|
|
306
|
+
meta: appendJayMetadata(context, id, {
|
|
307
|
+
format: compilerShared.SourceFileFormat.JayContract,
|
|
308
|
+
originId: resolved.id
|
|
309
|
+
})
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
async function resolveJayModeFile(context, source, importer, options) {
|
|
313
|
+
const idParts = source.split("?");
|
|
314
|
+
const idWithoutJayModeExtension = idParts.slice(0, -1).join("?");
|
|
315
|
+
const mode = idParts.slice(-1)[0];
|
|
316
|
+
const resolved = await context.resolve(idWithoutJayModeExtension, importer, {
|
|
317
|
+
...options,
|
|
318
|
+
skipSelf: false
|
|
319
|
+
});
|
|
320
|
+
if (!resolved)
|
|
321
|
+
return null;
|
|
322
|
+
const resolvedJayMeta = jayMetadataFromModuleMetadata(resolved.id, resolved.meta);
|
|
323
|
+
const format = resolvedJayMeta.format || compilerShared.SourceFileFormat.TypeScript;
|
|
324
|
+
const originId = resolvedJayMeta.originId || resolved.id;
|
|
325
|
+
const id = getResolvedId(resolved, mode, originId);
|
|
326
|
+
console.info(`[resolveId] resolved ${id} as ${format}`);
|
|
327
|
+
return { id, meta: appendJayMetadata(context, id, { format, originId }, resolvedJayMeta) };
|
|
328
|
+
}
|
|
329
|
+
async function removeSandboxPrefixForWorkerRoot(context, source, importer, options) {
|
|
330
|
+
const sourceWithoutPrefix = source.replace(SANDBOX_ROOT_PREFIX, "");
|
|
331
|
+
const resolved = await context.resolve(sourceWithoutPrefix, importer, {
|
|
332
|
+
...options,
|
|
333
|
+
skipSelf: true
|
|
334
|
+
});
|
|
335
|
+
if (!resolved)
|
|
336
|
+
return null;
|
|
337
|
+
const id = `${resolved.id}${compilerShared.JAY_QUERY_WORKER_TRUSTED_TS}`;
|
|
338
|
+
const originId = id.split("?")[0];
|
|
339
|
+
console.info(`[resolveId] resolved sandbox root ${id}`);
|
|
340
|
+
return {
|
|
341
|
+
id,
|
|
342
|
+
meta: appendJayMetadata(context, id, {
|
|
343
|
+
originId,
|
|
344
|
+
format: compilerShared.SourceFileFormat.TypeScript,
|
|
345
|
+
isWorkerRoot: true
|
|
346
|
+
})
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
function getResolvedId(resolved, mode, originId) {
|
|
350
|
+
const extension = resolved.id.split(".").pop();
|
|
351
|
+
const id = `${originId}?${mode}.${extension}`;
|
|
352
|
+
return id;
|
|
353
|
+
}
|
|
354
|
+
function stripTSExtension(id) {
|
|
355
|
+
return id.replace(compilerShared.TS_EXTENSION, "").replace(compilerShared.TSX_EXTENSION, "");
|
|
356
|
+
}
|
|
357
|
+
async function loadJayFile(context, id) {
|
|
358
|
+
console.info(`[load] start ${id}`);
|
|
359
|
+
let { originId } = getJayMetadata(context, id);
|
|
360
|
+
if (!Boolean(originId))
|
|
361
|
+
originId = stripTSExtension(id);
|
|
362
|
+
const code = checkCodeErrors(await readFileAsString(originId));
|
|
363
|
+
console.info(`[load] end ${id}`);
|
|
364
|
+
return { code };
|
|
365
|
+
}
|
|
366
|
+
async function loadContractFile(context, id) {
|
|
367
|
+
console.info(`[load] start ${id}`);
|
|
368
|
+
let { originId } = getJayMetadata(context, id);
|
|
369
|
+
if (!Boolean(originId))
|
|
370
|
+
originId = stripTSExtension(id);
|
|
371
|
+
const code = await readFileAsString(originId);
|
|
372
|
+
console.info(`[load] end ${id}`);
|
|
373
|
+
return { code };
|
|
374
|
+
}
|
|
375
|
+
class JayPluginContext {
|
|
376
|
+
constructor(jayOptions = {}) {
|
|
377
|
+
__publicField(this, "projectRoot");
|
|
378
|
+
__publicField(this, "outputDir");
|
|
379
|
+
__publicField(this, "tsPrinter");
|
|
380
|
+
__publicField(this, "compilerPatterns");
|
|
381
|
+
__publicField(this, "jayFileCache", /* @__PURE__ */ new Map());
|
|
382
|
+
__publicField(this, "globalFunctionsRepository");
|
|
383
|
+
this.jayOptions = jayOptions;
|
|
384
|
+
this.projectRoot = path.dirname(jayOptions.tsConfigFilePath ?? process.cwd());
|
|
385
|
+
this.outputDir = jayOptions.outputDir && path.join(this.projectRoot, jayOptions.outputDir);
|
|
386
|
+
this.tsPrinter = ts__namespace.createPrinter({ newLine: ts__namespace.NewLineKind.LineFeed });
|
|
387
|
+
let compilerPatternsParseResult = compiler.compileFunctionSplitPatternsBlock(
|
|
388
|
+
(jayOptions.compilerPatternFiles || []).map((fileName) => {
|
|
389
|
+
let fileContent = fs.readFileSync(fileName, { encoding: "utf8" });
|
|
390
|
+
return compiler.createTsSourceFileFromSource(fileName, fileContent);
|
|
391
|
+
})
|
|
392
|
+
);
|
|
393
|
+
if (compilerPatternsParseResult.validations.length > 0)
|
|
394
|
+
throw new Error(
|
|
395
|
+
"failed to parse or validate compilerPatternFiles. \n" + compilerPatternsParseResult.validations.join("\n")
|
|
396
|
+
);
|
|
397
|
+
this.compilerPatterns = compilerPatternsParseResult.val;
|
|
398
|
+
this.globalFunctionsRepository = new compiler.FunctionRepositoryBuilder();
|
|
399
|
+
}
|
|
400
|
+
cacheJayFile(id, jayFile) {
|
|
401
|
+
console.info("[cache] set", id);
|
|
402
|
+
this.jayFileCache.set(id, jayFile);
|
|
403
|
+
return jayFile;
|
|
404
|
+
}
|
|
405
|
+
getCachedJayFile(id) {
|
|
406
|
+
const jayFile = this.jayFileCache.get(id);
|
|
407
|
+
if (Boolean(jayFile)) {
|
|
408
|
+
console.info("[cache] hit", id);
|
|
409
|
+
}
|
|
410
|
+
return jayFile;
|
|
411
|
+
}
|
|
412
|
+
deleteCachedJayFile(id) {
|
|
413
|
+
return this.jayFileCache.delete(id);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
const GLOBAL_FUNC_REPOSITORY = "GLOBAL_FUNC_REPOSITORY.ts";
|
|
417
|
+
function jayRuntime(jayOptions = {}, givenJayContext) {
|
|
418
|
+
const jayContext = givenJayContext || new JayPluginContext(jayOptions);
|
|
419
|
+
return {
|
|
420
|
+
name: "jay:runtime",
|
|
421
|
+
// this name will show up in warnings and errors
|
|
422
|
+
async resolveId(source, importer, options) {
|
|
423
|
+
if (compilerShared.hasExtension(source, compilerShared.JAY_EXTENSION))
|
|
424
|
+
return await resolveJayHtml(
|
|
425
|
+
this,
|
|
426
|
+
source,
|
|
427
|
+
importer,
|
|
428
|
+
options,
|
|
429
|
+
jayOptions.generationTarget
|
|
430
|
+
);
|
|
431
|
+
if (compilerShared.hasExtension(source, compilerShared.JAY_CONTRACT_EXTENSION))
|
|
432
|
+
return await resolveJayContract(this, source, importer, options);
|
|
433
|
+
if (compilerShared.hasJayModeExtension(source))
|
|
434
|
+
return await resolveJayModeFile(this, source, importer, options);
|
|
435
|
+
if (source.includes(SANDBOX_ROOT_PREFIX) || jayOptions.isWorker && importer === void 0)
|
|
436
|
+
return await removeSandboxPrefixForWorkerRoot(this, source, importer, options);
|
|
437
|
+
if (source === compilerShared.Import.functionRepository.module)
|
|
438
|
+
return Promise.resolve(GLOBAL_FUNC_REPOSITORY);
|
|
439
|
+
return null;
|
|
440
|
+
},
|
|
441
|
+
async load(id) {
|
|
442
|
+
if (compilerShared.hasExtension(id, compilerShared.JAY_EXTENSION, { withTs: true }) || compilerShared.hasJayModeExtension(id, { withTs: true }))
|
|
443
|
+
return await loadJayFile(this, id);
|
|
444
|
+
else if (compilerShared.hasExtension(id, compilerShared.JAY_CONTRACT_EXTENSION, { withTs: true })) {
|
|
445
|
+
return await loadContractFile(this, id);
|
|
446
|
+
} else if (id === GLOBAL_FUNC_REPOSITORY) {
|
|
447
|
+
const { functionRepository } = jayContext.globalFunctionsRepository.generateGlobalFile();
|
|
448
|
+
return functionRepository;
|
|
449
|
+
}
|
|
450
|
+
return null;
|
|
451
|
+
},
|
|
452
|
+
async transform(code, id) {
|
|
453
|
+
if (compilerShared.hasExtension(id, compilerShared.JAY_EXTENSION, { withTs: true }) || compilerShared.hasJayModeExtension(id, { withTs: true }))
|
|
454
|
+
return await transformJayFile(jayContext, this, code, id);
|
|
455
|
+
else if (compilerShared.hasExtension(id, compilerShared.JAY_CONTRACT_EXTENSION, { withTs: true })) {
|
|
456
|
+
const { filename, dirname } = getFileContext(id, compilerShared.JAY_CONTRACT_EXTENSION);
|
|
457
|
+
const parsedFile = compilerJayHtml.parseContract(code, filename);
|
|
458
|
+
const tsCode = await compilerJayHtml.compileContract(
|
|
459
|
+
parsedFile,
|
|
460
|
+
`${dirname}/${filename}`,
|
|
461
|
+
compilerJayHtml.JAY_IMPORT_RESOLVER
|
|
462
|
+
);
|
|
463
|
+
if (tsCode.val)
|
|
464
|
+
return Promise.resolve({
|
|
465
|
+
code: tsCode.val
|
|
466
|
+
});
|
|
467
|
+
else
|
|
468
|
+
return Promise.reject(tsCode.validations);
|
|
469
|
+
}
|
|
470
|
+
return null;
|
|
471
|
+
},
|
|
472
|
+
watchChange(id, change) {
|
|
473
|
+
console.log(`[watchChange] ${id} ${change.event}`);
|
|
474
|
+
jayContext.deleteCachedJayFile(id);
|
|
475
|
+
}
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
exports.JayPluginContext = JayPluginContext;
|
|
479
|
+
exports.jayDefinitions = jayDefinitions;
|
|
480
|
+
exports.jayRuntime = jayRuntime;
|
|
481
|
+
Object.keys(compiler).forEach((k) => {
|
|
482
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(exports, k))
|
|
483
|
+
Object.defineProperty(exports, k, {
|
|
484
|
+
enumerable: true,
|
|
485
|
+
get: () => compiler[k]
|
|
486
|
+
});
|
|
487
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@jay-framework/rollup-plugin",
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"license": "Apache-2.0",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"jay",
|
|
8
|
+
"secure",
|
|
9
|
+
"rollup-plugin"
|
|
10
|
+
],
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"readme.md"
|
|
14
|
+
],
|
|
15
|
+
"scripts": {
|
|
16
|
+
"build": "npm run build:js && npm run build:types",
|
|
17
|
+
"build:watch": "npm run build:js -- --watch & npm run build:types -- --watch",
|
|
18
|
+
"build:watch:sandbox-counter": "vite -c test/jayRuntime/fixtures/counter/source/vite.config.ts",
|
|
19
|
+
"build:js": "vite build",
|
|
20
|
+
"build:types": "tsup lib/index.ts --dts-only --format cjs",
|
|
21
|
+
"build:check-types": "tsc",
|
|
22
|
+
"build:executable-bin": "chmod +x dist/index.js",
|
|
23
|
+
"clean": "rimraf dist && rimraf test/jayRuntime/fixtures/counter/dist && rimraf test/jayRuntime/fixtures/exec/dist",
|
|
24
|
+
"confirm": "npm run clean && npm run build && npm run build:check-types && npm run test",
|
|
25
|
+
"test": "vitest run",
|
|
26
|
+
"test:watch": "vitest"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@jay-framework/compiler": "workspace:^",
|
|
30
|
+
"@jay-framework/compiler-jay-html": "workspace:^",
|
|
31
|
+
"fast-glob": "^3.3.2",
|
|
32
|
+
"typescript": "^5.3.3"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@jay-framework/component": "workspace:^",
|
|
36
|
+
"@jay-framework/dev-environment": "workspace:^",
|
|
37
|
+
"@jay-framework/runtime": "workspace:^",
|
|
38
|
+
"@jay-framework/secure": "workspace:^",
|
|
39
|
+
"@types/node": "^20.11.5",
|
|
40
|
+
"rimraf": "^5.0.5",
|
|
41
|
+
"rollup": "^4.9.5",
|
|
42
|
+
"rollup-plugin-typescript2": "^0.36.0",
|
|
43
|
+
"tsup": "^8.0.1",
|
|
44
|
+
"vite": "^5.0.11",
|
|
45
|
+
"vitest": "^1.2.1",
|
|
46
|
+
"vitest-mock-extended": "^1.3.1"
|
|
47
|
+
}
|
|
48
|
+
}
|