@cedarjs/vite 6.0.0-canary.2619 → 6.0.0-canary.2621

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.
@@ -1 +1 @@
1
- {"version":3,"file":"buildApp.d.ts","sourceRoot":"","sources":["../src/buildApp.ts"],"names":[],"mappings":"AAkCA,MAAM,WAAW,oBAAoB;IACnC,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAA;IACpB,EAAE,CAAC,EAAE,OAAO,CAAA;CACb;AAED;;;;;;;;GAQG;AACH,wBAAsB,aAAa,CAAC,EAClC,OAAe,EACf,SAA0B,EAC1B,EAAU,GACX,GAAE,oBAAyB,iBA8U3B"}
1
+ {"version":3,"file":"buildApp.d.ts","sourceRoot":"","sources":["../src/buildApp.ts"],"names":[],"mappings":"AAmCA,MAAM,WAAW,oBAAoB;IACnC,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAA;IACpB,EAAE,CAAC,EAAE,OAAO,CAAA;CACb;AAED;;;;;;;;GAQG;AACH,wBAAsB,aAAa,CAAC,EAClC,OAAe,EACf,SAA0B,EAC1B,EAAU,GACX,GAAE,oBAAyB,iBAgV3B"}
package/dist/buildApp.js CHANGED
@@ -12,6 +12,7 @@ import { getConfig, getPaths, projectSideIsEsm } from "@cedarjs/project-config";
12
12
  import { getWorkspacePackageAliases } from "./lib/workspacePackageAliases.js";
13
13
  import { cedarGqlormInjectPlugin } from "./plugins/vite-plugin-cedar-gqlorm-inject.js";
14
14
  import { cedarGraphqlOptionsExtractPlugin } from "./plugins/vite-plugin-cedar-graphql-options-extract.js";
15
+ import { cedarMockCellDataPlugin } from "./plugins/vite-plugin-cedar-mock-cell-data.js";
15
16
  import { cedarOtelWrappingPlugin } from "./plugins/vite-plugin-cedar-otel-wrapping.js";
16
17
  import { handlerAlsWrappingPlugin } from "./plugins/vite-plugin-handler-als-wrapping.js";
