@wix/origami-transforms 0.1.24 → 0.1.25

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.
@@ -8,11 +8,11 @@ export function readCompanionRegistrations(files: Iterable<readonly [string, str
8
8
  * @typedef {object} CompanionModule
9
9
  * @property {CompanionRegistration} registration
10
10
  * @property {CompanionImport} import How a page imports it: a component as its entry exports it, a context provider
11
- * as the entry's default export.
11
+ * as the entry's default export. A page imports a function library's functions by name instead.
12
12
  */
13
13
  /**
14
14
  * Every `@site/site-code/<name>` a page can import, by its `companionApp` name. A provider's hook is imported from
15
- * the provider's module.
15
+ * the provider's module, and a library's functions from the library's.
16
16
  * @param {ReadonlyMap<string, CompanionRegistration>} registrations
17
17
  * @returns {Map<string, CompanionModule>}
18
18
  */
@@ -28,7 +28,7 @@ export type CompanionModule = {
28
28
  registration: CompanionRegistration;
29
29
  /**
30
30
  * How a page imports it: a component as its entry exports it, a context provider
31
- * as the entry's default export.
31
+ * as the entry's default export. A page imports a function library's functions by name instead.
32
32
  */
33
33
  import: CompanionImport;
34
34
  };
@@ -69,4 +69,16 @@ export type CompanionContextRegistration = {
69
69
  */
70
70
  hook: string;
71
71
  };
72
- export type CompanionRegistration = CompanionComponentRegistration | CompanionContextRegistration;
72
+ export type CompanionFunctionLibraryRegistration = {
73
+ kind: "functionLibrary";
74
+ /**
75
+ * The registration's file.
76
+ */
77
+ path: string;
78
+ /**
79
+ * `resources.client.url`, relative to the companion app's `src`, as for a provider. Each
80
+ * function is a named export of the module.
81
+ */
82
+ source: string;
83
+ };
84
+ export type CompanionRegistration = CompanionComponentRegistration | CompanionContextRegistration | CompanionFunctionLibraryRegistration;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/origami-transforms",
3
- "version": "0.1.24",
3
+ "version": "0.1.25",
4
4
  "description": "Babel and PostCSS checks and transforms for Wix origami page files.",
5
5
  "main": "./src/index.js",
6
6
  "files": [
@@ -69,5 +69,5 @@
69
69
  },
70
70
  "type": "module",
71
71
  "types": "./dist/index.d.ts",
72
- "falconPackageHash": "398f6cf44892edee778a3cce4a42b93f61f9ffbea23beb4703826cf8"
72
+ "falconPackageHash": "db3ec4aae3575547cf339696d8292fb79c71ee28b5c2fd4bc94a032c"
73
73
  }
@@ -1,4 +1,4 @@
1
- // A companion app's `editorReactComponent` and `contextProvider` registrations, read the same way by the export
1
+ // A companion app's `editorReactComponent`, `contextProvider` and `functionLibrary` registrations, read the same way by the export
2
2
  // (from the exported files) and by import-site (from disk), both in origami-site-generator.
3
3
  import { existsSync, readFileSync, readdirSync } from 'node:fs'
4
4
  import { posix } from 'node:path'
@@ -21,7 +21,17 @@ import * as t from '@babel/types'
21
21
  * The module's default export is the provider.
22
22
  * @property {string} hook `resources.contextSpecifier.hook`, the module's named export that reads the context.
23
23
  */
24
- /** @typedef {CompanionComponentRegistration | CompanionContextRegistration} CompanionRegistration */
24
+ /**
25
+ * @typedef {object} CompanionFunctionLibraryRegistration
26
+ * @property {'functionLibrary'} kind
27
+ * @property {string} path The registration's file.
28
+ * @property {string} source `resources.client.url`, relative to the companion app's `src`, as for a provider. Each
29
+ * function is a named export of the module.
30
+ */
31
+ /**
32
+ * @typedef {CompanionComponentRegistration | CompanionContextRegistration | CompanionFunctionLibraryRegistration}
33
+ * CompanionRegistration
34
+ */
25
35
 
26
36
  /** @param {string} path */
27
37
  export const isExtensionFile = path => /\.extension\.[cm]?[jt]sx?$/.test(path)
@@ -37,13 +47,19 @@ export function readCompanionRegistrations(files) {
37
47
  for (const [path, source] of files) {
38
48
  const ast = parse(source, { sourceType: 'module', plugins: ['jsx', 'typescript'] })
39
49
  t.traverseFast(ast, node => {
40
- if (!t.isCallExpression(node) || !t.isMemberExpression(node.callee)) return
41
- const isComponent = t.isIdentifier(node.callee.property, { name: 'editorReactComponent' })
42
- if (!isComponent && !t.isIdentifier(node.callee.property, { name: 'contextProvider' })) return
50
+ if (!t.isCallExpression(node) || !t.isMemberExpression(node.callee) || !t.isIdentifier(node.callee.property))
51
+ return
52
+ const kind = node.callee.property.name
53
+ if (!['editorReactComponent', 'contextProvider', 'functionLibrary'].includes(kind)) return
43
54
  const config = node.arguments[0]
44
55
  const type = property(config, 'type')
45
56
  const resources = property(config, 'resources')
46
- const registration = isComponent ? component(ast, path, resources) : contextProvider(path, resources)
57
+ const registration =
58
+ kind === 'editorReactComponent'
59
+ ? component(ast, path, resources)
60
+ : kind === 'contextProvider'
61
+ ? contextProvider(path, resources)
62
+ : functionLibrary(path, resources)
47
63
  if (!t.isStringLiteral(type)) throw new Error(`Unsupported component registration: ${path}`)
48
64
  if (!/^companionApp\.[^.]+$/.test(type.value))
49
65
  throw new Error(`Unsupported companion component namespace: ${type.value}`)
@@ -93,6 +109,16 @@ const contextProvider = (path, resources) => {
93
109
  return { kind: 'contextProvider', source: url.value, hook: hook.value }
94
110
  }
95
111
 
112
+ /**
113
+ * @param {string} path @param {t.Expression | undefined} resources
114
+ * @returns {Omit<CompanionFunctionLibraryRegistration, 'path'>}
115
+ */
116
+ const functionLibrary = (path, resources) => {
117
+ const url = property(property(resources, 'client'), 'url')
118
+ if (!t.isStringLiteral(url)) throw new Error(`Unsupported function library registration: ${path}`)
119
+ return { kind: 'functionLibrary', source: url.value }
120
+ }
121
+
96
122
  /**
97
123
  * The file a relative import in `from` points at, without its query, before `findFile` fills in its extension.
98
124
  * @param {string} from @param {string} specifier
@@ -135,12 +161,12 @@ export const findFile = (target, exists) => candidates(target).find(exists)
135
161
  * @typedef {object} CompanionModule
136
162
  * @property {CompanionRegistration} registration
137
163
  * @property {CompanionImport} import How a page imports it: a component as its entry exports it, a context provider
138
- * as the entry's default export.
164
+ * as the entry's default export. A page imports a function library's functions by name instead.
139
165
  */
140
166
 
141
167
  /**
142
168
  * Every `@site/site-code/<name>` a page can import, by its `companionApp` name. A provider's hook is imported from
143
- * the provider's module.
169
+ * the provider's module, and a library's functions from the library's.
144
170
  * @param {ReadonlyMap<string, CompanionRegistration>} registrations
145
171
  * @returns {Map<string, CompanionModule>}
146
172
  */