@teambit/preview 0.0.775 → 0.0.778

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 (33) hide show
  1. package/bundler/chunks.ts +2 -7
  2. package/bundler/create-peer-link.spec.ts +33 -0
  3. package/bundler/create-peers-link.ts +48 -0
  4. package/dist/bundler/chunks.d.ts +1 -1
  5. package/dist/bundler/chunks.js +1 -6
  6. package/dist/bundler/chunks.js.map +1 -1
  7. package/dist/bundler/create-peer-link.spec.d.ts +1 -0
  8. package/dist/bundler/create-peer-link.spec.js +42 -0
  9. package/dist/bundler/create-peer-link.spec.js.map +1 -0
  10. package/dist/bundler/create-peers-link.d.ts +2 -0
  11. package/dist/bundler/create-peers-link.js +100 -0
  12. package/dist/bundler/create-peers-link.js.map +1 -0
  13. package/dist/env-preview-template.task.js +13 -3
  14. package/dist/env-preview-template.task.js.map +1 -1
  15. package/dist/generate-link.js +11 -7
  16. package/dist/generate-link.js.map +1 -1
  17. package/dist/html-utils.d.ts +6 -0
  18. package/dist/html-utils.js +43 -0
  19. package/dist/html-utils.js.map +1 -0
  20. package/dist/preview.preview.runtime.js +27 -31
  21. package/dist/preview.preview.runtime.js.map +1 -1
  22. package/dist/strategies/component-strategy.js +13 -10
  23. package/dist/strategies/component-strategy.js.map +1 -1
  24. package/dist/strategies/generate-component-link.js +3 -3
  25. package/dist/strategies/generate-component-link.js.map +1 -1
  26. package/html-utils.tsx +29 -0
  27. package/package-tar/teambit-preview-0.0.778.tgz +0 -0
  28. package/package.json +21 -20
  29. package/{preview-1656559717916.js → preview-1656905226178.js} +3 -3
  30. package/preview.preview.runtime.tsx +19 -32
  31. package/strategies/component-strategy.ts +16 -16
  32. package/strategies/generate-component-link.ts +3 -3
  33. package/package-tar/teambit-preview-0.0.775.tgz +0 -0
@@ -12,6 +12,7 @@ import { ModuleFile, PreviewModule } from './types/preview-module';
12
12
  import { RenderingContext } from './rendering-context';
13
13
  import { fetchComponentAspects } from './gql/fetch-component-aspects';
14
14
  import { PREVIEW_MODULES } from './preview-modules';
15
+ import { loadScript, loadLink } from './html-utils';
15
16
 
16
17
  // forward linkModules() for generate-link.ts
17
18
  export { linkModules } from './preview-modules';
@@ -136,14 +137,17 @@ export class PreviewPreview {
136
137
  // It's a component bundled with the env
137
138
  if (allFiles === null) return {};
138
139
 
139
- allFiles.forEach((file) => {
140
- // We want to run the preview file always last
141
- if (file.endsWith('-preview.js')) {
142
- previewFile = file;
143
- } else {
144
- this.addComponentFileElement(id, file);
145
- }
146
- });
140
+ await Promise.all(
141
+ allFiles.map((file) => {
142
+ // We want to run the preview file always last
143
+ if (file.endsWith('-preview.js')) {
144
+ previewFile = file;
145
+ return undefined;
146
+ }
147
+
148
+ return this.addComponentFileElement(id, file);
149
+ })
150
+ );
147
151
 
148
152
  if (!previewFile) return {};
149
153
  return this.loadPreviewScript(id, name, previewFile);
@@ -177,42 +181,25 @@ export class PreviewPreview {
177
181
  }
178
182
 
179
183
  private addComponentFileScriptElement(id: ComponentID, previewBundleFileName: string) {
180
- const script = document.createElement('script');
181
- script.setAttribute('defer', 'defer');
182
- const stringId = id.toString();
183
184
  const previewRoute = `~aspect/component-preview`;
185
+ const stringId = id.toString();
184
186
  const src = `/api/${stringId}/${previewRoute}/${previewBundleFileName}`;
185
- script.src = src;
186
- document.head.appendChild(script);
187
- return script;
187
+ return loadScript({ src });
188
188
  }
189
189
 
190
190
  private addComponentFileLinkElement(id: ComponentID, previewBundleFileName: string) {
191
- const link = document.createElement('link');
192
191
  const stringId = id.toString();
193
192
  const previewRoute = `~aspect/component-preview`;
194
193
  const href = `/api/${stringId}/${previewRoute}/${previewBundleFileName}`;
195
- link.setAttribute('href', href);
196
- if (previewBundleFileName.endsWith('.css')) {
197
- link.setAttribute('rel', 'stylesheet');
198
- }
199
- document.head.appendChild(link);
200
- return link;
194
+ return loadLink({ href });
201
195
  }
202
196
 
203
197
  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
- });
198
+ const previewRoute = `~aspect/component-preview`;
199
+ const src = `/api/${id.toString()}/${previewRoute}/${previewBundleFileName}`;
200
+ await loadScript({ src });
215
201
 
