@teambit/preview 0.0.555 → 0.0.559

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/preview.task.ts DELETED
@@ -1,91 +0,0 @@
1
- import { resolve, join } from 'path';
2
- import { ExecutionContext } from '@teambit/envs';
3
- import { BuildContext, BuiltTaskResult, BuildTask, TaskLocation } from '@teambit/builder';
4
- import { Bundler, BundlerContext, BundlerMain, Target } from '@teambit/bundler';
5
- import { Compiler } from '@teambit/compiler';
6
- import { ComponentMap } from '@teambit/component';
7
- import { Capsule } from '@teambit/isolator';
8
- import { AbstractVinyl } from '@teambit/legacy/dist/consumer/component/sources';
9
- import { flatten } from 'lodash';
10
- import { PreviewDefinition } from './preview-definition';
11
- import { PreviewMain } from './preview.main.runtime';
12
-
13
- export class PreviewTask implements BuildTask {
14
- constructor(
15
- /**
16
- * bundler extension.
17
- */
18
- private bundler: BundlerMain,
19
-
20
- /**
21
- * preview extension.
22
- */
23
- private preview: PreviewMain
24
- ) {}
25
-
26
- aspectId = 'teambit.preview/preview';
27
- name = 'GeneratePreview';
28
- location: TaskLocation = 'end';
29
-
30
- async execute(context: BuildContext): Promise<BuiltTaskResult> {
31
- const defs = this.preview.getDefs();
32
- const url = `/preview/${context.envRuntime.id}`;
33
- const bundlingStrategy = this.preview.getBundlingStrategy();
34
-
35
- const targets: Target[] = await bundlingStrategy.computeTargets(context, defs, this);
36
-
37
- const bundlerContext: BundlerContext = Object.assign(context, {
38
- targets,
39
- entry: [],
40
- publicPath: this.getPreviewDirectory(context),
41
- rootPath: url,
42
- });
43
-
44
- const bundler: Bundler = await context.env.getBundler(bundlerContext, []);
45
- const bundlerResults = await bundler.run();
46
-
47
- return bundlingStrategy.computeResults(bundlerContext, bundlerResults, this);
48
- }
49
-
50
- async computePaths(capsule: Capsule, defs: PreviewDefinition[], context: BuildContext): Promise<string[]> {
51
- const previewMain = await this.preview.writePreviewRuntime(context);
52
-
53
- const moduleMapsPromise = defs.map(async (previewDef) => {
54
- const moduleMap = await previewDef.getModuleMap([capsule.component]);
55
- const paths = this.getPathsFromMap(capsule, moduleMap, context);
56
- const template = previewDef.renderTemplatePath ? await previewDef.renderTemplatePath(context) : 'undefined';
57
-
58
- const link = this.preview.writeLink(
59
- previewDef.prefix,
60
- paths,
61
- previewDef.renderTemplatePath ? await previewDef.renderTemplatePath(context) : undefined,
62
- capsule.path
63
- );
64
-
65
- const files = flatten(paths.toArray().map(([, file]) => file)).concat([link]);
66
-
67
- if (template) return files.concat([template]);
68
- return files;
69
- });
70
-
71
- const moduleMaps = await Promise.all(moduleMapsPromise);
72
-
73
- return flatten(moduleMaps.concat([previewMain]));
74
- }
75
-
76
- getPreviewDirectory(context: ExecutionContext) {
77
- const outputPath = resolve(`${context.id}/public`);
78
- return outputPath;
79
- }
80
-
81
- getPathsFromMap(
82
- capsule: Capsule,
83
- moduleMap: ComponentMap<AbstractVinyl[]>,
84
- context: BuildContext
85
- ): ComponentMap<string[]> {
86
- const compiler: Compiler = context.env.getCompiler(context);
87
- return moduleMap.map((files) => {
88
- return files.map((file) => join(capsule.path, compiler.getDistPathBySrcPath(file.relative)));
89
- });
90
- }
91
- }
@@ -1,12 +0,0 @@
1
- import { RenderingContextSlot } from './preview.preview.runtime';
2
-
3
- export class RenderingContext {
4
- constructor(private contexts: RenderingContextSlot) {}
5
-
6
- /**
7
- * obtain rendering context of a specific aspect.
8
- */
9
- get(aspectId: string) {
10
- return this.contexts.get(aspectId)?.();
11
- }
12
- }
@@ -1,34 +0,0 @@
1
- import { BuildContext } from '@teambit/builder';
2
- import { Target, BundlerResult, BundlerContext } from '@teambit/bundler';
3
- import { BundlingStrategy } from '../bundling-strategy';
4
- import { PreviewDefinition } from '../preview-definition';
5
- import { PreviewTask } from '../preview.task';
6
-
7
- export class ComponentBundlingStrategy implements BundlingStrategy {
8
- name = 'component';
9
-
10
- computeTargets(context: BuildContext, previewDefs: PreviewDefinition[], previewTask: PreviewTask): Promise<Target[]> {
11
- return Promise.all(
12
- context.capsuleNetwork.graphCapsules.map(async (capsule) => {
13
- return {
14
- entries: await previewTask.computePaths(capsule, previewDefs, context),
15
- components: [capsule.component],
16
- outputPath: capsule.path,
17
- };
18
- })
19
- );
20
- }
21
-
22
- async computeResults(context: BundlerContext, results: BundlerResult[], previewTask: PreviewTask) {
23
- return {
24
- componentsResults: results.map((result) => {
25
- return {
26
- errors: result.errors,
27
- component: result.components[0],
28
- warning: result.warnings,
29
- };
30
- }),
31
- artifacts: [{ name: 'preview', globPatterns: [previewTask.getPreviewDirectory(context)] }],
32
- };
33
- }
34
- }
@@ -1,118 +0,0 @@
1
- import { join, resolve } from 'path';
2
- import { existsSync, mkdirpSync } from 'fs-extra';
3
- import { flatten } from 'lodash';
4
- import { ComponentMap } from '@teambit/component';
5
- import { Compiler } from '@teambit/compiler';
6
- import { AbstractVinyl } from '@teambit/legacy/dist/consumer/component/sources';
7
- import { Capsule } from '@teambit/isolator';
8
- import { BuildContext, ComponentResult } from '@teambit/builder';
9
- import { BundlerResult, BundlerContext } from '@teambit/bundler';
10
- import { BundlingStrategy } from '../bundling-strategy';
11
- import { PreviewDefinition } from '../preview-definition';
12
- import { PreviewMain } from '../preview.main.runtime';
13
-
14
- /**
15
- * bundles all components in a given env into the same bundle.
16
- */
17
- export class EnvBundlingStrategy implements BundlingStrategy {
18
- name = 'env';
19
-
20
- constructor(private preview: PreviewMain) {}
21
-
22
- async computeTargets(context: BuildContext, previewDefs: PreviewDefinition[]) {
23
- const outputPath = this.getOutputPath(context);
24
- if (!existsSync(outputPath)) mkdirpSync(outputPath);
25
-
26
- return [
27
- {
28
- entries: await this.computePaths(outputPath, previewDefs, context),
29
- components: context.components,
30
- outputPath,
31
- },
32
- ];
33
- }
34
-
35
- async computeResults(context: BundlerContext, results: BundlerResult[]) {
36
- const result = results[0];
37
-
38
- const componentsResults: ComponentResult[] = result.components.map((component) => {
39
- return {
40
- component,
41
- errors: result.errors.map((err) => (typeof err === 'string' ? err : err.message)),
42
- warning: result.warnings,
43
- startTime: result.startTime,
44
- endTime: result.endTime,
45
- };
46
- });
47
-
48
- const artifacts = this.getArtifactDef(context);
49
-
50
- return {
51
- componentsResults,
52
- artifacts,
53
- };
54
- }
55
-
56
- private getArtifactDef(context: BuildContext) {
57
- // eslint-disable-next-line @typescript-eslint/prefer-as-const
58
- const env: 'env' = 'env';
59
- const rootDir = this.getDirName(context);
60
-
61
- return [
62
- {
63
- name: 'preview',
64
- globPatterns: ['public/**'],
65
- rootDir,
66
- context: env,
67
- },
68
- ];
69
- }
70
-
71
- getDirName(context: BuildContext) {
72
- const envName = context.id.replace('/', '__');
73
- return `${envName}-preview`;
74
- }
75
-
76
- private getOutputPath(context: BuildContext) {
77
- return resolve(`${context.capsuleNetwork.capsulesRootDir}/${this.getDirName(context)}`);
78
- }
79
-
80
- private getPaths(context: BuildContext, files: AbstractVinyl[], capsule: Capsule) {
81
- const compiler: Compiler = context.env.getCompiler();
82
- return files.map((file) => join(capsule.path, compiler.getDistPathBySrcPath(file.relative)));
83
- }
84
-
85
- private async computePaths(outputPath: string, defs: PreviewDefinition[], context: BuildContext): Promise<string[]> {
86
- const previewMain = await this.preview.writePreviewRuntime(context);
87
- const moduleMapsPromise = defs.map(async (previewDef) => {
88
- const moduleMap = await previewDef.getModuleMap(context.components);
89
-
90
- const paths = ComponentMap.as(context.components, (component) => {
91
- const capsule = context.capsuleNetwork.graphCapsules.getCapsule(component.id);
92
- const maybeFiles = moduleMap.get(component);
93
- if (!maybeFiles || !capsule) return [];
94
- const [, files] = maybeFiles;
95
- const compiledPaths = this.getPaths(context, files, capsule);
96
- return compiledPaths;
97
- });
98
-
99
- const template = previewDef.renderTemplatePath ? await previewDef.renderTemplatePath(context) : 'undefined';
100
-
101
- const link = this.preview.writeLink(
102
- previewDef.prefix,
103
- paths,
104
- previewDef.renderTemplatePath ? await previewDef.renderTemplatePath(context) : undefined,
105
- outputPath
106
- );
107
-
108
- const files = flatten(paths.toArray().map(([, file]) => file)).concat([link]);
109
-
110
- if (template) return files.concat([template]);
111
- return files;
112
- });
113
-
114
- const moduleMaps = await Promise.all(moduleMapsPromise);
115
-
116
- return flatten(moduleMaps.concat([previewMain]));
117
- }
118
- }
@@ -1,2 +0,0 @@
1
- export { EnvBundlingStrategy } from './env-strategy';
2
- export { ComponentBundlingStrategy } from './component-strategy';
package/types/asset.d.ts DELETED
@@ -1,29 +0,0 @@
1
- declare module '*.png' {
2
- const value: any;
3
- export = value;
4
- }
5
- declare module '*.svg' {
6
- import type { FunctionComponent, SVGProps } from 'react';
7
-
8
- export const ReactComponent: FunctionComponent<SVGProps<SVGSVGElement> & { title?: string }>;
9
- const src: string;
10
- export default src;
11
- }
12
-
13
- // @TODO Gilad
14
- declare module '*.jpg' {
15
- const value: any;
16
- export = value;
17
- }
18
- declare module '*.jpeg' {
19
- const value: any;
20
- export = value;
21
- }
22
- declare module '*.gif' {
23
- const value: any;
24
- export = value;
25
- }
26
- declare module '*.bmp' {
27
- const value: any;
28
- export = value;
29
- }
@@ -1,15 +0,0 @@
1
- /**
2
- * A full index of the preview data
3
- */
4
- export type PreviewModule<T = any> = {
5
- /** Dictionary mapping components to their module files. */
6
- componentMap: Record<string, ModuleFile<T>[]>;
7
-
8
- /** The 'main file' for this Preview type */
9
- mainModule: {
10
- default: (...args: any[]) => void;
11
- };
12
- };
13
-
14
- /** single preview module, e.g. compositions file */
15
- export type ModuleFile<T = any> = Record<string, T>;
package/types/style.d.ts DELETED
@@ -1,42 +0,0 @@
1
- declare module '*.module.css' {
2
- const classes: { readonly [key: string]: string };
3
- export default classes;
4
- }
5
- declare module '*.module.scss' {
6
- const classes: { readonly [key: string]: string };
7
- export default classes;
8
- }
9
- declare module '*.module.sass' {
10
- const classes: { readonly [key: string]: string };
11
- export default classes;
12
- }
13
-
14
- declare module '*.module.less' {
15
- const classes: { readonly [key: string]: string };
16
- export default classes;
17
- }
18
-
19
- declare module '*.less' {
20
- const classes: { readonly [key: string]: string };
21
- export default classes;
22
- }
23
-
24
- declare module '*.css' {
25
- const classes: { readonly [key: string]: string };
26
- export default classes;
27
- }
28
-
29
- declare module '*.sass' {
30
- const classes: { readonly [key: string]: string };
31
- export default classes;
32
- }
33
-
34
- declare module '*.scss' {
35
- const classes: { readonly [key: string]: string };
36
- export default classes;
37
- }
38
-
39
- declare module '*.mdx' {
40
- const component: any;
41
- export default component;
42
- }