@canva/cli 0.0.1-beta.26 → 0.0.1-beta.29

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 (56) hide show
  1. package/cli.js +469 -443
  2. package/package.json +7 -2
  3. package/templates/base/webpack.config.ts +12 -4
  4. package/templates/dam/eslint.config.mjs +1 -2
  5. package/templates/dam/src/config.ts +3 -0
  6. package/templates/dam/webpack.config.ts +12 -4
  7. package/templates/data_connector/README.md +84 -0
  8. package/templates/data_connector/declarations/declarations.d.ts +29 -0
  9. package/templates/data_connector/eslint.config.mjs +14 -0
  10. package/templates/data_connector/package.json +91 -0
  11. package/templates/data_connector/scripts/copy_env.ts +10 -0
  12. package/templates/data_connector/scripts/ssl/ssl.ts +131 -0
  13. package/templates/data_connector/scripts/start/app_runner.ts +201 -0
  14. package/templates/data_connector/scripts/start/context.ts +171 -0
  15. package/templates/data_connector/scripts/start/start.ts +46 -0
  16. package/templates/data_connector/scripts/start/tests/start.tests.ts +61 -0
  17. package/templates/data_connector/src/api/connect_client.ts +6 -0
  18. package/templates/data_connector/src/api/data_source.ts +96 -0
  19. package/templates/data_connector/src/api/data_sources/designs.tsx +253 -0
  20. package/templates/data_connector/src/api/data_sources/index.ts +4 -0
  21. package/templates/data_connector/src/api/data_sources/templates.tsx +287 -0
  22. package/templates/data_connector/src/api/fetch_data_table.ts +51 -0
  23. package/templates/data_connector/src/api/index.ts +4 -0
  24. package/templates/data_connector/src/api/oauth.ts +8 -0
  25. package/templates/data_connector/src/api/tests/data_source.test.tsx +99 -0
  26. package/templates/data_connector/src/app.tsx +24 -0
  27. package/templates/data_connector/src/components/app_error.tsx +15 -0
  28. package/templates/data_connector/src/components/footer.tsx +26 -0
  29. package/templates/data_connector/src/components/header.tsx +40 -0
  30. package/templates/data_connector/src/components/index.ts +3 -0
  31. package/templates/data_connector/src/components/inputs/messages.tsx +80 -0
  32. package/templates/data_connector/src/components/inputs/select_field.tsx +26 -0
  33. package/templates/data_connector/src/context/app_context.tsx +124 -0
  34. package/templates/data_connector/src/context/index.ts +2 -0
  35. package/templates/data_connector/src/context/use_app_context.ts +17 -0
  36. package/templates/data_connector/src/entrypoint.tsx +73 -0
  37. package/templates/data_connector/src/home.tsx +21 -0
  38. package/templates/data_connector/src/index.tsx +69 -0
  39. package/templates/data_connector/src/pages/data_source_config.tsx +9 -0
  40. package/templates/data_connector/src/pages/error.tsx +37 -0
  41. package/templates/data_connector/src/pages/index.ts +4 -0
  42. package/templates/data_connector/src/pages/login.tsx +145 -0
  43. package/templates/data_connector/src/pages/select_source.tsx +24 -0
  44. package/templates/data_connector/src/routes/index.ts +2 -0
  45. package/templates/data_connector/src/routes/protected_route.tsx +25 -0
  46. package/templates/data_connector/src/routes/routes.tsx +46 -0
  47. package/templates/data_connector/src/utils/data_params.ts +17 -0
  48. package/templates/data_connector/src/utils/data_table.ts +100 -0
  49. package/templates/data_connector/src/utils/fetch_result.ts +36 -0
  50. package/templates/data_connector/src/utils/index.ts +2 -0
  51. package/templates/data_connector/src/utils/tests/data_table.test.ts +133 -0
  52. package/templates/data_connector/styles/components.css +38 -0
  53. package/templates/data_connector/tsconfig.json +54 -0
  54. package/templates/data_connector/webpack.config.ts +270 -0
  55. package/templates/gen_ai/webpack.config.ts +12 -4
  56. package/templates/hello_world/webpack.config.ts +12 -4