202
+ // TODO - replace with jsonp
216
203
  const globalId = `${id.toStringWithoutVersion()}-preview`;
217
204
  const componentPreview = window[globalId];
218
205
  if (!componentPreview) throw new PreviewNotFound(previewName);
@@ -119,12 +119,16 @@ export class ComponentBundlingStrategy implements BundlingStrategy {
119
119
  component: Component,
120
120
  context: ComputeTargetsContext
121
121
  ): Promise<ComponentEntry> {
122
- const path = await this.computePaths(previewDefs, context, component);
122
+ const componentPreviewPath = await this.computePaths(previewDefs, context, component);
123
123
  const [componentPath] = this.getPaths(context, component, [component.mainFile]);
124
- const componentPreviewChunkId = this.getComponentChunkId(component.id, 'preview');
124
+
125
+ const chunks = {
126
+ componentPreview: this.getComponentChunkId(component.id, 'preview'),
127
+ component: context.splitComponentBundle ? component.id.toStringWithoutVersion() : undefined,
128
+ };
125
129
 
126
130
  const entries = {
127
- [componentPreviewChunkId]: {
131
+ [chunks.componentPreview]: {
128
132
  filename: this.getComponentChunkFileName(
129
133
  component.id.toString({
130
134
  fsCompatible: true,
@@ -132,17 +136,14 @@ export class ComponentBundlingStrategy implements BundlingStrategy {
132
136
  }),
133
137
  'preview'
134
138
  ),
135
- import: path,
136
- // dependOn: component.id.toStringWithoutVersion(),
137
- library: {
138
- name: componentPreviewChunkId,
139
- type: 'umd',
140
- },
139
+ import: componentPreviewPath,
140
+ dependOn: chunks.component,
141
+ library: { name: chunks.componentPreview, type: 'umd' },
141
142
  },
142
143
  };
143
- if (context.splitComponentBundle) {
144
- const componentChunkId = component.id.toStringWithoutVersion();
145
- entries[componentChunkId] = {
144
+
145
+ if (chunks.component) {
146
+ entries[chunks.component] = {
146
147
  filename: this.getComponentChunkFileName(
147
148
  component.id.toString({
148
149
  fsCompatible: true,
@@ -150,13 +151,12 @@ export class ComponentBundlingStrategy implements BundlingStrategy {
150
151
  }),
151
152
  'component'
152
153
  ),
154
+ dependOn: undefined,
153
155
  import: componentPath,
154
- library: {
155
- name: componentChunkId,
156
- type: 'umd',
157
- },
156
+ library: { name: chunks.component, type: 'umd' },
158
157
  };
159
158
  }
159
+
160
160
  return { component, entries };
161
161
  }
162
162
 
@@ -17,15 +17,15 @@ export function generateComponentLink(modules: ModuleVar[]): string {
17
17
  // import per preview file
18
18
  const importStr: string = links
19
19
  .map(({ entries }) => entries.map(({ path, linkName }) => `import * as ${linkName} from '${path}'`).join(';\n'))
20
- .join('\n');
20
+ .join(';\n');
21
21
 
22
22
  // export files group per preview
23
23
  const exportsString: string = links
24
24
  .map(({ name, entries }) => `export const ${name} = [${entries.map((entry) => entry.linkName).join(', ')}]`)
25
25
  .join(';\n');
26
26
 
27
- return `${importStr}
27
+ return `${importStr};
28
28
 
29
- ${exportsString}
29
+ ${exportsString};
30
30
  `;
31
31
  }