@fictjs/vite-plugin 0.16.0 → 0.17.1

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/README.md CHANGED
@@ -9,13 +9,17 @@ Vite plugin for Fict
9
9
  ## Usage
10
10
 
11
11
  ```bash
12
+ npm install fict
12
13
  npm install -D @fictjs/vite-plugin
13
14
  # or
15
+ yarn add fict
14
16
  yarn add -D @fictjs/vite-plugin
15
17
  ```
16
18
 
17
19
  You can visit [Fict](https://github.com/fictjs/fict) for more documentation.
18
20
 
21
+ Use `fict` as the runtime dependency for standard Fict apps. If you intentionally build directly on `@fictjs/runtime`, keep your source imports on that package family consistently.
22
+
19
23
  ## Options
20
24
 
21
25
  ```ts
package/dist/index.cjs CHANGED
@@ -36,6 +36,7 @@ __export(index_exports, {
36
36
  module.exports = __toCommonJS(index_exports);
37
37
  var import_node_crypto = require("crypto");
38
38
  var import_node_fs = require("fs");
39
+ var import_node_module = require("module");
39
40
  var import_node_path = __toESM(require("path"), 1);
40
41
  var import_node_url = require("url");
41
42
  var import_core = require("@babel/core");
@@ -44,9 +45,14 @@ var import_parser = require("@babel/parser");
44
45
  var import_traverse = __toESM(require("@babel/traverse"), 1);
45
46
  var t = __toESM(require("@babel/types"), 1);
46
47
  var import_compiler = require("@fictjs/compiler");
48
+ var import_meta = {};
47
49
  var traverse = typeof import_traverse.default === "function" ? import_traverse.default : import_traverse.default.default;
48
50
  var generate = typeof import_generator.default === "function" ? import_generator.default : import_generator.default.default;
49
- var CACHE_VERSION = 1;
51
+ var CACHE_VERSION = 3;
52
+ var require2 = (0, import_node_module.createRequire)(import_meta.url);
53
+ var TRANSFORM_CACHE_FINGERPRINT = hashString(
54
+ [getCompilerCacheFingerprint(), String(extractAndRewriteHandlers)].join("|")
55
+ );
50
56
  var MODULE_EXTENSIONS = [".ts", ".tsx", ".js", ".jsx", ".mjs", ".cjs", ".mts", ".cts"];
51
57
  var VIRTUAL_HANDLER_PREFIX = "\0fict-handler:";
52
58
  var VIRTUAL_HANDLER_RESOLVE_PREFIX = "virtual:fict-handler:";
@@ -162,6 +168,9 @@ function fict(options = {}) {
162
168
  "fict",
163
169
  "fict/plus",
164
170
  "fict/advanced",
171
+ "fict/internal",
172
+ "fict/internal/list",
173
+ "fict/loader",
165
174
  "fict/slim",
166
175
  "fict/jsx-runtime",
167
176
  "fict/jsx-dev-runtime",
@@ -182,7 +191,12 @@ function fict(options = {}) {
182
191
  include2.delete(dep);
183
192
  exclude2.add(dep);
184
193
  }
185
- const dedupePackages = ["fict", "@fictjs/runtime", "@fictjs/runtime/internal"];
194
+ const dedupePackages = [
195
+ "fict",
196
+ "fict/internal",
197
+ "@fictjs/runtime",
198
+ "@fictjs/runtime/internal"
199
+ ];
186
200
  for (const dep of dedupePackages) {
187
201
  dedupe.add(dep);
188
202
  }
@@ -301,16 +315,34 @@ function fict(options = {}) {
301
315
  }
302
316
  const cacheStore = ensureCache();
303
317
  const cacheKey = cacheStore.enabled ? buildCacheKey(filename, code, fictOptions, tsProject2) : null;
318
+ const shouldSplit = options.functionSplitting ?? (config?.command === "build" && (compilerOptions.resumable || !config?.build?.ssr));
304
319
  if (cacheKey) {
305
320
  const cached = await cacheStore.get(cacheKey);
306
321
  if (cached) {
307
- return cached;
322
+ if (shouldSplit && cached.extractedHandlers?.length) {
323
+ for (const handler of cached.extractedHandlers) {
324
+ const handlerId = createHandlerId(handler.sourceModule, handler.exportName);
325
+ extractedHandlers.set(handlerId, handler);
326
+ if (config?.command === "build" && !config?.build?.ssr) {
327
+ this.emitFile({
328
+ type: "chunk",
329
+ id: `${VIRTUAL_HANDLER_RESOLVE_PREFIX}${handlerId}`,
330
+ name: `handler-${handler.exportName}`
331
+ });
332
+ }
333
+ }
334
+ }
335
+ return {
336
+ code: cached.code,
337
+ map: cached.map
338
+ };
308
339
  }
309
340
  }
310
341
  try {
311
342
  const precompiledInput = isPrecompiledFictModule(code);
312
343
  let finalCode;
313
344
  let finalMap;
345
+ let splitResult = null;
314
346
  if (precompiledInput) {
315
347
  finalCode = code;
316
348
  finalMap = null;
@@ -332,7 +364,6 @@ function fict(options = {}) {
332
364
  finalCode = result.code;
333
365
  finalMap = result.map;
334
366
  }
335
- const shouldSplit = options.functionSplitting ?? (config?.command === "build" && (compilerOptions.resumable || !config?.build?.ssr));
336
367
  debugLog("Function split decision", {
337
368
  shouldSplit,
338
369
  ssr: config?.build?.ssr,
@@ -340,7 +371,6 @@ function fict(options = {}) {
340
371
  file: filename
341
372
  });
342
373
  if (shouldSplit) {
343
- let splitResult = null;
344
374
  try {
345
375
  splitResult = extractAndRewriteHandlers(finalCode, filename, extractedHandlers);
346
376
  } catch (error) {
@@ -374,7 +404,14 @@ function fict(options = {}) {
374
404
  map: finalMap
375
405
  };
376
406
  if (cacheKey) {
377
- await cacheStore.set(cacheKey, transformed);
407
+ const cachedTransform = {
408
+ code: finalCode,
409
+ map: finalMap
410
+ };
411
+ if (shouldSplit && splitResult?.handlers.length) {
412
+ cachedTransform.extractedHandlers = splitResult.handlers.map((handlerName) => extractedHandlers.get(createHandlerId(filename, handlerName))).filter((handler) => !!handler);
413
+ }
414
+ await cacheStore.set(cacheKey, cachedTransform);
378
415
  }
379
416
  return transformed;
380
417
  } catch (error) {
@@ -539,6 +576,14 @@ function applyAlias(source, aliases) {
539
576
  function hashString(value) {
540
577
  return (0, import_node_crypto.createHash)("sha256").update(value).digest("hex");
541
578
  }
579
+ function getCompilerCacheFingerprint() {
580
+ try {
581
+ const compilerEntry = require2.resolve("@fictjs/compiler");
582
+ return hashString((0, import_node_fs.readFileSync)(compilerEntry, "utf8"));
583
+ } catch {
584
+ return hashString(String(import_compiler.createFictPlugin));
585
+ }
586
+ }
542
587
  function stableStringify(value) {
543
588
  if (value === null || typeof value !== "object") {
544
589
  return JSON.stringify(value);
@@ -570,7 +615,9 @@ function buildCacheKey(filename, code, options, tsProject) {
570
615
  const codeHash = hashString(code);
571
616
  const optionsHash = hashString(stableStringify(normalizeOptionsForCache(options)));
572
617
  const tsKey = tsProject ? `${tsProject.configHash}:${tsProject.projectVersion}` : "";
573
- return hashString([CACHE_VERSION, filename, codeHash, optionsHash, tsKey].join("|"));
618
+ return hashString(
619
+ [CACHE_VERSION, TRANSFORM_CACHE_FINGERPRINT, filename, codeHash, optionsHash, tsKey].join("|")
620
+ );
574
621
  }
575
622
  var TransformCache = class {
576
623
  constructor(options) {
@@ -719,6 +766,30 @@ async function createTypeScriptProject(ts, rootDir, configPath) {
719
766
  function createHandlerId(sourceModule, exportName) {
720
767
  return `${sourceModule}$$${exportName}`;
721
768
  }
769
+ function detectRuntimeImportFamilyFromCode(body) {
770
+ let sawFictFamily = false;
771
+ let sawStandaloneRuntimeFamily = false;
772
+ for (const stmt of body) {
773
+ const source = stmt && typeof stmt === "object" && "source" in stmt ? stmt.source?.value : void 0;
774
+ if (typeof source !== "string") continue;
775
+ if (source === "fict" || source === "fict/advanced" || source === "fict/internal" || source === "fict/internal/list" || source === "fict/jsx-runtime" || source === "fict/jsx-dev-runtime" || source === "fict/loader" || source === "fict/plus" || source === "fict/slim") {
776
+ sawFictFamily = true;
777
+ continue;
778
+ }
779
+ if (source === "@fictjs/runtime" || source === "@fictjs/runtime/advanced" || source === "@fictjs/runtime/internal" || source === "@fictjs/runtime/internal/list" || source === "@fictjs/runtime/jsx-runtime" || source === "@fictjs/runtime/jsx-dev-runtime" || source === "@fictjs/runtime/loader") {
780
+ sawStandaloneRuntimeFamily = true;
781
+ }
782
+ }
783
+ if (sawFictFamily) return "fict";
784
+ if (sawStandaloneRuntimeFamily) return "runtime";
785
+ return "fict";
786
+ }
787
+ function getRuntimeHelperModule(helperName, family) {
788
+ if (helperName === "keyedList") {
789
+ return family === "runtime" ? "@fictjs/runtime/internal/list" : "fict/internal/list";
790
+ }
791
+ return family === "runtime" ? "@fictjs/runtime/internal" : "fict/internal";
792
+ }
722
793
  function generateHandlerModule(handler) {
723
794
  if (!handler.code) {
724
795
  return `export { ${handler.exportName} as default } from '${handler.sourceModule}';
@@ -728,11 +799,12 @@ function generateHandlerModule(handler) {
728
799
  for (const helperName of handler.helpersUsed) {
729
800
  const helper = RUNTIME_HELPERS[helperName];
730
801
  if (!helper) continue;
731
- const existing = importsByModule.get(helper.from) ?? [];
802
+ const moduleSource = getRuntimeHelperModule(helperName, handler.runtimeImportFamily);
803
+ const existing = importsByModule.get(moduleSource) ?? [];
732
804
  if (!existing.includes(helper.import)) {
733
805
  existing.push(helper.import);
734
806
  }
735
- importsByModule.set(helper.from, existing);
807
+ importsByModule.set(moduleSource, existing);
736
808
  }
737
809
  const imports = [];
738
810
  for (const [module2, names] of importsByModule) {
@@ -753,20 +825,21 @@ function registerExtractedHandler(sourceModule, exportName, helpersUsed, code, l
753
825
  exportName,
754
826
  helpersUsed,
755
827
  localDeps,
756
- code
828
+ code,
829
+ runtimeImportFamily: "fict"
757
830
  });
758
831
  return `${VIRTUAL_HANDLER_RESOLVE_PREFIX}${handlerId}`;
759
832
  }
760
833
  var RUNTIME_HELPERS = {
761
- __fictUseLexicalScope: { import: "__fictUseLexicalScope", from: "@fictjs/runtime/internal" },
762
- __fictGetScopeProps: { import: "__fictGetScopeProps", from: "@fictjs/runtime/internal" },
763
- __fictGetSSRScope: { import: "__fictGetSSRScope", from: "@fictjs/runtime/internal" },
764
- __fictEnsureScope: { import: "__fictEnsureScope", from: "@fictjs/runtime/internal" },
765
- __fictPrepareContext: { import: "__fictPrepareContext", from: "@fictjs/runtime/internal" },
766
- __fictPushContext: { import: "__fictPushContext", from: "@fictjs/runtime/internal" },
767
- __fictPopContext: { import: "__fictPopContext", from: "@fictjs/runtime/internal" },
768
- hydrateComponent: { import: "hydrateComponent", from: "@fictjs/runtime/internal" },
769
- __fictQrl: { import: "__fictQrl", from: "@fictjs/runtime/internal" }
834
+ __fictUseLexicalScope: { import: "__fictUseLexicalScope", from: "fict/internal" },
835
+ __fictGetScopeProps: { import: "__fictGetScopeProps", from: "fict/internal" },
836
+ __fictGetSSRScope: { import: "__fictGetSSRScope", from: "fict/internal" },
837
+ __fictEnsureScope: { import: "__fictEnsureScope", from: "fict/internal" },
838
+ __fictPrepareContext: { import: "__fictPrepareContext", from: "fict/internal" },
839
+ __fictPushContext: { import: "__fictPushContext", from: "fict/internal" },
840
+ __fictPopContext: { import: "__fictPopContext", from: "fict/internal" },
841
+ hydrateComponent: { import: "hydrateComponent", from: "fict/internal" },
842
+ __fictQrl: { import: "__fictQrl", from: "fict/internal" }
770
843
  };
771
844
  var GLOBAL_IDENTIFIERS = /* @__PURE__ */ new Set([
772
845
  // JavaScript globals
@@ -1039,6 +1112,7 @@ function extractAndRewriteHandlers(code, sourceModule, handlerRegistry) {
1039
1112
  const handlerNames = [];
1040
1113
  const nodesToRemove = /* @__PURE__ */ new Set();
1041
1114
  const allLocalDeps = /* @__PURE__ */ new Set();
1115
+ const runtimeImportFamily = detectRuntimeImportFamilyFromCode(ast.program.body);
1042
1116
  traverse(ast, {
1043
1117
  ExportNamedDeclaration(path2) {
1044
1118
  const declaration = path2.node.declaration;
@@ -1071,7 +1145,8 @@ function extractAndRewriteHandlers(code, sourceModule, handlerRegistry) {
1071
1145
  exportName: name,
1072
1146
  helpersUsed,
1073
1147
  localDeps,
1074
- code: handlerCode
1148
+ code: handlerCode,
1149
+ runtimeImportFamily
1075
1150
  });
1076
1151
  nodesToRemove.add(path2.node);
1077
1152
  }
@@ -1106,7 +1181,8 @@ function extractAndRewriteHandlers(code, sourceModule, handlerRegistry) {
1106
1181
  exportName: name,
1107
1182
  helpersUsed,
1108
1183
  localDeps,
1109
- code: handlerCode
1184
+ code: handlerCode,
1185
+ runtimeImportFamily
1110
1186
  });
1111
1187
  nodesToRemove.add(path2.node);
1112
1188
  }