mountly-vite-plugin 1.0.0
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/LICENSE +201 -0
- package/dist/fragment.d.ts +14 -0
- package/dist/host.d.ts +55 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.js +767 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Support. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or support.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Jag Reehal
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
200
|
+
implied. See the License for the specific language governing permissions
|
|
201
|
+
and limitations under the License.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Plugin } from "vite";
|
|
2
|
+
export interface ManifestFragmentPluginOptions {
|
|
3
|
+
verticalId: string;
|
|
4
|
+
url: string;
|
|
5
|
+
entry?: string;
|
|
6
|
+
team?: string;
|
|
7
|
+
version?: string;
|
|
8
|
+
featureExport?: string;
|
|
9
|
+
exports?: Record<string, string>;
|
|
10
|
+
exposeEntries?: Record<string, string>;
|
|
11
|
+
outDir?: string;
|
|
12
|
+
}
|
|
13
|
+
/** Write `mountly.manifest.fragment.json` after a vertical build for CI merge into the host manifest. */
|
|
14
|
+
export declare function mountlyManifestFragmentPlugin(options: ManifestFragmentPluginOptions): Plugin;
|
package/dist/host.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { Plugin } from "vite";
|
|
2
|
+
import { type ComposePlatformOptions } from "mountly-manifest";
|
|
3
|
+
export type MountlyHostPlatformOptions = ComposePlatformOptions;
|
|
4
|
+
export interface MountlyHostFragmentOptions {
|
|
5
|
+
/** Path to a built `mountly.manifest.fragment.json`, relative to the Vite root. */
|
|
6
|
+
fragment: string;
|
|
7
|
+
}
|
|
8
|
+
export interface MountlyHostPluginOptions {
|
|
9
|
+
/** Path to mountly-manifest v2 JSON (relative to the Vite root). */
|
|
10
|
+
manifest?: string;
|
|
11
|
+
/**
|
|
12
|
+
* Auto-compose a host manifest from built vertical fragments.
|
|
13
|
+
* React platform imports are filled in automatically.
|
|
14
|
+
*/
|
|
15
|
+
verticals?: MountlyHostFragmentOptions[];
|
|
16
|
+
/**
|
|
17
|
+
* Declare remotes by published URL (federation-style): `{ checkout: "https://cdn/checkout/" }`.
|
|
18
|
+
* The host fetches each remote's `mountly.manifest.fragment.json` (the file `mountlyRemote`
|
|
19
|
+
* emits), so it auto-discovers the remote's exposes, types, and entry — no local fragment
|
|
20
|
+
* file, no `shared` config. Each key becomes the import specifier (`import("checkout/Cart")`).
|
|
21
|
+
* The value is the directory URL the remote's `dist/` is served from, or a direct fragment URL.
|
|
22
|
+
*/
|
|
23
|
+
remotes?: Record<string, string>;
|
|
24
|
+
/** Platform import-map settings used in auto-compose mode. */
|
|
25
|
+
platform?: MountlyHostPlatformOptions;
|
|
26
|
+
/**
|
|
27
|
+
* Dev-only origin overrides per vertical id (e.g. `{ payments: "http://localhost:5001" }`).
|
|
28
|
+
* Subpath exports resolve against this origin when the manifest URL is CDN-relative.
|
|
29
|
+
*/
|
|
30
|
+
devOrigins?: Record<string, string>;
|
|
31
|
+
/** Inject `<script type="importmap">` during production HTML transform. Default true. */
|
|
32
|
+
injectImportMap?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Generate ambient module declarations for manifest remotes.
|
|
35
|
+
* Default: `src/mountly-remotes.d.ts` when `src/` exists, otherwise `mountly-remotes.d.ts`.
|
|
36
|
+
*/
|
|
37
|
+
types?: boolean | {
|
|
38
|
+
outFile?: string;
|
|
39
|
+
strict?: boolean;
|
|
40
|
+
};
|
|
41
|
+
/** Throw when a fragment file is missing. Default true. */
|
|
42
|
+
strictFragments?: boolean;
|
|
43
|
+
/** Write the composed manifest JSON to this path (relative to Vite root) for CI or plain HTML hosts. */
|
|
44
|
+
writeManifest?: string;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Apply Vite's `base` to a site-relative import-map URL.
|
|
48
|
+
*
|
|
49
|
+
* Import maps are resolved against the document, not the bundler, so a host
|
|
50
|
+
* deployed under a sub-path (GitHub Pages, a reverse proxy) needs `base` baked
|
|
51
|
+
* into the emitted URLs — otherwise `/remote/dist/x.js` requests the server
|
|
52
|
+
* root and 404s. Absolute URLs and data: URIs are left alone.
|
|
53
|
+
*/
|
|
54
|
+
export declare function applyBaseToImportUrl(url: string, base: string): string;
|
|
55
|
+
export declare function mountlyHostPlugin(options: MountlyHostPluginOptions): Plugin;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { Plugin, UserConfig } from "vite";
|
|
2
|
+
export { mountlyHostPlugin, type MountlyHostPluginOptions } from "./host.js";
|
|
3
|
+
export { mountlyManifestFragmentPlugin, type ManifestFragmentPluginOptions } from "./fragment.js";
|
|
4
|
+
export type MountlyWidgetFramework = "react" | "vue" | "svelte";
|
|
5
|
+
export declare function getFrameworkPeerExternals(framework: MountlyWidgetFramework): Array<string | RegExp>;
|
|
6
|
+
export declare function getSelfContainedExternals(framework: MountlyWidgetFramework): Array<string | RegExp>;
|
|
7
|
+
export declare function mountlyCssAsText(): Plugin;
|
|
8
|
+
export interface MountlyWidgetPluginOptions {
|
|
9
|
+
framework?: MountlyWidgetFramework;
|
|
10
|
+
}
|
|
11
|
+
export declare function mountlyWidgetPlugin(_options?: MountlyWidgetPluginOptions): Plugin[];
|
|
12
|
+
export interface MountlyRemotePluginOptions {
|
|
13
|
+
/** Remote id — the bare specifier the host imports (e.g. `"checkout"` → `import("checkout")`). */
|
|
14
|
+
name: string;
|
|
15
|
+
/**
|
|
16
|
+
* Modules this remote exposes, as import-map subpaths → source files.
|
|
17
|
+
* `"."` is the root specifier; `"./Cart"` becomes `import("checkout/Cart")`.
|
|
18
|
+
*/
|
|
19
|
+
exposes: Record<string, string>;
|
|
20
|
+
framework?: MountlyWidgetFramework;
|
|
21
|
+
team?: string;
|
|
22
|
+
version?: string;
|
|
23
|
+
/** Named export from the root entry that is an `OnDemandFeature` (enables `<mountly-feature>`). */
|
|
24
|
+
featureExport?: string;
|
|
25
|
+
outDir?: string;
|
|
26
|
+
minify?: boolean;
|
|
27
|
+
sourcemap?: boolean;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Drop-in Vite plugin for a mountly remote. Add it to `vite.config` and run `vite build` —
|
|
31
|
+
* no separate build script. It builds every `exposes` entry with the framework peers
|
|
32
|
+
* externalized (the host shares React/Vue/Svelte through its import map, so there is no
|
|
33
|
+
* `shared` config to keep in sync) and emits `mountly.manifest.fragment.json` for the host
|
|
34
|
+
* to compose. The host imports each entry as native ESM, fully typed.
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* // vite.config.ts
|
|
38
|
+
* export default defineConfig({
|
|
39
|
+
* plugins: [react(), mountlyRemote({
|
|
40
|
+
* name: "checkout",
|
|
41
|
+
* exposes: { ".": "src/index.ts", "./Cart": "src/Cart.tsx" },
|
|
42
|
+
* })],
|
|
43
|
+
* });
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare function mountlyRemote(options: MountlyRemotePluginOptions): Plugin[];
|
|
47
|
+
export interface DefineMountlyWidgetConfigOptions {
|
|
48
|
+
framework?: MountlyWidgetFramework;
|
|
49
|
+
entry?: string;
|
|
50
|
+
outDir?: string;
|
|
51
|
+
minify?: boolean;
|
|
52
|
+
sourcemap?: boolean;
|
|
53
|
+
/** Subpath exposes for import-map hosts: `"./Checkout": "src/Checkout.tsx"`. */
|
|
54
|
+
exposes?: Record<string, string>;
|
|
55
|
+
/** Vertical id for emitted manifest fragment (defaults to package name). */
|
|
56
|
+
verticalId?: string;
|
|
57
|
+
team?: string;
|
|
58
|
+
version?: string;
|
|
59
|
+
/** Named export that satisfies FeatureModule when using moduleUrl loading. */
|
|
60
|
+
featureExport?: string;
|
|
61
|
+
}
|
|
62
|
+
export declare function defineMountlyWidgetConfig(options?: DefineMountlyWidgetConfigOptions): UserConfig[];
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,767 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
3
|
+
import { exportKeyToSubpath as exportKeyToSubpath3 } from "mountly-manifest";
|
|
4
|
+
|
|
5
|
+
// src/fragment.ts
|
|
6
|
+
import { execFileSync } from "child_process";
|
|
7
|
+
import { existsSync, mkdirSync, rmSync, writeFileSync } from "fs";
|
|
8
|
+
import { dirname, join, parse, relative, resolve } from "path";
|
|
9
|
+
import { fileURLToPath } from "url";
|
|
10
|
+
import { exportKeyToSubpath } from "mountly-manifest";
|
|
11
|
+
import { API, SymbolFlags } from "typescript/unstable/sync";
|
|
12
|
+
function tscBin() {
|
|
13
|
+
return join(dirname(fileURLToPath(import.meta.resolve("typescript/package.json"))), "bin", "tsc");
|
|
14
|
+
}
|
|
15
|
+
function mountlyManifestFragmentPlugin(options) {
|
|
16
|
+
let outDir = options.outDir ?? "dist";
|
|
17
|
+
let root = process.cwd();
|
|
18
|
+
function findConfigFile() {
|
|
19
|
+
let dir = root;
|
|
20
|
+
for (; ; ) {
|
|
21
|
+
const candidate = join(dir, "tsconfig.json");
|
|
22
|
+
if (existsSync(candidate)) return candidate;
|
|
23
|
+
const next = dirname(dir);
|
|
24
|
+
if (next === dir || dir === parse(dir).root) return void 0;
|
|
25
|
+
dir = next;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
function collectNamedExports(entries) {
|
|
29
|
+
const sources = entries.filter((entry) => /\.(?:[cm]?[jt]sx?)$/i.test(entry));
|
|
30
|
+
const names = /* @__PURE__ */ new Map();
|
|
31
|
+
if (sources.length === 0) return names;
|
|
32
|
+
const api = new API();
|
|
33
|
+
try {
|
|
34
|
+
const snapshot = api.updateSnapshot({ openFiles: [...sources] });
|
|
35
|
+
for (const entry of sources) {
|
|
36
|
+
const project = snapshot.getDefaultProjectForFile(entry);
|
|
37
|
+
if (!project) continue;
|
|
38
|
+
const sourceFile = project.program.getSourceFile(entry);
|
|
39
|
+
const moduleSymbol = sourceFile && project.checker.getSymbolAtLocation(sourceFile);
|
|
40
|
+
if (!moduleSymbol) continue;
|
|
41
|
+
names.set(
|
|
42
|
+
entry,
|
|
43
|
+
project.checker.getExportsOfModule(moduleSymbol).filter(
|
|
44
|
+
(symbol) => symbol.name !== "default" && (symbol.flags & (SymbolFlags.Interface | SymbolFlags.TypeAlias)) === 0
|
|
45
|
+
).map((symbol) => symbol.name).sort()
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
} finally {
|
|
49
|
+
api.close();
|
|
50
|
+
}
|
|
51
|
+
return names;
|
|
52
|
+
}
|
|
53
|
+
function commonDir(paths) {
|
|
54
|
+
const parts = paths.map((value) => resolve(value).split("/"));
|
|
55
|
+
const shared = [];
|
|
56
|
+
for (let index = 0; ; index++) {
|
|
57
|
+
const segment = parts[0]?.[index];
|
|
58
|
+
if (segment === void 0 || parts.some((current) => current[index] !== segment)) break;
|
|
59
|
+
shared.push(segment);
|
|
60
|
+
}
|
|
61
|
+
return shared.length > 0 ? shared.join("/") || "/" : root;
|
|
62
|
+
}
|
|
63
|
+
function declarationPathFor(entry, rootDir) {
|
|
64
|
+
return relative(rootDir, entry).replace(/\.(?:[cm]?[jt]sx?)$/i, ".d.ts");
|
|
65
|
+
}
|
|
66
|
+
function emitDeclarations(entry, exposeEntries) {
|
|
67
|
+
if (!entry) return { exportDeclarations: {} };
|
|
68
|
+
const sourceEntries = [entry, ...Object.values(exposeEntries)];
|
|
69
|
+
const rootDir = commonDir(sourceEntries);
|
|
70
|
+
const declarationDir = resolve(outDir, "types");
|
|
71
|
+
mkdirSync(declarationDir, { recursive: true });
|
|
72
|
+
const emitConfigPath = join(declarationDir, "tsconfig.emit.json");
|
|
73
|
+
writeFileSync(
|
|
74
|
+
emitConfigPath,
|
|
75
|
+
JSON.stringify({
|
|
76
|
+
...findConfigFile() ? { extends: findConfigFile() } : {},
|
|
77
|
+
compilerOptions: {
|
|
78
|
+
declaration: true,
|
|
79
|
+
emitDeclarationOnly: true,
|
|
80
|
+
declarationMap: false,
|
|
81
|
+
noEmit: false,
|
|
82
|
+
noEmitOnError: false,
|
|
83
|
+
outDir: declarationDir,
|
|
84
|
+
declarationDir,
|
|
85
|
+
rootDir
|
|
86
|
+
},
|
|
87
|
+
files: sourceEntries
|
|
88
|
+
})
|
|
89
|
+
);
|
|
90
|
+
try {
|
|
91
|
+
execFileSync(process.execPath, [tscBin(), "-p", emitConfigPath], { stdio: "pipe" });
|
|
92
|
+
} catch {
|
|
93
|
+
} finally {
|
|
94
|
+
rmSync(emitConfigPath, { force: true });
|
|
95
|
+
}
|
|
96
|
+
const exportDeclarations = {};
|
|
97
|
+
for (const [exportKey, exportEntry] of Object.entries(exposeEntries)) {
|
|
98
|
+
exportDeclarations[exportKey] = `./types/${declarationPathFor(exportEntry, rootDir)}`;
|
|
99
|
+
}
|
|
100
|
+
return {
|
|
101
|
+
rootDeclaration: `./types/${declarationPathFor(entry, rootDir)}`,
|
|
102
|
+
exportDeclarations
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
name: "mountly:manifest-fragment",
|
|
107
|
+
configResolved(config) {
|
|
108
|
+
root = config.root;
|
|
109
|
+
outDir = resolve(root, options.outDir ?? "dist");
|
|
110
|
+
},
|
|
111
|
+
closeBundle() {
|
|
112
|
+
const entry = options.entry ? resolve(root, options.entry) : void 0;
|
|
113
|
+
const exposeEntries = Object.fromEntries(
|
|
114
|
+
Object.entries(options.exposeEntries ?? {}).map(([key, value]) => [
|
|
115
|
+
key,
|
|
116
|
+
resolve(root, value)
|
|
117
|
+
])
|
|
118
|
+
);
|
|
119
|
+
const vertical = {
|
|
120
|
+
id: options.verticalId,
|
|
121
|
+
url: options.url
|
|
122
|
+
};
|
|
123
|
+
if (options.team) vertical.team = options.team;
|
|
124
|
+
if (options.version) vertical.version = options.version;
|
|
125
|
+
if (options.featureExport) vertical.featureExport = options.featureExport;
|
|
126
|
+
const declarations = emitDeclarations(entry, exposeEntries);
|
|
127
|
+
const namedExports = collectNamedExports([
|
|
128
|
+
...entry ? [entry] : [],
|
|
129
|
+
...Object.values(exposeEntries)
|
|
130
|
+
]);
|
|
131
|
+
const moduleTypes = entry ? [...namedExports.get(entry) ?? []] : [];
|
|
132
|
+
if (options.featureExport) moduleTypes.push(options.featureExport);
|
|
133
|
+
if (options.exports && Object.keys(options.exports).length > 0) {
|
|
134
|
+
const mapped = {};
|
|
135
|
+
const typedExports = {};
|
|
136
|
+
for (const [key, value] of Object.entries(options.exports)) {
|
|
137
|
+
mapped[key] = value.startsWith("./") ? value : `./${exportKeyToSubpath(value)}`;
|
|
138
|
+
const exposeEntry = exposeEntries[key];
|
|
139
|
+
const exportNames = exposeEntry ? namedExports.get(exposeEntry) ?? [] : [];
|
|
140
|
+
const declaration = declarations.exportDeclarations[key];
|
|
141
|
+
if (exportNames.length > 0 || declaration) {
|
|
142
|
+
typedExports[key] = declaration || exportNames.length > 0 ? {
|
|
143
|
+
...declaration ? { declaration } : {},
|
|
144
|
+
...exportNames.length > 0 ? { names: exportNames } : {}
|
|
145
|
+
} : exportNames;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
vertical.exports = mapped;
|
|
149
|
+
if (moduleTypes.length > 0 || declarations.rootDeclaration || Object.keys(typedExports).length > 0) {
|
|
150
|
+
vertical.types = {
|
|
151
|
+
...declarations.rootDeclaration ? { declaration: declarations.rootDeclaration } : {},
|
|
152
|
+
...moduleTypes.length > 0 ? { module: [...new Set(moduleTypes)].sort() } : {},
|
|
153
|
+
...Object.keys(typedExports).length > 0 ? { exports: typedExports } : {}
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
} else if (moduleTypes.length > 0 || declarations.rootDeclaration) {
|
|
157
|
+
vertical.types = {
|
|
158
|
+
...declarations.rootDeclaration ? { declaration: declarations.rootDeclaration } : {},
|
|
159
|
+
module: [...new Set(moduleTypes)].sort()
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
const fragment = {
|
|
163
|
+
version: "2",
|
|
164
|
+
verticals: [vertical]
|
|
165
|
+
};
|
|
166
|
+
writeFileSync(
|
|
167
|
+
join(outDir, "mountly.manifest.fragment.json"),
|
|
168
|
+
`${JSON.stringify(fragment, null, 2)}
|
|
169
|
+
`
|
|
170
|
+
);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// src/host.ts
|
|
176
|
+
import { existsSync as existsSync2, mkdirSync as mkdirSync2, readFileSync, writeFileSync as writeFileSync2 } from "fs";
|
|
177
|
+
import { dirname as dirname2, join as join2, relative as relative2, resolve as resolve2 } from "path";
|
|
178
|
+
import {
|
|
179
|
+
codegenManifestTypes,
|
|
180
|
+
composeManifestFromFragments,
|
|
181
|
+
defaultReactPlatformImports,
|
|
182
|
+
exportKeyToSubpath as exportKeyToSubpath2,
|
|
183
|
+
manifestRemoteSpecifiers,
|
|
184
|
+
manifestToImportMap,
|
|
185
|
+
mergeManifests,
|
|
186
|
+
parseManifest,
|
|
187
|
+
resolveExportUrl,
|
|
188
|
+
resolveRemoteImport
|
|
189
|
+
} from "mountly-manifest";
|
|
190
|
+
function loadManifest(manifestPath, root) {
|
|
191
|
+
const absolute = resolve2(root, manifestPath);
|
|
192
|
+
const raw = JSON.parse(readFileSync(absolute, "utf8"));
|
|
193
|
+
return parseManifest(raw);
|
|
194
|
+
}
|
|
195
|
+
function resolveTypesOutFile(root, manifestPath, types) {
|
|
196
|
+
if (types === false) return null;
|
|
197
|
+
if (typeof types === "object" && types.outFile) {
|
|
198
|
+
return resolve2(root, types.outFile);
|
|
199
|
+
}
|
|
200
|
+
const srcDir = join2(root, "src");
|
|
201
|
+
if (existsSync2(srcDir)) return join2(srcDir, "mountly-remotes.d.ts");
|
|
202
|
+
if (manifestPath) return join2(dirname2(resolve2(root, manifestPath)), "mountly-remotes.d.ts");
|
|
203
|
+
return join2(root, "mountly-remotes.d.ts");
|
|
204
|
+
}
|
|
205
|
+
function sanitizePathSegment(value) {
|
|
206
|
+
return value.replace(/[^a-zA-Z0-9._-]+/g, "_");
|
|
207
|
+
}
|
|
208
|
+
function extractExportNames(value) {
|
|
209
|
+
if (!value) return [];
|
|
210
|
+
return Array.isArray(value) ? value : value.names ?? [];
|
|
211
|
+
}
|
|
212
|
+
function exportNamesForVertical(vertical, exportKey) {
|
|
213
|
+
if (!exportKey) {
|
|
214
|
+
const names = new Set(vertical.types?.module ?? []);
|
|
215
|
+
if (vertical.featureExport) names.add(vertical.featureExport);
|
|
216
|
+
return [...names];
|
|
217
|
+
}
|
|
218
|
+
return extractExportNames(vertical.types?.exports?.[exportKey]);
|
|
219
|
+
}
|
|
220
|
+
function extractDeclarationPath(vertical, exportKey) {
|
|
221
|
+
const types = vertical.types;
|
|
222
|
+
if (!exportKey) return types?.declaration ?? "./types/index.d.ts";
|
|
223
|
+
const value = types?.exports?.[exportKey];
|
|
224
|
+
if (!value || Array.isArray(value)) {
|
|
225
|
+
return `./types/${exportKeyToSubpath2(exportKey)}.d.ts`;
|
|
226
|
+
}
|
|
227
|
+
return value.declaration ?? `./types/${exportKeyToSubpath2(exportKey)}.d.ts`;
|
|
228
|
+
}
|
|
229
|
+
function applyRemoteOrigin(url, remoteOrigin) {
|
|
230
|
+
if (!remoteOrigin) return url;
|
|
231
|
+
if (url.startsWith("/")) {
|
|
232
|
+
return `${remoteOrigin.replace(/\/$/, "")}${url}`;
|
|
233
|
+
}
|
|
234
|
+
if (url.startsWith("http://") || url.startsWith("https://")) {
|
|
235
|
+
try {
|
|
236
|
+
const parsed = new URL(url);
|
|
237
|
+
return `${remoteOrigin.replace(/\/$/, "")}${parsed.pathname}${parsed.search}`;
|
|
238
|
+
} catch {
|
|
239
|
+
return url;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
return `${remoteOrigin.replace(/\/$/, "")}/${url.replace(/^\/+/, "")}`;
|
|
243
|
+
}
|
|
244
|
+
function tryResolveWorkspaceFile(root, value) {
|
|
245
|
+
if (!value.startsWith("/")) return null;
|
|
246
|
+
let current = root;
|
|
247
|
+
const suffix = value.replace(/^\/+/, "");
|
|
248
|
+
for (; ; ) {
|
|
249
|
+
const candidate = join2(current, suffix);
|
|
250
|
+
if (existsSync2(candidate)) return candidate;
|
|
251
|
+
const parent = dirname2(current);
|
|
252
|
+
if (parent === current) return null;
|
|
253
|
+
current = parent;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
async function readTypeSource(root, source) {
|
|
257
|
+
if (source.startsWith("http://") || source.startsWith("https://")) {
|
|
258
|
+
const response = await fetch(source);
|
|
259
|
+
if (!response.ok) {
|
|
260
|
+
throw new Error(
|
|
261
|
+
`[mountly:host] failed to fetch remote types from ${source} (${response.status})`
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
return await response.text();
|
|
265
|
+
}
|
|
266
|
+
const file = source.startsWith("/") ? tryResolveWorkspaceFile(root, source) ?? resolve2(root, `.${source}`) : resolve2(root, source);
|
|
267
|
+
return readFileSync(file, "utf8");
|
|
268
|
+
}
|
|
269
|
+
function resolveReferencedTypeSource(source, specifier) {
|
|
270
|
+
const normalizedSpecifier = specifier.replace(/\.(?:[cm]?js|jsx)$/i, ".d.ts");
|
|
271
|
+
if (source.startsWith("http://") || source.startsWith("https://")) {
|
|
272
|
+
return new URL(normalizedSpecifier, source).toString();
|
|
273
|
+
}
|
|
274
|
+
return resolve2(dirname2(source), normalizedSpecifier);
|
|
275
|
+
}
|
|
276
|
+
function localPathForRemoteType(cacheDir, verticalId, source) {
|
|
277
|
+
if (source.startsWith("http://") || source.startsWith("https://")) {
|
|
278
|
+
const url = new URL(source);
|
|
279
|
+
return join2(
|
|
280
|
+
cacheDir,
|
|
281
|
+
sanitizePathSegment(verticalId),
|
|
282
|
+
url.hostname,
|
|
283
|
+
url.pathname.replace(/^\/+/, "")
|
|
284
|
+
);
|
|
285
|
+
}
|
|
286
|
+
return join2(
|
|
287
|
+
cacheDir,
|
|
288
|
+
sanitizePathSegment(verticalId),
|
|
289
|
+
source.startsWith("/") ? source.replace(/^\/+/, "") : source
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
async function materializeTypeGraph(root, verticalId, source, cacheDir, seen) {
|
|
293
|
+
const cached = seen.get(source);
|
|
294
|
+
if (cached) return cached;
|
|
295
|
+
const text = await readTypeSource(root, source);
|
|
296
|
+
const localFile = localPathForRemoteType(cacheDir, verticalId, source);
|
|
297
|
+
seen.set(source, localFile);
|
|
298
|
+
mkdirSync2(dirname2(localFile), { recursive: true });
|
|
299
|
+
writeFileSync2(localFile, text);
|
|
300
|
+
const importRegex = /(?:from\s+|import\s*\(\s*|<reference\s+path=)["'](\.[^"']+)["']/g;
|
|
301
|
+
for (const match of text.matchAll(importRegex)) {
|
|
302
|
+
const ref = match[1];
|
|
303
|
+
if (!ref) continue;
|
|
304
|
+
await materializeTypeGraph(
|
|
305
|
+
root,
|
|
306
|
+
verticalId,
|
|
307
|
+
resolveReferencedTypeSource(source, ref),
|
|
308
|
+
cacheDir,
|
|
309
|
+
seen
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
return localFile;
|
|
313
|
+
}
|
|
314
|
+
async function writeTypesFile(manifest, outFile, root, remotes, strict) {
|
|
315
|
+
if (!outFile) return;
|
|
316
|
+
const cacheDir = join2(dirname2(outFile), ".mountly", "remotes");
|
|
317
|
+
const declarationModules = /* @__PURE__ */ new Map();
|
|
318
|
+
const seen = /* @__PURE__ */ new Map();
|
|
319
|
+
for (const vertical of manifest.verticals) {
|
|
320
|
+
const rootSpecifier = vertical.alias ?? vertical.id;
|
|
321
|
+
const remoteOrigin = remotes[vertical.id];
|
|
322
|
+
const entries = [
|
|
323
|
+
{ specifier: rootSpecifier }
|
|
324
|
+
];
|
|
325
|
+
for (const exportKey of Object.keys(vertical.exports ?? {})) {
|
|
326
|
+
entries.push({ specifier: `${rootSpecifier}/${exportKey.replace(/^\.\//, "")}`, exportKey });
|
|
327
|
+
}
|
|
328
|
+
for (const entry of entries) {
|
|
329
|
+
const declarationPath = extractDeclarationPath(vertical, entry.exportKey);
|
|
330
|
+
if (!declarationPath) continue;
|
|
331
|
+
try {
|
|
332
|
+
const resolved = applyRemoteOrigin(
|
|
333
|
+
resolveExportUrl(vertical, declarationPath),
|
|
334
|
+
remoteOrigin
|
|
335
|
+
);
|
|
336
|
+
const localFile = await materializeTypeGraph(root, vertical.id, resolved, cacheDir, seen);
|
|
337
|
+
let modulePath = relative2(dirname2(outFile), localFile).replace(/\\/g, "/");
|
|
338
|
+
modulePath = modulePath.replace(/\.d\.ts$/i, "");
|
|
339
|
+
if (!modulePath.startsWith("./") && !modulePath.startsWith("../")) {
|
|
340
|
+
modulePath = `./${modulePath}`;
|
|
341
|
+
}
|
|
342
|
+
declarationModules.set(entry.specifier, modulePath);
|
|
343
|
+
} catch (error) {
|
|
344
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
345
|
+
if (strict) {
|
|
346
|
+
throw new Error(
|
|
347
|
+
`[mountly:host] remote types unavailable for "${entry.specifier}": ${message}`
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
console.warn(
|
|
351
|
+
`[mountly:host] remote types unavailable for "${entry.specifier}": ${message}`
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
const next = codegenManifestTypes(manifest, {
|
|
357
|
+
resolveDeclarationModule: ({ specifier }) => declarationModules.get(specifier) ?? null,
|
|
358
|
+
resolveExportNames: ({ vertical, exportKey }) => exportNamesForVertical(vertical, exportKey)
|
|
359
|
+
});
|
|
360
|
+
mkdirSync2(dirname2(outFile), { recursive: true });
|
|
361
|
+
const prev = existsSync2(outFile) ? readFileSync(outFile, "utf8") : null;
|
|
362
|
+
if (prev === next) return;
|
|
363
|
+
writeFileSync2(outFile, next);
|
|
364
|
+
}
|
|
365
|
+
function verticalIdFromSpecifier(manifest, specifier) {
|
|
366
|
+
for (const vertical of manifest.verticals) {
|
|
367
|
+
const roots = [vertical.id, vertical.alias].filter(Boolean);
|
|
368
|
+
for (const root of roots) {
|
|
369
|
+
if (specifier === root || specifier.startsWith(`${root}/`)) {
|
|
370
|
+
return vertical.id;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return null;
|
|
375
|
+
}
|
|
376
|
+
function resolveDevUrl(manifest, specifier, remotes) {
|
|
377
|
+
const manifestUrl = resolveRemoteImport(manifest, specifier);
|
|
378
|
+
if (!manifestUrl) return null;
|
|
379
|
+
const verticalId = verticalIdFromSpecifier(manifest, specifier);
|
|
380
|
+
const remoteOrigin = verticalId ? remotes[verticalId] : void 0;
|
|
381
|
+
if (!remoteOrigin) return manifestUrl;
|
|
382
|
+
if (manifestUrl.startsWith("/")) {
|
|
383
|
+
return `${remoteOrigin.replace(/\/$/, "")}${manifestUrl}`;
|
|
384
|
+
}
|
|
385
|
+
if (manifestUrl.startsWith("http://") || manifestUrl.startsWith("https://")) {
|
|
386
|
+
try {
|
|
387
|
+
const parsed = new URL(manifestUrl);
|
|
388
|
+
return `${remoteOrigin.replace(/\/$/, "")}${parsed.pathname}${parsed.search}`;
|
|
389
|
+
} catch {
|
|
390
|
+
return manifestUrl;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
return `${remoteOrigin.replace(/\/$/, "")}/${manifestUrl.replace(/^\/+/, "")}`;
|
|
394
|
+
}
|
|
395
|
+
function isRemoteSpecifier(manifest, id) {
|
|
396
|
+
if (resolveRemoteImport(manifest, id)) return true;
|
|
397
|
+
return manifest.verticals.some((vertical) => {
|
|
398
|
+
const roots = [vertical.id, vertical.alias].filter(Boolean);
|
|
399
|
+
return roots.some((root) => id.startsWith(`${root}/`));
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
function joinRemoteUrl(base, segment) {
|
|
403
|
+
return `${base.replace(/\/+$/, "")}/${segment.replace(/^\.?\/+/, "")}`;
|
|
404
|
+
}
|
|
405
|
+
function applyBaseToImportUrl(url, base) {
|
|
406
|
+
if (/^[a-z][a-z0-9+.-]*:/i.test(url) || url.startsWith("//")) return url;
|
|
407
|
+
const prefix = base.replace(/\/+$/, "");
|
|
408
|
+
if (!prefix || prefix === ".") return url;
|
|
409
|
+
if (url.startsWith("/")) return `${prefix}${url}`;
|
|
410
|
+
return `${prefix}/${url.replace(/^\.\//, "")}`;
|
|
411
|
+
}
|
|
412
|
+
async function fetchRemoteFragments(remotes) {
|
|
413
|
+
const verticals = [];
|
|
414
|
+
for (const [name, urlRaw] of Object.entries(remotes)) {
|
|
415
|
+
const trimmed = urlRaw.replace(/\/+$/, "");
|
|
416
|
+
const isFragmentUrl = trimmed.endsWith(".json");
|
|
417
|
+
const fragmentUrl = isFragmentUrl ? trimmed : `${trimmed}/mountly.manifest.fragment.json`;
|
|
418
|
+
const base = isFragmentUrl ? trimmed.replace(/\/[^/]*$/, "") : trimmed;
|
|
419
|
+
const response = await fetch(fragmentUrl);
|
|
420
|
+
if (!response.ok) {
|
|
421
|
+
throw new Error(
|
|
422
|
+
`[mountly:host] remote "${name}": failed to fetch ${fragmentUrl} (${response.status}) \u2014 is the remote built and served?`
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
const fragment = await response.json();
|
|
426
|
+
const fragmentVerticals = fragment.verticals ?? [];
|
|
427
|
+
for (const vertical of fragmentVerticals) {
|
|
428
|
+
const absolute = /^https?:\/\//.test(vertical.url) || vertical.url.startsWith("/") ? vertical.url : joinRemoteUrl(base, vertical.url);
|
|
429
|
+
verticals.push({
|
|
430
|
+
...vertical,
|
|
431
|
+
// A `mountlyRemote` build emits one vertical; key it by the host's remote name.
|
|
432
|
+
id: fragmentVerticals.length === 1 ? name : vertical.id,
|
|
433
|
+
url: absolute
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
return verticals;
|
|
438
|
+
}
|
|
439
|
+
function mountlyHostPlugin(options) {
|
|
440
|
+
const devOrigins = options.devOrigins ?? {};
|
|
441
|
+
const injectImportMap = options.injectImportMap ?? true;
|
|
442
|
+
let urlRemoteVerticals = [];
|
|
443
|
+
let urlRemotesFetched = false;
|
|
444
|
+
const ensureUrlRemotes = async () => {
|
|
445
|
+
if (urlRemotesFetched) return;
|
|
446
|
+
urlRemotesFetched = true;
|
|
447
|
+
if (options.remotes && Object.keys(options.remotes).length > 0) {
|
|
448
|
+
urlRemoteVerticals = await fetchRemoteFragments(options.remotes);
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
const strictTypes = options.types !== false && typeof options.types === "object" ? options.types.strict ?? true : options.types !== false;
|
|
452
|
+
let manifest;
|
|
453
|
+
let root = process.cwd();
|
|
454
|
+
let isProduction = false;
|
|
455
|
+
let sourceFiles = [];
|
|
456
|
+
let typesOutFile = null;
|
|
457
|
+
let viteBase = "/";
|
|
458
|
+
const withUrlRemotes = (base) => urlRemoteVerticals.length > 0 ? mergeManifests(base, { verticals: urlRemoteVerticals }) : base;
|
|
459
|
+
const loadCurrentManifest = () => {
|
|
460
|
+
if (options.verticals?.length) {
|
|
461
|
+
const composed = composeManifestFromFragments({
|
|
462
|
+
root,
|
|
463
|
+
fragments: options.verticals.map(({ fragment }) => fragment),
|
|
464
|
+
platform: options.platform,
|
|
465
|
+
base: options.manifest,
|
|
466
|
+
strict: options.strictFragments ?? true
|
|
467
|
+
});
|
|
468
|
+
sourceFiles = composed.fragmentFiles;
|
|
469
|
+
if (options.manifest) {
|
|
470
|
+
sourceFiles.push(resolve2(root, options.manifest));
|
|
471
|
+
}
|
|
472
|
+
return withUrlRemotes(composed.manifest);
|
|
473
|
+
}
|
|
474
|
+
if (options.manifest) {
|
|
475
|
+
sourceFiles = [resolve2(root, options.manifest)];
|
|
476
|
+
return withUrlRemotes(loadManifest(options.manifest, root));
|
|
477
|
+
}
|
|
478
|
+
if (urlRemoteVerticals.length > 0) {
|
|
479
|
+
return parseManifest({
|
|
480
|
+
version: "2",
|
|
481
|
+
platform: { imports: defaultReactPlatformImports(options.platform) },
|
|
482
|
+
verticals: urlRemoteVerticals
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
throw new Error(
|
|
486
|
+
"[mountly:host] provide `manifest`, `verticals`, or `remotes` to mountlyHostPlugin()"
|
|
487
|
+
);
|
|
488
|
+
};
|
|
489
|
+
const maybeWriteManifest = (manifestToWrite) => {
|
|
490
|
+
if (!options.writeManifest) return;
|
|
491
|
+
const out = resolve2(root, options.writeManifest);
|
|
492
|
+
mkdirSync2(dirname2(out), { recursive: true });
|
|
493
|
+
writeFileSync2(out, `${JSON.stringify(manifestToWrite, null, 2)}
|
|
494
|
+
`);
|
|
495
|
+
};
|
|
496
|
+
return {
|
|
497
|
+
name: "mountly:host",
|
|
498
|
+
enforce: "pre",
|
|
499
|
+
async config(_config, env) {
|
|
500
|
+
isProduction = env.command === "build";
|
|
501
|
+
await ensureUrlRemotes();
|
|
502
|
+
manifest = loadCurrentManifest();
|
|
503
|
+
maybeWriteManifest(manifest);
|
|
504
|
+
typesOutFile = resolveTypesOutFile(root, options.manifest, options.types);
|
|
505
|
+
void writeTypesFile(manifest, typesOutFile, root, devOrigins, false);
|
|
506
|
+
const external = manifestRemoteSpecifiers(manifest);
|
|
507
|
+
return {
|
|
508
|
+
build: {
|
|
509
|
+
rollupOptions: {
|
|
510
|
+
external
|
|
511
|
+
}
|
|
512
|
+
},
|
|
513
|
+
optimizeDeps: {
|
|
514
|
+
exclude: external
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
},
|
|
518
|
+
async configResolved(config) {
|
|
519
|
+
root = config.root;
|
|
520
|
+
viteBase = config.base;
|
|
521
|
+
await ensureUrlRemotes();
|
|
522
|
+
manifest = loadCurrentManifest();
|
|
523
|
+
typesOutFile = resolveTypesOutFile(root, options.manifest, options.types);
|
|
524
|
+
void writeTypesFile(manifest, typesOutFile, root, devOrigins, false);
|
|
525
|
+
},
|
|
526
|
+
async buildStart() {
|
|
527
|
+
await ensureUrlRemotes();
|
|
528
|
+
manifest = loadCurrentManifest();
|
|
529
|
+
maybeWriteManifest(manifest);
|
|
530
|
+
await writeTypesFile(manifest, typesOutFile, root, devOrigins, strictTypes);
|
|
531
|
+
for (const file of sourceFiles) this.addWatchFile(file);
|
|
532
|
+
},
|
|
533
|
+
configureServer(server) {
|
|
534
|
+
server.watcher.add(sourceFiles);
|
|
535
|
+
},
|
|
536
|
+
handleHotUpdate(ctx) {
|
|
537
|
+
if (!sourceFiles.includes(ctx.file)) return;
|
|
538
|
+
manifest = loadCurrentManifest();
|
|
539
|
+
void writeTypesFile(manifest, typesOutFile, root, devOrigins, false);
|
|
540
|
+
},
|
|
541
|
+
resolveId(id) {
|
|
542
|
+
if (!isRemoteSpecifier(manifest, id)) return null;
|
|
543
|
+
const url = isProduction ? null : resolveDevUrl(manifest, id, devOrigins);
|
|
544
|
+
if (url) {
|
|
545
|
+
return { id: url, external: true };
|
|
546
|
+
}
|
|
547
|
+
return { id, external: true };
|
|
548
|
+
},
|
|
549
|
+
transformIndexHtml: {
|
|
550
|
+
order: "pre",
|
|
551
|
+
handler(html) {
|
|
552
|
+
let next = html;
|
|
553
|
+
if (injectImportMap) {
|
|
554
|
+
const imports = Object.fromEntries(
|
|
555
|
+
Object.entries(manifestToImportMap(manifest)).map(([specifier, url]) => [
|
|
556
|
+
specifier,
|
|
557
|
+
isProduction ? applyBaseToImportUrl(url, viteBase) : resolveDevUrl(manifest, specifier, devOrigins) ?? url
|
|
558
|
+
])
|
|
559
|
+
);
|
|
560
|
+
const snippet = `<script type="importmap">
|
|
561
|
+
${JSON.stringify({ imports }, null, 2)}
|
|
562
|
+
</script>`;
|
|
563
|
+
if (!next.includes('type="importmap"')) {
|
|
564
|
+
next = next.replace("<head>", `<head>
|
|
565
|
+
${snippet}`);
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
return next;
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
};
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
// src/index.ts
|
|
575
|
+
var PLATFORM_EXTERNALS = ["mountly", /^mountly\//];
|
|
576
|
+
var FRAMEWORK_EXTERNALS = {
|
|
577
|
+
react: ["react", "react/jsx-runtime", "react-dom", "react-dom/client", "mountly-react"],
|
|
578
|
+
vue: ["vue", "mountly-vue"],
|
|
579
|
+
svelte: ["svelte", "mountly-svelte"]
|
|
580
|
+
};
|
|
581
|
+
var FRAMEWORK_BUNDLE = {
|
|
582
|
+
react: ["react", "react/jsx-runtime", "react-dom", "react-dom/client"],
|
|
583
|
+
vue: ["vue"],
|
|
584
|
+
svelte: ["svelte"]
|
|
585
|
+
};
|
|
586
|
+
function getFrameworkPeerExternals(framework) {
|
|
587
|
+
return [...PLATFORM_EXTERNALS, ...FRAMEWORK_EXTERNALS[framework]];
|
|
588
|
+
}
|
|
589
|
+
function getSelfContainedExternals(framework) {
|
|
590
|
+
return [
|
|
591
|
+
...PLATFORM_EXTERNALS,
|
|
592
|
+
...FRAMEWORK_EXTERNALS[framework].filter((id) => !FRAMEWORK_BUNDLE[framework].includes(id))
|
|
593
|
+
];
|
|
594
|
+
}
|
|
595
|
+
function mountlyCssAsText() {
|
|
596
|
+
return {
|
|
597
|
+
name: "mountly-css-as-text",
|
|
598
|
+
enforce: "pre",
|
|
599
|
+
load(id) {
|
|
600
|
+
if (!id.endsWith(".css")) return null;
|
|
601
|
+
const source = readFileSync2(id, "utf8");
|
|
602
|
+
return {
|
|
603
|
+
code: `export default ${JSON.stringify(source)};`,
|
|
604
|
+
map: { mappings: "" }
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
};
|
|
608
|
+
}
|
|
609
|
+
function mountlyWidgetPlugin(_options = {}) {
|
|
610
|
+
return [mountlyCssAsText()];
|
|
611
|
+
}
|
|
612
|
+
function mountlyRemote(options) {
|
|
613
|
+
const framework = options.framework ?? "react";
|
|
614
|
+
const outDir = options.outDir ?? "dist";
|
|
615
|
+
const mainEntry = options.exposes["."];
|
|
616
|
+
if (!mainEntry) {
|
|
617
|
+
throw new Error('[mountly:remote] `exposes` must include a "." root entry');
|
|
618
|
+
}
|
|
619
|
+
const entry = {};
|
|
620
|
+
const exportManifestPaths = {};
|
|
621
|
+
const subpathExposes = {};
|
|
622
|
+
for (const [key, src] of Object.entries(options.exposes)) {
|
|
623
|
+
if (key === ".") {
|
|
624
|
+
entry.index = src;
|
|
625
|
+
continue;
|
|
626
|
+
}
|
|
627
|
+
const subpath = exportKeyToSubpath3(key);
|
|
628
|
+
entry[subpath] = src;
|
|
629
|
+
exportManifestPaths[key] = `./${subpath}.js`;
|
|
630
|
+
subpathExposes[key] = src;
|
|
631
|
+
}
|
|
632
|
+
return [
|
|
633
|
+
mountlyCssAsText(),
|
|
634
|
+
mountlyManifestFragmentPlugin({
|
|
635
|
+
verticalId: options.name,
|
|
636
|
+
url: "./index.js",
|
|
637
|
+
entry: mainEntry,
|
|
638
|
+
team: options.team,
|
|
639
|
+
version: options.version,
|
|
640
|
+
featureExport: options.featureExport,
|
|
641
|
+
exports: exportManifestPaths,
|
|
642
|
+
exposeEntries: subpathExposes,
|
|
643
|
+
outDir
|
|
644
|
+
}),
|
|
645
|
+
{
|
|
646
|
+
name: "mountly:remote",
|
|
647
|
+
config() {
|
|
648
|
+
return {
|
|
649
|
+
build: {
|
|
650
|
+
outDir,
|
|
651
|
+
emptyOutDir: true,
|
|
652
|
+
minify: options.minify ?? true,
|
|
653
|
+
sourcemap: options.sourcemap ?? true,
|
|
654
|
+
target: "es2020",
|
|
655
|
+
lib: {
|
|
656
|
+
entry,
|
|
657
|
+
formats: ["es"],
|
|
658
|
+
fileName: (_format, entryName) => `${entryName}.js`
|
|
659
|
+
},
|
|
660
|
+
rollupOptions: { external: getFrameworkPeerExternals(framework) }
|
|
661
|
+
}
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
];
|
|
666
|
+
}
|
|
667
|
+
function defineMountlyWidgetConfig(options = {}) {
|
|
668
|
+
const {
|
|
669
|
+
framework = "react",
|
|
670
|
+
entry = "src/index.ts",
|
|
671
|
+
outDir = "dist",
|
|
672
|
+
minify = true,
|
|
673
|
+
sourcemap = true,
|
|
674
|
+
exposes = {},
|
|
675
|
+
verticalId = "widget",
|
|
676
|
+
team,
|
|
677
|
+
version,
|
|
678
|
+
featureExport
|
|
679
|
+
} = options;
|
|
680
|
+
const peerExternals = getFrameworkPeerExternals(framework);
|
|
681
|
+
const selfContainedExternals = getSelfContainedExternals(framework);
|
|
682
|
+
const externalize = (externals) => ({
|
|
683
|
+
rollupOptions: { external: externals },
|
|
684
|
+
rolldownOptions: { external: externals }
|
|
685
|
+
});
|
|
686
|
+
const sharedBuild = {
|
|
687
|
+
outDir,
|
|
688
|
+
emptyOutDir: false,
|
|
689
|
+
minify,
|
|
690
|
+
sourcemap,
|
|
691
|
+
target: "es2020",
|
|
692
|
+
lib: {
|
|
693
|
+
entry,
|
|
694
|
+
formats: ["es"]
|
|
695
|
+
}
|
|
696
|
+
};
|
|
697
|
+
const exportManifestPaths = {};
|
|
698
|
+
for (const exportKey of Object.keys(exposes)) {
|
|
699
|
+
exportManifestPaths[exportKey] = `./${exportKeyToSubpath3(exportKey)}.js`;
|
|
700
|
+
}
|
|
701
|
+
const configs = [
|
|
702
|
+
{
|
|
703
|
+
plugins: [...mountlyWidgetPlugin({ framework })],
|
|
704
|
+
build: {
|
|
705
|
+
...sharedBuild,
|
|
706
|
+
emptyOutDir: true,
|
|
707
|
+
lib: {
|
|
708
|
+
entry,
|
|
709
|
+
formats: ["es"],
|
|
710
|
+
fileName: () => "index.js"
|
|
711
|
+
},
|
|
712
|
+
...externalize(selfContainedExternals)
|
|
713
|
+
}
|
|
714
|
+
},
|
|
715
|
+
{
|
|
716
|
+
plugins: [
|
|
717
|
+
...mountlyWidgetPlugin({ framework }),
|
|
718
|
+
mountlyManifestFragmentPlugin({
|
|
719
|
+
verticalId,
|
|
720
|
+
url: "./peer.js",
|
|
721
|
+
entry,
|
|
722
|
+
team,
|
|
723
|
+
version,
|
|
724
|
+
featureExport,
|
|
725
|
+
exports: exportManifestPaths,
|
|
726
|
+
exposeEntries: exposes,
|
|
727
|
+
outDir
|
|
728
|
+
})
|
|
729
|
+
],
|
|
730
|
+
build: {
|
|
731
|
+
...sharedBuild,
|
|
732
|
+
lib: {
|
|
733
|
+
entry,
|
|
734
|
+
formats: ["es"],
|
|
735
|
+
fileName: () => "peer.js"
|
|
736
|
+
},
|
|
737
|
+
...externalize(peerExternals)
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
];
|
|
741
|
+
for (const [exportKey, exportEntry] of Object.entries(exposes)) {
|
|
742
|
+
const subpath = exportKeyToSubpath3(exportKey);
|
|
743
|
+
configs.push({
|
|
744
|
+
plugins: [...mountlyWidgetPlugin({ framework })],
|
|
745
|
+
build: {
|
|
746
|
+
...sharedBuild,
|
|
747
|
+
lib: {
|
|
748
|
+
entry: exportEntry,
|
|
749
|
+
formats: ["es"],
|
|
750
|
+
fileName: () => `${subpath}.js`
|
|
751
|
+
},
|
|
752
|
+
...externalize(peerExternals)
|
|
753
|
+
}
|
|
754
|
+
});
|
|
755
|
+
}
|
|
756
|
+
return configs;
|
|
757
|
+
}
|
|
758
|
+
export {
|
|
759
|
+
defineMountlyWidgetConfig,
|
|
760
|
+
getFrameworkPeerExternals,
|
|
761
|
+
getSelfContainedExternals,
|
|
762
|
+
mountlyCssAsText,
|
|
763
|
+
mountlyHostPlugin,
|
|
764
|
+
mountlyManifestFragmentPlugin,
|
|
765
|
+
mountlyRemote,
|
|
766
|
+
mountlyWidgetPlugin
|
|
767
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mountly-vite-plugin",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Vite lib build plugin for mountly vertical widgets (dual ESM output)",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/jagreehal/mountly",
|
|
9
|
+
"directory": "packages/mountly-vite-plugin"
|
|
10
|
+
},
|
|
11
|
+
"files": [
|
|
12
|
+
"dist"
|
|
13
|
+
],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"main": "./dist/index.js",
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"import": "./dist/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"typescript": "^7.0.2"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"tsup": "^8.5.1",
|
|
31
|
+
"typescript": "^7.0.2",
|
|
32
|
+
"vite": "^8.1.5",
|
|
33
|
+
"mountly-manifest": "1.0.0"
|
|
34
|
+
},
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"vite": "^8.2.1",
|
|
37
|
+
"mountly-manifest": "1.0.0"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsup && tsc -p tsconfig.json --noEmit false --declaration --emitDeclarationOnly --declarationMap false --outDir dist",
|
|
41
|
+
"typecheck": "tsc --noEmit",
|
|
42
|
+
"dev": "tsup --watch"
|
|
43
|
+
}
|
|
44
|
+
}
|