@teambit/preview 0.0.621 → 0.0.629
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-1642994756471.js +1 -0
- package/dist/artifact-file-middleware.d.ts +12 -0
- package/dist/artifact-file-middleware.js +81 -0
- package/dist/artifact-file-middleware.js.map +1 -0
- package/dist/bundling-strategy.d.ts +6 -1
- package/dist/component-preview.route.d.ts +20 -0
- package/dist/component-preview.route.js +83 -0
- package/dist/component-preview.route.js.map +1 -0
- package/dist/env-preview-template.task.d.ts +42 -0
- package/dist/env-preview-template.task.js +309 -0
- package/dist/env-preview-template.task.js.map +1 -0
- package/dist/env-template.route.d.ts +26 -0
- package/dist/env-template.route.js +89 -0
- package/dist/env-template.route.js.map +1 -0
- package/dist/generate-link.d.ts +1 -1
- package/dist/generate-link.js +3 -3
- package/dist/generate-link.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js.map +1 -1
- package/dist/preview-artifact.d.ts +2 -0
- package/dist/preview-artifact.js +21 -0
- package/dist/preview-artifact.js.map +1 -1
- package/dist/preview-assets.route.d.ts +19 -0
- package/dist/preview-assets.route.js +66 -0
- package/dist/preview-assets.route.js.map +1 -0
- package/dist/preview-definition.d.ts +16 -3
- package/dist/preview.graphql.d.ts +8 -1
- package/dist/preview.graphql.js +13 -2
- package/dist/preview.graphql.js.map +1 -1
- package/dist/preview.main.runtime.d.ts +78 -11
- package/dist/preview.main.runtime.js +251 -18
- package/dist/preview.main.runtime.js.map +1 -1
- package/dist/preview.preview.runtime.d.ts +11 -2
- package/dist/preview.preview.runtime.js +124 -6
- package/dist/preview.preview.runtime.js.map +1 -1
- package/dist/preview.route.d.ts +3 -7
- package/dist/preview.route.js +22 -21
- package/dist/preview.route.js.map +1 -1
- package/dist/preview.task.d.ts +2 -2
- package/dist/preview.task.js +25 -24
- package/dist/preview.task.js.map +1 -1
- package/dist/strategies/component-strategy.d.ts +52 -12
- package/dist/strategies/component-strategy.js +348 -20
- package/dist/strategies/component-strategy.js.map +1 -1
- package/dist/strategies/env-strategy.d.ts +7 -5
- package/dist/strategies/env-strategy.js +27 -1
- package/dist/strategies/env-strategy.js.map +1 -1
- package/dist/strategies/generate-component-link.d.ts +5 -0
- package/dist/strategies/generate-component-link.js +25 -0
- package/dist/strategies/generate-component-link.js.map +1 -0
- package/dist/types/preview-module.d.ts +1 -0
- package/dist/webpack/html.d.ts +2 -0
- package/dist/webpack/html.js +31 -0
- package/dist/webpack/html.js.map +1 -0
- package/dist/webpack/index.d.ts +1 -0
- package/dist/webpack/index.js +23 -0
- package/dist/webpack/index.js.map +1 -0
- package/package-tar/teambit-preview-0.0.629.tgz +0 -0
- package/package.json +29 -26
- package/preview.main.runtime.tsx +235 -23
- package/preview.preview.runtime.tsx +123 -7
- package/strategies/component-strategy.ts +329 -23
- package/strategies/env-strategy.ts +28 -10
- package/strategies/generate-component-link.ts +19 -0
- package/types/preview-module.ts +2 -0
- package/webpack/html.ts +22 -0
- package/webpack/index.ts +1 -0
- package/package-tar/teambit-preview-0.0.621.tgz +0 -0
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.html = html;
|
|
7
|
+
|
|
8
|
+
/** html template for Previews (docs, compositions, etc) */
|
|
9
|
+
function html(title) {
|
|
10
|
+
return `
|
|
11
|
+
<!DOCTYPE html>
|
|
12
|
+
<html lang="en">
|
|
13
|
+
<head>
|
|
14
|
+
<meta charset="utf-8">
|
|
15
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
16
|
+
<title>${title}</title>
|
|
17
|
+
<script>
|
|
18
|
+
// Allow to use react dev-tools inside the examples
|
|
19
|
+
try { window.__REACT_DEVTOOLS_GLOBAL_HOOK__ = window.parent.__REACT_DEVTOOLS_GLOBAL_HOOK__; } catch {}
|
|
20
|
+
</script>
|
|
21
|
+
<!-- minimal css resets -->
|
|
22
|
+
<style> html { height: 100%; } body { margin: 0; height: 100%; } #root { height: 100%; } </style>
|
|
23
|
+
</head>
|
|
24
|
+
<body>
|
|
25
|
+
<div id="root"></div>
|
|
26
|
+
</body>
|
|
27
|
+
</html>
|
|
28
|
+
`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
//# sourceMappingURL=html.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["html.ts"],"names":["html","title"],"mappings":";;;;;;;AAAA;AACO,SAASA,IAAT,CAAcC,KAAd,EAA6B;AAClC,SAAQ;AACV;AACA;AACA;AACA;AACA;AACA,eAAeA,KAAM;AACrB;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GAlBE;AAmBD","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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { html } from './html';
|
|
@@ -0,0 +1,23 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA","sourcesContent":["export { html } from './html';\n"]}
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,48 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@teambit/preview",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.629",
|
|
4
4
|
"homepage": "https://bit.dev/teambit/preview/preview",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"componentId": {
|
|
7
7
|
"scope": "teambit.preview",
|
|
8
8
|
"name": "preview",
|
|
9
|
-
"version": "0.0.
|
|
9
|
+
"version": "0.0.629"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
+
"mime": "2.5.2",
|
|
12
13
|
"fs-extra": "10.0.0",
|
|
14
|
+
"lodash": "4.17.21",
|
|
13
15
|
"@teambit/harmony": "0.2.11",
|
|
14
16
|
"graphql-tag": "2.12.1",
|
|
15
|
-
"lodash": "4.17.21",
|
|
16
17
|
"object-hash": "2.1.1",
|
|
17
|
-
"
|
|
18
|
+
"cross-fetch": "3.0.6",
|
|
18
19
|
"@babel/runtime": "7.12.18",
|
|
19
20
|
"core-js": "^3.0.0",
|
|
20
|
-
"@teambit/
|
|
21
|
-
"@teambit/
|
|
22
|
-
"@teambit/component": "0.0.621",
|
|
23
|
-
"@teambit/envs": "0.0.621",
|
|
24
|
-
"@teambit/toolbox.path.to-windows-compatible-path": "0.0.482",
|
|
25
|
-
"@teambit/aspect-loader": "0.0.621",
|
|
26
|
-
"@teambit/cli": "0.0.427",
|
|
27
|
-
"@teambit/logger": "0.0.516",
|
|
28
|
-
"@teambit/pkg": "0.0.621",
|
|
29
|
-
"@teambit/pubsub": "0.0.621",
|
|
30
|
-
"@teambit/ui": "0.0.621",
|
|
31
|
-
"@teambit/workspace": "0.0.621",
|
|
32
|
-
"@teambit/component-id": "0.0.398",
|
|
33
|
-
"@teambit/express": "0.0.520",
|
|
21
|
+
"@teambit/express": "0.0.526",
|
|
22
|
+
"@teambit/logger": "0.0.522",
|
|
34
23
|
"@teambit/ui-foundation.ui.pages.static-error": "0.0.70",
|
|
35
|
-
"@teambit/
|
|
36
|
-
"@teambit/
|
|
37
|
-
"@teambit/
|
|
38
|
-
"@teambit/
|
|
39
|
-
"@teambit/
|
|
24
|
+
"@teambit/builder": "0.0.629",
|
|
25
|
+
"@teambit/bundler": "0.0.629",
|
|
26
|
+
"@teambit/component": "0.0.629",
|
|
27
|
+
"@teambit/envs": "0.0.629",
|
|
28
|
+
"@teambit/isolator": "0.0.629",
|
|
29
|
+
"@teambit/toolbox.path.to-windows-compatible-path": "0.0.482",
|
|
30
|
+
"@teambit/aspect-loader": "0.0.629",
|
|
31
|
+
"@teambit/bit-error": "0.0.394",
|
|
32
|
+
"@teambit/cli": "0.0.433",
|
|
33
|
+
"@teambit/dependency-resolver": "0.0.629",
|
|
34
|
+
"@teambit/graphql": "0.0.629",
|
|
35
|
+
"@teambit/pkg": "0.0.629",
|
|
36
|
+
"@teambit/pubsub": "0.0.629",
|
|
37
|
+
"@teambit/ui": "0.0.629",
|
|
38
|
+
"@teambit/workspace": "0.0.629",
|
|
39
|
+
"@teambit/component-id": "0.0.400",
|
|
40
|
+
"@teambit/compiler": "0.0.629",
|
|
41
|
+
"@teambit/preview.cli.preview-server-status": "0.0.490",
|
|
42
|
+
"@teambit/preview.cli.webpack-events-listener": "0.0.160"
|
|
40
43
|
},
|
|
41
44
|
"devDependencies": {
|
|
45
|
+
"@types/mime": "2.0.3",
|
|
42
46
|
"@types/fs-extra": "9.0.7",
|
|
43
47
|
"@types/lodash": "4.14.165",
|
|
44
48
|
"@types/object-hash": "1.3.4",
|
|
45
|
-
"@types/mime": "2.0.3",
|
|
46
49
|
"@types/react": "^17.0.8",
|
|
47
50
|
"@types/mocha": "5.2.7",
|
|
48
51
|
"@types/testing-library__jest-dom": "5.9.5",
|
|
@@ -52,7 +55,7 @@
|
|
|
52
55
|
"@teambit/preview.aspect-docs.preview": "0.0.131"
|
|
53
56
|
},
|
|
54
57
|
"peerDependencies": {
|
|
55
|
-
"@teambit/legacy": "1.0.
|
|
58
|
+
"@teambit/legacy": "1.0.209",
|
|
56
59
|
"react-dom": "^16.8.0 || ^17.0.0",
|
|
57
60
|
"react": "^16.8.0 || ^17.0.0"
|
|
58
61
|
},
|
|
@@ -80,7 +83,7 @@
|
|
|
80
83
|
"react": "-"
|
|
81
84
|
},
|
|
82
85
|
"peerDependencies": {
|
|
83
|
-
"@teambit/legacy": "1.0.
|
|
86
|
+
"@teambit/legacy": "1.0.209",
|
|
84
87
|
"react-dom": "^16.8.0 || ^17.0.0",
|
|
85
88
|
"react": "^16.8.0 || ^17.0.0"
|
|
86
89
|
}
|
package/preview.main.runtime.tsx
CHANGED
|
@@ -1,31 +1,49 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ArtifactFactory, BuilderAspect } from '@teambit/builder';
|
|
2
|
+
import type { BuilderMain } from '@teambit/builder';
|
|
3
|
+
import { Asset, BundlerAspect, BundlerMain } from '@teambit/bundler';
|
|
3
4
|
import { PubsubAspect, PubsubMain } from '@teambit/pubsub';
|
|
4
5
|
import { MainRuntime } from '@teambit/cli';
|
|
5
6
|
import { Component, ComponentAspect, ComponentMain, ComponentMap, ComponentID } from '@teambit/component';
|
|
6
|
-
import { EnvsAspect
|
|
7
|
+
import { EnvsAspect } from '@teambit/envs';
|
|
8
|
+
import type { EnvsMain, ExecutionContext, PreviewEnv } from '@teambit/envs';
|
|
7
9
|
import { Slot, SlotRegistry, Harmony } from '@teambit/harmony';
|
|
8
10
|
import { UIAspect, UiMain } from '@teambit/ui';
|
|
9
11
|
import { CACHE_ROOT } from '@teambit/legacy/dist/constants';
|
|
12
|
+
import { BitError } from '@teambit/bit-error';
|
|
10
13
|
import objectHash from 'object-hash';
|
|
11
14
|
import { uniq } from 'lodash';
|
|
12
15
|
import { writeFileSync, existsSync, mkdirSync } from 'fs-extra';
|
|
13
16
|
import { join } from 'path';
|
|
14
17
|
import { PkgAspect, PkgMain } from '@teambit/pkg';
|
|
15
|
-
import {
|
|
18
|
+
import { AspectLoaderAspect, getAspectDir, getAspectDirFromBvm } from '@teambit/aspect-loader';
|
|
19
|
+
import type { AspectDefinition, AspectLoaderMain } from '@teambit/aspect-loader';
|
|
16
20
|
import WorkspaceAspect, { Workspace } from '@teambit/workspace';
|
|
17
21
|
import { LoggerAspect, LoggerMain, Logger } from '@teambit/logger';
|
|
22
|
+
import { DependencyResolverAspect } from '@teambit/dependency-resolver';
|
|
23
|
+
import type { DependencyResolverMain } from '@teambit/dependency-resolver';
|
|
24
|
+
import { ArtifactFiles } from '@teambit/legacy/dist/consumer/component/sources/artifact-files';
|
|
25
|
+
import GraphqlAspect, { GraphqlMain } from '@teambit/graphql';
|
|
18
26
|
import { BundlingStrategyNotFound } from './exceptions';
|
|
19
27
|
import { generateLink } from './generate-link';
|
|
20
28
|
import { PreviewArtifact } from './preview-artifact';
|
|
21
29
|
import { PreviewDefinition } from './preview-definition';
|
|
22
30
|
import { PreviewAspect, PreviewRuntime } from './preview.aspect';
|
|
23
31
|
import { PreviewRoute } from './preview.route';
|
|
24
|
-
import { PreviewTask } from './preview.task';
|
|
32
|
+
import { PreviewTask, PREVIEW_TASK_NAME } from './preview.task';
|
|
25
33
|
import { BundlingStrategy } from './bundling-strategy';
|
|
26
34
|
import { EnvBundlingStrategy, ComponentBundlingStrategy } from './strategies';
|
|
27
35
|
import { ExecutionRef } from './execution-ref';
|
|
28
36
|
import { PreviewStartPlugin } from './preview.start-plugin';
|
|
37
|
+
import {
|
|
38
|
+
EnvPreviewTemplateTask,
|
|
39
|
+
GENERATE_ENV_TEMPLATE_TASK_NAME,
|
|
40
|
+
getArtifactDef as getEnvTemplateArtifactDef,
|
|
41
|
+
} from './env-preview-template.task';
|
|
42
|
+
import { EnvTemplateRoute } from './env-template.route';
|
|
43
|
+
import { ComponentPreviewRoute } from './component-preview.route';
|
|
44
|
+
import { COMPONENT_STRATEGY_ARTIFACT_NAME, COMPONENT_STRATEGY_SIZE_KEY_NAME } from './strategies/component-strategy';
|
|
45
|
+
import { previewSchema } from './preview.graphql';
|
|
46
|
+
import { PreviewAssetsRoute } from './preview-assets.route';
|
|
29
47
|
|
|
30
48
|
const noopResult = {
|
|
31
49
|
results: [],
|
|
@@ -36,13 +54,38 @@ const DEFAULT_TEMP_DIR = join(CACHE_ROOT, PreviewAspect.id);
|
|
|
36
54
|
|
|
37
55
|
export type PreviewDefinitionRegistry = SlotRegistry<PreviewDefinition>;
|
|
38
56
|
|
|
57
|
+
type PreviewFiles = {
|
|
58
|
+
files: string[];
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
export type ComponentPreviewSizedFile = Asset;
|
|
62
|
+
|
|
63
|
+
export type ComponentPreviewSize = {
|
|
64
|
+
files: ComponentPreviewSizedFile[];
|
|
65
|
+
assets: ComponentPreviewSizedFile[];
|
|
66
|
+
totalFiles: number;
|
|
67
|
+
totalAssets: number;
|
|
68
|
+
total: number;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
export type ComponentPreviewMetaData = {
|
|
72
|
+
size?: ComponentPreviewSize;
|
|
73
|
+
};
|
|
74
|
+
|
|
39
75
|
export type PreviewConfig = {
|
|
40
|
-
bundlingStrategy
|
|
76
|
+
bundlingStrategy?: string;
|
|
41
77
|
disabled: boolean;
|
|
42
78
|
};
|
|
43
79
|
|
|
80
|
+
export type EnvPreviewConfig = {
|
|
81
|
+
strategyName?: string;
|
|
82
|
+
splitComponentBundle?: boolean;
|
|
83
|
+
};
|
|
84
|
+
|
|
44
85
|
export type BundlingStrategySlot = SlotRegistry<BundlingStrategy>;
|
|
45
86
|
|
|
87
|
+
export type GenerateLinkFn = (prefix: string, componentMap: ComponentMap<string[]>, defaultModule?: string) => string;
|
|
88
|
+
|
|
46
89
|
export class PreviewMain {
|
|
47
90
|
constructor(
|
|
48
91
|
/**
|
|
@@ -59,6 +102,8 @@ export class PreviewMain {
|
|
|
59
102
|
|
|
60
103
|
private envs: EnvsMain,
|
|
61
104
|
|
|
105
|
+
private componentAspect: ComponentMain,
|
|
106
|
+
|
|
62
107
|
private pkg: PkgMain,
|
|
63
108
|
|
|
64
109
|
private aspectLoader: AspectLoaderMain,
|
|
@@ -71,20 +116,139 @@ export class PreviewMain {
|
|
|
71
116
|
|
|
72
117
|
private workspace: Workspace | undefined,
|
|
73
118
|
|
|
74
|
-
private logger: Logger
|
|
119
|
+
private logger: Logger,
|
|
120
|
+
|
|
121
|
+
private dependencyResolver: DependencyResolverMain
|
|
75
122
|
) {}
|
|
76
123
|
|
|
77
124
|
get tempFolder(): string {
|
|
78
125
|
return this.workspace?.getTempDir(PreviewAspect.id) || DEFAULT_TEMP_DIR;
|
|
79
126
|
}
|
|
80
127
|
|
|
128
|
+
getComponentBundleSize(component: Component): ComponentPreviewSize | undefined {
|
|
129
|
+
const data = this.builder.getDataByAspect(component, PreviewAspect.id);
|
|
130
|
+
|
|
131
|
+
if (!data) return undefined;
|
|
132
|
+
return data[COMPONENT_STRATEGY_SIZE_KEY_NAME];
|
|
133
|
+
}
|
|
134
|
+
|
|
81
135
|
async getPreview(component: Component): Promise<PreviewArtifact | undefined> {
|
|
82
|
-
const artifacts = await this.builder.
|
|
83
|
-
|
|
136
|
+
const artifacts = await this.builder.getArtifactsVinylByExtensionAndTaskName(
|
|
137
|
+
component,
|
|
138
|
+
PreviewAspect.id,
|
|
139
|
+
PREVIEW_TASK_NAME
|
|
140
|
+
);
|
|
141
|
+
if (!artifacts || !artifacts.length) return undefined;
|
|
142
|
+
return new PreviewArtifact(artifacts);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Get a list of all the artifact files generated during the GeneratePreview task
|
|
147
|
+
* @param component
|
|
148
|
+
* @returns
|
|
149
|
+
*/
|
|
150
|
+
async getPreviewFiles(component: Component): Promise<PreviewFiles | undefined> {
|
|
151
|
+
const artifacts = await this.getPreview(component);
|
|
152
|
+
if (!artifacts) return undefined;
|
|
153
|
+
return {
|
|
154
|
+
files: artifacts.getPaths(),
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Check if the component preview bundle contain the env as part of the bundle or only the component code
|
|
160
|
+
* (we used in the past to bundle them together, there might also be specific envs which still uses the env strategy)
|
|
161
|
+
* @param component
|
|
162
|
+
* @returns
|
|
163
|
+
*/
|
|
164
|
+
async isBundledWithEnv(component: Component): Promise<boolean> {
|
|
165
|
+
const artifacts = await this.builder.getArtifactsVinylByExtensionAndName(
|
|
166
|
+
component,
|
|
167
|
+
PreviewAspect.id,
|
|
168
|
+
COMPONENT_STRATEGY_ARTIFACT_NAME
|
|
169
|
+
);
|
|
170
|
+
if (!artifacts || !artifacts.length) return true;
|
|
171
|
+
|
|
172
|
+
return false;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Getting the env template artifact
|
|
177
|
+
* This should be called with the env itself or it will return undefined
|
|
178
|
+
* If you want to get the env template from the env of the component,
|
|
179
|
+
* use: getEnvTemplateFromComponentEnv below
|
|
180
|
+
*
|
|
181
|
+
* @param component
|
|
182
|
+
* @returns
|
|
183
|
+
*/
|
|
184
|
+
async getEnvTemplate(component: Component): Promise<PreviewArtifact | undefined> {
|
|
185
|
+
const artifacts = await this.builder.getArtifactsVinylByExtensionAndTaskName(
|
|
186
|
+
component,
|
|
187
|
+
PreviewAspect.id,
|
|
188
|
+
GENERATE_ENV_TEMPLATE_TASK_NAME
|
|
189
|
+
);
|
|
190
|
+
if (!artifacts || !artifacts.length) return undefined;
|
|
84
191
|
|
|
85
192
|
return new PreviewArtifact(artifacts);
|
|
86
193
|
}
|
|
87
194
|
|
|
195
|
+
/**
|
|
196
|
+
* This is a special method to get a core env template
|
|
197
|
+
* As the core envs doesn't exist in the scope we need to bring it from other place
|
|
198
|
+
* We will bring it from the core env package files
|
|
199
|
+
*/
|
|
200
|
+
private async getCoreEnvTemplate(envId: string): Promise<PreviewArtifact | undefined> {
|
|
201
|
+
const coreEnvDir = getAspectDir(envId);
|
|
202
|
+
// const finalDir = join(coreEnvDir, getEnvTemplateArtifactDirectory());
|
|
203
|
+
const artifactDef = getEnvTemplateArtifactDef()[0];
|
|
204
|
+
const artifactFactory = new ArtifactFactory();
|
|
205
|
+
|
|
206
|
+
let rootDir = artifactFactory.getRootDir(coreEnvDir, artifactDef);
|
|
207
|
+
if (!existsSync(rootDir)) {
|
|
208
|
+
// fallback to the bvm folder
|
|
209
|
+
const coreEnvDirFromBvm = getAspectDirFromBvm(envId);
|
|
210
|
+
rootDir = artifactFactory.getRootDir(coreEnvDirFromBvm, artifactDef);
|
|
211
|
+
}
|
|
212
|
+
if (!existsSync(rootDir)) {
|
|
213
|
+
return undefined;
|
|
214
|
+
}
|
|
215
|
+
const paths = artifactFactory.resolvePaths(rootDir, artifactDef);
|
|
216
|
+
if (!paths || !paths.length) {
|
|
217
|
+
return undefined;
|
|
218
|
+
}
|
|
219
|
+
const artifactFiles = new ArtifactFiles(paths);
|
|
220
|
+
|
|
221
|
+
artifactFiles.populateVinylsFromPaths(rootDir);
|
|
222
|
+
return new PreviewArtifact(artifactFiles.vinyls);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* This will fetch the component env, then will take the env template from the component env
|
|
227
|
+
* @param component
|
|
228
|
+
*/
|
|
229
|
+
async getEnvTemplateFromComponentEnv(component: Component): Promise<PreviewArtifact | undefined> {
|
|
230
|
+
const envId = this.envs.getEnvId(component);
|
|
231
|
+
return this.getEnvTemplateByEnvId(envId);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* This will fetch the component env, then will take the env template from the component env
|
|
236
|
+
* @param component
|
|
237
|
+
*/
|
|
238
|
+
async getEnvTemplateByEnvId(envId: string): Promise<PreviewArtifact | undefined> {
|
|
239
|
+
// Special treatment for core envs
|
|
240
|
+
if (this.aspectLoader.isCoreEnv(envId)) {
|
|
241
|
+
return this.getCoreEnvTemplate(envId);
|
|
242
|
+
}
|
|
243
|
+
const host = this.componentAspect.getHost();
|
|
244
|
+
const resolvedEnvId = await host.resolveComponentId(envId);
|
|
245
|
+
const envComponent = await host.get(resolvedEnvId);
|
|
246
|
+
if (!envComponent) {
|
|
247
|
+
throw new BitError(`can't load env. env id is ${envId}`);
|
|
248
|
+
}
|
|
249
|
+
return this.getEnvTemplate(envComponent);
|
|
250
|
+
}
|
|
251
|
+
|
|
88
252
|
getDefs(): PreviewDefinition[] {
|
|
89
253
|
return this.previewSlot.values();
|
|
90
254
|
}
|
|
@@ -99,10 +263,20 @@ export class PreviewMain {
|
|
|
99
263
|
* @param defaultModule
|
|
100
264
|
* @param dirName
|
|
101
265
|
*/
|
|
102
|
-
writeLink(
|
|
103
|
-
|
|
266
|
+
writeLink(
|
|
267
|
+
prefix: string,
|
|
268
|
+
moduleMap: ComponentMap<string[]>,
|
|
269
|
+
defaultModule: string | undefined,
|
|
270
|
+
dirName: string,
|
|
271
|
+
isSplitComponentBundle: boolean
|
|
272
|
+
) {
|
|
273
|
+
const contents = generateLink(prefix, moduleMap, defaultModule, isSplitComponentBundle);
|
|
274
|
+
return this.writeLinkContents(contents, dirName, prefix);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
writeLinkContents(contents: string, targetDir: string, prefix: string) {
|
|
104
278
|
const hash = objectHash(contents);
|
|
105
|
-
const targetPath = join(
|
|
279
|
+
const targetPath = join(targetDir, `__${prefix}-${this.timestamp}.js`);
|
|
106
280
|
|
|
107
281
|
// write only if link has changed (prevents triggering fs watches)
|
|
108
282
|
if (this.writeHash.get(targetPath) !== hash) {
|
|
@@ -138,6 +312,7 @@ export class PreviewMain {
|
|
|
138
312
|
|
|
139
313
|
const map = await previewDef.getModuleMap(components);
|
|
140
314
|
const environment = context.envRuntime.env;
|
|
315
|
+
const isSplitComponentBundle = this.getEnvPreviewConfig().splitComponentBundle ?? false;
|
|
141
316
|
const compilerInstance = environment.getCompiler?.();
|
|
142
317
|
const withPaths = map.map<string[]>((files, component) => {
|
|
143
318
|
const modulePath =
|
|
@@ -155,7 +330,7 @@ export class PreviewMain {
|
|
|
155
330
|
const dirPath = join(this.tempFolder, context.id);
|
|
156
331
|
if (!existsSync(dirPath)) mkdirSync(dirPath, { recursive: true });
|
|
157
332
|
|
|
158
|
-
const link = this.writeLink(previewDef.prefix, withPaths, templatePath, dirPath);
|
|
333
|
+
const link = this.writeLink(previewDef.prefix, withPaths, templatePath, dirPath, isSplitComponentBundle);
|
|
159
334
|
return link;
|
|
160
335
|
});
|
|
161
336
|
|
|
@@ -197,7 +372,7 @@ export class PreviewMain {
|
|
|
197
372
|
}
|
|
198
373
|
|
|
199
374
|
private getDefaultStrategies() {
|
|
200
|
-
return [new EnvBundlingStrategy(this), new ComponentBundlingStrategy()];
|
|
375
|
+
return [new EnvBundlingStrategy(this), new ComponentBundlingStrategy(this, this.pkg, this.dependencyResolver)];
|
|
201
376
|
}
|
|
202
377
|
|
|
203
378
|
// TODO - executionContext should be responsible for updating components list, and emit 'update' events
|
|
@@ -233,12 +408,20 @@ export class PreviewMain {
|
|
|
233
408
|
return this.handleComponentChange(component, (currentComponents) => currentComponents.remove(cId));
|
|
234
409
|
};
|
|
235
410
|
|
|
411
|
+
getEnvPreviewConfig(env?: PreviewEnv): EnvPreviewConfig {
|
|
412
|
+
const config = env?.getPreviewConfig && typeof env?.getPreviewConfig === 'function' ? env?.getPreviewConfig() : {};
|
|
413
|
+
|
|
414
|
+
return config;
|
|
415
|
+
}
|
|
416
|
+
|
|
236
417
|
/**
|
|
237
418
|
* return the configured bundling strategy.
|
|
238
419
|
*/
|
|
239
|
-
getBundlingStrategy(): BundlingStrategy {
|
|
420
|
+
getBundlingStrategy(env?: PreviewEnv): BundlingStrategy {
|
|
240
421
|
const defaultStrategies = this.getDefaultStrategies();
|
|
241
|
-
const
|
|
422
|
+
const envPreviewConfig = this.getEnvPreviewConfig(env);
|
|
423
|
+
const strategyFromEnv = envPreviewConfig?.strategyName;
|
|
424
|
+
const strategyName = strategyFromEnv || this.config.bundlingStrategy || 'env';
|
|
242
425
|
const strategies = this.bundlingStrategySlot.values().concat(defaultStrategies);
|
|
243
426
|
const selected = strategies.find((strategy) => {
|
|
244
427
|
return strategy.name === strategyName;
|
|
@@ -278,15 +461,30 @@ export class PreviewMain {
|
|
|
278
461
|
PubsubAspect,
|
|
279
462
|
AspectLoaderAspect,
|
|
280
463
|
LoggerAspect,
|
|
464
|
+
DependencyResolverAspect,
|
|
465
|
+
GraphqlAspect,
|
|
281
466
|
];
|
|
282
467
|
|
|
283
468
|
static defaultConfig = {
|
|
284
|
-
bundlingStrategy: 'env',
|
|
285
469
|
disabled: false,
|
|
286
470
|
};
|
|
287
471
|
|
|
288
472
|
static async provider(
|
|
289
|
-
|
|
473
|
+
// eslint-disable-next-line max-len
|
|
474
|
+
[
|
|
475
|
+
bundler,
|
|
476
|
+
builder,
|
|
477
|
+
componentExtension,
|
|
478
|
+
uiMain,
|
|
479
|
+
envs,
|
|
480
|
+
workspace,
|
|
481
|
+
pkg,
|
|
482
|
+
pubsub,
|
|
483
|
+
aspectLoader,
|
|
484
|
+
loggerMain,
|
|
485
|
+
dependencyResolver,
|
|
486
|
+
graphql,
|
|
487
|
+
]: [
|
|
290
488
|
BundlerMain,
|
|
291
489
|
BuilderMain,
|
|
292
490
|
ComponentMain,
|
|
@@ -296,38 +494,50 @@ export class PreviewMain {
|
|
|
296
494
|
PkgMain,
|
|
297
495
|
PubsubMain,
|
|
298
496
|
AspectLoaderMain,
|
|
299
|
-
LoggerMain
|
|
497
|
+
LoggerMain,
|
|
498
|
+
DependencyResolverMain,
|
|
499
|
+
GraphqlMain
|
|
300
500
|
],
|
|
301
501
|
config: PreviewConfig,
|
|
302
502
|
[previewSlot, bundlingStrategySlot]: [PreviewDefinitionRegistry, BundlingStrategySlot],
|
|
303
503
|
harmony: Harmony
|
|
304
504
|
) {
|
|
305
505
|
const logger = loggerMain.createLogger(PreviewAspect.id);
|
|
306
|
-
|
|
506
|
+
// app.registerApp(new PreviewApp());
|
|
307
507
|
const preview = new PreviewMain(
|
|
308
508
|
harmony,
|
|
309
509
|
previewSlot,
|
|
310
510
|
uiMain,
|
|
311
511
|
envs,
|
|
512
|
+
componentExtension,
|
|
312
513
|
pkg,
|
|
313
514
|
aspectLoader,
|
|
314
515
|
config,
|
|
315
516
|
bundlingStrategySlot,
|
|
316
517
|
builder,
|
|
317
518
|
workspace,
|
|
318
|
-
logger
|
|
519
|
+
logger,
|
|
520
|
+
dependencyResolver
|
|
319
521
|
);
|
|
320
522
|
|
|
321
523
|
if (workspace) uiMain.registerStartPlugin(new PreviewStartPlugin(workspace, bundler, uiMain, pubsub, logger));
|
|
322
524
|
|
|
323
|
-
componentExtension.registerRoute([
|
|
525
|
+
componentExtension.registerRoute([
|
|
526
|
+
new PreviewRoute(preview, logger),
|
|
527
|
+
new ComponentPreviewRoute(preview, logger),
|
|
528
|
+
// @ts-ignore
|
|
529
|
+
new EnvTemplateRoute(preview, logger),
|
|
530
|
+
new PreviewAssetsRoute(preview, logger),
|
|
531
|
+
]);
|
|
532
|
+
|
|
324
533
|
bundler.registerTarget([
|
|
325
534
|
{
|
|
326
535
|
entry: preview.getPreviewTarget.bind(preview),
|
|
327
536
|
},
|
|
328
537
|
]);
|
|
329
538
|
|
|
330
|
-
if (!config.disabled)
|
|
539
|
+
if (!config.disabled)
|
|
540
|
+
builder.registerBuildTasks([new EnvPreviewTemplateTask(preview, envs), new PreviewTask(bundler, preview)]);
|
|
331
541
|
|
|
332
542
|
if (workspace) {
|
|
333
543
|
workspace.registerOnComponentAdd((c) =>
|
|
@@ -339,6 +549,8 @@ export class PreviewMain {
|
|
|
339
549
|
workspace.registerOnComponentRemove((cId) => preview.handleComponentRemoval(cId));
|
|
340
550
|
}
|
|
341
551
|
|
|
552
|
+
graphql.register(previewSchema(preview));
|
|
553
|
+
|
|
342
554
|
return preview;
|
|
343
555
|
}
|
|
344
556
|
}
|