17
18
  function resolveWithExtensions(id) {
@@ -233,6 +234,7 @@ async function buildCedarApp({
233
234
  handlerAlsWrappingPlugin({ projectIsEsm: projectSideIsEsm("api") })
234
235
  );
235
236
  }
237
+ plugins.push(cedarMockCellDataPlugin());
236
238
  if (babelPlugins) {
237
239
  plugins.push({
238
240
  name: "cedar-vite-api-babel-transform",
@@ -42,6 +42,7 @@ var import_project_config = require("@cedarjs/project-config");
42
42
  var import_workspacePackageAliases = require("./lib/workspacePackageAliases.js");
43
43
  var import_vite_plugin_cedar_gqlorm_inject = require("./plugins/vite-plugin-cedar-gqlorm-inject.js");
44
44
  var import_vite_plugin_cedar_graphql_options_extract = require("./plugins/vite-plugin-cedar-graphql-options-extract.js");
45
+ var import_vite_plugin_cedar_mock_cell_data = require("./plugins/vite-plugin-cedar-mock-cell-data.js");
45
46
  var import_vite_plugin_cedar_otel_wrapping = require("./plugins/vite-plugin-cedar-otel-wrapping.js");
46
47
  var import_vite_plugin_handler_als_wrapping = require("./plugins/vite-plugin-handler-als-wrapping.js");
47
48
  function resolveWithExtensions(id) {
@@ -263,6 +264,7 @@ async function buildCedarApp({
263
264
  (0, import_vite_plugin_handler_als_wrapping.handlerAlsWrappingPlugin)({ projectIsEsm: (0, import_project_config.projectSideIsEsm)("api") })
264
265
  );
265
266
  }
267
+ plugins.push((0, import_vite_plugin_cedar_mock_cell_data.cedarMockCellDataPlugin)());
266
268
  if (babelPlugins) {
267
269
  plugins.push({
268
270
  name: "cedar-vite-api-babel-transform",
package/dist/cjs/index.js CHANGED
@@ -40,6 +40,7 @@ __export(index_exports, {
40
40
  cedarHtmlEnvPlugin: () => import_vite_plugin_cedar_html_env2.cedarHtmlEnvPlugin,
41
41
  cedarImportDirPlugin: () => import_vite_plugin_cedar_import_dir.cedarImportDirPlugin,
42
42
  cedarMergedConfig: () => import_vite_plugin_merged_config2.cedarMergedConfig,
43
+ cedarMockCellDataPlugin: () => import_vite_plugin_cedar_mock_cell_data2.cedarMockCellDataPlugin,
43
44
  cedarOtelWrappingPlugin: () => import_vite_plugin_cedar_otel_wrapping.cedarOtelWrappingPlugin,
44
45
  cedarRemoveDevFatalErrorPage: () => import_vite_plugin_cedar_remove_dev_fatal_error_page2.cedarRemoveDevFatalErrorPage,
45
46
  cedarRemoveFromBundle: () => import_vite_plugin_cedar_remove_from_bundle2.cedarRemoveFromBundle,
@@ -62,6 +63,7 @@ var import_vite_plugin_cedar_cell = require("./plugins/vite-plugin-cedar-cell.js
62
63
  var import_vite_plugin_cedar_data_uri_shim = require("./plugins/vite-plugin-cedar-data-uri-shim.js");
63
64
  var import_vite_plugin_cedar_entry_injection = require("./plugins/vite-plugin-cedar-entry-injection.js");
64
65
  var import_vite_plugin_cedar_html_env = require("./plugins/vite-plugin-cedar-html-env.js");
66
+ var import_vite_plugin_cedar_mock_cell_data = require("./plugins/vite-plugin-cedar-mock-cell-data.js");
65
67
  var import_vite_plugin_cedar_remove_dev_fatal_error_page = require("./plugins/vite-plugin-cedar-remove-dev-fatal-error-page.js");
66
68
  var import_vite_plugin_cedar_remove_from_bundle = require("./plugins/vite-plugin-cedar-remove-from-bundle.js");
67
69
  var import_vite_plugin_cedar_routes_auto_loader = require("./plugins/vite-plugin-cedar-routes-auto-loader.js");
@@ -86,6 +88,7 @@ var import_vite_plugin_cedar_routes_auto_loader2 = require("./plugins/vite-plugi
86
88
  var import_vite_plugin_cedar_remove_from_bundle2 = require("./plugins/vite-plugin-cedar-remove-from-bundle.js");
87
89
  var import_vite_plugin_cedarjs_resolve_cedar_style_imports2 = require("./plugins/vite-plugin-cedarjs-resolve-cedar-style-imports.js");
88
90
  var import_vite_plugin_cedarjs_job_path_injector = require("./plugins/vite-plugin-cedarjs-job-path-injector.js");
91
+ var import_vite_plugin_cedar_mock_cell_data2 = require("./plugins/vite-plugin-cedar-mock-cell-data.js");
89
92
  var import_vite_plugin_jsx_loader2 = require("./plugins/vite-plugin-jsx-loader.js");
90
93
  var import_vite_plugin_merged_config2 = require("./plugins/vite-plugin-merged-config.js");
91
94
  var import_vite_plugin_swap_apollo_provider2 = require("./plugins/vite-plugin-swap-apollo-provider.js");
@@ -117,6 +120,7 @@ function cedar({ mode } = {}) {
117
120
  (0, import_vite_plugin_cedar_remove_dev_fatal_error_page.cedarRemoveDevFatalErrorPage)(),
118
121
  // RSC handles route auto-loading differently in each build stage
119
122
  !rscEnabled && (0, import_vite_plugin_cedar_routes_auto_loader.cedarRoutesAutoLoaderPlugin)(),
123
+ (0, import_vite_plugin_cedar_mock_cell_data.cedarMockCellDataPlugin)(),
120
124
  (0, import_plugin_react.default)({ babel: babelConfig })
121
125
  ];
122
126
  }
@@ -135,6 +139,7 @@ var index_default = cedar;
135
139
  cedarHtmlEnvPlugin,
136
140
  cedarImportDirPlugin,
137
141
  cedarMergedConfig,
142
+ cedarMockCellDataPlugin,
138
143
  cedarOtelWrappingPlugin,
139
144
  cedarRemoveDevFatalErrorPage,
140
145
  cedarRemoveFromBundle,
@@ -0,0 +1,282 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var vite_plugin_cedar_mock_cell_data_exports = {};
30
+ __export(vite_plugin_cedar_mock_cell_data_exports, {
31
+ cedarMockCellDataPlugin: () => cedarMockCellDataPlugin,
32
+ getCellMetadata: () => getCellMetadata
33
+ });
34
+ module.exports = __toCommonJS(vite_plugin_cedar_mock_cell_data_exports);
35
+ var import_node_fs = __toESM(require("node:fs"), 1);
36
+ var import_node_path = __toESM(require("node:path"), 1);
37
+ var import_generator = __toESM(require("@babel/generator"), 1);
38
+ var import_parser = require("@babel/parser");
39
+ var import_traverse = __toESM(require("@babel/traverse"), 1);
40
+ var t = __toESM(require("@babel/types"), 1);
41
+ var import_fast_glob = __toESM(require("fast-glob"), 1);
42
+ var import_graphql = require("graphql");
43
+ const traverse = import_traverse.default.default ?? import_traverse.default;
44
+ const generate = import_generator.default.default ?? import_generator.default;
45
+ function cedarMockCellDataPlugin() {
46
+ return {
47
+ name: "cedar-mock-cell-data",
48
+ transform(code, id) {
49
+ if (!id.endsWith("Cell.mock.js") && !id.endsWith("Cell.mock.ts")) {
50
+ return null;
51
+ }
52
+ if (process.env.NODE_ENV === "production") {
53
+ return null;
54
+ }
55
+ const ast = parse(id, code);
56
+ if (!ast) {
57
+ return null;
58
+ }
59
+ let pathsToRemove = [];
60
+ const nodesToInsert = [];
61
+ const createVisitors = (addWatchFile) => ({
62
+ ExportNamedDeclaration(p) {
63
+ const d = p.node.declaration;
64
+ let mockFunction = null;
65
+ switch (d?.type) {
66
+ case "VariableDeclaration": {
67
+ const standardMockExport = d.declarations[0];
68
+ const exportId = standardMockExport.id;
69
+ const exportName = exportId?.name;
70
+ if (exportName !== "standard") {
71
+ return;
72
+ }
73
+ const mockFunctionMaybe = standardMockExport?.init;
74
+ if (!mockFunctionMaybe) {
75
+ return;
76
+ }
77
+ if (mockFunctionMaybe.type !== "ArrowFunctionExpression" && mockFunctionMaybe.type !== "FunctionExpression") {
78
+ throw new Error(
79
+ `
80
+
81
+ Mock Error: You must export your standard mock as a function
82
+
83
+ `
84
+ );
85
+ }
86
+ mockFunction = mockFunctionMaybe;
87
+ break;
88
+ }
89
+ case "FunctionDeclaration": {
90
+ const exportName = d.id?.name;
91
+ if (exportName !== "standard") {
92
+ return;
93
+ }
94
+ mockFunction = t.arrowFunctionExpression(d.params, d.body);
95
+ break;
96
+ }
97
+ default:
98
+ return;
99
+ }
100
+ const dirname = import_node_path.default.dirname(id);
101
+ const cellName = import_node_path.default.basename(dirname);
102
+ const [cellPath] = import_fast_glob.default.sync(`${cellName}.{js,jsx,ts,tsx}`, {
103
+ cwd: dirname,
104
+ absolute: true,
105
+ ignore: ["node_modules"]
106
+ });
107
+ if (!cellPath) {
108
+ return;
109
+ }
110
+ addWatchFile?.(cellPath);
111
+ const cellMetadata = getCellMetadata(cellPath);
112
+ if (cellMetadata.hasDefaultExport || !cellMetadata.hasQueryExport) {
113
+ return;
114
+ }
115
+ if (!cellMetadata.operationName) {
116
+ console.warn(
117
+ `[cedar-mock-cell-data] Cell ${cellPath} has an unnamed GraphQL operation. The mock for ${id} will not work. Ensure the QUERY is named, e.g., "query GetUser { ... }"`
118
+ );
119
+ return;
120
+ }
121
+ const mockGraphQLCall = t.callExpression(
122
+ t.identifier("mockGraphQLQuery"),
123
+ [
124
+ t.stringLiteral(cellMetadata.operationName),
125
+ mockFunction
126
+ ]
127
+ );
128
+ pathsToRemove = [...pathsToRemove, p];
129
+ if (cellMetadata.hasAfterQueryExport) {
130
+ const importAfterQuery = t.importDeclaration(
131
+ [
132
+ t.importSpecifier(
133
+ t.identifier("afterQuery"),
134
+ t.identifier("afterQuery")
135
+ )
136
+ ],
137
+ t.stringLiteral(`./${import_node_path.default.basename(cellPath)}`)
138
+ );
139
+ nodesToInsert.push(
140
+ importAfterQuery,
141
+ createExportStandard(
142
+ t,
143
+ t.arrowFunctionExpression(
144
+ [],
145
+ t.callExpression(t.identifier("afterQuery"), [
146
+ t.callExpression(mockGraphQLCall, [])
147
+ ])
148
+ )
149
+ )
150
+ );
151
+ } else {
152
+ nodesToInsert.push(createExportStandard(t, mockGraphQLCall));
153
+ }
154
+ }
155
+ });
156
+ traverse(ast, createVisitors(this.addWatchFile?.bind(this)));
157
+ if (nodesToInsert.length === 0) {
158
+ return null;
159
+ }
160
+ for (const p of pathsToRemove) {
161
+ p.remove();
162
+ }
163
+ ;
164
+ ast.program.body.unshift(...nodesToInsert);
165
+ const result = generate(ast, { retainLines: false }, code);
166
+ return {
167
+ code: result.code,
168
+ map: result.map
169
+ };
170
+ }
171
+ };
172
+ }
173
+ function createExportStandard(babelTypes, ex) {
174
+ return babelTypes.exportNamedDeclaration(
175
+ babelTypes.variableDeclaration("const", [
176
+ babelTypes.variableDeclarator(babelTypes.identifier("standard"), ex)
177
+ ])
178
+ );
179
+ }
180
+ const getCellMetadata = (p) => {
181
+ const ast = getCellAst(p);
182
+ let hasDefaultExport = false;
183
+ const namedExports = [];
184
+ let operation;
185
+ traverse(ast, {
186
+ ExportDefaultDeclaration() {
187
+ hasDefaultExport = true;
188
+ },
189
+ ExportNamedDeclaration(path2) {
190
+ const specifiers = path2.node?.specifiers;
191
+ if (specifiers.length) {
192
+ for (const s of specifiers) {
193
+ const id = s.exported;
194
+ namedExports.push({
195
+ name: id.name,
196
+ type: "re-export"
197
+ });
198
+ }
199
+ return;
200
+ }
201
+ const declaration = path2.node.declaration;
202
+ if (!declaration) {
203
+ return;
204
+ }
205
+ if (declaration.type === "VariableDeclaration") {
206
+ const id = declaration.declarations[0].id;
207
+ namedExports.push({
208
+ name: id.name,
209
+ type: "variable"
210
+ });
211
+ } else if (declaration.type === "FunctionDeclaration") {
212
+ namedExports.push({
213
+ name: declaration?.id?.name,
214
+ type: "function"
215
+ });
216
+ } else if (declaration.type === "ClassDeclaration") {
217
+ namedExports.push({
218
+ name: declaration?.id?.name,
219
+ type: "class"
220
+ });
221
+ }
222
+ },
223
+ TaggedTemplateExpression(path2) {
224
+ if (path2.parent?.id?.name !== "QUERY") {
225
+ return;
226
+ }
227
+ operation = path2.node.quasi.quasis[0].value.raw;
228
+ }
229
+ });
230
+ const hasQueryExport = namedExports.find(({ name }) => name === "QUERY");
231
+ const hasAfterQueryExport = namedExports.find(
232
+ ({ name }) => name === "afterQuery"
233
+ );
234
+ let operationName = "";
235
+ if (operation) {
236
+ const document = (0, import_graphql.parse)(operation);
237
+ for (const definition of document.definitions) {
238
+ if (definition.kind === import_graphql.Kind.OPERATION_DEFINITION && definition.name?.value) {
239
+ operationName = definition.name.value;
240
+ }
241
+ }
242
+ }
243
+ return {
244
+ hasDefaultExport,
245
+ namedExports,
246
+ hasQueryExport,
247
+ hasAfterQueryExport,
248
+ operationName
249
+ };
250
+ };
251
+ function getCellAst(filePath) {
252
+ const code = import_node_fs.default.readFileSync(filePath, "utf-8");
253
+ const plugins = ["typescript", "jsx"].filter(Boolean);
254
+ try {
255
+ return (0, import_parser.parse)(code, {
256
+ sourceType: "module",
257
+ plugins
258
+ });
259
+ } catch (e) {
260
+ console.error(`Error parsing: ${filePath}`);
261
+ console.error(e);
262
+ throw new Error(e?.message);
263
+ }
264
+ }
265
+ function parse(filePath, code) {
266
+ const plugins = ["typescript", "jsx"].filter(Boolean);
267
+ try {
268
+ return (0, import_parser.parse)(code, {
269
+ sourceType: "module",
270
+ plugins
271
+ });
272
+ } catch (e) {
273
+ console.error(`Error parsing: ${filePath}`);
274
+ console.error(e);
275
+ return null;
276
+ }
277
+ }
278
+ // Annotate the CommonJS export names for ESM import in node:
279
+ 0 && (module.exports = {
280
+ cedarMockCellDataPlugin,
281
+ getCellMetadata
282
+ });
package/dist/index.d.ts CHANGED
@@ -15,6 +15,7 @@ export { cedarRoutesAutoLoaderPlugin } from './plugins/vite-plugin-cedar-routes-
15
15
  export { cedarRemoveFromBundle } from './plugins/vite-plugin-cedar-remove-from-bundle.js';
16
16
  export { cedarjsResolveCedarStyleImportsPlugin } from './plugins/vite-plugin-cedarjs-resolve-cedar-style-imports.js';
17
17
  export { cedarjsJobPathInjectorPlugin } from './plugins/vite-plugin-cedarjs-job-path-injector.js';
18
+ export { cedarMockCellDataPlugin } from './plugins/vite-plugin-cedar-mock-cell-data.js';
18
19
  export { cedarTransformJsAsJsx } from './plugins/vite-plugin-jsx-loader.js';
19
20
  export { cedarMergedConfig } from './plugins/vite-plugin-merged-config.js';
20
21
  export { cedarSwapApolloProvider } from './plugins/vite-plugin-swap-apollo-provider.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AAuBxC,OAAO,EAAE,sBAAsB,EAAE,MAAM,4CAA4C,CAAA;AACnF,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAA;AAChF,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAA;AACxE,OAAO,EAAE,uBAAuB,EAAE,MAAM,8CAA8C,CAAA;AACtF,OAAO,EAAE,gCAAgC,EAAE,MAAM,wDAAwD,CAAA;AACzG,OAAO,EAAE,uBAAuB,EAAE,MAAM,8CAA8C,CAAA;AACtF,OAAO,EACL,uBAAuB,EACvB,wBAAwB,GACzB,MAAM,+CAA+C,CAAA;AACtD,OAAO,EAAE,yBAAyB,EAAE,MAAM,gDAAgD,CAAA;AAC1F,OAAO,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAA;AAC5E,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAA;AAChF,OAAO,EAAE,gBAAgB,EAAE,MAAM,8CAA8C,CAAA;AAC/E,OAAO,EAAE,4BAA4B,EAAE,MAAM,4DAA4D,CAAA;AACzG,OAAO,EAAE,2BAA2B,EAAE,MAAM,mDAAmD,CAAA;AAC/F,OAAO,EAAE,qBAAqB,EAAE,MAAM,mDAAmD,CAAA;AACzF,OAAO,EAAE,qCAAqC,EAAE,MAAM,8DAA8D,CAAA;AACpH,OAAO,EAAE,4BAA4B,EAAE,MAAM,oDAAoD,CAAA;AACjG,OAAO,EAAE,qBAAqB,EAAE,MAAM,qCAAqC,CAAA;AAC3E,OAAO,EAAE,iBAAiB,EAAE,MAAM,wCAAwC,CAAA;AAC1E,OAAO,EAAE,uBAAuB,EAAE,MAAM,+CAA+C,CAAA;AACvF,OAAO,EAAE,0BAA0B,EAAE,MAAM,iDAAiD,CAAA;AAC5F,OAAO,EAAE,qBAAqB,EAAE,MAAM,oDAAoD,CAAA;AAE1F,KAAK,aAAa,GAAG;IACnB,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC1B,CAAA;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,EAAE,IAAI,EAAE,GAAE,aAAkB,GAAG,YAAY,EAAE,CAgClE;AAED,8DAA8D;AAC9D,eAAe,KAAK,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,MAAM,CAAA;AAwBxC,OAAO,EAAE,sBAAsB,EAAE,MAAM,4CAA4C,CAAA;AACnF,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAA;AAChF,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAA;AACxE,OAAO,EAAE,uBAAuB,EAAE,MAAM,8CAA8C,CAAA;AACtF,OAAO,EAAE,gCAAgC,EAAE,MAAM,wDAAwD,CAAA;AACzG,OAAO,EAAE,uBAAuB,EAAE,MAAM,8CAA8C,CAAA;AACtF,OAAO,EACL,uBAAuB,EACvB,wBAAwB,GACzB,MAAM,+CAA+C,CAAA;AACtD,OAAO,EAAE,yBAAyB,EAAE,MAAM,gDAAgD,CAAA;AAC1F,OAAO,EAAE,kBAAkB,EAAE,MAAM,yCAAyC,CAAA;AAC5E,OAAO,EAAE,oBAAoB,EAAE,MAAM,2CAA2C,CAAA;AAChF,OAAO,EAAE,gBAAgB,EAAE,MAAM,8CAA8C,CAAA;AAC/E,OAAO,EAAE,4BAA4B,EAAE,MAAM,4DAA4D,CAAA;AACzG,OAAO,EAAE,2BAA2B,EAAE,MAAM,mDAAmD,CAAA;AAC/F,OAAO,EAAE,qBAAqB,EAAE,MAAM,mDAAmD,CAAA;AACzF,OAAO,EAAE,qCAAqC,EAAE,MAAM,8DAA8D,CAAA;AACpH,OAAO,EAAE,4BAA4B,EAAE,MAAM,oDAAoD,CAAA;AACjG,OAAO,EAAE,uBAAuB,EAAE,MAAM,+CAA+C,CAAA;AACvF,OAAO,EAAE,qBAAqB,EAAE,MAAM,qCAAqC,CAAA;AAC3E,OAAO,EAAE,iBAAiB,EAAE,MAAM,wCAAwC,CAAA;AAC1E,OAAO,EAAE,uBAAuB,EAAE,MAAM,+CAA+C,CAAA;AACvF,OAAO,EAAE,0BAA0B,EAAE,MAAM,iDAAiD,CAAA;AAC5F,OAAO,EAAE,qBAAqB,EAAE,MAAM,oDAAoD,CAAA;AAE1F,KAAK,aAAa,GAAG;IACnB,IAAI,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;CAC1B,CAAA;AAED;;GAEG;AACH,wBAAgB,KAAK,CAAC,EAAE,IAAI,EAAE,GAAE,aAAkB,GAAG,YAAY,EAAE,CAiClE;AAED,8DAA8D;AAC9D,eAAe,KAAK,CAAA"}
package/dist/index.js CHANGED
@@ -10,6 +10,7 @@ import { cedarCellTransform } from "./plugins/vite-plugin-cedar-cell.js";
10
10
  import { cedarDataUriShim } from "./plugins/vite-plugin-cedar-data-uri-shim.js";
11
11
  import { cedarEntryInjectionPlugin } from "./plugins/vite-plugin-cedar-entry-injection.js";
12
12
  import { cedarHtmlEnvPlugin } from "./plugins/vite-plugin-cedar-html-env.js";
13
+ import { cedarMockCellDataPlugin } from "./plugins/vite-plugin-cedar-mock-cell-data.js";
13
14
  import { cedarRemoveDevFatalErrorPage } from "./plugins/vite-plugin-cedar-remove-dev-fatal-error-page.js";
14
15
  import { cedarRemoveFromBundle } from "./plugins/vite-plugin-cedar-remove-from-bundle.js";
15
16
  import { cedarRoutesAutoLoaderPlugin } from "./plugins/vite-plugin-cedar-routes-auto-loader.js";
@@ -37,6 +38,7 @@ import { cedarRoutesAutoLoaderPlugin as cedarRoutesAutoLoaderPlugin2 } from "./p
37
38
  import { cedarRemoveFromBundle as cedarRemoveFromBundle2 } from "./plugins/vite-plugin-cedar-remove-from-bundle.js";
38
39
  import { cedarjsResolveCedarStyleImportsPlugin as cedarjsResolveCedarStyleImportsPlugin2 } from "./plugins/vite-plugin-cedarjs-resolve-cedar-style-imports.js";
39
40
  import { cedarjsJobPathInjectorPlugin } from "./plugins/vite-plugin-cedarjs-job-path-injector.js";
41
+ import { cedarMockCellDataPlugin as cedarMockCellDataPlugin2 } from "./plugins/vite-plugin-cedar-mock-cell-data.js";
40
42
  import { cedarTransformJsAsJsx as cedarTransformJsAsJsx2 } from "./plugins/vite-plugin-jsx-loader.js";
41
43
  import { cedarMergedConfig as cedarMergedConfig2 } from "./plugins/vite-plugin-merged-config.js";
42
44
  import { cedarSwapApolloProvider as cedarSwapApolloProvider2 } from "./plugins/vite-plugin-swap-apollo-provider.js";
@@ -68,6 +70,7 @@ function cedar({ mode } = {}) {
68
70
  cedarRemoveDevFatalErrorPage(),
69
71
  // RSC handles route auto-loading differently in each build stage
70
72
  !rscEnabled && cedarRoutesAutoLoaderPlugin(),
73
+ cedarMockCellDataPlugin(),
71
74
  react({ babel: babelConfig })
72
75
  ];
73
76
  }
@@ -85,6 +88,7 @@ export {
85
88
  cedarHtmlEnvPlugin2 as cedarHtmlEnvPlugin,
86
89
  cedarImportDirPlugin,
87
90
  cedarMergedConfig2 as cedarMergedConfig,
91
+ cedarMockCellDataPlugin2 as cedarMockCellDataPlugin,
88
92
  cedarOtelWrappingPlugin,
89
93
  cedarRemoveDevFatalErrorPage2 as cedarRemoveDevFatalErrorPage,
90
94
  cedarRemoveFromBundle2 as cedarRemoveFromBundle,
@@ -0,0 +1,28 @@
1
+ import type { Plugin } from 'vite';
2
+ /**
3
+ * Vite plugin that automatically wraps the `standard` export in Cell mock files
4
+ * with `mockGraphQLQuery`.
5
+ *
6
+ * Only active in non-production environments (for Storybook and Jest).
7
+ *
8
+ * Transforms `*.Cell.mock.[js,ts]` files by:
9
+ * 1. Determining the query operation name from the adjacent Cell's QUERY export
10
+ * 2. Wrapping the `standard` function export in `mockGraphQLQuery(operationName, fn)`
11
+ * 3. If the Cell has an `afterQuery` export, also wraps with `afterQuery(fn())`
12
+ *
13
+ * This is a 1:1 port of `babel-plugin-redwood-mock-cell-data`.
14
+ */
15
+ export declare function cedarMockCellDataPlugin(): Plugin;
16
+ export declare const getCellMetadata: (p: string) => {
17
+ hasDefaultExport: boolean;
18
+ namedExports: NamedExports[];
19
+ hasQueryExport: NamedExports | undefined;
20
+ hasAfterQueryExport: NamedExports | undefined;
21
+ operationName: string;
22
+ };
23
+ interface NamedExports {
24
+ name: string;
25
+ type: 're-export' | 'variable' | 'function' | 'class';
26
+ }
27
+ export {};
28
+ //# sourceMappingURL=vite-plugin-cedar-mock-cell-data.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite-plugin-cedar-mock-cell-data.d.ts","sourceRoot":"","sources":["../../src/plugins/vite-plugin-cedar-mock-cell-data.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAA;AAMlC;;;;;;;;;;;;GAYG;AACH,wBAAgB,uBAAuB,IAAI,MAAM,CA2KhD;AAcD,eAAO,MAAM,eAAe,GAAI,GAAG,MAAM;;;;;;CAoFxC,CAAA;AAgCD,UAAU,YAAY;IACpB,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,WAAW,GAAG,UAAU,GAAG,UAAU,GAAG,OAAO,CAAA;CACtD"}
@@ -0,0 +1,247 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+ import babelGenerator from "@babel/generator";
4
+ import { parse as babelParse } from "@babel/parser";
5
+ import babelTraverse from "@babel/traverse";
6
+ import * as t from "@babel/types";
7
+ import fg from "fast-glob";
8
+ import { parse as graphqlParse, Kind } from "graphql";
9
+ const traverse = babelTraverse.default ?? babelTraverse;
10
+ const generate = babelGenerator.default ?? babelGenerator;
11
+ function cedarMockCellDataPlugin() {
12
+ return {
13
+ name: "cedar-mock-cell-data",
14
+ transform(code, id) {
15
+ if (!id.endsWith("Cell.mock.js") && !id.endsWith("Cell.mock.ts")) {
16
+ return null;
17
+ }
18
+ if (process.env.NODE_ENV === "production") {
19
+ return null;
20
+ }
21
+ const ast = parse(id, code);
22
+ if (!ast) {
23
+ return null;
24
+ }
25
+ let pathsToRemove = [];
26
+ const nodesToInsert = [];
27
+ const createVisitors = (addWatchFile) => ({
28
+ ExportNamedDeclaration(p) {
29
+ const d = p.node.declaration;
30
+ let mockFunction = null;
31
+ switch (d?.type) {
32
+ case "VariableDeclaration": {
33
+ const standardMockExport = d.declarations[0];
34
+ const exportId = standardMockExport.id;
35
+ const exportName = exportId?.name;
36
+ if (exportName !== "standard") {
37
+ return;
38
+ }
39
+ const mockFunctionMaybe = standardMockExport?.init;
40
+ if (!mockFunctionMaybe) {
41
+ return;
42
+ }
43
+ if (mockFunctionMaybe.type !== "ArrowFunctionExpression" && mockFunctionMaybe.type !== "FunctionExpression") {
44
+ throw new Error(
45
+ `
46
+
47
+ Mock Error: You must export your standard mock as a function
48
+
49
+ `
50
+ );
51
+ }
52
+ mockFunction = mockFunctionMaybe;
53
+ break;
54
+ }
55
+ case "FunctionDeclaration": {
56
+ const exportName = d.id?.name;
57
+ if (exportName !== "standard") {
58
+ return;
59
+ }
60
+ mockFunction = t.arrowFunctionExpression(d.params, d.body);
61
+ break;
62
+ }
63
+ default:
64
+ return;
65
+ }
66
+ const dirname = path.dirname(id);
67
+ const cellName = path.basename(dirname);
68
+ const [cellPath] = fg.sync(`${cellName}.{js,jsx,ts,tsx}`, {
69
+ cwd: dirname,
70
+ absolute: true,
71
+ ignore: ["node_modules"]
72
+ });
73
+ if (!cellPath) {
74
+ return;
75
+ }
76
+ addWatchFile?.(cellPath);
77
+ const cellMetadata = getCellMetadata(cellPath);
78
+ if (cellMetadata.hasDefaultExport || !cellMetadata.hasQueryExport) {
79
+ return;
80
+ }
81
+ if (!cellMetadata.operationName) {
82
+ console.warn(
83
+ `[cedar-mock-cell-data] Cell ${cellPath} has an unnamed GraphQL operation. The mock for ${id} will not work. Ensure the QUERY is named, e.g., "query GetUser { ... }"`
84
+ );
85
+ return;
86
+ }
87
+ const mockGraphQLCall = t.callExpression(
88
+ t.identifier("mockGraphQLQuery"),
89
+ [
90
+ t.stringLiteral(cellMetadata.operationName),
91
+ mockFunction
92
+ ]
93
+ );
94
+ pathsToRemove = [...pathsToRemove, p];
95
+ if (cellMetadata.hasAfterQueryExport) {
96
+ const importAfterQuery = t.importDeclaration(
97
+ [
98
+ t.importSpecifier(
99
+ t.identifier("afterQuery"),
100
+ t.identifier("afterQuery")
101
+ )
102
+ ],
103
+ t.stringLiteral(`./${path.basename(cellPath)}`)
104
+ );
105
+ nodesToInsert.push(
106
+ importAfterQuery,
107
+ createExportStandard(
108
+ t,
109
+ t.arrowFunctionExpression(
110
+ [],
111
+ t.callExpression(t.identifier("afterQuery"), [
112
+ t.callExpression(mockGraphQLCall, [])
113
+ ])
114
+ )
115
+ )
116
+ );
117
+ } else {
118
+ nodesToInsert.push(createExportStandard(t, mockGraphQLCall));
119
+ }
120
+ }
121
+ });
122
+ traverse(ast, createVisitors(this.addWatchFile?.bind(this)));
123
+ if (nodesToInsert.length === 0) {
124
+ return null;
125
+ }
126
+ for (const p of pathsToRemove) {
127
+ p.remove();
128
+ }
129
+ ;
130
+ ast.program.body.unshift(...nodesToInsert);
131
+ const result = generate(ast, { retainLines: false }, code);
132
+ return {
133
+ code: result.code,
134
+ map: result.map
135
+ };
136
+ }
137
+ };
138
+ }
139
+ function createExportStandard(babelTypes, ex) {
140
+ return babelTypes.exportNamedDeclaration(
141
+ babelTypes.variableDeclaration("const", [
142
+ babelTypes.variableDeclarator(babelTypes.identifier("standard"), ex)
143
+ ])
144
+ );
145
+ }
146
+ const getCellMetadata = (p) => {
147
+ const ast = getCellAst(p);
148
+ let hasDefaultExport = false;
149
+ const namedExports = [];
150
+ let operation;
151
+ traverse(ast, {
152
+ ExportDefaultDeclaration() {
153
+ hasDefaultExport = true;
154
+ },
155
+ ExportNamedDeclaration(path2) {
156
+ const specifiers = path2.node?.specifiers;
157
+ if (specifiers.length) {
158
+ for (const s of specifiers) {
159
+ const id = s.exported;
160
+ namedExports.push({
161
+ name: id.name,
162
+ type: "re-export"
163
+ });
164
+ }
165
+ return;
166
+ }
167
+ const declaration = path2.node.declaration;
168
+ if (!declaration) {
169
+ return;
170
+ }
171
+ if (declaration.type === "VariableDeclaration") {
172
+ const id = declaration.declarations[0].id;
173
+ namedExports.push({
174
+ name: id.name,
175
+ type: "variable"
176
+ });
177
+ } else if (declaration.type === "FunctionDeclaration") {
178
+ namedExports.push({
179
+ name: declaration?.id?.name,
180
+ type: "function"
181
+ });
182
+ } else if (declaration.type === "ClassDeclaration") {
183
+ namedExports.push({
184
+ name: declaration?.id?.name,
185
+ type: "class"
186
+ });
187
+ }
188
+ },
189
+ TaggedTemplateExpression(path2) {
190
+ if (path2.parent?.id?.name !== "QUERY") {
191
+ return;
192
+ }
193
+ operation = path2.node.quasi.quasis[0].value.raw;
194
+ }
195
+ });
196
+ const hasQueryExport = namedExports.find(({ name }) => name === "QUERY");
197
+ const hasAfterQueryExport = namedExports.find(
198
+ ({ name }) => name === "afterQuery"
199
+ );
200
+ let operationName = "";
201
+ if (operation) {
202
+ const document = graphqlParse(operation);
203
+ for (const definition of document.definitions) {
204
+ if (definition.kind === Kind.OPERATION_DEFINITION && definition.name?.value) {
205
+ operationName = definition.name.value;
206
+ }
207
+ }
208
+ }
209
+ return {
210
+ hasDefaultExport,
211
+ namedExports,
212
+ hasQueryExport,
213
+ hasAfterQueryExport,
214
+ operationName
215
+ };
216
+ };
217
+ function getCellAst(filePath) {
218
+ const code = fs.readFileSync(filePath, "utf-8");
219
+ const plugins = ["typescript", "jsx"].filter(Boolean);
220
+ try {
221
+ return babelParse(code, {
222
+ sourceType: "module",
223
+ plugins
224
+ });
225
+ } catch (e) {
226
+ console.error(`Error parsing: ${filePath}`);
227
+ console.error(e);
228
+ throw new Error(e?.message);
229
+ }
230
+ }
231
+ function parse(filePath, code) {
232
+ const plugins = ["typescript", "jsx"].filter(Boolean);
233
+ try {
234
+ return babelParse(code, {
235
+ sourceType: "module",
236
+ plugins
237
+ });
238
+ } catch (e) {
239
+ console.error(`Error parsing: ${filePath}`);
240
+ console.error(e);
241
+ return null;
242
+ }
243
+ }
244
+ export {
245
+ cedarMockCellDataPlugin,
246
+ getCellMetadata
247
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cedarjs/vite",
3
- "version": "6.0.0-canary.2619",
3
+ "version": "6.0.0-canary.2621",
4
4
  "description": "Vite configuration package for CedarJS",
5
5
  "repository": {
6
6
  "type": "git",
@@ -63,15 +63,15 @@
63
63
  "@babel/parser": "7.29.7",
64
64
  "@babel/traverse": "7.29.7",
65
65
  "@babel/types": "7.29.7",
66
- "@cedarjs/api": "6.0.0-canary.2619",
67
- "@cedarjs/babel-config": "6.0.0-canary.2619",
68
- "@cedarjs/context": "6.0.0-canary.2619",
69
- "@cedarjs/cookie-jar": "6.0.0-canary.2619",
70
- "@cedarjs/graphql-server": "6.0.0-canary.2619",
71
- "@cedarjs/internal": "6.0.0-canary.2619",
72
- "@cedarjs/project-config": "6.0.0-canary.2619",
73
- "@cedarjs/server-store": "6.0.0-canary.2619",
74
- "@cedarjs/testing": "6.0.0-canary.2619",
66
+ "@cedarjs/api": "6.0.0-canary.2621",
67
+ "@cedarjs/babel-config": "6.0.0-canary.2621",
68
+ "@cedarjs/context": "6.0.0-canary.2621",
69
+ "@cedarjs/cookie-jar": "6.0.0-canary.2621",
70
+ "@cedarjs/graphql-server": "6.0.0-canary.2621",
71
+ "@cedarjs/internal": "6.0.0-canary.2621",
72
+ "@cedarjs/project-config": "6.0.0-canary.2621",
73
+ "@cedarjs/server-store": "6.0.0-canary.2621",
74
+ "@cedarjs/testing": "6.0.0-canary.2621",
75
75
  "@fastify/url-data": "6.0.3",
76
76
  "@swc/core": "1.15.41",
77
77
  "@universal-deploy/store": "^0.2.1",
@@ -104,9 +104,9 @@
104
104
  },
105
105
  "devDependencies": {
106
106
  "@arethetypeswrong/cli": "0.18.4",
107
- "@cedarjs/auth": "6.0.0-canary.2619",
108
- "@cedarjs/router": "6.0.0-canary.2619",
109
- "@cedarjs/web": "6.0.0-canary.2619",
107
+ "@cedarjs/auth": "6.0.0-canary.2621",
108
+ "@cedarjs/router": "6.0.0-canary.2621",
109
+ "@cedarjs/web": "6.0.0-canary.2621",
110
110
  "@hyrious/esbuild-plugin-commonjs": "0.2.6",
111
111
  "@jridgewell/trace-mapping": "0.3.31",
112
112
  "@types/aws-lambda": "8.10.162",