@teambit/envs 1.0.106 → 1.0.108

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/env.plugin.ts ADDED
@@ -0,0 +1,60 @@
1
+ import { PluginDefinition } from '@teambit/aspect-loader';
2
+ import { Aspect, Harmony } from '@teambit/harmony';
3
+ import { ComponentID } from '@teambit/component';
4
+ import { WorkerMain } from '@teambit/worker';
5
+ import { MainRuntime } from '@teambit/cli';
6
+ import { LoggerMain } from '@teambit/logger';
7
+ import { flatten } from 'lodash';
8
+ import { ServiceHandlerContext as EnvContext } from './services/service-handler-context';
9
+ import { Env } from './env-interface';
10
+ import { EnvsRegistry, ServicesRegistry } from './environments.main.runtime';
11
+
12
+ export class EnvPlugin implements PluginDefinition {
13
+ constructor(
14
+ private envSlot: EnvsRegistry,
15
+ private servicesRegistry: ServicesRegistry,
16
+ private loggerMain: LoggerMain,
17
+ private workerMain: WorkerMain,
18
+ private harmony: Harmony
19
+ ) {}
20
+
21
+ pattern = '*.bit-env.*';
22
+
23
+ runtimes = [MainRuntime.name];
24
+
25
+ private createContext(envId: ComponentID) {
26
+ return new EnvContext(envId, this.loggerMain, this.workerMain, this.harmony);
27
+ }
28
+
29
+ private transformToLegacyEnv(envId: string, env: Env) {
30
+ const envComponentId = ComponentID.fromString(envId);
31
+ const envContext = this.createContext(envComponentId);
32
+ const allServices = flatten(this.servicesRegistry.values());
33
+ const transformers = allServices.reduce((acc, service) => {
34
+ if (!service.transform) return acc;
35
+ const currTransformer = service.transform(env, envContext);
36
+ if (!currTransformer) return acc;
37
+ return { ...acc, ...currTransformer };
38
+ }, {});
39
+
40
+ if (!env.preview && !env.compiler) return undefined;
41
+
42
+ return {
43
+ ...transformers,
44
+ name: env.name,
45
+ icon: env.icon,
46
+ __getDescriptor: async () => {
47
+ return {
48
+ type: env.name,
49
+ };
50
+ },
51
+ id: envId,
52
+ };
53
+ }
54
+
55
+ register(object: any, aspect: Aspect) {
56
+ const env = this.transformToLegacyEnv(aspect.id, object);
57
+ if (!env) return undefined;
58
+ return this.envSlot.register(env);
59
+ }
60
+ }
package/environment.ts ADDED
@@ -0,0 +1,236 @@
1
+ // TODO: @gilad refactor to an abstract env.
2
+ import type { Linter, LinterContext } from '@teambit/linter';
3
+ import type { Formatter, FormatterContext } from '@teambit/formatter';
4
+ import type { Tester } from '@teambit/tester';
5
+ import type { Compiler } from '@teambit/compiler';
6
+ import type { Bundler, BundlerContext, DevServer, DevServerContext } from '@teambit/bundler';
7
+ import type { BuildTask } from '@teambit/builder';
8
+ import type { SchemaExtractor } from '@teambit/schema';
9
+ import type { WebpackConfigTransformer } from '@teambit/webpack';
10
+ import type { PackageJsonProps } from '@teambit/pkg';
11
+ import type { DependencyDetector, EnvPolicyConfigObject } from '@teambit/dependency-resolver';
12
+ import type { Capsule } from '@teambit/isolator';
13
+ import type { Component } from '@teambit/component';
14
+ import { EnvPreviewConfig } from '@teambit/preview';
15
+
16
+ export type EnvDescriptor = {
17
+ type: string;
18
+ };
19
+
20
+ /**
21
+ * add a custom type and include all properties from within the environment.
22
+ */
23
+ export interface Environment {
24
+ /**
25
+ * name of the environment.
26
+ */
27
+ name?: string;
28
+
29
+ /**
30
+ * description of the environment.
31
+ */
32
+ description?: string;
33
+
34
+ /**
35
+ * icon of the environment.
36
+ */
37
+ icon?: string;
38
+
39
+ [key: string]: any; // :TODO need to define an abstract type for service handlers (now using any)
40
+
41
+ /**
42
+ * Returns the Environment descriptor
43
+ * Required for any task
44
+ */
45
+ __getDescriptor?: () => Promise<EnvDescriptor>;
46
+
47
+ /**
48
+ * Returns a schema generator instance
49
+ */
50
+ getSchemaExtractor?: (config?: any, tsserverPath?: string, contextPath?: string) => SchemaExtractor;
51
+
52
+ /**
53
+ * Returns the dev patterns to match doc files
54
+ */
55
+ getDocsDevPatterns?: (component: Component) => string[];
56
+
57
+ /**
58
+ * Returns the dev patterns to match composition files
59
+ */
60
+ getCompositionsDevPatterns?: (component: Component) => string[];
61
+
62
+ /**
63
+ * Returns additional dev patterns for the component.
64
+ * Patterns that were provided by getDocsDevPatterns, getTestsDevPatterns will be considered as dev files as well, without need to add them here.
65
+ */
66
+ getDevPatterns?: (component: Component) => string[];
67
+ }
68
+
69
+ export interface DependenciesEnv extends Environment {
70
+ /**
71
+ * Returns the list of dependencies
72
+ * Required for any task
73
+ */
74
+ getDependencies?: () => EnvPolicyConfigObject | Promise<EnvPolicyConfigObject>;
75
+
76
+ /**
77
+ * Returns a list of additional test host dependencies
78
+ * this will be added to the tester context
79
+ * This can be used in cases when you want specific dependencies to be resolved from the env during testing
80
+ * but you don't want these dependencies as peer dependencies of the component (as they are not used during runtime)
81
+ * An example for this is @angular/compiler, which during running tests you want to resolve from the env, but you don't
82
+ * need it during component runtime.
83
+ */
84
+ getAdditionalTestHostDependencies?: () => string[] | Promise<string[]>;
85
+
86
+ /**
87
+ * Returns a list of additional host dependencies
88
+ * this list will be provided as globals on the window after bit preview bundle
89
+ * by default bit will merge this list with the peers from the getDependencies function
90
+ */
91
+ getAdditionalHostDependencies?: () => string[] | Promise<string[]>;
92
+
93
+ /**
94
+ * Returns a list of dependency detectors
95
+ * this list will be used to detect all the dependencies in each file of the component
96
+ */
97
+ getDepDetectors?: () => DependencyDetector[] | null;
98
+ }
99
+
100
+ export type GetNpmIgnoreContext = {
101
+ capsule: Capsule;
102
+ component: Component;
103
+ };
104
+ export interface PackageEnv extends Environment {
105
+ /**
106
+ * define the package json properties to add to each component.
107
+ * Used by `bit link` to augment package.json with new properties
108
+ */
109
+ getPackageJsonProps?: () => PackageJsonProps;
110
+
111
+ /**
112
+ * return `.npmignore` entries to be written before packing the component
113
+ */
114
+ getNpmIgnore?: (npmIgnoreContext?: GetNpmIgnoreContext) => string[];
115
+ }
116
+
117
+ export interface LinterEnv extends Environment {
118
+ /**
119
+ * Returns & configures the linter to use (ESLint, ...)
120
+ * Required for `bit lint`
121
+ */
122
+ getLinter?: (context: LinterContext, transformers: any[]) => Linter;
123
+ }
124
+
125
+ export interface FormatterEnv extends Environment {
126
+ /**
127
+ * Returns & configures the formatter to use (prettier, ...)
128
+ * Required for `bit format`
129
+ */
130
+ getFormatter?: (context: FormatterContext, transformers: any[]) => Formatter;
131
+ }
132
+
133
+ export interface PreviewEnv extends Environment {
134
+ /**
135
+ * Returns a paths to a function which mounts a given component to DOM
136
+ * Required for `bit start` & `bit build`
137
+ */
138
+ getMounter?: () => string;
139
+
140
+ /**
141
+ * Returns a path to a docs template.
142
+ * Required for `bit start` & `bit build`
143
+ */
144
+ getDocsTemplate?: () => string;
145
+
146
+ /**
147
+ * Returns a bundler for the preview.
148
+ * Required for `bit build` & `bit start`
149
+ */
150
+ getBundler?: (context: BundlerContext, transformers: any[]) => Promise<Bundler>;
151
+
152
+ /**
153
+ * Returns preview config like the strategy name to use when bundling the components for the preview
154
+ */
155
+ getPreviewConfig?: () => EnvPreviewConfig;
156
+
157
+ /**
158
+ * Returns a bundler for the env template.
159
+ * this bundler will be used to bundle the docs/compositions (or other preview) apps
160
+ * Required for `bit build` & `bit tag`
161
+ */
162
+ getTemplateBundler?: (context: BundlerContext, transformers?: any[]) => Promise<Bundler>;
163
+ }
164
+
165
+ export type PipeServiceModifiersMap = Record<string, PipeServiceModifier>;
166
+
167
+ export interface PipeServiceModifier {
168
+ transformers?: Function[];
169
+ module?: any;
170
+ }
171
+
172
+ export interface BuilderEnv extends PreviewEnv {
173
+ /**
174
+ * @deprecated Fatal: a breaking API was introduced. Use getBuildPipe() instead.
175
+ */
176
+ getPipe?: () => BuildTask[];
177
+
178
+ /**
179
+ * Returns the component build pipeline
180
+ * Either `getBuildPipe`, `getTagPipe`, or `getSnapPipe` is required for `bit build`
181
+ */
182
+ getBuildPipe?: (modifiersMap?: PipeServiceModifiersMap) => BuildTask[];
183
+
184
+ /**
185
+ * Returns the component tag pipeline
186
+ * Either `getBuildPipe`, `getTagPipe`, or `getSnapPipe` is required for `bit build`
187
+ */
188
+ getTagPipe?: (modifiersMap?: PipeServiceModifiersMap) => BuildTask[];
189
+
190
+ /**
191
+ * Returns the component snap pipeline
192
+ * Either `getBuildPipe`, `getTagPipe`, or `getSnapPipe` is required for `bit build`
193
+ */
194
+ getSnapPipe?: (modifiersMap?: PipeServiceModifiersMap) => BuildTask[];
195
+ }
196
+
197
+ export interface TesterEnv extends Environment {
198
+ /**
199
+ * Returns a tester
200
+ * Required for `bit start` & `bit test`
201
+ */
202
+ getTester?: (path: string, tester: any) => Tester;
203
+
204
+ /**
205
+ * Returns the dev patterns to match test files
206
+ */
207
+ getTestsDevPatterns?: (component: Component) => string[];
208
+ }
209
+
210
+ export interface CompilerEnv {
211
+ /**
212
+ * Returns a compiler
213
+ * Required for making and reading dists, especially for `bit compile`
214
+ */
215
+ getCompiler: () => Compiler;
216
+ }
217
+
218
+ export function hasCompiler(obj: Environment): obj is CompilerEnv {
219
+ return typeof obj.getCompiler === 'function';
220
+ }
221
+
222
+ export interface DevEnv extends PreviewEnv {
223
+ /**
224
+ * Required for `bit start`
225
+ */
226
+ getDevEnvId?: (context?: any) => string;
227
+
228
+ /**
229
+ * Returns and configures the dev server
230
+ * Required for `bit start`
231
+ */
232
+ getDevServer?: (
233
+ context: DevServerContext,
234
+ transformers: WebpackConfigTransformer[]
235
+ ) => DevServer | Promise<DevServer>;
236
+ }
@@ -0,0 +1,5 @@
1
+ import { Aspect } from '@teambit/harmony';
2
+
3
+ export const EnvsAspect = Aspect.create({
4
+ id: 'teambit.envs/envs',
5
+ });
@@ -0,0 +1,27 @@
1
+ import { Component } from '@teambit/component';
2
+ import { Schema } from '@teambit/graphql';
3
+ import gql from 'graphql-tag';
4
+
5
+ import { EnvsMain } from './environments.main.runtime';
6
+
7
+ export function environmentsSchema(environments: EnvsMain): Schema {
8
+ return {
9
+ typeDefs: gql`
10
+ extend type Component {
11
+ env: ExtensionDescriptor
12
+ }
13
+
14
+ type ExtensionDescriptor {
15
+ id: String
16
+ icon: String
17
+ }
18
+ `,
19
+ resolvers: {
20
+ Component: {
21
+ env: (component: Component) => {
22
+ return environments.getDescriptor(component);
23
+ },
24
+ },
25
+ },
26
+ };
27
+ }