@knapsack/renderer-react 4.69.13--canary.4739.37477c8.0 → 4.69.13--canary.4821.022ca72.0
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +20 -0
- package/dist/client/init.js +33 -44
- package/dist/client/init.js.map +1 -1
- package/dist/client/init.mjs +33 -49
- package/dist/client/init.mjs.map +1 -1
- package/dist/demo-wrapper.d.mts +1 -2
- package/dist/demo-wrapper.d.ts +1 -2
- package/dist/index.d.mts +5 -7
- package/dist/index.d.ts +5 -7
- package/dist/index.js +140 -166
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +145 -170
- package/dist/index.mjs.map +1 -1
- package/dist/types-CivTHEEo.d.mts +10 -0
- package/dist/types-CivTHEEo.d.ts +10 -0
- package/package.json +13 -13
- package/dist/types-D2eCZXsg.d.mts +0 -13
- package/dist/types-D2eCZXsg.d.ts +0 -13
package/dist/index.mjs
CHANGED
@@ -17,9 +17,10 @@ var __dirname = /* @__PURE__ */ getDirname();
|
|
17
17
|
// src/renderer-react.ts
|
18
18
|
import sleep from "sleep-promise";
|
19
19
|
import {
|
20
|
-
RendererWebpackBase
|
20
|
+
RendererWebpackBase,
|
21
|
+
convertImportMapToNeededImportsByPath
|
21
22
|
} from "@knapsack/renderer-webpack-base";
|
22
|
-
import { pascalCase, isFirstLetterCapital
|
23
|
+
import { pascalCase, isFirstLetterCapital } from "@knapsack/utils";
|
23
24
|
import { knapsackEvents, log as log2 } from "@knapsack/app";
|
24
25
|
import {
|
25
26
|
babelCodeForBrowser,
|
@@ -34,9 +35,9 @@ import {
|
|
34
35
|
getJsExportNames as getJsExportNames2,
|
35
36
|
readFile as readFile2,
|
36
37
|
formatCode as formatCode2,
|
37
|
-
|
38
|
+
assertFileExists
|
38
39
|
} from "@knapsack/file-utils";
|
39
|
-
import { join, relative, parse } from "path";
|
40
|
+
import { join, relative, parse, isAbsolute } from "path";
|
40
41
|
|
41
42
|
// src/utils.ts
|
42
43
|
import { log } from "@knapsack/app";
|
@@ -734,6 +735,7 @@ var rendererMetaScriptTagId = "ks-react-meta";
|
|
734
735
|
// src/renderer-react.ts
|
735
736
|
var { pkg } = findUpPkgJson(__dirname);
|
736
737
|
log2.setupUpdateNotifier({ ...pkg, name: pkg.name, version: pkg.version });
|
738
|
+
var errorCatcherPath = join(__dirname, "./error-catcher.mjs");
|
737
739
|
var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpackBase {
|
738
740
|
static {
|
739
741
|
__name(this, "KnapsackReactRenderer");
|
@@ -753,22 +755,29 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
|
|
753
755
|
} = {}) {
|
754
756
|
super({
|
755
757
|
id,
|
756
|
-
extension: ".jsx",
|
757
758
|
language: "jsx",
|
758
759
|
webpackConfig,
|
759
760
|
extraScripts: [
|
760
761
|
// this is the code in `./client/init.mts`
|
761
762
|
"@knapsack/renderer-react/client"
|
762
|
-
]
|
763
|
+
],
|
764
|
+
codeSrcs: [demoWrapperPath, errorCatcherPath]
|
763
765
|
});
|
764
766
|
this.language = "jsx";
|
765
767
|
this.assets = [];
|
766
|
-
this.demoWrapperPath = demoWrapperPath
|
768
|
+
this.demoWrapperPath = isAbsolute(demoWrapperPath) ? demoWrapperPath : this.resolvePathSync({
|
769
|
+
path: demoWrapperPath,
|
770
|
+
resolveFromDir: this.userConfigDir
|
771
|
+
}).absolutePath;
|
772
|
+
assertFileExists(
|
773
|
+
this.demoWrapperPath,
|
774
|
+
`Could not find demo wrapper at: "${this.demoWrapperPath}"
|
775
|
+
Please adjust setting in "knapsack.config.js" or pass a different path when creating the React Renderer.`
|
776
|
+
);
|
767
777
|
this.disableReactStrictMode = disableReactStrictMode;
|
768
778
|
this.creators = [createReactPattern];
|
769
779
|
}
|
770
|
-
init = /* @__PURE__ */ __name(async (
|
771
|
-
await super.init(opt);
|
780
|
+
init = /* @__PURE__ */ __name(async () => {
|
772
781
|
this.assets = await copyReactAssets(this.outputDir, this.publicPath);
|
773
782
|
if (!await fileExists(this.demoWrapperPath)) {
|
774
783
|
throw new Error(
|
@@ -814,80 +823,37 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
|
|
814
823
|
};
|
815
824
|
return config;
|
816
825
|
}, "createWebpackConfig");
|
817
|
-
getJsImports = /* @__PURE__ */ __name(() => {
|
818
|
-
const imports = super.getJsImports();
|
819
|
-
imports.push(
|
820
|
-
{
|
821
|
-
type: "extra",
|
822
|
-
importInfo: {
|
823
|
-
type: "default",
|
824
|
-
path: this.demoWrapperPath,
|
825
|
-
name: "DemoWrapper"
|
826
|
-
}
|
827
|
-
},
|
828
|
-
{
|
829
|
-
type: "extra",
|
830
|
-
importInfo: {
|
831
|
-
type: "default",
|
832
|
-
path: join(__dirname, "./error-catcher.mjs"),
|
833
|
-
name: "ErrorCatcher"
|
834
|
-
}
|
835
|
-
}
|
836
|
-
);
|
837
|
-
return imports;
|
838
|
-
}, "getJsImports");
|
839
826
|
async prepClientRenderResults({
|
840
827
|
usage,
|
841
828
|
demoApp,
|
842
|
-
|
843
|
-
renderOptions: {
|
829
|
+
importMap,
|
830
|
+
renderOptions: { demo, state, patternId, templateId }
|
844
831
|
}) {
|
845
|
-
const extraImports = this.getJsImports().filter(
|
846
|
-
(imp) => imp.type === "extra"
|
847
|
-
);
|
848
|
-
const { imports, isDeclaredVarsUnique, nameCollisions } = this.makeKsJsImportsUnique({ imports: [...xImports, ...extraImports] });
|
849
|
-
if (!isDeclaredVarsUnique) {
|
850
|
-
log2.error(`${nameCollisions.join(", ")} are declared multiple times`, {
|
851
|
-
imports
|
852
|
-
});
|
853
|
-
}
|
854
|
-
const importInfos = imports.map(({ importInfo }) => importInfo);
|
855
832
|
const meta = {
|
856
833
|
demo,
|
857
|
-
moduleImports: groupBy(importInfos, (importInfo) => importInfo.path),
|
858
|
-
patternId: pattern.id,
|
859
|
-
templateId: template.id,
|
860
|
-
imports,
|
861
834
|
disableReactStrictMode: this.disableReactStrictMode,
|
835
|
+
neededImportsByPath: convertImportMapToNeededImportsByPath(importMap),
|
836
|
+
extraImports: {
|
837
|
+
DemoWrapper: {
|
838
|
+
type: "default",
|
839
|
+
path: this.demoWrapperPath,
|
840
|
+
name: "DemoWrapper"
|
841
|
+
},
|
842
|
+
ErrorCatcher: {
|
843
|
+
type: "default",
|
844
|
+
path: errorCatcherPath,
|
845
|
+
name: "ErrorCatcher"
|
846
|
+
}
|
847
|
+
},
|
862
848
|
demoWrapperProps: {
|
863
|
-
|
864
|
-
|
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
|
-
];
|
874
|
-
}
|
875
|
-
if (imp.type === "pattern-template-demo") {
|
876
|
-
return [
|
877
|
-
{
|
878
|
-
patternId: imp.patternId,
|
879
|
-
templateId: imp.templateId,
|
880
|
-
demoId: imp.demoId
|
881
|
-
}
|
882
|
-
];
|
883
|
-
}
|
884
|
-
return [];
|
885
|
-
})
|
849
|
+
patternId,
|
850
|
+
templateId,
|
851
|
+
demo
|
886
852
|
}
|
887
853
|
};
|
888
854
|
let code = `
|
889
855
|
window.knapsack = window.knapsack || {};
|
890
|
-
window.knapsack.getDemoApp = ({ ${
|
856
|
+
window.knapsack.getDemoApp = ({ ${[...importMap.keys()].join(", ")} }) => {
|
891
857
|
${demoApp}
|
892
858
|
return ${demoAppName}
|
893
859
|
}
|
@@ -909,7 +875,7 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
|
|
909
875
|
<script type="application/javascript">${code}</script>
|
910
876
|
<div id="render-root" class="knapsack-pattern-direct-parent" data-dev-note="Knapsack React Template Wrapper"></div>
|
911
877
|
${this.assets.map((asset) => `<script src="${asset}"></script>`).join("\n")}
|
912
|
-
${
|
878
|
+
${RendererWebpackBase.createHtmlTagsForAssetPaths({
|
913
879
|
assets: this.getWebpackAssetPaths(),
|
914
880
|
// 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
|
915
881
|
scriptTagsAreAsync: false
|
@@ -934,11 +900,9 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
|
|
934
900
|
let attempt = 0;
|
935
901
|
while (true) {
|
936
902
|
try {
|
937
|
-
const { absolutePath, exists } = await
|
938
|
-
|
939
|
-
|
940
|
-
resolveFromDir: this.dataDir
|
941
|
-
});
|
903
|
+
const { absolutePath, exists } = await this.resolvePath(
|
904
|
+
opt.demo.templateInfo.path
|
905
|
+
);
|
942
906
|
if (!exists) {
|
943
907
|
throw new Error(
|
944
908
|
`Template demo file does not exist: ${absolutePath}`
|
@@ -955,9 +919,12 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
|
|
955
919
|
await sleep(waitTime);
|
956
920
|
}
|
957
921
|
}
|
958
|
-
const [templateFileContents, { usage,
|
922
|
+
const [templateFileContents, { usage, importMap }] = await Promise.all([
|
959
923
|
readFile2(templateDemoPath),
|
960
|
-
this.getUsageAndImports(
|
924
|
+
this.getUsageAndImports({
|
925
|
+
...opt,
|
926
|
+
importMap: /* @__PURE__ */ new Map()
|
927
|
+
})
|
961
928
|
]);
|
962
929
|
const demoApp = await getDemoAppUsage({
|
963
930
|
children: usage
|
@@ -965,15 +932,18 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
|
|
965
932
|
const results = await this.prepClientRenderResults({
|
966
933
|
usage: templateFileContents,
|
967
934
|
demoApp,
|
968
|
-
|
969
|
-
|
935
|
+
renderOptions: opt,
|
936
|
+
importMap
|
970
937
|
});
|
971
938
|
return results;
|
972
939
|
}
|
973
940
|
if (opt.demo?.type === "data") {
|
974
|
-
const { usage,
|
975
|
-
|
976
|
-
|
941
|
+
const { usage, importMap } = await this.getUsageAndImports({
|
942
|
+
...opt,
|
943
|
+
importMap: /* @__PURE__ */ new Map()
|
944
|
+
});
|
945
|
+
const importCode = RendererWebpackBase.createJsImportCodeBlock({
|
946
|
+
importMap
|
977
947
|
});
|
978
948
|
const [demoAppUsage, demoApp] = await Promise.all([
|
979
949
|
getDemoAppUsage({
|
@@ -988,32 +958,40 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
|
|
988
958
|
return this.prepClientRenderResults({
|
989
959
|
demoApp,
|
990
960
|
usage: demoAppUsage,
|
991
|
-
|
992
|
-
|
961
|
+
renderOptions: opt,
|
962
|
+
importMap
|
993
963
|
});
|
994
964
|
}
|
995
965
|
}, "render");
|
996
966
|
getUsageAndImports = /* @__PURE__ */ __name(async ({
|
997
|
-
|
998
|
-
|
999
|
-
|
1000
|
-
|
967
|
+
patternId,
|
968
|
+
templateId,
|
969
|
+
demo,
|
970
|
+
state,
|
971
|
+
importMap
|
1001
972
|
}) => {
|
1002
|
-
if (demo
|
973
|
+
if (!demo) {
|
974
|
+
throw new Error(
|
975
|
+
`No demo provided while rendering ${patternId} ${templateId}`
|
976
|
+
);
|
977
|
+
}
|
978
|
+
const pattern = state.patterns[patternId];
|
979
|
+
if (!pattern) {
|
980
|
+
throw new Error(`Could not find pattern: ${patternId}`);
|
981
|
+
}
|
982
|
+
if (demo.type === "data") {
|
983
|
+
const template = pattern.templates.find((t) => t.id === templateId);
|
984
|
+
if (!template) {
|
985
|
+
throw new Error(`Could not find template: ${templateId}`);
|
986
|
+
}
|
1003
987
|
const {
|
1004
988
|
data: { props, slots, slotsOptionsComputed }
|
1005
989
|
} = demo;
|
1006
|
-
const
|
1007
|
-
|
1008
|
-
|
990
|
+
const { name: templateName } = await this.addUniqueValueToImportMap({
|
991
|
+
importMap,
|
992
|
+
path: template.path,
|
993
|
+
alias: template.alias || "default"
|
1009
994
|
});
|
1010
|
-
if (!importInfo) {
|
1011
|
-
throw new Error(
|
1012
|
-
`Could not find import for pattern-template: ${pattern.id}-${template.id}`
|
1013
|
-
);
|
1014
|
-
}
|
1015
|
-
const { type, name: templateName } = importInfo.importInfo;
|
1016
|
-
const importInfos = [importInfo];
|
1017
995
|
const children = [];
|
1018
996
|
const extraProps = [];
|
1019
997
|
if (slots) {
|
@@ -1023,6 +1001,7 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
|
|
1023
1001
|
const slotItems = slots[slotName];
|
1024
1002
|
const slotItemsUsages = await Promise.all(
|
1025
1003
|
slotItems.filter((slotItem) => {
|
1004
|
+
if (!slotItem) return false;
|
1026
1005
|
if (slotItem.type !== "text") {
|
1027
1006
|
if (!slotItem.patternId) return false;
|
1028
1007
|
if (!slotItem.templateId) return false;
|
@@ -1038,31 +1017,39 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
|
|
1038
1017
|
}
|
1039
1018
|
return slotItem.text;
|
1040
1019
|
}
|
1041
|
-
const slotPattern = patternManifest.getPattern(
|
1042
|
-
slotItem.patternId
|
1043
|
-
);
|
1044
|
-
const slotTemplate = slotPattern.templates.find(
|
1045
|
-
(t) => t.id === slotItem.templateId
|
1046
|
-
);
|
1047
1020
|
if (slotItem.type === "template-reference") {
|
1048
|
-
const
|
1049
|
-
|
1050
|
-
|
1051
|
-
|
1021
|
+
const slottedTemplate = state.patterns[slotItem.patternId]?.templates.find((t) => t.id === slotItem.templateId);
|
1022
|
+
if (!slottedTemplate) {
|
1023
|
+
throw new Error(
|
1024
|
+
`Could not find slotted template: ${slotItem.patternId} ${slotItem.templateId}`
|
1025
|
+
);
|
1026
|
+
}
|
1027
|
+
const templateRefImport = await this.addUniqueValueToImportMap({
|
1028
|
+
importMap,
|
1029
|
+
path: slottedTemplate.path,
|
1030
|
+
alias: slottedTemplate.alias || "default"
|
1052
1031
|
});
|
1053
|
-
|
1054
|
-
return usage2;
|
1032
|
+
return templateRefImport.name;
|
1055
1033
|
}
|
1056
1034
|
if (slotItem.type === "template-demo") {
|
1057
|
-
const
|
1058
|
-
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1035
|
+
const thisDemo = slotItem.demo;
|
1036
|
+
if (!thisDemo) {
|
1037
|
+
throw new Error(
|
1038
|
+
`Could not find slotted template demo ${JSON.stringify(
|
1039
|
+
slotItem
|
1040
|
+
)}`
|
1041
|
+
);
|
1042
|
+
}
|
1043
|
+
const { usage: usage2 } = await this.getUsageAndImports({
|
1044
|
+
patternId: thisDemo.patternId,
|
1045
|
+
templateId: thisDemo.templateId,
|
1046
|
+
demo: thisDemo,
|
1047
|
+
state,
|
1048
|
+
importMap
|
1062
1049
|
});
|
1063
|
-
importInfos.push(...imports);
|
1064
1050
|
return usage2;
|
1065
1051
|
}
|
1052
|
+
const _exhaustiveCheck2 = slotItem;
|
1066
1053
|
throw new Error(
|
1067
1054
|
`Unknown slot item: ${JSON.stringify(slotItem)}`
|
1068
1055
|
);
|
@@ -1121,50 +1108,25 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
|
|
1121
1108
|
});
|
1122
1109
|
return {
|
1123
1110
|
usage,
|
1124
|
-
|
1111
|
+
importMap
|
1125
1112
|
};
|
1126
1113
|
}
|
1127
|
-
if (demo
|
1128
|
-
const
|
1129
|
-
|
1130
|
-
|
1131
|
-
|
1114
|
+
if (demo.type === "template") {
|
1115
|
+
const { templateInfo } = demo;
|
1116
|
+
const { name: templateName } = await this.addUniqueValueToImportMap({
|
1117
|
+
importMap,
|
1118
|
+
path: templateInfo.path,
|
1119
|
+
alias: templateInfo.alias || "default"
|
1132
1120
|
});
|
1133
|
-
if (!importInfo) {
|
1134
|
-
throw new Error(
|
1135
|
-
`Could not find import for pattern-template-demo: ${pattern.id}-${template.id}-${demo.id}`
|
1136
|
-
);
|
1137
|
-
}
|
1138
|
-
const { type, name: templateName } = importInfo.importInfo;
|
1139
1121
|
const usage = await getUsage({ templateName });
|
1140
1122
|
return {
|
1141
1123
|
usage,
|
1142
|
-
|
1143
|
-
};
|
1144
|
-
}
|
1145
|
-
if (!demo) {
|
1146
|
-
const importInfo = this.getJsImport({
|
1147
|
-
patternId: pattern.id,
|
1148
|
-
templateId: template.id
|
1149
|
-
});
|
1150
|
-
if (!importInfo) {
|
1151
|
-
throw new Error(
|
1152
|
-
`Could not find import for pattern-template: ${pattern.id}-${template.id}`
|
1153
|
-
);
|
1154
|
-
}
|
1155
|
-
const { type, name: templateName } = importInfo.importInfo;
|
1156
|
-
return {
|
1157
|
-
/**
|
1158
|
-
* i.e. Given a React Component, `Button`, normally this would be `<Button>` with a demo, but since there is none this will just be a reference to it: `Button`
|
1159
|
-
* @see {KsSlotInfo['isTemplateReference']}
|
1160
|
-
* @see {SlottedTemplate}
|
1161
|
-
*/
|
1162
|
-
usage: templateName,
|
1163
|
-
imports: [importInfo]
|
1124
|
+
importMap
|
1164
1125
|
};
|
1165
1126
|
}
|
1127
|
+
const _exhaustiveCheck = demo;
|
1166
1128
|
throw new Error(
|
1167
|
-
`Unhandled demo type for ${
|
1129
|
+
`Unhandled demo type for ${patternId}-${templateId}: ${JSON.stringify(
|
1168
1130
|
demo
|
1169
1131
|
)}`
|
1170
1132
|
);
|
@@ -1187,8 +1149,8 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
|
|
1187
1149
|
}
|
1188
1150
|
return spec;
|
1189
1151
|
}, "inferSpec");
|
1190
|
-
watch = /* @__PURE__ */ __name(async (
|
1191
|
-
super.watch(
|
1152
|
+
watch = /* @__PURE__ */ __name(async () => {
|
1153
|
+
super.watch();
|
1192
1154
|
knapsackEvents.onPatternTemplateChanged(() => {
|
1193
1155
|
clearInferSpecCache();
|
1194
1156
|
});
|
@@ -1266,21 +1228,34 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
|
|
1266
1228
|
];
|
1267
1229
|
}, "alterTemplateMetaFiles");
|
1268
1230
|
getTemplateSuggestions = /* @__PURE__ */ __name(async ({
|
1269
|
-
newPath
|
1231
|
+
newPath,
|
1232
|
+
state
|
1270
1233
|
}) => {
|
1271
|
-
const
|
1272
|
-
|
1273
|
-
|
1274
|
-
|
1275
|
-
|
1276
|
-
|
1277
|
-
|
1278
|
-
|
1279
|
-
|
1280
|
-
|
1281
|
-
|
1234
|
+
const usedSuggestions = Object.values(state.patterns).reduce(
|
1235
|
+
(acc, { templateDemos, templates }) => {
|
1236
|
+
templates.forEach(({ path: path3, alias, templateLanguageId }) => {
|
1237
|
+
if (templateLanguageId !== this.id) return;
|
1238
|
+
acc.push({ path: path3, alias });
|
1239
|
+
});
|
1240
|
+
templateDemos.forEach(
|
1241
|
+
({ templateInfo: { path: path3, alias }, templateLanguageId }) => {
|
1242
|
+
if (templateLanguageId !== this.id) return;
|
1243
|
+
acc.push({ path: path3, alias });
|
1244
|
+
}
|
1245
|
+
);
|
1246
|
+
return acc;
|
1247
|
+
},
|
1248
|
+
[]
|
1249
|
+
);
|
1250
|
+
const codeSrcs = new Set(this.getCodeSrcs());
|
1251
|
+
codeSrcs.delete(this.demoWrapperPath);
|
1252
|
+
codeSrcs.delete(errorCatcherPath);
|
1282
1253
|
const allPaths = [
|
1283
|
-
.../* @__PURE__ */ new Set([
|
1254
|
+
.../* @__PURE__ */ new Set([
|
1255
|
+
newPath,
|
1256
|
+
...Object.keys(this.pkgPathAliases || {}),
|
1257
|
+
...codeSrcs
|
1258
|
+
])
|
1284
1259
|
];
|
1285
1260
|
const allSuggestions = await Promise.all(
|
1286
1261
|
allPaths.map(async (path3) => {
|
@@ -1288,7 +1263,7 @@ var KnapsackReactRenderer = class _KnapsackReactRenderer extends RendererWebpack
|
|
1288
1263
|
try {
|
1289
1264
|
const { exports, errorMsg } = await getJsExportNames2({
|
1290
1265
|
path: path3,
|
1291
|
-
resolveFromDir: dataDir,
|
1266
|
+
resolveFromDir: this.dataDir,
|
1292
1267
|
pkgPathAliases: this.pkgPathAliases
|
1293
1268
|
});
|
1294
1269
|
if (errorMsg) {
|