@knapsack/renderer-react 4.69.9--canary.011c229.0 → 4.69.9

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.mjs CHANGED
@@ -16,8 +16,10 @@ 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";
20
- import { pascalCase, isFirstLetterCapital } from "@knapsack/utils";
19
+ import {
20
+ RendererWebpackBase
21
+ } from "@knapsack/renderer-webpack-base";
22
+ import { pascalCase, isFirstLetterCapital, groupBy } from "@knapsack/utils";
21
23
  import { knapsackEvents, log as log2 } from "@knapsack/app";
22
24
  import {
23
25
  babelCodeForBrowser,
@@ -63,13 +65,14 @@ function renderUsageTemplate({
63
65
  `;
64
66
  }
65
67
  __name(renderUsageTemplate, "renderUsageTemplate");
68
+ var demoAppName = "DemoApp";
66
69
  function renderDemoAppTemplate({
67
- imports,
70
+ imports = "",
68
71
  children
69
72
  }) {
70
73
  return `${imports}
71
74
 
72
- function DemoApp() {
75
+ function ${demoAppName}() {
73
76
  return (
74
77
  ${children}
75
78
  )
@@ -128,12 +131,14 @@ async function getUsage(data) {
128
131
  __name(getUsage, "getUsage");
129
132
  async function getDemoAppUsage({
130
133
  children,
131
- imports
134
+ imports,
135
+ format
132
136
  }) {
133
137
  const code = renderDemoAppTemplate({
134
138
  children,
135
139
  imports
136
140
  });
141
+ if (!format) return code;
137
142
  return formatCode({
138
143
  contents: code,
139
144
  path: "x.tsx"
@@ -723,6 +728,9 @@ var createReactPattern = createCreator({
723
728
  }, "getTasks")
724
729
  });
725
730
 
731
+ // src/types.ts
732
+ var rendererMetaScriptTagId = "ks-react-meta";
733
+
726
734
  // src/renderer-react.ts
727
735
  var { pkg } = findUpPkgJson(__dirname);
728
736
  log2.setupUpdateNotifier({ ...pkg, name: pkg.name, version: pkg.version });
@@ -747,7 +755,11 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
747
755
  id,
748
756
  extension: ".jsx",
749
757
  language: "jsx",
750
- webpackConfig
758
+ webpackConfig,
759
+ extraScripts: [
760
+ // this is the code in `./client/init.mts`
761
+ "@knapsack/renderer-react/client"
762
+ ]
751
763
  });
752
764
  this.language = "jsx";
753
765
  this.assets = [];
@@ -830,167 +842,82 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
830
842
  imports: xImports,
831
843
  renderOptions: { pattern, template, demo }
832
844
  }) {
833
- const extraImportNames = this.getJsImports().flatMap(
834
- (imp) => imp.type === "extra" ? [imp.importInfo.name] : []
845
+ const extraImports = this.getJsImports().filter(
846
+ (imp) => imp.type === "extra"
835
847
  );
836
- const { imports, isDeclaredVarsUnique, nameCollisions } = this.makeKsJsImportsUnique({ imports: xImports });
848
+ const { imports, isDeclaredVarsUnique, nameCollisions } = this.makeKsJsImportsUnique({ imports: [...xImports, ...extraImports] });
837
849
  if (!isDeclaredVarsUnique) {
838
850
  log2.error(`${nameCollisions.join(", ")} are declared multiple times`, {
839
851
  imports
840
852
  });
841
853
  }
842
- const importNames = [
843
- ...imports.map(({ importInfo: { name } }) => name),
844
- ...extraImportNames
845
- ];
846
- const patternsUsed = imports.flatMap((imp) => {
847
- if (imp.type === "pattern-template") {
848
- return [
849
- {
850
- patternId: imp.patternId,
851
- templateId: imp.templateId
854
+ const importInfos = imports.map(({ importInfo }) => importInfo);
855
+ const meta = {
856
+ demo,
857
+ moduleImports: groupBy(importInfos, (importInfo) => importInfo.path),
858
+ patternId: pattern.id,
859
+ templateId: template.id,
860
+ imports,
861
+ disableReactStrictMode: this.disableReactStrictMode,
862
+ demoWrapperProps: {
863
+ pattern,
864
+ template,
865
+ demo,
866
+ patternsUsed: imports.flatMap((imp) => {
867
+ if (imp.type === "pattern-template") {
868
+ return [
869
+ {
870
+ patternId: imp.patternId,
871
+ templateId: imp.templateId
872
+ }
873
+ ];
852
874
  }
853
- ];
854
- }
855
- if (imp.type === "pattern-template-demo") {
856
- return [
857
- {
858
- patternId: imp.patternId,
859
- templateId: imp.templateId,
860
- demoId: imp.demoId
875
+ if (imp.type === "pattern-template-demo") {
876
+ return [
877
+ {
878
+ patternId: imp.patternId,
879
+ templateId: imp.templateId,
880
+ demoId: imp.demoId
881
+ }
882
+ ];
861
883
  }
862
- ];
884
+ return [];
885
+ })
863
886
  }
864
- return [];
865
- });
866
- const demoWrapperProps = {
867
- patternsUsed,
868
- pattern,
869
- template,
870
- demo
871
887
  };
872
- const demoWrapperCode = await getUsage({
873
- templateName: "DemoWrapper",
874
- props: demoWrapperProps,
875
- children: `<DemoApp />`
876
- // extraProps,
877
- });
878
- async function renderReact({
879
- patterns,
880
- theExtraImportNames,
881
- ksGlobal
882
- }) {
883
- if (!ksGlobal) {
884
- throw new Error('"window.knapsack" is null');
885
- }
886
- const components = await ksGlobal.getAllImports({
887
- patterns,
888
- extras: theExtraImportNames.map((name) => ({ name }))
889
- });
890
- return components;
891
- }
892
- __name(renderReact, "renderReact");
893
888
  let code = `
894
-
895
- ${renderReact.toString()}
896
- ${renderReact.name}({
897
- ksGlobal: window.knapsack,
898
- patterns: ${JSON.stringify(patternsUsed)},
899
- theExtraImportNames: ${JSON.stringify(extraImportNames)},
900
- }).then((components) => {
901
- const { ${importNames.join(", ")} } = components;
902
- const root = document.getElementById('render-root');
903
-
904
- ${demoApp}
905
-
906
- const reactDomVersion = parseInt(ReactDOM.version.split('.')[0], 10);
907
-
908
- const ReactRendererResult = () => {
909
- if (reactDomVersion < 17 || ${this.disableReactStrictMode}){
910
- return (
911
- <ErrorCatcher>
912
- ${demoWrapperCode}
913
- </ErrorCatcher>
914
- )
889
+ window.knapsack = window.knapsack || {};
890
+ window.knapsack.getDemoApp = ({ ${imports.map((i) => i.importInfo.name).join(", ")} }) => {
891
+ ${demoApp}
892
+ return ${demoAppName}
915
893
  }
916
-
917
- return (
918
- <React.StrictMode>
919
- <ErrorCatcher>
920
- ${demoWrapperCode}
921
- </ErrorCatcher>
922
- </React.StrictMode>
923
- )
924
- };
925
-
926
-
927
- if (reactDomVersion > 17){
928
- ReactDOM.createRoot(root).render(
929
- <ReactRendererResult />
930
- )
931
- } else {
932
- ReactDOM.render(
933
- <ReactRendererResult />,
934
- root
935
- );
936
- }
937
- }).catch((err) => {
938
- console.error(err);
939
- const root = document.getElementById('render-root');
940
- // Create a new <div> element to contain the error message
941
- const errorMsgDiv = document.createElement('div');
942
- // Insert the new <div> element before the existing element
943
- root.parentNode.insertBefore(errorMsgDiv, root);
944
- // add error message so users can see it
945
- errorMsgDiv.innerHTML = '<h1>Component Error</h1><p>' + err.message + '</p>';
946
- });
947
894
  `;
948
- let codeHadError = false;
949
895
  let errorHtmlMsg = "";
950
896
  try {
951
- code = await babelCodeForBrowser({
952
- code
953
- });
897
+ code = await babelCodeForBrowser({ code });
954
898
  } catch (e) {
955
- codeHadError = true;
956
899
  console.log(code);
957
900
  console.log("---original code before babel error ^---");
958
901
  console.trace(e.message);
959
902
  code = `console.error(${JSON.stringify(e.message)});`;
960
903
  errorHtmlMsg = `<pre><code>${e.message}</code></pre>`;
961
904
  }
962
- if (!codeHadError) {
963
- try {
964
- code = await formatCode2({
965
- contents: code,
966
- path: "x.tsx"
967
- // doing this to set format language
968
- });
969
- } catch (e) {
970
- codeHadError = true;
971
- console.log(code);
972
- console.log("---original code before prettier error ^---");
973
- console.trace(e.message);
974
- code = `console.error(${JSON.stringify(e.message)});`;
975
- errorHtmlMsg = `<pre><code>${e.message}</code></pre>`;
976
- }
977
- }
978
905
  const html = `
906
+ <script type="application/json" id="${rendererMetaScriptTagId}">${JSON.stringify(
907
+ meta
908
+ )}</script>
909
+ <script type="application/javascript">${code}</script>
910
+ <div id="render-root" class="knapsack-pattern-direct-parent" data-dev-note="Knapsack React Template Wrapper"></div>
979
911
  ${this.assets.map((asset) => `<script src="${asset}"></script>`).join("\n")}
980
-
981
912
  ${this.createHtmlTagsForAssetPaths({
982
913
  assets: this.getWebpackAssetPaths(),
983
914
  // we need the scripts to finish adding methods to the global knapsack object synchronously before the client-side code runs that is in the <script> tag below
984
915
  scriptTagsAreAsync: false
985
916
  })}
986
-
987
- <div id="render-root" class="knapsack-pattern-direct-parent" data-dev-note="Knapsack React Template Wrapper"></div>
988
- <script type="application/javascript">${code}</script>
989
- ${errorHtmlMsg}
990
-
917
+ ${errorHtmlMsg}
991
918
  `;
992
919
  return {
993
- ok: !codeHadError,
920
+ ok: !errorHtmlMsg,
994
921
  html: await formatCode2({
995
922
  contents: html,
996
923
  path: "x.html"
@@ -1051,7 +978,8 @@ ${errorHtmlMsg}
1051
978
  const [demoAppUsage, demoApp] = await Promise.all([
1052
979
  getDemoAppUsage({
1053
980
  children: usage,
1054
- imports: importCode
981
+ imports: importCode,
982
+ format: true
1055
983
  }),
1056
984
  getDemoAppUsage({
1057
985
  children: usage