create-sitecore-jss 22.2.0-canary.8 → 22.2.0-canary.80

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.
Files changed (119) hide show
  1. package/dist/bin.js +17 -43
  2. package/dist/common/processes/next.js +4 -1
  3. package/dist/common/processes/transform.js +2 -12
  4. package/dist/common/prompts/base.js +0 -13
  5. package/dist/common/prompts/proxy.js +35 -0
  6. package/dist/common/prompts/sxp.js +16 -2
  7. package/dist/common/utils/helpers.js +27 -2
  8. package/dist/init-runner.js +1 -1
  9. package/dist/initializers/angular/prompts.js +4 -4
  10. package/dist/initializers/angular-xmcloud/index.js +21 -7
  11. package/dist/initializers/node-xmcloud-proxy/index.js +2 -1
  12. package/dist/templates/angular/.env +4 -5
  13. package/dist/templates/angular/.eslintrc +1 -0
  14. package/dist/templates/angular/gitignore +5 -0
  15. package/dist/templates/angular/package.json +1 -3
  16. package/dist/templates/angular/scripts/config/plugins/fallback.ts +0 -1
  17. package/dist/templates/angular/scripts/generate-component-factory/index.ts +45 -0
  18. package/dist/templates/angular/scripts/generate-component-factory/plugins/component-factory.ts +147 -0
  19. package/dist/templates/angular/scripts/generate-component-factory/plugins/components.ts +19 -0
  20. package/dist/templates/angular/scripts/generate-component-factory/plugins/packages.ts +29 -0
  21. package/dist/templates/angular/scripts/generate-component-factory/template.ts +46 -0
  22. package/dist/templates/angular/scripts/generate-config.ts +25 -6
  23. package/dist/templates/angular/scripts/generate-plugins.ts +5 -0
  24. package/dist/templates/angular/scripts/update-graphql-fragment-data.ts +21 -30
  25. package/dist/templates/angular/server.bundle.ts +3 -23
  26. package/dist/templates/angular/server.exports.ts +13 -0
  27. package/dist/templates/angular/src/app/JssState.ts +2 -9
  28. package/dist/templates/angular/src/app/app.module.ts +5 -4
  29. package/dist/templates/angular/src/app/app.server.module.ts +9 -6
  30. package/dist/templates/angular/src/app/components/gitignore +1 -1
  31. package/dist/templates/angular/src/app/i18n/jss-translation-client-loader.service.ts +15 -7
  32. package/dist/templates/angular/src/app/i18n/jss-translation-server-loader.service.ts +14 -2
  33. package/dist/templates/angular/src/app/jss-context.server-side.service.ts +4 -2
  34. package/dist/templates/angular/src/app/jss-context.service.ts +14 -11
  35. package/dist/templates/angular/src/app/jss-graphql.service.ts +7 -7
  36. package/dist/templates/angular/src/app/layout/jss-layout.service.ts +2 -2
  37. package/dist/templates/angular/src/app/lib/dictionary-service-factory.ts +4 -1
  38. package/dist/templates/angular/src/app/lib/graphql-client-factory/config.ts +21 -0
  39. package/dist/templates/angular/src/app/lib/graphql-client-factory/index.ts +16 -0
  40. package/dist/templates/angular/src/app/lib/layout-service-factory.ts +1 -1
  41. package/dist/templates/angular/src/app/routing/layout/layout.component.ts +10 -9
  42. package/dist/templates/angular/src/environments/gitignore +2 -1
  43. package/dist/templates/angular-sxp/.env +2 -0
  44. package/dist/templates/angular-sxp/scripts/config/plugins/disconnected.ts +4 -2
  45. package/dist/templates/angular-sxp/src/app/components/graph-ql-layout/graph-ql-layout.component.ts +1 -1
  46. package/dist/templates/angular-xmcloud/.env +15 -2
  47. package/dist/templates/angular-xmcloud/angular.json +0 -1
  48. package/dist/templates/angular-xmcloud/package.json +2 -0
  49. package/dist/templates/angular-xmcloud/scripts/bootstrap.ts +28 -0
  50. package/dist/templates/angular-xmcloud/scripts/config/plugins/xmcloud.ts +16 -0
  51. package/dist/templates/angular-xmcloud/scripts/generate-component-factory/plugins/packages.ts +34 -0
  52. package/dist/templates/angular-xmcloud/scripts/generate-component-factory/template.ts +57 -0
  53. package/dist/templates/angular-xmcloud/scripts/generate-metadata.ts +25 -0
  54. package/dist/templates/angular-xmcloud/server.exports.ts +24 -0
  55. package/dist/templates/angular-xmcloud/src/app/components/app-components.shared.module.ts +21 -0
  56. package/dist/templates/angular-xmcloud/src/app/components/column-splitter/column-splitter.component.html +5 -0
  57. package/dist/templates/angular-xmcloud/src/app/components/column-splitter/column-splitter.component.ts +40 -0
  58. package/dist/templates/angular-xmcloud/src/app/components/container/container.component.html +2 -3
  59. package/dist/templates/angular-xmcloud/src/app/components/image/image.component.html +36 -0
  60. package/dist/templates/angular-xmcloud/src/app/components/image/image.component.ts +67 -0
  61. package/dist/templates/angular-xmcloud/src/app/components/link-list/link-list.component.html +15 -0
  62. package/dist/templates/angular-xmcloud/src/app/components/link-list/link-list.component.ts +41 -0
  63. package/dist/templates/angular-xmcloud/src/app/components/navigation/navigation-item.component.html +23 -0
  64. package/dist/templates/angular-xmcloud/src/app/components/navigation/navigation-item.component.ts +65 -0
  65. package/dist/templates/angular-xmcloud/src/app/components/navigation/navigation.component.html +21 -0
  66. package/dist/templates/angular-xmcloud/src/app/components/navigation/navigation.component.ts +49 -0
  67. package/dist/templates/angular-xmcloud/src/app/components/page-content/page-content.component.html +5 -0
  68. package/dist/templates/angular-xmcloud/src/app/components/page-content/page-content.component.ts +39 -0
  69. package/dist/templates/angular-xmcloud/src/app/components/partial-design-dynamic-placeholder/partial-design-dynamic-placeholder.component.html +1 -0
  70. package/dist/templates/angular-xmcloud/src/app/components/partial-design-dynamic-placeholder/partial-design-dynamic-placeholder.component.ts +15 -0
  71. package/dist/templates/angular-xmcloud/src/app/components/promo/promo.component.html +21 -0
  72. package/dist/templates/angular-xmcloud/src/app/components/promo/promo.component.ts +13 -0
  73. package/dist/templates/angular-xmcloud/src/app/components/richtext/richtext.component.html +7 -12
  74. package/dist/templates/angular-xmcloud/src/app/components/richtext/richtext.component.ts +6 -1
  75. package/dist/templates/angular-xmcloud/src/app/components/row-splitter/row-splitter.component.html +11 -0
  76. package/dist/templates/angular-xmcloud/src/app/components/row-splitter/row-splitter.component.ts +35 -0
  77. package/dist/templates/angular-xmcloud/src/app/components/sxa.component.ts +3 -3
  78. package/dist/templates/angular-xmcloud/src/app/components/title/title.component.html +10 -0
  79. package/dist/templates/angular-xmcloud/src/app/components/title/title.component.ts +56 -0
  80. package/dist/templates/angular-xmcloud/src/app/jss-link.service.ts +55 -0
  81. package/dist/templates/angular-xmcloud/src/app/lib/config.ts +2 -0
  82. package/dist/templates/angular-xmcloud/src/app/lib/graphql-client-factory/config.ts +58 -0
  83. package/dist/templates/angular-xmcloud/src/app/routing/layout/layout.component.html +38 -0
  84. package/dist/templates/angular-xmcloud/src/app/routing/layout/layout.component.ts +104 -0
  85. package/dist/templates/angular-xmcloud/src/app/routing/scripts/cdp-page-view.component.ts +77 -0
  86. package/dist/templates/angular-xmcloud/src/app/routing/scripts/cloud-sdk-init.component.ts +48 -0
  87. package/dist/templates/angular-xmcloud/src/app/routing/scripts/scripts.component.html +5 -0
  88. package/dist/templates/angular-xmcloud/src/app/routing/scripts/scripts.module.ts +12 -0
  89. package/dist/templates/angular-xmcloud/src/assets/styles/basic/_header.scss +3 -1
  90. package/dist/templates/angular-xmcloud/src/assets/styles/main.scss +10 -0
  91. package/dist/templates/angular-xmcloud/src/assets/styles/sass/components/_component-image.scss +1 -1
  92. package/dist/templates/nextjs/package.json +1 -1
  93. package/dist/templates/nextjs/scripts/config/plugins/fallback.ts +0 -1
  94. package/dist/templates/nextjs/scripts/generate-config.ts +8 -1
  95. package/dist/templates/nextjs/src/lib/page-props-factory/plugins/component-props.ts +2 -1
  96. package/dist/templates/nextjs-styleguide/scripts/config/plugins/disconnected.ts +1 -0
  97. package/dist/templates/nextjs-sxa/src/assets/sass/components/_component-image.scss +1 -1
  98. package/dist/templates/nextjs-sxa/src/assets/sass/components/common/_alignment.scss +34 -7
  99. package/dist/templates/nextjs-sxa/src/components/Container.tsx +6 -14
  100. package/dist/templates/nextjs-xmcloud/src/lib/page-props-factory/plugins/component-themes.ts +2 -1
  101. package/dist/templates/nextjs-xmcloud/src/lib/page-props-factory/plugins/preview-mode.ts +2 -1
  102. package/dist/templates/node-headless-ssr-experience-edge/gitignore +19 -0
  103. package/dist/templates/node-headless-ssr-proxy/gitignore +19 -0
  104. package/dist/templates/node-headless-ssr-proxy/src/config.ts +3 -3
  105. package/dist/templates/node-headless-ssr-proxy/src/httpAgents.ts +2 -2
  106. package/dist/templates/node-headless-ssr-proxy/src/index.ts +9 -2
  107. package/dist/templates/node-xmcloud-proxy/.env +7 -1
  108. package/dist/templates/node-xmcloud-proxy/README.md +1 -1
  109. package/dist/templates/node-xmcloud-proxy/gitignore +33 -0
  110. package/dist/templates/node-xmcloud-proxy/package.json +3 -2
  111. package/dist/templates/node-xmcloud-proxy/src/config.ts +9 -3
  112. package/dist/templates/node-xmcloud-proxy/src/index.ts +54 -5
  113. package/dist/templates/node-xmcloud-proxy/src/types.ts +10 -42
  114. package/dist/templates/react/scripts/generate-config.js +10 -3
  115. package/dist/templates/vue/scripts/generate-config.js +5 -0
  116. package/package.json +2 -2
  117. package/dist/templates/angular/scripts/generate-component-factory.ts +0 -166
  118. package/dist/templates/angular/src/app/lib/graphql-client-factory.ts +0 -28
  119. package/dist/templates/angular-xmcloud/src/app/lib/graphql-client-factory.ts +0 -44
