@wix/origami-site 1.0.11 → 1.0.12
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.
- package/package.json +2 -2
- package/src/build/companionComponents.js +108 -0
- package/src/build/index.js +2 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wix/origami-site",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.12",
|
|
4
4
|
"description": "Runs an exported Wix origami site: file-based routing, Wix dynamic pages, popups and build checks. Use with @wix/origami-site-astro or @wix/origami-site-express.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -93,5 +93,5 @@
|
|
|
93
93
|
"optional": true
|
|
94
94
|
}
|
|
95
95
|
},
|
|
96
|
-
"falconPackageHash": "
|
|
96
|
+
"falconPackageHash": "226e497d1f66aa056dfc45ea0418bce7b71939d846baa680a8ce5e1c"
|
|
97
97
|
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
|
|
2
|
+
import { relative, resolve, sep } from 'node:path'
|
|
3
|
+
import {
|
|
4
|
+
companionExports,
|
|
5
|
+
companionModules,
|
|
6
|
+
entryTarget,
|
|
7
|
+
findFile,
|
|
8
|
+
isExtensionFile,
|
|
9
|
+
readCompanionRegistrationsIn,
|
|
10
|
+
} from '@wix/origami-transforms/companion-registrations'
|
|
11
|
+
|
|
12
|
+
// Companion app components and context providers skip the component wrapper server, so they arrive without its
|
|
13
|
+
// wrapping. Each `@site/site-code/<Name>` import resolves to a module that wraps the client entry registered as
|
|
14
|
+
// `companionApp.<Name>` the way the server wraps its components and providers, without the presets. TypeScript doesn't run this plugin: it resolves the same import through
|
|
15
|
+
// site-code's `package.json` `exports`, which this plugin keeps in step with the registrations.
|
|
16
|
+
const IMPORT = /^@site\/site-code\/([A-Za-z_$][\w$]*)$/
|
|
17
|
+
const PREFIX = '\0origami-companion:'
|
|
18
|
+
|
|
19
|
+
/** @param {string} siteRoot */
|
|
20
|
+
export const companionComponents = siteRoot => {
|
|
21
|
+
const appRoot = resolve(siteRoot, '../site-code')
|
|
22
|
+
const sourceRoot = resolve(appRoot, 'src')
|
|
23
|
+
// Read once, and again after an extension file changes.
|
|
24
|
+
/** @type {Map<string, import('@wix/origami-transforms/companion-registrations').CompanionModule> | undefined} */
|
|
25
|
+
let cached
|
|
26
|
+
const modules = () => (cached ??= companionModules(readCompanionRegistrationsIn(sourceRoot)))
|
|
27
|
+
/** @param {import('@wix/origami-transforms/companion-registrations').CompanionRegistration} registration */
|
|
28
|
+
const entry = registration => {
|
|
29
|
+
const target = entryTarget(registration, sourceRoot)
|
|
30
|
+
// As written when no file matches, so Vite reports it.
|
|
31
|
+
return findFile(target, existsSync) ?? target
|
|
32
|
+
}
|
|
33
|
+
// A broken registration mustn't stop the server or the build: the import that needs it reports the error.
|
|
34
|
+
/** @param {(message: string) => void} report */
|
|
35
|
+
const syncExports = report => {
|
|
36
|
+
cached = undefined
|
|
37
|
+
try {
|
|
38
|
+
writeExports(appRoot, companionExports(modules(), registration => relativePath(appRoot, entry(registration))))
|
|
39
|
+
} catch (error) {
|
|
40
|
+
report(String(error))
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return {
|
|
44
|
+
name: 'origami-companion-components',
|
|
45
|
+
enforce: /** @type {const} */ ('pre'),
|
|
46
|
+
/** @this {import('vite').Rollup.PluginContext} */
|
|
47
|
+
buildStart() {
|
|
48
|
+
syncExports(message => this.warn(message))
|
|
49
|
+
},
|
|
50
|
+
/** @param {import('vite').ViteDevServer} server */
|
|
51
|
+
configureServer: server => {
|
|
52
|
+
// Vite watches only the site, so the companion app's registrations are added to its watcher.
|
|
53
|
+
server.watcher.add(sourceRoot)
|
|
54
|
+
const sync = /** @param {string} file */ file =>
|
|
55
|
+
isExtensionFile(file) && syncExports(message => server.config.logger.error(message))
|
|
56
|
+
for (const event of ['add', 'change', 'unlink']) server.watcher.on(event, sync)
|
|
57
|
+
},
|
|
58
|
+
/** @param {string} source */
|
|
59
|
+
resolveId: source => {
|
|
60
|
+
const name = IMPORT.exec(source)?.[1]
|
|
61
|
+
return name && modules().has(name) ? PREFIX + name : undefined
|
|
62
|
+
},
|
|
63
|
+
/** @this {import('vite').Rollup.PluginContext} @param {string} id */
|
|
64
|
+
load(id) {
|
|
65
|
+
if (!id.startsWith(PREFIX)) return undefined
|
|
66
|
+
const module = modules().get(id.slice(PREFIX.length))
|
|
67
|
+
if (!module) return undefined
|
|
68
|
+
// The wrapper comes from the registration file, so editing it reloads the module.
|
|
69
|
+
this.addWatchFile(module.registration.path)
|
|
70
|
+
const from = JSON.stringify(entry(module.registration))
|
|
71
|
+
const exported = exportedName(module.import)
|
|
72
|
+
const wrapped =
|
|
73
|
+
module.registration.kind === 'component' ? 'withInteractions(withOrigami(Raw))' : 'withOrigami(Raw)'
|
|
74
|
+
// The entry's other exports, such as a provider's hook, pass through, as TypeScript sees them; the explicit
|
|
75
|
+
// export shadows the raw one.
|
|
76
|
+
return [
|
|
77
|
+
"import { withInteractions, withOrigami } from '@wix/origami-runtime'",
|
|
78
|
+
`import { ${exported} as Raw } from ${from}`,
|
|
79
|
+
`export * from ${from}`,
|
|
80
|
+
`const Wrapped = ${wrapped}`,
|
|
81
|
+
`export { Wrapped as ${exported} }`,
|
|
82
|
+
].join('\n')
|
|
83
|
+
},
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** @param {import('@wix/origami-transforms/companion-registrations').CompanionImport} companionImport */
|
|
88
|
+
const exportedName = companionImport => {
|
|
89
|
+
const name = companionImport.kind === 'named' ? companionImport.exportName : 'default'
|
|
90
|
+
// A string export name (`'Fancy-Button'`) is quoted; ES2022 allows it on both sides.
|
|
91
|
+
return /^[A-Za-z_$][\w$]*$/.test(name) ? name : JSON.stringify(name)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** @param {string} from @param {string} to */
|
|
95
|
+
const relativePath = (from, to) => relative(from, to).split(sep).join('/')
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Writes site-code's `package.json` `exports`, as the export does, so a component or context added, renamed or
|
|
99
|
+
* removed after the export type-checks with no manual entry. import-site uploads the stored `exports`, not these.
|
|
100
|
+
* @param {string} appRoot @param {Record<string, string>} exports
|
|
101
|
+
*/
|
|
102
|
+
const writeExports = (appRoot, exports) => {
|
|
103
|
+
const packagePath = resolve(appRoot, 'package.json')
|
|
104
|
+
if (!existsSync(packagePath)) return
|
|
105
|
+
const packageJson = JSON.parse(readFileSync(packagePath, 'utf8'))
|
|
106
|
+
if (JSON.stringify(exports) === JSON.stringify(packageJson.exports)) return
|
|
107
|
+
writeFileSync(packagePath, `${JSON.stringify({ ...packageJson, exports }, null, 2)}\n`)
|
|
108
|
+
}
|
package/src/build/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { transforms } from './transforms.js'
|
|
2
|
+
import { companionComponents } from './companionComponents.js'
|
|
2
3
|
import { globalImports } from './globalImports.js'
|
|
3
4
|
import { pageList } from './pageList.js'
|
|
4
5
|
import { sharedPackages } from './sharedPackages.js'
|
|
@@ -15,6 +16,7 @@ export function origamiVitePlugins({ root, siteTree }) {
|
|
|
15
16
|
pageList(siteTree),
|
|
16
17
|
...transforms({ root }),
|
|
17
18
|
servicesConfigsReload(root),
|
|
19
|
+
companionComponents(root),
|
|
18
20
|
sharedPackages(),
|
|
19
21
|
wrapperAliases(root),
|
|
20
22
|
browserEnv(),
|