@@ -0,0 +1,270 @@
1
+ import type { Configuration } from "webpack";
2
+ import { DefinePlugin, optimize } from "webpack";
3
+ import * as path from "path";
4
+ import * as TerserPlugin from "terser-webpack-plugin";
5
+ import { transform } from "@formatjs/ts-transformer";
6
+ import * as chalk from "chalk";
7
+ import { config } from "dotenv";
8
+ import { Configuration as DevServerConfiguration } from "webpack-dev-server";
9
+
10
+ config();
11
+
12
+ type DevConfig = {
13
+ port: number;
14
+ enableHmr: boolean;
15
+ enableHttps: boolean;
16
+ appOrigin?: string;
17
+ appId?: string; // Deprecated in favour of appOrigin
18
+ certFile?: string;
19
+ keyFile?: string;
20
+ };
21
+
22
+ export function buildConfig({
23
+ devConfig,
24
+ appEntry = path.join(process.cwd(), "src", "index.tsx"),
25
+ backendHost = process.env.CANVA_BACKEND_HOST,
26
+ // For IN_HARNESS, refer to the following docs for more information: https://www.canva.dev/docs/apps/mcp-server/harness-setup/
27
+ inHarness = process.env.IN_HARNESS === "true",
28
+ }: {
29
+ devConfig?: DevConfig;
30
+ appEntry?: string;
31
+ backendHost?: string;
32
+ inHarness?: boolean;
33
+ } = {}): Configuration & DevServerConfiguration {
34
+ const mode = devConfig ? "development" : "production";
35
+
36
+ if (!backendHost) {
37
+ console.error(
38
+ chalk.redBright.bold("BACKEND_HOST is undefined."),
39
+ `Refer to "Customizing the backend host" in the README.md for more information.`,
40
+ );
41
+ process.exit(-1);
42
+ } else if (backendHost.includes("localhost") && mode === "production") {
43
+ console.error(
44
+ chalk.redBright.bold(
45
+ "BACKEND_HOST should not be set to localhost for production builds!",
46
+ ),
47
+ `Refer to "Customizing the backend host" in the README.md for more information.`,
48
+ );
49
+ }
50
+
51
+ return {
52
+ mode,
53
+ context: path.resolve(process.cwd(), "./"),
54
+ entry: inHarness
55
+ ? {
56
+ harness: path.join(process.cwd(), "harness", "harness.tsx"),
57
+ init: path.join(process.cwd(), "harness", "init.ts"),
58
+ }
59
+ : {
60
+ app: appEntry,
61
+ },
62
+ target: "web",
63
+ resolve: {
64
+ alias: {
65
+ assets: path.resolve(process.cwd(), "assets"),
66
+ utils: path.resolve(process.cwd(), "utils"),
67
+ styles: path.resolve(process.cwd(), "styles"),
68
+ src: path.resolve(process.cwd(), "src"),
69
+ },
70
+ extensions: [".ts", ".tsx", ".js", ".css", ".svg", ".woff", ".woff2"],
71
+ },
72
+ infrastructureLogging: {
73
+ level: inHarness ? "info" : "none",
74
+ },
75
+ module: {
76
+ rules: [
77
+ {
78
+ test: /\.tsx?$/,
79
+ exclude: /node_modules/,
80
+ use: [
81
+ {
82
+ loader: "ts-loader",
83
+ options: {
84
+ transpileOnly: true,
85
+ getCustomTransformers() {
86
+ return {
87
+ before: [
88
+ transform({
89
+ overrideIdFn: "[sha512:contenthash:base64:6]",
90
+ }),
91
+ ],
92
+ };
93
+ },
94
+ },
95
+ },
96
+ ],
97
+ },
98
+ {
99
+ test: /\.css$/,
100
+ exclude: /node_modules/,
101
+ use: [
102
+ "style-loader",
103
+ {
104
+ loader: "css-loader",
105
+ options: {
106
+ modules: true,
107
+ },
108
+ },
109
+ {
110
+ loader: "postcss-loader",
111
+ options: {
112
+ postcssOptions: {
113
+ plugins: [require("cssnano")({ preset: "default" })],
114
+ },
115
+ },
116
+ },
117
+ ],
118
+ },
119
+ {
120
+ test: /\.(png|jpg|jpeg)$/i,
121
+ type: "asset/inline",
122
+ },
123
+ {
124
+ test: /\.(woff|woff2)$/,
125
+ type: "asset/inline",
126
+ },
127
+ {
128
+ test: /\.svg$/,
129
+ oneOf: [
130
+ {
131
+ issuer: /\.[jt]sx?$/,
132
+ resourceQuery: /react/, // *.svg?react
133
+ use: ["@svgr/webpack", "url-loader"],
134
+ },
135
+ {
136
+ type: "asset/resource",
137
+ parser: {
138
+ dataUrlCondition: {
139
+ maxSize: 200,
140
+ },
141
+ },
142
+ },
143
+ ],
144
+ },
145
+ {
146
+ test: /\.css$/,
147
+ include: /node_modules/,
148
+ use: [
149
+ "style-loader",
150
+ "css-loader",
151
+ {
152
+ loader: "postcss-loader",
153
+ options: {
154
+ postcssOptions: {
155
+ plugins: [require("cssnano")({ preset: "default" })],
156
+ },
157
+ },
158
+ },
159
+ ],
160
+ },
161
+ ],
162
+ },
163
+ optimization: {
164
+ minimizer: [
165
+ new TerserPlugin({
166
+ terserOptions: {
167
+ format: {
168
+ // Turned on because emoji and regex is not minified properly using default
169
+ // https://github.com/facebook/create-react-app/issues/2488
170
+ ascii_only: true,
171
+ },
172
+ },
173
+ }),
174
+ ],
175
+ },
176
+ output: {
177
+ filename: `[name].js`,
178
+ path: path.resolve(process.cwd(), "dist"),
179
+ clean: true,
180
+ },
181
+ plugins: [
182
+ new DefinePlugin({
183
+ BACKEND_HOST: JSON.stringify(backendHost),
184
+ }),
185
+ // Apps can only submit a single JS file via the developer portal
186
+ new optimize.LimitChunkCountPlugin({ maxChunks: 1 }),
187
+ ].filter(Boolean),
188
+ ...buildDevConfig(devConfig),
189
+ };
190
+ }
191
+
192
+ function buildDevConfig(options?: DevConfig): {
193
+ devtool?: string;
194
+ devServer?: DevServerConfiguration;
195
+ } {
196
+ if (!options) {
197
+ return {};
198
+ }
199
+
200
+ const { port, enableHmr, appOrigin, appId, enableHttps, certFile, keyFile } =
201
+ options;
202
+
203
+ let devServer: DevServerConfiguration = {
204
+ server: enableHttps
205
+ ? {
206
+ type: "https",
207
+ options: {
208
+ cert: certFile,
209
+ key: keyFile,
210
+ },
211
+ }
212
+ : "http",
213
+ host: "localhost",
214
+ historyApiFallback: {
215
+ rewrites: [{ from: /^\/$/, to: "/app.js" }],
216
+ },
217
+ port,
218
+ client: {
219
+ logging: "verbose",
220
+ },
221
+ static: {
222
+ directory: path.resolve(process.cwd(), "assets"),
223
+ publicPath: "/assets",
224
+ },
225
+ };
226
+
227
+ if (enableHmr && appOrigin) {
228
+ devServer = {
229
+ ...devServer,
230
+ allowedHosts: new URL(appOrigin).hostname,
231
+ headers: {
232
+ "Access-Control-Allow-Origin": appOrigin,
233
+ "Access-Control-Allow-Credentials": "true",
234
+ "Access-Control-Allow-Private-Network": "true",
235
+ },
236
+ };
237
+ } else if (enableHmr && appId) {
238
+ // Deprecated - App ID should not be used to configure HMR in the future and can be safely removed
239
+ // after a few months.
240
+
241
+ console.warn(
242
+ "Enabling Hot Module Replacement (HMR) with an App ID is deprecated, please see the README.md on how to update.",
243
+ );
244
+
245
+ const appDomain = `app-${appId.toLowerCase().trim()}.canva-apps.com`;
246
+ devServer = {
247
+ ...devServer,
248
+ allowedHosts: appDomain,
249
+ headers: {
250
+ "Access-Control-Allow-Origin": `https://${appDomain}`,
251
+ "Access-Control-Allow-Credentials": "true",
252
+ "Access-Control-Allow-Private-Network": "true",
253
+ },
254
+ };
255
+ } else {
256
+ if (enableHmr && !appOrigin) {
257
+ console.warn(
258
+ "Attempted to enable Hot Module Replacement (HMR) without configuring App Origin... Disabling HMR.",
259
+ );
260
+ }
261
+ devServer.webSocketServer = false;
262
+ }
263
+
264
+ return {
265
+ devtool: "source-map",
266
+ devServer,
267
+ };
268
+ }
269
+
270
+ export default buildConfig;
@@ -23,10 +23,13 @@ export function buildConfig({
23
23
  devConfig,
24
24
  appEntry = path.join(process.cwd(), "src", "index.tsx"),
25
25
  backendHost = process.env.CANVA_BACKEND_HOST,
26
+ // For IN_HARNESS, refer to the following docs for more information: https://www.canva.dev/docs/apps/mcp-server/harness-setup/
27
+ inHarness = process.env.IN_HARNESS === "true",
26
28
  }: {
27
29
  devConfig?: DevConfig;
28
30
  appEntry?: string;
29
31
  backendHost?: string;
32
+ inHarness?: boolean;
30
33
  } = {}): Configuration & DevServerConfiguration {
31
34
  const mode = devConfig ? "development" : "production";
32
35
 
@@ -48,9 +51,14 @@ export function buildConfig({
48
51
  return {
49
52
  mode,
50
53
  context: path.resolve(process.cwd(), "./"),
51
- entry: {
52
- app: appEntry,
53
- },
54
+ entry: inHarness
55
+ ? {
56
+ harness: path.join(process.cwd(), "harness", "harness.tsx"),
57
+ init: path.join(process.cwd(), "harness", "init.ts"),
58
+ }
59
+ : {
60
+ app: appEntry,
61
+ },
54
62
  target: "web",
55
63
  resolve: {
56
64
  alias: {
@@ -62,7 +70,7 @@ export function buildConfig({
62
70
  extensions: [".ts", ".tsx", ".js", ".css", ".svg", ".woff", ".woff2"],
63
71
  },
64
72
  infrastructureLogging: {
65
- level: "none",
73
+ level: inHarness ? "info" : "none",
66
74
  },
67
75
  module: {
68
76
  rules: [
@@ -23,10 +23,13 @@ export function buildConfig({
23
23
  devConfig,
24
24
  appEntry = path.join(process.cwd(), "src", "index.tsx"),
25
25
  backendHost = process.env.CANVA_BACKEND_HOST,
26
+ // For IN_HARNESS, refer to the following docs for more information: https://www.canva.dev/docs/apps/mcp-server/harness-setup/
27
+ inHarness = process.env.IN_HARNESS === "true",
26
28
  }: {
27
29
  devConfig?: DevConfig;
28
30
  appEntry?: string;
29
31
  backendHost?: string;
32
+ inHarness?: boolean;
30
33
  } = {}): Configuration & DevServerConfiguration {
31
34
  const mode = devConfig ? "development" : "production";
32
35
 
@@ -48,9 +51,14 @@ export function buildConfig({
48
51
  return {
49
52
  mode,
50
53
  context: path.resolve(process.cwd(), "./"),
51
- entry: {
52
- app: appEntry,
53
- },
54
+ entry: inHarness
55
+ ? {
56
+ harness: path.join(process.cwd(), "harness", "harness.tsx"),
57
+ init: path.join(process.cwd(), "harness", "init.ts"),
58
+ }
59
+ : {
60
+ app: appEntry,
61
+ },
54
62
  target: "web",
55
63
  resolve: {
56
64
  alias: {
@@ -62,7 +70,7 @@ export function buildConfig({
62
70
  extensions: [".ts", ".tsx", ".js", ".css", ".svg", ".woff", ".woff2"],
63
71
  },
64
72
  infrastructureLogging: {
65
- level: "none",
73
+ level: inHarness ? "info" : "none",
66
74
  },
67
75
  module: {
68
76
  rules: [