@@ -1,7 +1,7 @@
1
1
  import express from 'express';
2
2
  import compression from 'compression';
3
3
  import 'dotenv/config';
4
- import scProxy from '@sitecore-jss/sitecore-jss-proxy';
4
+ import { headlessProxy } from '@sitecore-jss/sitecore-jss-proxy';
5
5
  import { config } from './config';
6
6
  //import { cacheMiddleware } from './cacheMiddleware';
7
7
 
@@ -42,7 +42,14 @@ server.use((req, _res, next) => {
42
42
  });
43
43
 
44
44
  // For any other requests, we render app routes server-side and return them
45
- server.use('*', scProxy(config.serverBundle.renderView, config, config.serverBundle.parseRouteUrl));
45
+ server.use(
46
+ '*',
47
+ headlessProxy.middleware(
48
+ config.serverBundle.renderView,
49
+ config,
50
+ config.serverBundle.parseRouteUrl
51
+ )
52
+ );
46
53
 
47
54
  server.listen(port, () => {
48
55
  console.log(`server listening on port ${port}!`);
@@ -1,4 +1,10 @@
1
- # Your proxy port (default: 3001)
1
+ # To secure the Sitecore editor endpoint exposed by your proxy app
2
+ # (`/api/editing/render` by default), a secret token is used.
3
+ # The environment variable is used by `editingRouter`
4
+ # We recommend an alphanumeric value of at least 16 characters.
5
+ JSS_EDITING_SECRET=
6
+
7
+ # Your proxy port (default: 3000)
2
8
  PROXY_PORT=
3
9
 
4
10
  # Your proxy server bundle path
@@ -33,4 +33,4 @@ The following environment variables can be set to configure the Proxy sample ins
33
33
  1. Run `npm run start`
34
34
 
35
35
  You should be able to see the following message:
36
- `server listening on port 3001!`.
36
+ `server listening on port 3000!`.
@@ -0,0 +1,33 @@
1
+ # See http://help.github.com/ignore-files/ for more about ignoring files.
2
+
3
+ # compiled output
4
+ /dist
5
+
6
+ # dependencies
7
+ /node_modules
8
+
9
+ # local env files
10
+ .env.local
11
+ .env.*.local
12
+
13
+ # IDE - VSCode
14
+ .vscode/*
15
+ !.vscode/settings.json
16
+ !.vscode/tasks.json
17
+ !.vscode/launch.json
18
+ !.vscode/extensions.json
19
+
20
+ # misc
21
+ /connect.lock
22
+ /libpeerconnection.log
23
+ npm-debug.log
24
+ testem.log
25
+ /typings
26
+
27
+ # System Files
28
+ .DS_Store
29
+ Thumbs.db
30
+
31
+ # deploy platforms
32
+ .vercel
33
+ .netlify
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-xmcloud-sample",
3
- "version": "22.1.0-canary",
3
+ "version": "22.2.0-canary",
4
4
  "description": "Node XM Cloud Proxy sample for running XM Cloud JSS SPA apps",
5
5
  "author": {
6
6
  "name": "Sitecore Corporation",
@@ -10,13 +10,14 @@
10
10
  "start": "ts-node ./src/index.ts"
11
11
  },
12
12
  "dependencies": {
13
+ "@sitecore-jss/sitecore-jss-proxy": "~22.2.0-canary",
13
14
  "compression": "^1.7.4",
14
15
  "express": "^4.18.2",
15
16
  "dotenv": "^16.0.3",
16
17
  "http-proxy-middleware": "^3.0.0"
17
18
  },
18
19
  "devDependencies": {
19
- "@sitecore-jss/sitecore-jss": "~22.1.0-canary",
20
+ "@sitecore-jss/sitecore-jss": "~22.2.0-canary",
20
21
  "@types/compression": "^1.7.2",
21
22
  "@types/express": "^4.17.17",
22
23
  "ts-node": "^10.9.1",
@@ -3,9 +3,15 @@ import { Config, ServerBundle } from './types';
3
3
  /**
4
4
  * The server.bundle.js file from your pre-built SPA app.
5
5
  */
6
- const bundlePath = process.env.PROXY_BUNDLE_PATH || `../dist/server.bundle`;
6
+ const bundlePath = process.env.PROXY_BUNDLE_PATH || '../dist/server.bundle';
7
7
 
8
- const serverBundle: ServerBundle = require(bundlePath);
8
+ let serverBundle: ServerBundle;
9
+
10
+ try {
11
+ serverBundle = require(bundlePath);
12
+ } catch (error) {
13
+ throw new Error(`ERROR: The server.bundle.js error. ${error}`);
14
+ }
9
15
 
10
16
  export const config: Config = {
11
17
  /**
@@ -15,5 +21,5 @@ export const config: Config = {
15
21
  /**
16
22
  * Port which will be used when start the proxy
17
23
  */
18
- port: process.env.PROXY_PORT || 3001,
24
+ port: process.env.PROXY_PORT || 3000,
19
25
  };
@@ -3,6 +3,8 @@ import express, { Response } from 'express';
3
3
  import compression from 'compression';
4
4
  import { createProxyMiddleware } from 'http-proxy-middleware';
5
5
  import { debug } from '@sitecore-jss/sitecore-jss';
6
+ import { editingRouter } from '@sitecore-jss/sitecore-jss-proxy';
7
+ import { healthCheck } from '@sitecore-jss/sitecore-jss-proxy';
6
8
  import { config } from './config';
7
9
 
8
10
  const server = express();
@@ -21,12 +23,12 @@ const requiredProperties = [
21
23
  'renderView',
22
24
  'parseRouteUrl',
23
25
  'clientFactory',
24
- 'siteName',
26
+ 'getClientFactoryConfig',
25
27
  'defaultLanguage',
26
28
  'layoutServiceFactory',
27
29
  'dictionaryServiceFactory',
28
- 'graphQLEndpointPath',
29
- 'graphQLEndpoint',
30
+ 'components',
31
+ 'metadata',
30
32
  ];
31
33
 
32
34
  const missingProperties = requiredProperties.filter((property) => !config.serverBundle[property]);
@@ -43,6 +45,31 @@ const layoutService = layoutServiceFactory.create();
43
45
 
44
46
  const dictionaryService = dictionaryServiceFactory.create();
45
47
 
48
+ const clientFactoryConfig = config.serverBundle.getClientFactoryConfig();
49
+
50
+ /**
51
+ * GraphQL endpoint resolution to meet the requirements of the http-proxy-middleware
52
+ */
53
+ const graphQLEndpoint = (() => {
54
+ try {
55
+ const graphQLEndpoint = new URL(clientFactoryConfig.endpoint);
56
+ // Browser request path to the proxy. Includes only the pathname.
57
+ const pathname = graphQLEndpoint.pathname;
58
+ // Target URL for the proxy. Can't include the query string.
59
+ const target = `${graphQLEndpoint.protocol}//${graphQLEndpoint.hostname}${pathname}`;
60
+
61
+ return {
62
+ target,
63
+ path: pathname,
64
+ };
65
+ } catch (error) {
66
+ throw new Error(
67
+ `ERROR: The serverBundle should export a getClientFactoryConfig function with valid GraphQL endpoint URL returned, current value is ${clientFactoryConfig.endpoint}. ` +
68
+ 'Please check your server bundle.'
69
+ );
70
+ }
71
+ })();
72
+
46
73
  /**
47
74
  * Parse requested url in order to detect current route and language
48
75
  * @param {string} reqRoute requested route
@@ -94,13 +121,35 @@ server.use(
94
121
  * Proxy browser GraphQL requests to the Sitecore GraphQL endpoint
95
122
  */
96
123
  server.use(
97
- config.serverBundle.graphQLEndpointPath,
124
+ graphQLEndpoint.path,
98
125
  createProxyMiddleware({
99
- target: config.serverBundle.graphQLEndpoint,
126
+ target: graphQLEndpoint.target,
100
127
  changeOrigin: true,
101
128
  })
102
129
  );
103
130
 
131
+ /**
132
+ * The health check endpoint
133
+ */
134
+ server.use(healthCheck());
135
+
136
+ /**
137
+ * Proxy editing requests through the editing router
138
+ */
139
+ server.use(
140
+ '/api/editing',
141
+ editingRouter({
142
+ config: {
143
+ components: config.serverBundle.components,
144
+ metadata: config.serverBundle.metadata,
145
+ },
146
+ render: {
147
+ clientFactory: config.serverBundle.clientFactory,
148
+ renderView,
149
+ },
150
+ })
151
+ );
152
+
104
153
  server.use(async (req, res) => {
105
154
  debug.proxy(`performing SSR for ${req.originalUrl}`);
106
155
 
@@ -1,55 +1,23 @@
1
- import { GraphQLRequestClientFactory } from '@sitecore-jss/sitecore-jss';
1
+ import {
2
+ GraphQLRequestClientFactory,
3
+ GraphQLRequestClientFactoryConfig,
4
+ } from '@sitecore-jss/sitecore-jss';
2
5
  import { DictionaryService } from '@sitecore-jss/sitecore-jss/i18n';
3
- import { LayoutService, LayoutServiceData } from '@sitecore-jss/sitecore-jss/layout';
4
-
5
- interface ServerResponse {
6
- /**
7
- * The rendered HTML to return to the client
8
- */
9
- html: string;
10
- /**
11
- * Set the HTTP status code. If not set, the status code returned from Layout Service is returned.
12
- */
13
- status?: number;
14
- /**
15
- * Sets a redirect URL, causing the reply to send a HTTP redirect instead of the HTML content.
16
- * Note: when using this you must set the status code to 301 or 302.
17
- */
18
- redirect?: string;
19
- }
20
-
21
- declare type AppRenderer = (
22
- callback: (error: Error | null, result: ServerResponse | null) => void,
23
- path: string,
24
- /**
25
- * Data returned by Layout Service. If the route does not exist, null.
26
- */
27
- layoutData: LayoutServiceData,
28
- viewBag: {
29
- [key: string]: unknown;
30
- dictionary: { [key: string]: string };
31
- }
32
- ) => void;
33
-
34
- declare type RouteUrlParser = (
35
- url: string
36
- ) => {
37
- sitecoreRoute?: string;
38
- lang?: string;
39
- qsParams?: string;
40
- };
6
+ import { Metadata } from '@sitecore-jss/sitecore-jss/utils';
7
+ import { LayoutService } from '@sitecore-jss/sitecore-jss/layout';
8
+ import { AppRenderer, RouteUrlParser } from '@sitecore-jss/sitecore-jss-proxy';
41
9
 
42
10
  export interface ServerBundle {
43
11
  [key: string]: unknown;
44
12
  renderView: AppRenderer;
45
13
  parseRouteUrl: RouteUrlParser;
46
14
  clientFactory: GraphQLRequestClientFactory;
47
- siteName: string;
15
+ getClientFactoryConfig: () => GraphQLRequestClientFactoryConfig;
48
16
  defaultLanguage: string;
49
17
  layoutServiceFactory: { create: () => LayoutService };
50
18
  dictionaryServiceFactory: { create: () => DictionaryService };
51
- graphQLEndpointPath: string;
52
- graphQLEndpoint: string;
19
+ components: string[] | Map<string, unknown>;
20
+ metadata: Metadata;
53
21
  }
54
22
 
55
23
  export interface Config {
@@ -31,6 +31,11 @@ generateConfig();
31
31
  * NOTE! Any configs returned here will be written into the client-side JS bundle. DO NOT PUT SECRETS HERE.
32
32
  */
33
33
  function generateConfig() {
34
+ // Handle undefined values
35
+ Object.keys(defaultConfig).forEach((prop) => {
36
+ defaultConfig[prop] = defaultConfig[prop] || '';
37
+ });
38
+
34
39
  try {
35
40
  config = jssConfigFactory.create(defaultConfig);
36
41
  } catch (error) {
@@ -50,9 +55,11 @@ const config = {};\n`;
50
55
 
51
56
  // Set base configuration values, allowing override with environment variables
52
57
  Object.keys(config).forEach(prop => {
53
- configText += `config.${prop} = process.env.REACT_APP_${constantCase(prop)} || "${
54
- config[prop]?.trim()
55
- }";\n`;
58
+ // Handle undefined values
59
+ const value = config[prop] || '';
60
+ configText += `config.${prop} = process.env.REACT_APP_${constantCase(
61
+ prop
62
+ )} || "${value.trim()}";\n`;
56
63
  });
57
64
  configText += 'module.exports = config;';
58
65
 
@@ -33,6 +33,11 @@ module.exports = function generateConfig(configOverrides) {
33
33
  // and finally config passed in the configOverrides param wins.
34
34
  const config = Object.assign(defaultConfig, scjssConfig, packageJson, configOverrides);
35
35
 
36
+ // Handle undefined values
37
+ Object.keys(config).forEach((prop) => {
38
+ config[prop] = config[prop] || '';
39
+ });
40
+
36
41
  // The GraphQL endpoint is an example of making a _computed_ config setting
37
42
  // based on other config settings.
38
43
  const computedConfig = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-sitecore-jss",
3
- "version": "22.2.0-canary.8",
3
+ "version": "22.2.0-canary.80",
4
4
  "description": "Sitecore JSS initializer",
5
5
  "bin": "./dist/index.js",
6
6
  "scripts": {
@@ -63,5 +63,5 @@
63
63
  "ts-node": "^10.9.1",
64
64
  "typescript": "~4.9.5"
65
65
  },
66
- "gitHead": "755525262fcb998452876d2f28a57d130e29731c"
66
+ "gitHead": "0ecca7f10712e929cfe4375ccfbd479020cd30fc"
67
67
  }
@@ -1,166 +0,0 @@
1
- import * as fs from 'fs';
2
- const path = require('path');
3
- const chokidar = require('chokidar');
4
-
5
- /*
6
- COMPONENT FACTORY GENERATION
7
- Generates the /src/app/components/app-components.module.ts file which maps Angular components
8
- to JSS components.
9
-
10
- The component factory module defines a mapping between a string component name and a Angular component instance.
11
- When the Sitecore Layout service returns a layout definition, it returns named components.
12
- This mapping is used to construct the component hierarchy for the layout.
13
-
14
- NOTE: this script can run in two modes. The default mode, the component factory file is written once.
15
- But if `--watch` is a process argument, the component factory source folder will be watched,
16
- and the componentFactory.js rewritten on added or deleted files.
17
- This is used during `jss start` to pick up new or removed components at runtime.
18
- */
19
-
20
- export interface PackageDefinition {
21
- name: string;
22
- components: {
23
- moduleName: string;
24
- componentName: string;
25
- }[];
26
- }
27
-
28
- const componentFactoryPath = path.resolve('src/app/components/app-components.module.ts');
29
- const componentRootPath = 'src/app/components';
30
-
31
- const isWatch = process.argv.some((arg) => arg === '--watch');
32
-
33
- if (isWatch) {
34
- watchComponentFactory();
35
- } else {
36
- writeComponentFactory();
37
- }
38
-
39
- function watchComponentFactory() {
40
- console.log(`Watching for changes to component factory sources in ${componentRootPath}...`);
41
-
42
- chokidar
43
- .watch(componentRootPath, { ignoreInitial: true, awaitWriteFinish: true })
44
- .on('add', writeComponentFactory)
45
- .on('unlink', writeComponentFactory);
46
- }
47
-
48
- function writeComponentFactory() {
49
- const componentFactory = generateComponentFactory();
50
-
51
- console.log(`Writing component factory to ${componentFactoryPath}`);
52
-
53
- fs.writeFileSync(componentFactoryPath, componentFactory, { encoding: 'utf8' });
54
- }
55
-
56
- function generateComponentFactory() {
57
- // By convention, we expect to find Angular components
58
- // under /src/app/components/component-name/component-name.component.ts
59
- // If a component-name.module.ts file exists, we will treat it as lazy loaded.
60
- // If you'd like to use your own convention, encode it below.
61
- // NOTE: generating the component factory module is also totally optional,
62
- // and it can be maintained manually if preferred.
63
-
64
- const imports: string[] = [];
65
- /**
66
- * You can specify components which you want to import from external/internal packages
67
- * in format:
68
- * {
69
- * name: 'package name',
70
- * components: [
71
- * {
72
- * componentName: 'component name', // component rendering name,
73
- * moduleName: 'module name' // component name to import from the package
74
- * }
75
- * ]
76
- * }
77
- */
78
- const packages: PackageDefinition[] = [];
79
- const registrations: string[] = [];
80
- const lazyRegistrations: string[] = [];
81
- const declarations: string[] = [];
82
-
83
- packages.forEach((p) => {
84
- const variables = p.components
85
- .map((c) => {
86
- registrations.push(`{ name: '${c.componentName}', type: ${c.moduleName} },`);
87
- declarations.push(`${c.moduleName},`);
88
-
89
- return c.moduleName;
90
- })
91
- .join(', ');
92
- imports.push(`import { ${variables} } from '${p.name}'`);
93
- });
94
-
95
- fs.readdirSync(componentRootPath).forEach((componentFolder) => {
96
- // ignore ts files in component root folder
97
- if (componentFolder.endsWith('.ts') || componentFolder === '.gitignore') {
98
- return;
99
- }
100
-
101
- const componentFilePath = path.join(componentRootPath, componentFolder, `${componentFolder}.component.ts`);
102
-
103
- if (!fs.existsSync(componentFilePath)) {
104
- return;
105
- }
106
-
107
- const componentFileContents = fs.readFileSync(componentFilePath, 'utf8');
108
-
109
- // ASSUMPTION: your component should export a class directly that follows Angular conventions,
110
- // i.e. `export class FooComponent` - so we can detect the component's name for auto registration.
111
- const componentClassMatch = /export class (.+?)Component\b/g.exec(componentFileContents);
112
-
113
- if (componentClassMatch === null) {
114
- console.debug(`Component ${componentFilePath} did not seem to export a component class. It will be skipped.`);
115
- return;
116
- }
117
-
118
- const componentName = componentClassMatch[1];
119
- const importVarName = `${componentName}Component`;
120
-
121
- // check for lazy loading needs
122
- const moduleFilePath = path.join(componentRootPath, componentFolder, `${componentFolder}.module.ts`);
123
- const isLazyLoaded = fs.existsSync(moduleFilePath);
124
-
125
- if (isLazyLoaded) {
126
- console.debug(`Registering JSS component (lazy) ${componentName}`);
127
- lazyRegistrations.push(`{ path: '${componentName}', loadChildren: () => import('./${componentFolder}/${componentFolder}.module').then(m => m.${componentName}Module) },`);
128
- } else {
129
- console.debug(`Registering JSS component ${componentName}`);
130
- imports.push(`import { ${importVarName} } from './${componentFolder}/${componentFolder}.component';`);
131
- registrations.push(`{ name: '${componentName}', type: ${importVarName} },`);
132
- declarations.push(`${importVarName},`);
133
- }
134
- });
135
-
136
- return `// Do not edit this file, it is auto-generated at build time!
137
- // See scripts/generate-component-factory.js to modify the generation of this file.
138
- // Use app-components.shared.module.ts to modify the imports, etc of this module.
139
- // Note: code-generation is optional! See ./.gitignore for directions to remove it,
140
- // if you do not want it.
141
-
142
- import { NgModule } from '@angular/core';
143
- import { JssModule } from '@sitecore-jss/sitecore-jss-angular';
144
- import { AppComponentsSharedModule } from './app-components.shared.module';
145
- ${imports.join('\n')}
146
-
147
- @NgModule({
148
- imports: [
149
- AppComponentsSharedModule,
150
- JssModule.withComponents([
151
- ${registrations.join('\n ')}
152
- ], [
153
- ${lazyRegistrations.join('\n ')}
154
- ]),
155
- ],
156
- exports: [
157
- JssModule,
158
- AppComponentsSharedModule,
159
- ],
160
- declarations: [
161
- ${declarations.join('\n ')}
162
- ],
163
- })
164
- export class AppComponentsModule { }
165
- `;
166
- }
@@ -1,28 +0,0 @@
1
- import {
2
- GraphQLRequestClientFactoryConfig,
3
- GraphQLRequestClient,
4
- } from '@sitecore-jss/sitecore-jss-angular';
5
- import { environment as env } from '../../environments/environment';
6
-
7
- // The GraphQLRequestClientFactory serves as the central hub for executing GraphQL requests within the application
8
-
9
- /**
10
- * Creates a new GraphQLRequestClientFactory instance
11
- * @returns GraphQLRequestClientFactory instance
12
- */
13
- export const createGraphQLClientFactory = () => {
14
- let clientConfig: GraphQLRequestClientFactoryConfig;
15
-
16
- if (env.graphQLEndpoint && env.sitecoreApiKey) {
17
- clientConfig = {
18
- endpoint: env.graphQLEndpoint,
19
- apiKey: env.sitecoreApiKey,
20
- };
21
- } else {
22
- throw new Error('Please configure your graphQLEndpoint and sitecoreApiKey.');
23
- }
24
-
25
- return GraphQLRequestClient.createClientFactory(clientConfig);
26
- };
27
-
28
- export const clientFactory = createGraphQLClientFactory();
@@ -1,44 +0,0 @@
1
- import {
2
- GraphQLRequestClientFactoryConfig,
3
- GraphQLRequestClient,
4
- } from '@sitecore-jss/sitecore-jss-angular';
5
- import { environment as env } from '../../environments/environment';
6
-
7
- // The GraphQLRequestClientFactory serves as the central hub for executing GraphQL requests within the application
8
-
9
- /**
10
- * Creates a new GraphQLRequestClientFactory instance
11
- * @returns GraphQLRequestClientFactory instance
12
- */
13
- export const createGraphQLClientFactory = () => {
14
- let clientConfig: GraphQLRequestClientFactoryConfig;
15
-
16
- // If we are in a production environment we are going to use Node XM Cloud proxy
17
- if (env.production === 'true') {
18
- if (env.proxyHost && env.sitecoreApiKey && env.graphQLEndpoint) {
19
- // Server side requests should go directly to the Sitecore, browser requests should go through the proxy.
20
- clientConfig = {
21
- endpoint:
22
- typeof window === 'undefined'
23
- ? env.graphQLEndpoint
24
- : `${env.proxyHost}${env.graphQLEndpointPath}`,
25
- apiKey: env.sitecoreApiKey,
26
- };
27
- } else {
28
- throw new Error('Please configure your proxyHost, sitecoreApiKey, graphQLEndpoint.');
29
- }
30
- } else {
31
- if (env.graphQLEndpoint && env.sitecoreApiKey) {
32
- clientConfig = {
33
- endpoint: env.graphQLEndpoint,
34
- apiKey: env.sitecoreApiKey,
35
- };
36
- } else {
37
- throw new Error('Please configure your graphQLEndpoint and sitecoreApiKey.');
38
- }
39
- }
40
-
41
- return GraphQLRequestClient.createClientFactory(clientConfig);
42
- };
43
-
44
- export const clientFactory = createGraphQLClientFactory();