@teambit/preview 0.0.777 → 0.0.780
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 +2 -7
- package/bundler/create-peer-link.spec.ts +33 -0
- package/bundler/create-peers-link.ts +48 -0
- package/dist/artifact-file-middleware.d.ts +1 -1
- package/dist/bundler/chunks.d.ts +1 -1
- package/dist/bundler/chunks.js +1 -6
- package/dist/bundler/chunks.js.map +1 -1
- package/dist/bundler/create-peer-link.spec.d.ts +1 -0
- package/dist/bundler/create-peer-link.spec.js +42 -0
- package/dist/bundler/create-peer-link.spec.js.map +1 -0
- package/dist/bundler/create-peers-link.d.ts +2 -0
- package/dist/bundler/create-peers-link.js +100 -0
- package/dist/bundler/create-peers-link.js.map +1 -0
- package/dist/component-preview.route.d.ts +1 -1
- package/dist/env-preview-template.task.js +13 -3
- package/dist/env-preview-template.task.js.map +1 -1
- package/dist/env-template.route.d.ts +1 -1
- package/dist/generate-link.js +11 -7
- package/dist/generate-link.js.map +1 -1
- package/dist/html-utils.d.ts +6 -0
- package/dist/html-utils.js +43 -0
- package/dist/html-utils.js.map +1 -0
- package/dist/preview-assets.route.d.ts +1 -1
- package/dist/preview.preview.runtime.js +27 -31
- package/dist/preview.preview.runtime.js.map +1 -1
- package/dist/preview.route.d.ts +1 -1
- package/dist/strategies/component-strategy.js +13 -10
- package/dist/strategies/component-strategy.js.map +1 -1
- package/dist/strategies/generate-component-link.js +3 -3
- package/dist/strategies/generate-component-link.js.map +1 -1
- package/html-utils.tsx +29 -0
- package/package-tar/teambit-preview-0.0.780.tgz +0 -0
- package/package.json +24 -23
- package/{preview-1656732493790.js → preview-1657039361770.js} +3 -3
- package/preview.preview.runtime.tsx +19 -32
- package/strategies/component-strategy.ts +16 -16
- package/strategies/generate-component-link.ts +3 -3
- package/package-tar/teambit-preview-0.0.777.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
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
205
|
-
|
|
206
|
-
|
|
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
|
|
122
|
+
const componentPreviewPath = await this.computePaths(previewDefs, context, component);
|
|
123
123
|
const [componentPath] = this.getPaths(context, component, [component.mainFile]);
|
|
124
|
-
|
|
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
|
-
[
|
|
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:
|
|
136
|
-
|
|
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
|
-
|
|
144
|
-
|
|
145
|
-
entries[
|
|
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('
|
|
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
|
}
|
|
Binary file
|