@knapsack/renderer-react 4.69.8--canary.4746.1acd21a.0 → 4.69.8--canary.4739.f95990d.0

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -16,23 +16,21 @@ var __dirname = /* @__PURE__ */ getDirname();
16
16
 
17
17
  // src/renderer-react.ts
18
18
  import sleep from "sleep-promise";
19
- import { RendererWebpackBase } from "@knapsack/renderer-webpack-base";
19
+ import { KnapsackRendererWebpackBase } from "@knapsack/renderer-webpack-base";
20
20
  import { pascalCase, isFirstLetterCapital } from "@knapsack/utils";
21
- import { knapsackEvents, log as log2 } from "@knapsack/app";
22
- import {
23
- babelCodeForBrowser,
24
- createSlotOptionsHtmlTags
25
- } from "@knapsack/app/renderers";
21
+ import { knapsackEvents, log as log2, KnapsackRendererBase } from "@knapsack/app";
26
22
  import {
23
+ isSlottedText,
24
+ isSlottedTemplateReference,
25
+ isSlottedTemplateDemo,
27
26
  rendererIds
28
27
  } from "@knapsack/types";
29
28
  import {
30
- exists as fileExists,
29
+ exists,
31
30
  findUpPkgJson,
32
31
  getJsExportNames as getJsExportNames2,
33
32
  readFile as readFile2,
34
- formatCode as formatCode2,
35
- resolvePath as resolvePath2
33
+ formatCode as formatCode2
36
34
  } from "@knapsack/file-utils";
37
35
  import { join, relative, parse } from "path";
38
36
 
