@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.
Files changed (35) hide show
  1. package/bundler/chunks.ts +51 -0
  2. package/bundler/html-plugin.ts +24 -0
  3. package/{webpack/html.ts → bundler/html-template.ts} +0 -0
  4. package/dist/bundler/chunks.d.ts +17 -0
  5. package/dist/bundler/chunks.js +68 -0
  6. package/dist/bundler/chunks.js.map +1 -0
  7. package/dist/bundler/html-plugin.d.ts +5 -0
  8. package/dist/bundler/html-plugin.js +56 -0
  9. package/dist/bundler/html-plugin.js.map +1 -0
  10. package/dist/{webpack/html.d.ts → bundler/html-template.d.ts} +0 -0
  11. package/dist/{webpack/html.js → bundler/html-template.js} +1 -1
  12. package/dist/bundler/html-template.js.map +1 -0
  13. package/dist/env-preview-template.task.d.ts +4 -9
  14. package/dist/env-preview-template.task.js +64 -74
  15. package/dist/env-preview-template.task.js.map +1 -1
  16. package/dist/preview-modules.d.ts +10 -0
  17. package/dist/preview-modules.js +54 -0
  18. package/dist/preview-modules.js.map +1 -0
  19. package/dist/preview.preview.runtime.d.ts +8 -5
  20. package/dist/preview.preview.runtime.js +76 -43
  21. package/dist/preview.preview.runtime.js.map +1 -1
  22. package/dist/strategies/env-strategy.js +4 -4
  23. package/dist/strategies/env-strategy.js.map +1 -1
  24. package/package-tar/teambit-preview-0.0.774.tgz +0 -0
  25. package/package.json +23 -21
  26. package/{preview-1656339215962.js → preview-1656473363813.js} +2 -2
  27. package/preview-modules.tsx +28 -0
  28. package/preview.preview.runtime.tsx +71 -51
  29. package/strategies/env-strategy.ts +1 -1
  30. package/dist/webpack/html.js.map +0 -1
  31. package/dist/webpack/index.d.ts +0 -1
  32. package/dist/webpack/index.js +0 -23
  33. package/dist/webpack/index.js.map +0 -1
  34. package/package-tar/teambit-preview-0.0.771.tgz +0 -0
  35. 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
- export type PreviewSlot = SlotRegistry<PreviewType>;
16
+ // forward linkModules() for generate-link.ts
17
+ export { linkModules } from './preview-modules';
16
18
 
17
- const PREVIEW_MODULES: Record<string, PreviewModule> = {};
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 (prevName) => {
65
- const includedPreview = this.getPreview(prevName);
92
+ (preview.include || []).map(async (inclPreviewName) => {
93
+ const includedPreview = this.getPreview(inclPreviewName);
66
94
  if (!includedPreview) return undefined;
67
95
 
68
- return includedPreview.selectPreviewModel?.(
69
- componentId.fullName,
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(name: string, id: ComponentID, parentPreviewName?: string): Promise<PreviewModule> {
88
- const relevantModel = PREVIEW_MODULES[name];
89
- if (relevantModel.componentMap[id.fullName]) return relevantModel;
90
-
91
- let component;
92
- // Handle case when there is overview but no composition on the workspace dev server
93
- if (!parentPreviewName || !PREVIEW_MODULES[parentPreviewName].componentMap[id.fullName]) {
94
- // if (!window[name]) throw new PreviewNotFound(name);
95
- // const isSplitComponentBundle = relevantModel.isSplitComponentBundle ?? false;
96
- // const component = window[id.toStringWithoutVersion()];
97
- component = await this.fetchComponentPreview(id, name);
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
- return Promise.resolve(undefined);
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
- return new Promise((resolve, reject) => {
124
- const previewScriptElement = this.getPreviewScriptElement(id, name, previewFile, resolve, reject);
125
- document.head.appendChild(previewScriptElement);
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 getPreviewScriptElement(id: ComponentID, name: string, previewBundleFileName: string, resolve, reject) {
181
- const script = document.createElement('script');
182
- script.setAttribute('defer', 'defer');
183
- const stringId = id.toString();
184
- // const previewRoute = `~aspect/preview`;
185
- const previewRoute = `~aspect/component-preview`;
186
- const src = `/api/${stringId}/${previewRoute}/${previewBundleFileName}`;
187
- script.src = src; // generate path to remote scope. [scope url]/
188
- script.onload = () => {
189
- const componentPreview = window[`${id.toStringWithoutVersion()}-preview`];
190
- if (!componentPreview) return reject(new PreviewNotFound(name));
191
- const targetPreview = componentPreview[name];
192
- if (!targetPreview) return resolve(undefined);
193
-
194
- return resolve(targetPreview);
195
- };
196
- return script;
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 '../webpack';
15
+ import { html } from '../bundler/html-template';
16
16
 
17
17
  export const ENV_STRATEGY_ARTIFACT_NAME = 'preview';
18
18
 
@@ -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"}
@@ -1 +0,0 @@
1
- export { html } from './html';
@@ -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"}
package/webpack/index.ts DELETED
@@ -1 +0,0 @@
1
- export { html } from './html';