@teambit/preview 0.0.771 → 0.0.774
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/bundler/chunks.ts +51 -0
- package/bundler/html-plugin.ts +24 -0
- package/{webpack/html.ts → bundler/html-template.ts} +0 -0
- package/dist/bundler/chunks.d.ts +17 -0
- package/dist/bundler/chunks.js +68 -0
- package/dist/bundler/chunks.js.map +1 -0
- package/dist/bundler/html-plugin.d.ts +5 -0
- package/dist/bundler/html-plugin.js +56 -0
- package/dist/bundler/html-plugin.js.map +1 -0
- package/dist/{webpack/html.d.ts → bundler/html-template.d.ts} +0 -0
- package/dist/{webpack/html.js → bundler/html-template.js} +1 -1
- package/dist/bundler/html-template.js.map +1 -0
- package/dist/env-preview-template.task.d.ts +4 -9
- package/dist/env-preview-template.task.js +64 -74
- package/dist/env-preview-template.task.js.map +1 -1
- package/dist/preview-modules.d.ts +10 -0
- package/dist/preview-modules.js +54 -0
- package/dist/preview-modules.js.map +1 -0
- package/dist/preview.preview.runtime.d.ts +8 -5
- package/dist/preview.preview.runtime.js +76 -43
- package/dist/preview.preview.runtime.js.map +1 -1
- package/dist/strategies/env-strategy.js +4 -4
- package/dist/strategies/env-strategy.js.map +1 -1
- package/package-tar/teambit-preview-0.0.774.tgz +0 -0
- package/package.json +23 -21
- package/{preview-1656339215962.js → preview-1656473363813.js} +2 -2
- package/preview-modules.tsx +28 -0
- package/preview.preview.runtime.tsx +71 -51
- package/strategies/env-strategy.ts +1 -1
- package/dist/webpack/html.js.map +0 -1
- package/dist/webpack/index.d.ts +0 -1
- package/dist/webpack/index.js +0 -23
- package/dist/webpack/index.js.map +0 -1
- package/package-tar/teambit-preview-0.0.771.tgz +0 -0
- package/webpack/index.ts +0 -1
|
@@ -8,13 +8,15 @@ import { PreviewNotFound } from './exceptions';
|
|
|
8
8
|
import { PreviewType } from './preview-type';
|
|
9
9
|
import { PreviewAspect, PreviewRuntime } from './preview.aspect';
|
|
10
10
|
import { ClickInsideAnIframeEvent } from './events';
|
|
11
|
-
import { PreviewModule } from './types/preview-module';
|
|
11
|
+
import { ModuleFile, PreviewModule } from './types/preview-module';
|
|
12
12
|
import { RenderingContext } from './rendering-context';
|
|
13
13
|
import { fetchComponentAspects } from './gql/fetch-component-aspects';
|
|
14
|
+
import { PREVIEW_MODULES } from './preview-modules';
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
// forward linkModules() for generate-link.ts
|
|
17
|
+
export { linkModules } from './preview-modules';
|
|
16
18
|
|
|
17
|
-
|
|
19
|
+
export type PreviewSlot = SlotRegistry<PreviewType>;
|
|
18
20
|
|
|
19
21
|
export type RenderingContextOptions = { aspectsFilter?: string[] };
|
|
20
22
|
export type RenderingContextProvider = (options: RenderingContextOptions) => { [key: string]: any };
|
|
@@ -47,6 +49,32 @@ export class PreviewPreview {
|
|
|
47
49
|
|
|
48
50
|
private isDev = false;
|
|
49
51
|
|
|
52
|
+
private isReady() {
|
|
53
|
+
const { previewName } = this.getLocation();
|
|
54
|
+
const name = previewName || this.getDefault();
|
|
55
|
+
|
|
56
|
+
if (!PREVIEW_MODULES.has(name)) return false;
|
|
57
|
+
const preview = this.getPreview(name);
|
|
58
|
+
if (!preview) return false;
|
|
59
|
+
const includedReady = preview.include?.every((included) => PREVIEW_MODULES.has(included)) ?? true;
|
|
60
|
+
if (!includedReady) return false;
|
|
61
|
+
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
private _setupPromise?: Promise<void>;
|
|
66
|
+
setup = () => {
|
|
67
|
+
if (this.isReady()) return Promise.resolve();
|
|
68
|
+
|
|
69
|
+
this._setupPromise ??= new Promise((resolve) => {
|
|
70
|
+
PREVIEW_MODULES.onSet.add(() => {
|
|
71
|
+
if (this.isReady()) resolve();
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return this._setupPromise;
|
|
76
|
+
};
|
|
77
|
+
|
|
50
78
|
/**
|
|
51
79
|
* render the preview.
|
|
52
80
|
*/
|
|
@@ -61,14 +89,12 @@ export class PreviewPreview {
|
|
|
61
89
|
}
|
|
62
90
|
|
|
63
91
|
const includesAll = await Promise.all(
|
|
64
|
-
(preview.include || []).map(async (
|
|
65
|
-
const includedPreview = this.getPreview(
|
|
92
|
+
(preview.include || []).map(async (inclPreviewName) => {
|
|
93
|
+
const includedPreview = this.getPreview(inclPreviewName);
|
|
66
94
|
if (!includedPreview) return undefined;
|
|
67
95
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
await this.getPreviewModule(prevName, componentId, name)
|
|
71
|
-
);
|
|
96
|
+
const inclPreviewModule = await this.getPreviewModule(inclPreviewName, componentId);
|
|
97
|
+
return includedPreview.selectPreviewModel?.(componentId.fullName, inclPreviewModule);
|
|
72
98
|
})
|
|
73
99
|
);
|
|
74
100
|
|
|
@@ -84,18 +110,17 @@ export class PreviewPreview {
|
|
|
84
110
|
);
|
|
85
111
|
};
|
|
86
112
|
|
|
87
|
-
async getPreviewModule(
|
|
88
|
-
const
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
113
|
+
async getPreviewModule(previewName: string, id: ComponentID): Promise<PreviewModule> {
|
|
114
|
+
const compShortId = id.fullName;
|
|
115
|
+
|
|
116
|
+
const relevantModel = PREVIEW_MODULES.get(previewName);
|
|
117
|
+
if (!relevantModel) throw new Error(`[preview.preview] missing preview "${previewName}"`);
|
|
118
|
+
if (relevantModel.componentMap[compShortId]) return relevantModel;
|
|
119
|
+
|
|
120
|
+
const componentPreviews = await this.fetchComponentPreview(id, previewName);
|
|
121
|
+
PREVIEW_MODULES.loadComponentPreviews(compShortId, componentPreviews);
|
|
122
|
+
|
|
123
|
+
const component = componentPreviews[previewName];
|
|
99
124
|
|
|
100
125
|
return {
|
|
101
126
|
mainModule: relevantModel.mainModule,
|
|
@@ -105,13 +130,12 @@ export class PreviewPreview {
|
|
|
105
130
|
};
|
|
106
131
|
}
|
|
107
132
|
|
|
108
|
-
async fetchComponentPreview(id: ComponentID, name: string) {
|
|
109
|
-
let previewFile;
|
|
133
|
+
async fetchComponentPreview(id: ComponentID, name: string): Promise<Record<string, ModuleFile[]>> {
|
|
134
|
+
let previewFile: string | undefined;
|
|
110
135
|
const allFiles = await this.fetchComponentPreviewFiles(id, name);
|
|
111
136
|
// It's a component bundled with the env
|
|
112
|
-
if (allFiles === null) {
|
|
113
|
-
|
|
114
|
-
}
|
|
137
|
+
if (allFiles === null) return {};
|
|
138
|
+
|
|
115
139
|
allFiles.forEach((file) => {
|
|
116
140
|
// We want to run the preview file always last
|
|
117
141
|
if (file.endsWith('-preview.js')) {
|
|
@@ -120,10 +144,9 @@ export class PreviewPreview {
|
|
|
120
144
|
this.addComponentFileElement(id, file);
|
|
121
145
|
}
|
|
122
146
|
});
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
});
|
|
147
|
+
|
|
148
|
+
if (!previewFile) return {};
|
|
149
|
+
return this.loadPreviewScript(id, name, previewFile);
|
|
127
150
|
}
|
|
128
151
|
|
|
129
152
|
private addComponentFileElement(id: ComponentID, previewBundleFileName: string) {
|
|
@@ -177,23 +200,24 @@ export class PreviewPreview {
|
|
|
177
200
|
return link;
|
|
178
201
|
}
|
|
179
202
|
|
|
180
|
-
private
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
203
|
+
private async loadPreviewScript(id: ComponentID, previewName: string, previewBundleFileName: string) {
|
|
204
|
+
await new Promise<void>((resolve, reject) => {
|
|
205
|
+
const script = document.createElement('script');
|
|
206
|
+
const previewRoute = `~aspect/component-preview`;
|
|
207
|
+
const src = `/api/${id.toString()}/${previewRoute}/${previewBundleFileName}`;
|
|
208
|
+
script.src = src;
|
|
209
|
+
script.setAttribute('defer', 'defer');
|
|
210
|
+
script.onload = () => resolve();
|
|
211
|
+
script.onerror = (message, _, _1, _2, error) =>
|
|
212
|
+
reject(error || new Error(`[preview.preview] failed to load preview script: ${message}`));
|
|
213
|
+
document.head.appendChild(script);
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
const globalId = `${id.toStringWithoutVersion()}-preview`;
|
|
217
|
+
const componentPreview = window[globalId];
|
|
218
|
+
if (!componentPreview) throw new PreviewNotFound(previewName);
|
|
219
|
+
|
|
220
|
+
return componentPreview as Record<string, ModuleFile[]>;
|
|
197
221
|
}
|
|
198
222
|
|
|
199
223
|
private getComponentAspects = memoize(fetchComponentAspects, {
|
|
@@ -276,8 +300,4 @@ export class PreviewPreview {
|
|
|
276
300
|
}
|
|
277
301
|
}
|
|
278
302
|
|
|
279
|
-
export function linkModules(previewName: string, previewModule: PreviewModule) {
|
|
280
|
-
PREVIEW_MODULES[previewName] = previewModule;
|
|
281
|
-
}
|
|
282
|
-
|
|
283
303
|
PreviewAspect.addRuntime(PreviewPreview);
|
|
@@ -12,7 +12,7 @@ import { PkgMain } from '@teambit/pkg';
|
|
|
12
12
|
import type { BundlingStrategy, ComputeTargetsContext } from '../bundling-strategy';
|
|
13
13
|
import { PreviewDefinition } from '../preview-definition';
|
|
14
14
|
import { PreviewMain } from '../preview.main.runtime';
|
|
15
|
-
import { html } from '../
|
|
15
|
+
import { html } from '../bundler/html-template';
|
|
16
16
|
|
|
17
17
|
export const ENV_STRATEGY_ARTIFACT_NAME = 'preview';
|
|
18
18
|
|
package/dist/webpack/html.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":["html","title"],"sources":["html.ts"],"sourcesContent":["/** html template for Previews (docs, compositions, etc) */\nexport function html(title: string) {\n return `\n <!DOCTYPE html>\n <html lang=\"en\">\n <head>\n <meta charset=\"utf-8\">\n <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n <title>${title}</title>\n <script>\n // Allow to use react dev-tools inside the examples\n try { window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = window.parent.__REACT_DEVTOOLS_GLOBAL_HOOK__; } catch {}\n </script>\n <!-- minimal css resets -->\n <style> html { height: 100%; } body { margin: 0; height: 100%; } #root { height: 100%; } </style>\n </head>\n <body>\n <div id=\"root\"></div>\n </body>\n </html>\n `;\n}\n"],"mappings":";;;;;;;AAAA;AACO,SAASA,IAAT,CAAcC,KAAd,EAA6B;EAClC,OAAQ;AACV;AACA;AACA;AACA;AACA;AACA,eAAeA,KAAM;AACrB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GAlBE;AAmBD"}
|
package/dist/webpack/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { html } from './html';
|
package/dist/webpack/index.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
Object.defineProperty(exports, "html", {
|
|
7
|
-
enumerable: true,
|
|
8
|
-
get: function () {
|
|
9
|
-
return _html().html;
|
|
10
|
-
}
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
function _html() {
|
|
14
|
-
const data = require("./html");
|
|
15
|
-
|
|
16
|
-
_html = function () {
|
|
17
|
-
return data;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
return data;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
//# sourceMappingURL=index.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":[],"sources":["index.ts"],"sourcesContent":["export { html } from './html';\n"],"mappings":";;;;;;;;;;;;AAAA;EAAA;;EAAA;IAAA;EAAA;;EAAA;AAAA"}
|
|
Binary file
|
package/webpack/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export { html } from './html';
|