@@ -726,7 +724,8 @@ var createReactPattern = createCreator({
726
724
  // src/renderer-react.ts
727
725
  var { pkg } = findUpPkgJson(__dirname);
728
726
  log2.setupUpdateNotifier({ ...pkg, name: pkg.name, version: pkg.version });
729
- var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpackBase {
727
+ var { createSlotOptionsHtmlTags } = KnapsackRendererBase;
728
+ var KnapsackReactRenderer = class _KnapsackReactRenderer extends KnapsackRendererWebpackBase {
730
729
  static {
731
730
  __name(this, "KnapsackReactRenderer");
732
731
  }
@@ -735,6 +734,7 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
735
734
  */
736
735
  assets;
737
736
  babelConfig;
737
+ creators;
738
738
  demoWrapperPath;
739
739
  disableReactStrictMode;
740
740
  constructor({
@@ -755,15 +755,15 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
755
755
  this.disableReactStrictMode = disableReactStrictMode;
756
756
  this.creators = [createReactPattern];
757
757
  }
758
- init = /* @__PURE__ */ __name(async (opt) => {
758
+ async init(opt) {
759
759
  await super.init(opt);
760
760
  this.assets = await copyReactAssets(this.outputDir, this.publicPath);
761
- if (!await fileExists(this.demoWrapperPath)) {
761
+ if (!await exists(this.demoWrapperPath)) {
762
762
  throw new Error(
763
763
  `Could not find demo wrapper at: "${this.demoWrapperPath}"`
764
764
  );
765
765
  }
766
- }, "init");
766
+ }
767
767
  getMeta = /* @__PURE__ */ __name(() => ({
768
768
  id: this.id,
769
769
  title: "React",
@@ -793,16 +793,18 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
793
793
  }
794
794
  }
795
795
  }), "getMeta");
796
- changeCase = /* @__PURE__ */ __name((str) => pascalCase(str), "changeCase");
797
- createWebpackConfig = /* @__PURE__ */ __name(() => {
796
+ changeCase(str) {
797
+ return pascalCase(str);
798
+ }
799
+ createWebpackConfig() {
798
800
  const config = super.createWebpackConfig();
799
801
  config.externals = {
800
802
  react: "React",
801
803
  "react-dom": "ReactDOM"
802
804
  };
803
805
  return config;
804
- }, "createWebpackConfig");
805
- getJsImports = /* @__PURE__ */ __name(() => {
806
+ }
807
+ getJsImports() {
806
808
  const imports = super.getJsImports();
807
809
  imports.push(
808
810
  {
@@ -823,7 +825,7 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
823
825
  }
824
826
  );
825
827
  return imports;
826
- }, "getJsImports");
828
+ }
827
829
  async prepClientRenderResults({
828
830
  usage,
829
831
  demoApp,
@@ -948,7 +950,7 @@ ${renderReact.name}({
948
950
  let codeHadError = false;
949
951
  let errorHtmlMsg = "";
950
952
  try {
951
- code = await babelCodeForBrowser({
953
+ code = await this.babelCodeForBrowser({
952
954
  code
953
955
  });
954
956
  } catch (e) {
@@ -1000,24 +1002,18 @@ ${errorHtmlMsg}
1000
1002
  templateLanguage: this.language
1001
1003
  };
1002
1004
  }
1003
- render = /* @__PURE__ */ __name(async (opt) => {
1004
- if (opt.demo?.type === "template") {
1005
+ async render(opt) {
1006
+ if (_KnapsackReactRenderer.isTemplateDemo(opt.demo)) {
1005
1007
  const waits = [5, 10, 20, 50, 100, 1e3, 1e3];
1006
1008
  let templateDemoPath;
1007
1009
  let attempt = 0;
1008
1010
  while (true) {
1009
1011
  try {
1010
- const { absolutePath, exists } = await resolvePath2({
1011
- path: opt.demo.templateInfo.path,
1012
- pkgPathAliases: this.pkgPathAliases,
1013
- resolveFromDir: this.dataDir
1012
+ templateDemoPath = opt.patternManifest.getTemplateDemoAbsolutePath({
1013
+ patternId: opt.pattern.id,
1014
+ templateId: opt.template.id,
1015
+ demoId: opt.demo.id
1014
1016
  });
1015
- if (!exists) {
1016
- throw new Error(
1017
- `Template demo file does not exist: ${absolutePath}`
1018
- );
1019
- }
1020
- templateDemoPath = absolutePath;
1021
1017
  break;
1022
1018
  } catch (e) {
1023
1019
  const waitTime = waits[attempt];
@@ -1043,7 +1039,7 @@ ${errorHtmlMsg}
1043
1039
  });
1044
1040
  return results;
1045
1041
  }
1046
- if (opt.demo?.type === "data") {
1042
+ if (_KnapsackReactRenderer.isDataDemo(opt.demo)) {
1047
1043
  const { usage, imports } = await this.getUsageAndImports(opt);
1048
1044
  const { code: importCode } = this.createJsImportCodeBlock({
1049
1045
  imports
@@ -1064,14 +1060,10 @@ ${errorHtmlMsg}
1064
1060
  renderOptions: opt
1065
1061
  });
1066
1062
  }
1067
- }, "render");
1068
- getUsageAndImports = /* @__PURE__ */ __name(async ({
1069
- pattern,
1070
- template,
1071
- patternManifest,
1072
- demo
1073
- }) => {
1074
- if (demo?.type && demo.type === "data") {
1063
+ }
1064
+ async getUsageAndImports(opt) {
1065
+ const { pattern, template, patternManifest, demo } = opt;
1066
+ if (demo?.type && _KnapsackReactRenderer.isDataDemo(demo)) {
1075
1067
  const {
1076
1068
  data: { props, slots, slotsOptionsComputed }
1077
1069
  } = demo;
@@ -1095,16 +1087,16 @@ ${errorHtmlMsg}
1095
1087
  const slotItems = slots[slotName];
1096
1088
  const slotItemsUsages = await Promise.all(
1097
1089
  slotItems.filter((slotItem) => {
1098
- if (slotItem.type !== "text") {
1090
+ if (!isSlottedText(slotItem)) {
1099
1091
  if (!slotItem.patternId) return false;
1100
1092
  if (!slotItem.templateId) return false;
1101
- if (slotItem.type === "template-demo" && !slotItem.demoId) {
1093
+ if (isSlottedTemplateDemo(slotItem) && !slotItem.demoId) {
1102
1094
  return false;
1103
1095
  }
1104
1096
  }
1105
1097
  return true;
1106
1098
  }).map(async (slotItem) => {
1107
- if (slotItem.type === "text") {
1099
+ if (isSlottedText(slotItem)) {
1108
1100
  if (slotItems.length === 1 && slotName !== "children") {
1109
1101
  return `\`${slotItem.text}\``;
1110
1102
  }
@@ -1116,7 +1108,7 @@ ${errorHtmlMsg}
1116
1108
  const slotTemplate = slotPattern.templates.find(
1117
1109
  (t) => t.id === slotItem.templateId
1118
1110
  );
1119
- if (slotItem.type === "template-reference") {
1111
+ if (isSlottedTemplateReference(slotItem)) {
1120
1112
  const { usage: usage2, imports } = await this.getUsageAndImports({
1121
1113
  pattern: slotPattern,
1122
1114
  template: slotTemplate,
@@ -1125,7 +1117,7 @@ ${errorHtmlMsg}
1125
1117
  importInfos.push(...imports);
1126
1118
  return usage2;
1127
1119
  }
1128
- if (slotItem.type === "template-demo") {
1120
+ if (isSlottedTemplateDemo(slotItem)) {
1129
1121
  const { usage: usage2, imports } = await this.getUsageAndImports({
1130
1122
  pattern: slotPattern,
1131
1123
  template: slotTemplate,
@@ -1196,7 +1188,7 @@ ${errorHtmlMsg}
1196
1188
  imports: importInfos
1197
1189
  };
1198
1190
  }
1199
- if (demo?.type && demo.type === "template") {
1191
+ if (demo?.type && _KnapsackReactRenderer.isTemplateDemo(demo)) {
1200
1192
  const importInfo = this.getJsImport({
1201
1193
  patternId: pattern.id,
1202
1194
  templateId: template.id,
@@ -1240,7 +1232,11 @@ ${errorHtmlMsg}
1240
1232
  demo
1241
1233
  )}`
1242
1234
  );
1243
- }, "getUsageAndImports");
1235
+ }
1236
+ async getUsage(opt) {
1237
+ const { usage } = await this.getUsageAndImports(opt);
1238
+ return usage;
1239
+ }
1244
1240
  inferSpec = /* @__PURE__ */ __name(async ({
1245
1241
  template,
1246
1242
  templatePath
@@ -1259,12 +1255,12 @@ ${errorHtmlMsg}
1259
1255
  }
1260
1256
  return spec;
1261
1257
  }, "inferSpec");
1262
- watch = /* @__PURE__ */ __name(async (opt) => {
1263
- super.watch(opt);
1258
+ async watch(opt) {
1259
+ await super.watch(opt);
1264
1260
  knapsackEvents.onPatternTemplateChanged(() => {
1265
1261
  clearInferSpecCache();
1266
1262
  });
1267
- }, "watch");
1263
+ }
1268
1264
  getTemplateMeta = /* @__PURE__ */ __name(async ({
1269
1265
  pattern,
1270
1266
  template
@@ -1307,10 +1303,7 @@ ${errorHtmlMsg}
1307
1303
  }
1308
1304
  return files;
1309
1305
  }, "getTemplateMeta");
1310
- alterTemplateMetaFiles = /* @__PURE__ */ __name(async ({
1311
- files,
1312
- metaDir
1313
- }) => {
1306
+ alterTemplateMetaFiles = /* @__PURE__ */ __name(async ({ files, metaDir }) => {
1314
1307
  const imports = [];
1315
1308
  const ext = ".spec.d.ts";
1316
1309
  files.forEach((file) => {
@@ -1337,9 +1330,9 @@ ${errorHtmlMsg}
1337
1330
  }
1338
1331
  ];
1339
1332
  }, "alterTemplateMetaFiles");
1340
- getTemplateSuggestions = /* @__PURE__ */ __name(async ({
1333
+ async getTemplateSuggestions({
1341
1334
  newPath
1342
- }) => {
1335
+ }) {
1343
1336
  const { data: dataDir } = this.patterns.userConfig;
1344
1337
  const { allTemplateDemos, allTemplates } = this.getMyTemplates();
1345
1338
  const usedSuggestions = [
@@ -1390,7 +1383,7 @@ ${errorHtmlMsg}
1390
1383
  return {
1391
1384
  suggestions
1392
1385
  };
1393
- }, "getTemplateSuggestions");
1386
+ }
1394
1387
  };
1395
1388
  export {
1396
1389
  KnapsackReactRenderer