@teambit/preview 0.0.627 → 0.0.632
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-1643167379538.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.632.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.627.tgz +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const overview = [require('/home/circleci/Library/Caches/Bit/capsules/8891be5ad3d35bfc38b9cd90c0e05b598a5a55af/teambit.preview_preview@0.0.632/dist/preview.docs.mdx')]
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/// <reference types="express" />
|
|
2
|
+
import type { Request, Response } from '@teambit/express';
|
|
3
|
+
import type { Logger } from '@teambit/logger';
|
|
4
|
+
export declare type PreviewUrlParams = {
|
|
5
|
+
/**
|
|
6
|
+
* preview name like overview or composition
|
|
7
|
+
*/
|
|
8
|
+
previewName?: string;
|
|
9
|
+
/** `/preview/:filePath(*)` */
|
|
10
|
+
filePath?: string;
|
|
11
|
+
};
|
|
12
|
+
export declare function getArtifactFileMiddleware(logger: Logger): (req: Request<PreviewUrlParams>, res: Response) => Promise<import("express").Response<any>>;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
require("core-js/modules/es.promise.js");
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "__esModule", {
|
|
8
|
+
value: true
|
|
9
|
+
});
|
|
10
|
+
exports.getArtifactFileMiddleware = getArtifactFileMiddleware;
|
|
11
|
+
|
|
12
|
+
function _mime() {
|
|
13
|
+
const data = _interopRequireDefault(require("mime"));
|
|
14
|
+
|
|
15
|
+
_mime = function () {
|
|
16
|
+
return data;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function _uiFoundationUiPages() {
|
|
23
|
+
const data = require("@teambit/ui-foundation.ui.pages.static-error");
|
|
24
|
+
|
|
25
|
+
_uiFoundationUiPages = function () {
|
|
26
|
+
return data;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
return data;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function getArtifactFileMiddleware(logger) {
|
|
33
|
+
return async (req, res) => {
|
|
34
|
+
try {
|
|
35
|
+
// @ts-ignore
|
|
36
|
+
const artifact = req.artifact; // @ts-ignore
|
|
37
|
+
|
|
38
|
+
const isLegacyPath = req.isLegacyPath;
|
|
39
|
+
let file;
|
|
40
|
+
|
|
41
|
+
if (!isLegacyPath) {
|
|
42
|
+
file = getEnvTemplateFile(artifact, req.params.previewName, req.params.filePath);
|
|
43
|
+
} else {
|
|
44
|
+
file = getPreviewFile(artifact, req.params.previewName, req.params.filePath);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (!file) return res.status(404).send((0, _uiFoundationUiPages().noPreview)());
|
|
48
|
+
const contents = file.contents;
|
|
49
|
+
const str = `${file.cwd}/${file.path}`;
|
|
50
|
+
|
|
51
|
+
const contentType = _mime().default.getType(str);
|
|
52
|
+
|
|
53
|
+
if (contentType) res.set('Content-Type', contentType);
|
|
54
|
+
return res.send(contents);
|
|
55
|
+
} catch (e) {
|
|
56
|
+
logger.error('failed getting preview', e);
|
|
57
|
+
return res.status(500).send((0, _uiFoundationUiPages().serverError)());
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function getEnvTemplateFile(artifact, previewName, filePath) {
|
|
63
|
+
const prevName = previewName || 'overview';
|
|
64
|
+
const finalFilePath = filePath || `${prevName}.html`;
|
|
65
|
+
const matchedFile = artifact === null || artifact === void 0 ? void 0 : artifact.getFileEndsWith(finalFilePath);
|
|
66
|
+
return matchedFile;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function getPreviewFile(artifact, previewName, filePath) {
|
|
70
|
+
let finalFilePath = 'index.html';
|
|
71
|
+
|
|
72
|
+
if (previewName || filePath) {
|
|
73
|
+
const parts = [previewName, filePath].filter(x => x);
|
|
74
|
+
finalFilePath = parts.join('/');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const matchedFile = artifact === null || artifact === void 0 ? void 0 : artifact.getFileEndsWith(finalFilePath);
|
|
78
|
+
return matchedFile;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
//# sourceMappingURL=artifact-file-middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["artifact-file-middleware.ts"],"names":["getArtifactFileMiddleware","logger","req","res","artifact","isLegacyPath","file","getEnvTemplateFile","params","previewName","filePath","getPreviewFile","status","send","contents","str","cwd","path","contentType","mime","getType","set","e","error","prevName","finalFilePath","matchedFile","getFileEndsWith","parts","filter","x","join"],"mappings":";;;;;;;;;;;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAaO,SAASA,yBAAT,CAAmCC,MAAnC,EAAmD;AACxD,SAAO,OAAOC,GAAP,EAAuCC,GAAvC,KAAyD;AAC9D,QAAI;AACF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,IAAK,GAAEc,QAAS,OAA9C;AACA,QAAME,WAAW,GAAGtB,QAAH,aAAGA,QAAH,uBAAGA,QAAQ,CAAEuB,eAAV,CAA0BF,aAA1B,CAApB;AACA,SAAOC,WAAP;AACD;;AAED,SAASf,cAAT,CAAwBP,QAAxB,EAAmDK,WAAnD,EAAyEC,QAAzE,EAA4F;AAC1F,MAAIe,aAAa,GAAG,YAApB;;AACA,MAAIhB,WAAW,IAAIC,QAAnB,EAA6B;AAC3B,UAAMkB,KAAK,GAAG,CAACnB,WAAD,EAAcC,QAAd,EAAwBmB,MAAxB,CAAgCC,CAAD,IAAOA,CAAtC,CAAd;AACAL,IAAAA,aAAa,GAAGG,KAAK,CAACG,IAAN,CAAW,GAAX,CAAhB;AACD;;AACD,QAAML,WAAW,GAAGtB,QAAH,aAAGA,QAAH,uBAAGA,QAAQ,CAAEuB,eAAV,CAA0BF,aAA1B,CAApB;AACA,SAAOC,WAAP;AACD","sourcesContent":["import mime from 'mime';\nimport type { Request, Response } from '@teambit/express';\nimport { noPreview, serverError } from '@teambit/ui-foundation.ui.pages.static-error';\nimport type { Logger } from '@teambit/logger';\nimport type { PreviewArtifact } from './preview-artifact';\n\nexport type PreviewUrlParams = {\n /**\n * preview name like overview or composition\n */\n previewName?: string;\n /** `/preview/:filePath(*)` */\n filePath?: string;\n};\n\nexport function getArtifactFileMiddleware(logger: Logger) {\n return async (req: Request<PreviewUrlParams>, res: Response) => {\n try {\n // @ts-ignore\n const artifact: PreviewArtifact = req.artifact;\n // @ts-ignore\n const isLegacyPath = req.isLegacyPath;\n let file;\n if (!isLegacyPath) {\n file = getEnvTemplateFile(artifact, req.params.previewName, req.params.filePath);\n } else {\n file = getPreviewFile(artifact, req.params.previewName, req.params.filePath);\n }\n if (!file) return res.status(404).send(noPreview());\n\n const contents = file.contents;\n const str = `${file.cwd}/${file.path}`;\n const contentType = mime.getType(str);\n if (contentType) res.set('Content-Type', contentType);\n return res.send(contents);\n } catch (e: any) {\n logger.error('failed getting preview', e);\n return res.status(500).send(serverError());\n }\n };\n}\n\nfunction getEnvTemplateFile(artifact: PreviewArtifact, previewName?: string, filePath?: string) {\n const prevName = previewName || 'overview';\n const finalFilePath = filePath || `${prevName}.html`;\n const matchedFile = artifact?.getFileEndsWith(finalFilePath);\n return matchedFile;\n}\n\nfunction getPreviewFile(artifact: PreviewArtifact, previewName?: string, filePath?: string) {\n let finalFilePath = 'index.html';\n if (previewName || filePath) {\n const parts = [previewName, filePath].filter((x) => x);\n finalFilePath = parts.join('/');\n }\n const matchedFile = artifact?.getFileEndsWith(finalFilePath);\n return matchedFile;\n}\n"]}
|
|
@@ -2,6 +2,10 @@ import { BuildContext, BuiltTaskResult } from '@teambit/builder';
|
|
|
2
2
|
import { Target, BundlerResult, BundlerContext } from '@teambit/bundler';
|
|
3
3
|
import { PreviewDefinition } from './preview-definition';
|
|
4
4
|
import { PreviewTask } from './preview.task';
|
|
5
|
+
declare type ComputeTargetsExtraContextKeys = {
|
|
6
|
+
splitComponentBundle?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare type ComputeTargetsContext = BuildContext & ComputeTargetsExtraContextKeys;
|
|
5
9
|
export interface BundlingStrategy {
|
|
6
10
|
/**
|
|
7
11
|
* name of the bundling strategy.
|
|
@@ -10,9 +14,10 @@ export interface BundlingStrategy {
|
|
|
10
14
|
/**
|
|
11
15
|
* compute bundling targets for the build context.
|
|
12
16
|
*/
|
|
13
|
-
computeTargets(context:
|
|
17
|
+
computeTargets(context: ComputeTargetsContext, previewDefs: PreviewDefinition[], previewTask: PreviewTask): Promise<Target[]>;
|
|
14
18
|
/**
|
|
15
19
|
* compute the results of the bundler.
|
|
16
20
|
*/
|
|
17
21
|
computeResults(context: BundlerContext, results: BundlerResult[], previewTask: PreviewTask): Promise<BuiltTaskResult>;
|
|
18
22
|
}
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/// <reference types="express" />
|
|
2
|
+
import type { NextFunction, Request, Response, Route } from '@teambit/express';
|
|
3
|
+
import type { Logger } from '@teambit/logger';
|
|
4
|
+
import { PreviewMain } from './preview.main.runtime';
|
|
5
|
+
import type { PreviewUrlParams } from './artifact-file-middleware';
|
|
6
|
+
export declare class ComponentPreviewRoute implements Route {
|
|
7
|
+
/**
|
|
8
|
+
* preview extension.
|
|
9
|
+
*/
|
|
10
|
+
private preview;
|
|
11
|
+
private logger;
|
|
12
|
+
constructor(
|
|
13
|
+
/**
|
|
14
|
+
* preview extension.
|
|
15
|
+
*/
|
|
16
|
+
preview: PreviewMain, logger: Logger);
|
|
17
|
+
route: string;
|
|
18
|
+
method: string;
|
|
19
|
+
middlewares: ((req: Request<PreviewUrlParams>, res: Response, next: NextFunction) => Promise<void | import("express").Response<any>>)[];
|
|
20
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
require("core-js/modules/es.promise.js");
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(exports, "__esModule", {
|
|
8
|
+
value: true
|
|
9
|
+
});
|
|
10
|
+
exports.ComponentPreviewRoute = void 0;
|
|
11
|
+
|
|
12
|
+
function _defineProperty2() {
|
|
13
|
+
const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
14
|
+
|
|
15
|
+
_defineProperty2 = function () {
|
|
16
|
+
return data;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function _uiFoundationUiPages() {
|
|
23
|
+
const data = require("@teambit/ui-foundation.ui.pages.static-error");
|
|
24
|
+
|
|
25
|
+
_uiFoundationUiPages = function () {
|
|
26
|
+
return data;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
return data;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function _artifactFileMiddleware() {
|
|
33
|
+
const data = require("./artifact-file-middleware");
|
|
34
|
+
|
|
35
|
+
_artifactFileMiddleware = function () {
|
|
36
|
+
return data;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
return data;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
class ComponentPreviewRoute {
|
|
43
|
+
constructor(
|
|
44
|
+
/**
|
|
45
|
+
* preview extension.
|
|
46
|
+
*/
|
|
47
|
+
preview, logger) {
|
|
48
|
+
this.preview = preview;
|
|
49
|
+
this.logger = logger;
|
|
50
|
+
(0, _defineProperty2().default)(this, "route", `/component-preview/:filePath(*)`);
|
|
51
|
+
(0, _defineProperty2().default)(this, "method", 'get');
|
|
52
|
+
(0, _defineProperty2().default)(this, "middlewares", [async (req, res, next) => {
|
|
53
|
+
try {
|
|
54
|
+
let isLegacyPath = false; // @ts-ignore TODO: @guy please fix.
|
|
55
|
+
|
|
56
|
+
const component = req.component;
|
|
57
|
+
if (!component) return res.status(404).send((0, _uiFoundationUiPages().noPreview)());
|
|
58
|
+
let artifact; // TODO - prevent error `getVinylsAndImportIfMissing is not a function` #4680
|
|
59
|
+
|
|
60
|
+
try {
|
|
61
|
+
isLegacyPath = true;
|
|
62
|
+
artifact = await this.preview.getPreview(component);
|
|
63
|
+
} catch (e) {
|
|
64
|
+
return res.status(404).send((0, _uiFoundationUiPages().noPreview)());
|
|
65
|
+
} // @ts-ignore
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
req.artifact = artifact; // @ts-ignore
|
|
69
|
+
|
|
70
|
+
req.isLegacyPath = isLegacyPath;
|
|
71
|
+
return next();
|
|
72
|
+
} catch (e) {
|
|
73
|
+
this.logger.error('failed getting preview', e);
|
|
74
|
+
return res.status(500).send((0, _uiFoundationUiPages().serverError)());
|
|
75
|
+
}
|
|
76
|
+
}, (0, _artifactFileMiddleware().getArtifactFileMiddleware)(this.logger)]);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
exports.ComponentPreviewRoute = ComponentPreviewRoute;
|
|
82
|
+
|
|
83
|
+
//# sourceMappingURL=component-preview.route.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["component-preview.route.ts"],"names":["ComponentPreviewRoute","constructor","preview","logger","req","res","next","isLegacyPath","component","status","send","artifact","getPreview","e","error"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAEA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAMA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAEO,MAAMA,qBAAN,CAA6C;AAClDC,EAAAA,WAAW;AACT;AACJ;AACA;AACYC,EAAAA,OAJC,EAKDC,MALC,EAMT;AAAA,SAFQD,OAER,GAFQA,OAER;AAAA,SADQC,MACR,GADQA,MACR;AAAA,mDAEO,iCAFP;AAAA,oDAGO,KAHP;AAAA,yDAKY,CACZ,OAAOC,GAAP,EAAuCC,GAAvC,EAAsDC,IAAtD,KAA6E;AAC3E,UAAI;AACF,YAAIC,YAAY,GAAG,KAAnB,CADE,CAEF;;AACA,cAAMC,SAAS,GAAGJ,GAAG,CAACI,SAAtB;AACA,YAAI,CAACA,SAAL,EAAgB,OAAOH,GAAG,CAACI,MAAJ,CAAW,GAAX,EAAgBC,IAAhB,CAAqB,uCAArB,CAAP;AAEhB,YAAIC,QAAJ,CANE,CAOF;;AACA,YAAI;AACFJ,UAAAA,YAAY,GAAG,IAAf;AACAI,UAAAA,QAAQ,GAAG,MAAM,KAAKT,OAAL,CAAaU,UAAb,CAAwBJ,SAAxB,CAAjB;AACD,SAHD,CAGE,OAAOK,CAAP,EAAe;AACf,iBAAOR,GAAG,CAACI,MAAJ,CAAW,GAAX,EAAgBC,IAAhB,CAAqB,uCAArB,CAAP;AACD,SAbC,CAcF;;;AACAN,QAAAA,GAAG,CAACO,QAAJ,GAAeA,QAAf,CAfE,CAgBF;;AACAP,QAAAA,GAAG,CAACG,YAAJ,GAAmBA,YAAnB;AACA,eAAOD,IAAI,EAAX;AACD,OAnBD,CAmBE,OAAOO,CAAP,EAAe;AACf,aAAKV,MAAL,CAAYW,KAAZ,CAAkB,wBAAlB,EAA4CD,CAA5C;AACA,eAAOR,GAAG,CAACI,MAAJ,CAAW,GAAX,EAAgBC,IAAhB,CAAqB,yCAArB,CAAP;AACD;AACF,KAzBW,EA0BZ,yDAA0B,KAAKP,MAA/B,CA1BY,CALZ;AAAE;;AAP8C","sourcesContent":["import type { NextFunction, Request, Response, Route } from '@teambit/express';\nimport type { Component } from '@teambit/component';\nimport { noPreview, serverError } from '@teambit/ui-foundation.ui.pages.static-error';\nimport type { Logger } from '@teambit/logger';\n\nimport { PreviewMain } from './preview.main.runtime';\nimport type { PreviewArtifact } from './preview-artifact';\nimport type { PreviewUrlParams } from './artifact-file-middleware';\nimport { getArtifactFileMiddleware } from './artifact-file-middleware';\n\nexport class ComponentPreviewRoute implements Route {\n constructor(\n /**\n * preview extension.\n */\n private preview: PreviewMain,\n private logger: Logger\n ) {}\n\n route = `/component-preview/:filePath(*)`;\n method = 'get';\n\n middlewares = [\n async (req: Request<PreviewUrlParams>, res: Response, next: NextFunction) => {\n try {\n let isLegacyPath = false;\n // @ts-ignore TODO: @guy please fix.\n const component = req.component as Component | undefined;\n if (!component) return res.status(404).send(noPreview());\n\n let artifact: PreviewArtifact | undefined;\n // TODO - prevent error `getVinylsAndImportIfMissing is not a function` #4680\n try {\n isLegacyPath = true;\n artifact = await this.preview.getPreview(component);\n } catch (e: any) {\n return res.status(404).send(noPreview());\n }\n // @ts-ignore\n req.artifact = artifact;\n // @ts-ignore\n req.isLegacyPath = isLegacyPath;\n return next();\n } catch (e: any) {\n this.logger.error('failed getting preview', e);\n return res.status(500).send(serverError());\n }\n },\n getArtifactFileMiddleware(this.logger),\n ];\n}\n"]}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { BuildContext, BuiltTaskResult, BuildTask, TaskLocation, ComponentResult } from '@teambit/builder';
|
|
2
|
+
import { Component } from '@teambit/component';
|
|
3
|
+
import { Capsule } from '@teambit/isolator';
|
|
4
|
+
import { BundlerContext, BundlerEntryMap, BundlerResult } from '@teambit/bundler';
|
|
5
|
+
import type { EnvsMain } from '@teambit/envs';
|
|
6
|
+
import type { PreviewMain } from './preview.main.runtime';
|
|
7
|
+
export declare type ModuleExpose = {
|
|
8
|
+
name: string;
|
|
9
|
+
path: string;
|
|
10
|
+
include?: string[];
|
|
11
|
+
};
|
|
12
|
+
export declare const GENERATE_ENV_TEMPLATE_TASK_NAME = "GenerateEnvTemplate";
|
|
13
|
+
export declare const PREVIEW_ROOT_CHUNK_NAME = "previewRoot";
|
|
14
|
+
export declare const PEERS_CHUNK_NAME = "peers";
|
|
15
|
+
export declare class EnvPreviewTemplateTask implements BuildTask {
|
|
16
|
+
private preview;
|
|
17
|
+
private envs;
|
|
18
|
+
aspectId: string;
|
|
19
|
+
name: string;
|
|
20
|
+
location: TaskLocation;
|
|
21
|
+
constructor(preview: PreviewMain, envs: EnvsMain);
|
|
22
|
+
execute(context: BuildContext): Promise<BuiltTaskResult>;
|
|
23
|
+
private generateHtmlConfig;
|
|
24
|
+
private generateHtmlConfigForPreviewDef;
|
|
25
|
+
getEntries(previewModules: ModuleExpose[], capsule: Capsule, previewRoot: string, isSplitComponentBundle?: boolean, peers?: string[]): BundlerEntryMap;
|
|
26
|
+
computeResults(context: BundlerContext, results: BundlerResult[]): Promise<{
|
|
27
|
+
componentsResults: ComponentResult[];
|
|
28
|
+
artifacts: {
|
|
29
|
+
name: string;
|
|
30
|
+
globPatterns: string[];
|
|
31
|
+
rootDir: string;
|
|
32
|
+
}[];
|
|
33
|
+
}>;
|
|
34
|
+
getPreviewModules(envComponent: Component): Promise<ModuleExpose[]>;
|
|
35
|
+
private computeOutputPath;
|
|
36
|
+
}
|
|
37
|
+
export declare function getArtifactDirectory(): string;
|
|
38
|
+
export declare function getArtifactDef(): {
|
|
39
|
+
name: string;
|
|
40
|
+
globPatterns: string[];
|
|
41
|
+
rootDir: string;
|
|
42
|
+
}[];
|
|
@@ -0,0 +1,309 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
|
|
5
|
+
require("core-js/modules/es.array.iterator.js");
|
|
6
|
+
|
|
7
|
+
require("core-js/modules/es.promise.js");
|
|
8
|
+
|
|
9
|
+
Object.defineProperty(exports, "__esModule", {
|
|
10
|
+
value: true
|
|
11
|
+
});
|
|
12
|
+
exports.PREVIEW_ROOT_CHUNK_NAME = exports.PEERS_CHUNK_NAME = exports.GENERATE_ENV_TEMPLATE_TASK_NAME = exports.EnvPreviewTemplateTask = void 0;
|
|
13
|
+
exports.getArtifactDef = getArtifactDef;
|
|
14
|
+
exports.getArtifactDirectory = getArtifactDirectory;
|
|
15
|
+
|
|
16
|
+
function _defineProperty2() {
|
|
17
|
+
const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
|
|
18
|
+
|
|
19
|
+
_defineProperty2 = function () {
|
|
20
|
+
return data;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
return data;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function _builder() {
|
|
27
|
+
const data = require("@teambit/builder");
|
|
28
|
+
|
|
29
|
+
_builder = function () {
|
|
30
|
+
return data;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function _component() {
|
|
37
|
+
const data = require("@teambit/component");
|
|
38
|
+
|
|
39
|
+
_component = function () {
|
|
40
|
+
return data;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
return data;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function _path() {
|
|
47
|
+
const data = require("path");
|
|
48
|
+
|
|
49
|
+
_path = function () {
|
|
50
|
+
return data;
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
return data;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function _lodash() {
|
|
57
|
+
const data = require("lodash");
|
|
58
|
+
|
|
59
|
+
_lodash = function () {
|
|
60
|
+
return data;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return data;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function _fsExtra() {
|
|
67
|
+
const data = require("fs-extra");
|
|
68
|
+
|
|
69
|
+
_fsExtra = function () {
|
|
70
|
+
return data;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
return data;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function _webpack() {
|
|
77
|
+
const data = require("./webpack");
|
|
78
|
+
|
|
79
|
+
_webpack = function () {
|
|
80
|
+
return data;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
return data;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const GENERATE_ENV_TEMPLATE_TASK_NAME = 'GenerateEnvTemplate';
|
|
87
|
+
exports.GENERATE_ENV_TEMPLATE_TASK_NAME = GENERATE_ENV_TEMPLATE_TASK_NAME;
|
|
88
|
+
const PREVIEW_ROOT_CHUNK_NAME = 'previewRoot';
|
|
89
|
+
exports.PREVIEW_ROOT_CHUNK_NAME = PREVIEW_ROOT_CHUNK_NAME;
|
|
90
|
+
const PEERS_CHUNK_NAME = 'peers';
|
|
91
|
+
exports.PEERS_CHUNK_NAME = PEERS_CHUNK_NAME;
|
|
92
|
+
|
|
93
|
+
class EnvPreviewTemplateTask {
|
|
94
|
+
// readonly dependencies = [CompilerAspect.id];
|
|
95
|
+
constructor(preview, envs) {
|
|
96
|
+
this.preview = preview;
|
|
97
|
+
this.envs = envs;
|
|
98
|
+
(0, _defineProperty2().default)(this, "aspectId", 'teambit.preview/preview');
|
|
99
|
+
(0, _defineProperty2().default)(this, "name", GENERATE_ENV_TEMPLATE_TASK_NAME);
|
|
100
|
+
(0, _defineProperty2().default)(this, "location", 'end');
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
async execute(context) {
|
|
104
|
+
const previewDefs = this.preview.getDefs();
|
|
105
|
+
const htmlConfig = this.generateHtmlConfig(previewDefs, PREVIEW_ROOT_CHUNK_NAME, PEERS_CHUNK_NAME, {
|
|
106
|
+
dev: context.dev
|
|
107
|
+
});
|
|
108
|
+
const targets = (0, _lodash().compact)(await Promise.all(context.components.map(async component => {
|
|
109
|
+
var _envPreviewConfig$spl;
|
|
110
|
+
|
|
111
|
+
const envDef = this.envs.getEnvFromComponent(component);
|
|
112
|
+
if (!envDef) return undefined;
|
|
113
|
+
const env = envDef.env;
|
|
114
|
+
const bundlingStrategy = this.preview.getBundlingStrategy(envDef.env);
|
|
115
|
+
|
|
116
|
+
if (bundlingStrategy.name === 'env') {
|
|
117
|
+
return undefined;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const envComponent = component;
|
|
121
|
+
const envPreviewConfig = this.preview.getEnvPreviewConfig(envDef.env);
|
|
122
|
+
const isSplitComponentBundle = (_envPreviewConfig$spl = envPreviewConfig.splitComponentBundle) !== null && _envPreviewConfig$spl !== void 0 ? _envPreviewConfig$spl : false;
|
|
123
|
+
const envGetDeps = (await env.getDependencies()) || {};
|
|
124
|
+
const envComponentPeers = Object.keys(envGetDeps.peerDependencies || {}) || [];
|
|
125
|
+
const envHostDeps = env.getHostDependencies() || [];
|
|
126
|
+
const peers = envComponentPeers.concat(envHostDeps); // const module = await this.getPreviewModule(envComponent);
|
|
127
|
+
// const entries = Object.keys(module).map((key) => module.exposes[key]);
|
|
128
|
+
|
|
129
|
+
const capsule = context.capsuleNetwork.graphCapsules.getCapsule(envComponent.id);
|
|
130
|
+
if (!capsule) throw new Error('no capsule found');
|
|
131
|
+
const previewRoot = await this.preview.writePreviewRuntime(context);
|
|
132
|
+
const previewModules = await this.getPreviewModules(envComponent); // const templatesFile = previewModules.map((template) => {
|
|
133
|
+
// return this.preview.writeLink(template.name, ComponentMap.create([]), template.path, capsule.path);
|
|
134
|
+
// });
|
|
135
|
+
|
|
136
|
+
const outputPath = this.computeOutputPath(context, envComponent);
|
|
137
|
+
if (!(0, _fsExtra().existsSync)(outputPath)) (0, _fsExtra().mkdirpSync)(outputPath);
|
|
138
|
+
const entries = this.getEntries(previewModules, capsule, previewRoot, isSplitComponentBundle, peers); // const entries = this.getEntries(
|
|
139
|
+
// previewModules.concat({
|
|
140
|
+
// name: 'main',
|
|
141
|
+
// path: previewRoot,
|
|
142
|
+
// })
|
|
143
|
+
// );
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
// entries: templatesFile.concat(previewRoot),
|
|
147
|
+
peers,
|
|
148
|
+
runtimeChunkName: 'runtime',
|
|
149
|
+
html: htmlConfig,
|
|
150
|
+
entries,
|
|
151
|
+
chunking: {
|
|
152
|
+
splitChunks: true
|
|
153
|
+
},
|
|
154
|
+
components: [envComponent],
|
|
155
|
+
outputPath // modules: [module],
|
|
156
|
+
|
|
157
|
+
};
|
|
158
|
+
})));
|
|
159
|
+
if (!targets.length) return {
|
|
160
|
+
componentsResults: []
|
|
161
|
+
};
|
|
162
|
+
const bundlerContext = Object.assign({}, context, {
|
|
163
|
+
targets,
|
|
164
|
+
entry: [],
|
|
165
|
+
externalizePeer: false,
|
|
166
|
+
development: context.dev
|
|
167
|
+
});
|
|
168
|
+
const bundler = await context.env.getBundler(bundlerContext);
|
|
169
|
+
const bundlerResults = await bundler.run();
|
|
170
|
+
const results = await this.computeResults(bundlerContext, bundlerResults);
|
|
171
|
+
return results;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
generateHtmlConfig(previewDefs, previewRootChunkName, peersChunkName, options) {
|
|
175
|
+
const htmlConfigs = previewDefs.map(previewModule => this.generateHtmlConfigForPreviewDef(previewModule, previewRootChunkName, peersChunkName, options));
|
|
176
|
+
return htmlConfigs;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
generateHtmlConfigForPreviewDef(previewDef, previewRootChunkName, peersChunkName, options) {
|
|
180
|
+
var _options$dev;
|
|
181
|
+
|
|
182
|
+
const previewDeps = previewDef.include || [];
|
|
183
|
+
const chunks = [...previewDeps, previewDef.prefix, previewRootChunkName];
|
|
184
|
+
|
|
185
|
+
if (previewDef.includePeers) {
|
|
186
|
+
chunks.unshift(peersChunkName);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const config = {
|
|
190
|
+
title: 'Preview',
|
|
191
|
+
templateContent: (0, _webpack().html)('Preview'),
|
|
192
|
+
minify: (_options$dev = options === null || options === void 0 ? void 0 : options.dev) !== null && _options$dev !== void 0 ? _options$dev : true,
|
|
193
|
+
chunks,
|
|
194
|
+
filename: `${previewDef.prefix}.html`
|
|
195
|
+
};
|
|
196
|
+
return config;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
getEntries(previewModules, capsule, previewRoot, isSplitComponentBundle = false, peers = []) {
|
|
200
|
+
const previewRootEntry = {
|
|
201
|
+
filename: 'preview-root.[chunkhash].js',
|
|
202
|
+
import: previewRoot
|
|
203
|
+
};
|
|
204
|
+
const peersRootEntry = {
|
|
205
|
+
filename: 'peers.[chunkhash].js',
|
|
206
|
+
import: peers
|
|
207
|
+
};
|
|
208
|
+
const entries = previewModules.reduce((acc, module) => {
|
|
209
|
+
const linkFile = this.preview.writeLink(module.name, _component().ComponentMap.create([]), module.path, capsule.path, isSplitComponentBundle);
|
|
210
|
+
acc[module.name] = {
|
|
211
|
+
// filename: `${module.name}.[contenthash].js`,
|
|
212
|
+
filename: `${module.name}.[chunkhash].js`,
|
|
213
|
+
// filename: `${module.name}.js`,
|
|
214
|
+
import: linkFile // library: {
|
|
215
|
+
// name: module.name,
|
|
216
|
+
// type: 'umd',
|
|
217
|
+
// },
|
|
218
|
+
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
if (module.include) {
|
|
222
|
+
acc[module.name].dependOn = module.include;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return acc;
|
|
226
|
+
}, {
|
|
227
|
+
[PREVIEW_ROOT_CHUNK_NAME]: previewRootEntry,
|
|
228
|
+
[PEERS_CHUNK_NAME]: peersRootEntry
|
|
229
|
+
});
|
|
230
|
+
return entries; // const mergedEntries = return entriesArr.reduce((entriesMap, entry) => {
|
|
231
|
+
// entriesMap[entry.library.name] = entry;
|
|
232
|
+
// return entriesMap;
|
|
233
|
+
// }, {previewRoot: previewRootEntry});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async computeResults(context, results) {
|
|
237
|
+
const result = results[0];
|
|
238
|
+
const componentsResults = result.components.map(component => {
|
|
239
|
+
return {
|
|
240
|
+
component,
|
|
241
|
+
errors: result.errors.map(err => typeof err === 'string' ? err : err.message),
|
|
242
|
+
warning: result.warnings,
|
|
243
|
+
startTime: result.startTime,
|
|
244
|
+
endTime: result.endTime
|
|
245
|
+
};
|
|
246
|
+
});
|
|
247
|
+
const artifacts = getArtifactDef();
|
|
248
|
+
return {
|
|
249
|
+
componentsResults,
|
|
250
|
+
artifacts
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async getPreviewModules(envComponent) {
|
|
255
|
+
const env = this.envs.getEnv(envComponent);
|
|
256
|
+
const previewDefs = this.preview.getDefs();
|
|
257
|
+
const modules = (0, _lodash().compact)(await Promise.all(previewDefs.map(async def => {
|
|
258
|
+
if (!def.renderTemplatePathByEnv) return undefined;
|
|
259
|
+
return {
|
|
260
|
+
name: def.prefix,
|
|
261
|
+
path: await def.renderTemplatePathByEnv(env.env),
|
|
262
|
+
include: def.include
|
|
263
|
+
};
|
|
264
|
+
})));
|
|
265
|
+
return modules;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
computeOutputPath(context, component) {
|
|
269
|
+
const capsule = context.capsuleNetwork.graphCapsules.getCapsule(component.id);
|
|
270
|
+
if (!capsule) throw new Error('no capsule found');
|
|
271
|
+
return (0, _path().join)(capsule.path, getArtifactDirectory());
|
|
272
|
+
} // private async getPreviewModule(envComponent: Component): Promise<ModuleTarget> {
|
|
273
|
+
// const env = this.envs.getEnv(envComponent);
|
|
274
|
+
// const previewDefs = this.preview.getDefs();
|
|
275
|
+
// const modules = compact(await Promise.all(previewDefs.map(async (def) => {
|
|
276
|
+
// if (!def.renderTemplatePathByEnv) return undefined;
|
|
277
|
+
// return [def.prefix, await def.renderTemplatePathByEnv(env.env)];
|
|
278
|
+
// })));
|
|
279
|
+
// const exposes = modules.reduce((exposesAcc, [prefix, path]) => {
|
|
280
|
+
// const internalPath = `./${prefix}`;
|
|
281
|
+
// exposesAcc[internalPath] = path;
|
|
282
|
+
// return exposesAcc;
|
|
283
|
+
// }, {});
|
|
284
|
+
// return {
|
|
285
|
+
// component: envComponent,
|
|
286
|
+
// exposes
|
|
287
|
+
// };
|
|
288
|
+
// }
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
exports.EnvPreviewTemplateTask = EnvPreviewTemplateTask;
|
|
294
|
+
|
|
295
|
+
function getArtifactDirectory() {
|
|
296
|
+
return (0, _path().join)(_builder().CAPSULE_ARTIFACTS_DIR, 'env-template');
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
function getArtifactDef() {
|
|
300
|
+
return [{
|
|
301
|
+
name: 'env-template',
|
|
302
|
+
// globPatterns: [`${getArtifactDirectory()}/**`],
|
|
303
|
+
globPatterns: ['**'],
|
|
304
|
+
rootDir: getArtifactDirectory() // context: env,
|
|
305
|
+
|
|
306
|
+
}];
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
//# sourceMappingURL=env-preview-template.task.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["env-preview-template.task.ts"],"names":["GENERATE_ENV_TEMPLATE_TASK_NAME","PREVIEW_ROOT_CHUNK_NAME","PEERS_CHUNK_NAME","EnvPreviewTemplateTask","constructor","preview","envs","execute","context","previewDefs","getDefs","htmlConfig","generateHtmlConfig","dev","targets","Promise","all","components","map","component","envDef","getEnvFromComponent","undefined","env","bundlingStrategy","getBundlingStrategy","name","envComponent","envPreviewConfig","getEnvPreviewConfig","isSplitComponentBundle","splitComponentBundle","envGetDeps","getDependencies","envComponentPeers","Object","keys","peerDependencies","envHostDeps","getHostDependencies","peers","concat","capsule","capsuleNetwork","graphCapsules","getCapsule","id","Error","previewRoot","writePreviewRuntime","previewModules","getPreviewModules","outputPath","computeOutputPath","entries","getEntries","runtimeChunkName","html","chunking","splitChunks","length","componentsResults","bundlerContext","assign","entry","externalizePeer","development","bundler","getBundler","bundlerResults","run","results","computeResults","previewRootChunkName","peersChunkName","options","htmlConfigs","previewModule","generateHtmlConfigForPreviewDef","previewDef","previewDeps","include","chunks","prefix","includePeers","unshift","config","title","templateContent","minify","filename","previewRootEntry","import","peersRootEntry","reduce","acc","module","linkFile","writeLink","ComponentMap","create","path","dependOn","result","errors","err","message","warning","warnings","startTime","endTime","artifacts","getArtifactDef","getEnv","modules","def","renderTemplatePathByEnv","getArtifactDirectory","CAPSULE_ARTIFACTS_DIR","globPatterns","rootDir"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAQA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAIA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AACA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAGA;AAAA;;AAAA;AAAA;AAAA;;AAAA;AAAA;;AAQO,MAAMA,+BAA+B,GAAG,qBAAxC;;AACA,MAAMC,uBAAuB,GAAG,aAAhC;;AACA,MAAMC,gBAAgB,GAAG,OAAzB;;;AAEA,MAAMC,sBAAN,CAAkD;AAIvD;AAEAC,EAAAA,WAAW,CAASC,OAAT,EAAuCC,IAAvC,EAAuD;AAAA,SAA9CD,OAA8C,GAA9CA,OAA8C;AAAA,SAAhBC,IAAgB,GAAhBA,IAAgB;AAAA,sDALvD,yBAKuD;AAAA,kDAJ3DN,+BAI2D;AAAA,sDAHzC,KAGyC;AAAE;;AAEvD,QAAPO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AD,EAAwBW,SAAxB,EAA8C;AACrE,UAAMuB,OAAO,GAAGlC,OAAO,CAACmC,cAAR,CAAuBC,aAAvB,CAAqCC,UAArC,CAAgD1B,SAAS,CAAC2B,EAA1D,CAAhB;AACA,QAAI,CAACJ,OAAL,EAAc,MAAM,IAAIK,KAAJ,CAAU,kBAAV,CAAN;AACd,WAAO,kBAAKL,OAAO,CAAC2D,IAAb,EAAmBgB,oBAAoB,EAAvC,CAAP;AACD,GApNsD,CAsNvD;AACA;AACA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;AAEA;AACA;AACA;AACA;AACA;;;AAxOuD;;;;AA2OlD,SAASA,oBAAT,GAAgC;AACrC,SAAO,kBAAKC,gCAAL,EAA4B,cAA5B,CAAP;AACD;;AAEM,SAASN,cAAT,GAA0B;AAC/B,SAAO,CACL;AACEtF,IAAAA,IAAI,EAAE,cADR;AAEE;AACA6F,IAAAA,YAAY,EAAE,CAAC,IAAD,CAHhB;AAIEC,IAAAA,OAAO,EAAEH,oBAAoB,EAJ/B,CAKE;;AALF,GADK,CAAP;AASD","sourcesContent":["import {\n BuildContext,\n BuiltTaskResult,\n BuildTask,\n TaskLocation,\n ComponentResult,\n CAPSULE_ARTIFACTS_DIR,\n} from '@teambit/builder';\nimport { Component, ComponentMap } from '@teambit/component';\nimport { Capsule } from '@teambit/isolator';\nimport { Bundler, BundlerContext, BundlerEntryMap, BundlerHtmlConfig, BundlerResult, Target } from '@teambit/bundler';\nimport type { EnvsMain } from '@teambit/envs';\nimport { join } from 'path';\nimport { compact } from 'lodash';\nimport { existsSync, mkdirpSync } from 'fs-extra';\nimport type { PreviewMain } from './preview.main.runtime';\nimport { PreviewDefinition } from '.';\nimport { html } from './webpack';\n\nexport type ModuleExpose = {\n name: string;\n path: string;\n include?: string[];\n};\n\nexport const GENERATE_ENV_TEMPLATE_TASK_NAME = 'GenerateEnvTemplate';\nexport const PREVIEW_ROOT_CHUNK_NAME = 'previewRoot';\nexport const PEERS_CHUNK_NAME = 'peers';\n\nexport class EnvPreviewTemplateTask implements BuildTask {\n aspectId = 'teambit.preview/preview';\n name = GENERATE_ENV_TEMPLATE_TASK_NAME;\n location: TaskLocation = 'end';\n // readonly dependencies = [CompilerAspect.id];\n\n constructor(private preview: PreviewMain, private envs: EnvsMain) {}\n\n async execute(context: BuildContext): Promise<BuiltTaskResult> {\n const previewDefs = this.preview.getDefs();\n const htmlConfig = this.generateHtmlConfig(previewDefs, PREVIEW_ROOT_CHUNK_NAME, PEERS_CHUNK_NAME, {\n dev: context.dev,\n });\n const targets: Target[] = compact(\n await Promise.all(\n context.components.map(async (component) => {\n const envDef = this.envs.getEnvFromComponent(component);\n if (!envDef) return undefined;\n const env = envDef.env;\n const bundlingStrategy = this.preview.getBundlingStrategy(envDef.env);\n if (bundlingStrategy.name === 'env') {\n return undefined;\n }\n const envComponent = component;\n const envPreviewConfig = this.preview.getEnvPreviewConfig(envDef.env);\n const isSplitComponentBundle = envPreviewConfig.splitComponentBundle ?? false;\n const envGetDeps = (await env.getDependencies()) || {};\n const envComponentPeers = Object.keys(envGetDeps.peerDependencies || {}) || [];\n const envHostDeps = env.getHostDependencies() || [];\n const peers = envComponentPeers.concat(envHostDeps);\n\n // const module = await this.getPreviewModule(envComponent);\n // const entries = Object.keys(module).map((key) => module.exposes[key]);\n const capsule = context.capsuleNetwork.graphCapsules.getCapsule(envComponent.id);\n if (!capsule) throw new Error('no capsule found');\n const previewRoot = await this.preview.writePreviewRuntime(context);\n const previewModules = await this.getPreviewModules(envComponent);\n // const templatesFile = previewModules.map((template) => {\n // return this.preview.writeLink(template.name, ComponentMap.create([]), template.path, capsule.path);\n // });\n const outputPath = this.computeOutputPath(context, envComponent);\n if (!existsSync(outputPath)) mkdirpSync(outputPath);\n const entries = this.getEntries(previewModules, capsule, previewRoot, isSplitComponentBundle, peers);\n\n // const entries = this.getEntries(\n // previewModules.concat({\n // name: 'main',\n // path: previewRoot,\n // })\n // );\n\n return {\n // entries: templatesFile.concat(previewRoot),\n peers,\n runtimeChunkName: 'runtime',\n html: htmlConfig,\n entries,\n chunking: {\n splitChunks: true,\n },\n components: [envComponent],\n outputPath,\n // modules: [module],\n };\n })\n )\n );\n\n if (!targets.length) return { componentsResults: [] };\n const bundlerContext: BundlerContext = Object.assign({}, context, {\n targets,\n entry: [],\n externalizePeer: false,\n development: context.dev,\n });\n\n const bundler: Bundler = await context.env.getBundler(bundlerContext);\n const bundlerResults = await bundler.run();\n const results = await this.computeResults(bundlerContext, bundlerResults);\n return results;\n }\n\n private generateHtmlConfig(\n previewDefs: PreviewDefinition[],\n previewRootChunkName: string,\n peersChunkName: string,\n options: { dev?: boolean }\n ): BundlerHtmlConfig[] {\n const htmlConfigs = previewDefs.map((previewModule) =>\n this.generateHtmlConfigForPreviewDef(previewModule, previewRootChunkName, peersChunkName, options)\n );\n return htmlConfigs;\n }\n\n private generateHtmlConfigForPreviewDef(\n previewDef: PreviewDefinition,\n previewRootChunkName: string,\n peersChunkName: string,\n options: { dev?: boolean }\n ): BundlerHtmlConfig {\n const previewDeps = previewDef.include || [];\n const chunks = [...previewDeps, previewDef.prefix, previewRootChunkName];\n if (previewDef.includePeers) {\n chunks.unshift(peersChunkName);\n }\n\n const config = {\n title: 'Preview',\n templateContent: html('Preview'),\n minify: options?.dev ?? true,\n chunks,\n filename: `${previewDef.prefix}.html`,\n };\n return config;\n }\n\n getEntries(\n previewModules: ModuleExpose[],\n capsule: Capsule,\n previewRoot: string,\n isSplitComponentBundle = false,\n peers: string[] = []\n ): BundlerEntryMap {\n const previewRootEntry = {\n filename: 'preview-root.[chunkhash].js',\n import: previewRoot,\n };\n\n const peersRootEntry = {\n filename: 'peers.[chunkhash].js',\n import: peers,\n };\n\n const entries = previewModules.reduce(\n (acc, module) => {\n const linkFile = this.preview.writeLink(\n module.name,\n ComponentMap.create([]),\n module.path,\n capsule.path,\n isSplitComponentBundle\n );\n acc[module.name] = {\n // filename: `${module.name}.[contenthash].js`,\n filename: `${module.name}.[chunkhash].js`,\n // filename: `${module.name}.js`,\n import: linkFile,\n // library: {\n // name: module.name,\n // type: 'umd',\n // },\n };\n if (module.include) {\n acc[module.name].dependOn = module.include;\n }\n return acc;\n },\n { [PREVIEW_ROOT_CHUNK_NAME]: previewRootEntry, [PEERS_CHUNK_NAME]: peersRootEntry }\n );\n\n return entries;\n\n // const mergedEntries = return entriesArr.reduce((entriesMap, entry) => {\n // entriesMap[entry.library.name] = entry;\n // return entriesMap;\n // }, {previewRoot: previewRootEntry});\n }\n\n async computeResults(context: BundlerContext, results: BundlerResult[]) {\n const result = results[0];\n\n const componentsResults: ComponentResult[] = result.components.map((component) => {\n return {\n component,\n errors: result.errors.map((err) => (typeof err === 'string' ? err : err.message)),\n warning: result.warnings,\n startTime: result.startTime,\n endTime: result.endTime,\n };\n });\n\n const artifacts = getArtifactDef();\n\n return {\n componentsResults,\n artifacts,\n };\n }\n\n async getPreviewModules(envComponent: Component): Promise<ModuleExpose[]> {\n const env = this.envs.getEnv(envComponent);\n const previewDefs = this.preview.getDefs();\n const modules = compact(\n await Promise.all(\n previewDefs.map(async (def) => {\n if (!def.renderTemplatePathByEnv) return undefined;\n return {\n name: def.prefix,\n path: await def.renderTemplatePathByEnv(env.env),\n include: def.include,\n };\n })\n )\n );\n\n return modules;\n }\n\n private computeOutputPath(context: BuildContext, component: Component) {\n const capsule = context.capsuleNetwork.graphCapsules.getCapsule(component.id);\n if (!capsule) throw new Error('no capsule found');\n return join(capsule.path, getArtifactDirectory());\n }\n\n // private async getPreviewModule(envComponent: Component): Promise<ModuleTarget> {\n // const env = this.envs.getEnv(envComponent);\n // const previewDefs = this.preview.getDefs();\n // const modules = compact(await Promise.all(previewDefs.map(async (def) => {\n // if (!def.renderTemplatePathByEnv) return undefined;\n // return [def.prefix, await def.renderTemplatePathByEnv(env.env)];\n // })));\n\n // const exposes = modules.reduce((exposesAcc, [prefix, path]) => {\n // const internalPath = `./${prefix}`;\n // exposesAcc[internalPath] = path;\n // return exposesAcc;\n // }, {});\n\n // return {\n // component: envComponent,\n // exposes\n // };\n // }\n}\n\nexport function getArtifactDirectory() {\n return join(CAPSULE_ARTIFACTS_DIR, 'env-template');\n}\n\nexport function getArtifactDef() {\n return [\n {\n name: 'env-template',\n // globPatterns: [`${getArtifactDirectory()}/**`],\n globPatterns: ['**'],\n rootDir: getArtifactDirectory(),\n // context: env,\n },\n ];\n}\n"]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/// <reference types="qs" />
|
|
2
|
+
/// <reference types="express" />
|
|
3
|
+
import type { NextFunction, Request, Response } from '@teambit/express';
|
|
4
|
+
import type { ComponentUrlParams, RegisteredComponentRoute } from '@teambit/component';
|
|
5
|
+
import type { Logger } from '@teambit/logger';
|
|
6
|
+
import type { PreviewMain } from './preview.main.runtime';
|
|
7
|
+
declare type UrlParams = ComponentUrlParams & {
|
|
8
|
+
filePath?: string;
|
|
9
|
+
};
|
|
10
|
+
export declare class EnvTemplateRoute implements RegisteredComponentRoute {
|
|
11
|
+
/**
|
|
12
|
+
* preview extension.
|
|
13
|
+
*/
|
|
14
|
+
private preview;
|
|
15
|
+
private logger;
|
|
16
|
+
constructor(
|
|
17
|
+
/**
|
|
18
|
+
* preview extension.
|
|
19
|
+
*/
|
|
20
|
+
preview: PreviewMain, logger: Logger);
|
|
21
|
+
route: string;
|
|
22
|
+
method: string;
|
|
23
|
+
resolveComponent: boolean;
|
|
24
|
+
middlewares: (((req: Request<import("./artifact-file-middleware").PreviewUrlParams, any, any, import("qs").ParsedQs>, res: import("express").Response<any>) => Promise<import("express").Response<any>>) | ((req: Request<UrlParams>, res: Response, next: NextFunction) => Promise<void | import("express").Response<any>>))[];
|
|
25
|
+
}
|
|
26
|
+
export {};
|