Skip to content

Commit cfebfdb

Browse files
committed
Merge branch 'feat/vite-preview-2024'
2 parents dc8ebde + 70f1871 commit cfebfdb

File tree

212 files changed

+1749
-2910
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

212 files changed

+1749
-2910
lines changed

.bitmap

Lines changed: 67 additions & 53 deletions
Large diffs are not rendered by default.

angular/app-types/angular-app-type/angular-app-options.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
2-
AngularEnvOptions,
32
ApplicationOptions,
43
BrowserOptions,
54
DevServerOptions
@@ -53,9 +52,4 @@ export type AngularAppOptions = {
5352
* Angular options for `bit run`
5453
*/
5554
angularServeOptions: (BrowserOptions & DevServerOptions) | (ApplicationOptions & DevServerOptions);
56-
57-
/**
58-
* Env-specific options depending on the version of Angular used.
59-
*/
60-
ngEnvOptions: AngularEnvOptions;
6155
};

angular/app-types/angular-app-type/angular.application.ts

Lines changed: 39 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { VERSION } from '@angular/cli';
22
import { getWorkspace, NG_APP_NAME, normalizePath } from '@bitdev/angular.dev-services.common';
33
import { ApplicationBuilderOptions, SsrClass } from '@bitdev/angular.dev-services.ng-compat';
4-
import { AngularPreview } from '@bitdev/angular.dev-services.preview.preview';
4+
import { AngularVitePreview } from '@bitdev/angular.dev-services.preview.vite-preview';
5+
import { buildApplication, generateAppTsConfig, getEnvFile, serveApplication } from '@bitdev/angular.dev-services.vite';
56
import {
67
AppBuildContext,
78
AppBuildResult,
@@ -11,25 +12,17 @@ import {
1112
DeployFn
1213
} from '@teambit/application';
1314
import { Bundler, BundlerContext, DevServerContext } from '@teambit/bundler';
14-
import { Component } from '@teambit/component';
1515
import { DependencyResolverAspect, DependencyResolverMain } from '@teambit/dependency-resolver';
1616
import { EnvContext, EnvHandler } from '@teambit/envs';
17-
import { CACHE_ROOT } from '@teambit/legacy/dist/constants.js';
17+
import { CACHE_ROOT } from '@teambit/legacy.constants';
1818
import { Preview } from '@teambit/preview';
1919
import { Port } from '@teambit/toolbox.network.get-port';
2020
import { Workspace } from '@teambit/workspace';
2121
import assert from 'assert';
2222
import fs from 'fs-extra';
2323
import { cloneDeep } from 'lodash-es';
24-
import objectHash from 'object-hash';
2524
import { join } from 'path';
26-
import ts from 'typescript';
2725
import { AngularAppOptions } from './angular-app-options.js';
28-
import { buildApplication } from './application.bundler.js';
29-
import { serveApplication } from './application.dev-server.js';
30-
import { expandIncludeExclude, JsonObject } from './utils.js';
31-
32-
const writeHash = new Map<string, string>();
3326

3427
export class AngularApp implements Application {
3528
readonly name: string;
@@ -63,10 +56,9 @@ export class AngularApp implements Application {
6356
}
6457

6558
private getDevServerContext(context: AppContext): DevServerContext {
66-
// const ngEnvOptions = this.angularEnv.getNgEnvOptions();
6759
return Object.assign(cloneDeep(context), {
6860
entry: [],
69-
rootPath: /*ngEnvOptions.devServer === 'vite' ? _appRootPath : */'',
61+
rootPath: '',
7062
publicPath: `${this.publicDir}/${this.options.name}`,
7163
title: this.options.name
7264
});
@@ -93,48 +85,11 @@ export class AngularApp implements Application {
9385
const angularServeOptions: any = Object.assign(cloneDeep(this.options.angularServeOptions), { tsConfig: tsconfigPath });
9486
const angularBuildOptions: any = Object.assign(cloneDeep(this.options.angularBuildOptions), { tsConfig: tsconfigPath });
9587

96-
return AngularPreview.from({
97-
webpackServeTransformers: this.options.webpackServeTransformers,
98-
webpackBuildTransformers: this.options.webpackBuildTransformers,
88+
return AngularVitePreview.from({
9989
angularServeOptions,
10090
angularBuildOptions,
101-
ngEnvOptions: this.options.ngEnvOptions,
10291
sourceRoot: this.options.sourceRoot
10392
});
104-
105-
}
106-
107-
private generateTsConfig(bitCmps: Component[], appRootPath: string, appTsconfigPath: string, tsconfigPath: string, depsResolver: DependencyResolverMain, workspace?: Workspace, serverEntry?: string): void {
108-
const tsconfigJSON: JsonObject = ts.readConfigFile(appTsconfigPath, ts.sys.readFile).config;
109-
110-
// Add the paths to tsconfig to remap bit components to local folders
111-
tsconfigJSON.compilerOptions.paths = tsconfigJSON.compilerOptions.paths || {};
112-
bitCmps.forEach((dep: Component) => {
113-
let componentDir = workspace?.componentDir(dep.id, {
114-
ignoreVersion: true
115-
});
116-
if (componentDir) {
117-
componentDir = normalizePath(componentDir);
118-
const pkgName = depsResolver.getPackageName(dep);
119-
// TODO we should find a way to use the real entry file based on the component config because people can change it
120-
if (fs.existsSync(join(componentDir, 'public-api.ts'))) {
121-
tsconfigJSON.compilerOptions.paths[pkgName] = [`${componentDir}/public-api.ts`, `${componentDir}`];
122-
}
123-
tsconfigJSON.compilerOptions.paths[`${pkgName}/*`] = [`${componentDir}/*`];
124-
}
125-
});
126-
127-
if (serverEntry) {
128-
tsconfigJSON.files.push(serverEntry);
129-
}
130-
131-
const tsconfigContent = expandIncludeExclude(tsconfigJSON, tsconfigPath, [appRootPath]);
132-
const hash = objectHash(tsconfigContent);
133-
// write only if link has changed (prevents triggering fs watches)
134-
if (writeHash.get(tsconfigPath) !== hash) {
135-
fs.outputJsonSync(tsconfigPath, tsconfigContent, { spaces: 2 });
136-
writeHash.set(tsconfigPath, hash);
137-
}
13893
}
13994

14095
/**
@@ -145,16 +100,6 @@ export class AngularApp implements Application {
145100
return context as any as EnvContext;
146101
}
147102

148-
private async getEnvFile(mode: string, rootDir: string, overrides?: Record<string, string>) {
149-
// TODO: enable this one we have ESM envs, otherwise we get a warning message about loading the deprecated CJS build of Vite
150-
// const vite = await import('vite');
151-
// const dotenv = vite.loadEnv(mode, rootDir);
152-
return {
153-
...overrides
154-
// ...dotenv
155-
};
156-
}
157-
158103
// TODO: fix return type once bit has a new stable version
159104
async run(context: AppContext): Promise<ApplicationInstance> {
160105
const depsResolver = context.getAspect<DependencyResolverMain>(DependencyResolverAspect.id);
@@ -171,32 +116,23 @@ export class AngularApp implements Application {
171116
const bitCmps = await workspace.getMany(workspaceCmpsIDs);
172117
const tempFolder = this.getTempFolder(workspace);
173118
const tsconfigPath = this.getTsconfigPath(tempFolder);
174-
this.generateTsConfig(bitCmps, appRootPath, appTsconfigPath, tsconfigPath, depsResolver, workspace);
175-
176-
if (Number(VERSION.major) >= 16) {
177-
const envVars = await this.getEnvFile('development', appRootPath, context.envVariables as any);
178-
await serveApplication({
179-
angularOptions: {
180-
...this.options.angularBuildOptions as ApplicationBuilderOptions,
181-
tsConfig: tsconfigPath
182-
},
183-
sourceRoot: this.options.sourceRoot || 'src',
184-
workspaceRoot: appRootPath,
185-
port,
186-
logger: logger,
187-
tempFolder: tempFolder,
188-
envVars: {
189-
process: { env: envVars }
190-
}
191-
});
192-
} else {
193-
const devServerContext = this.getDevServerContext(context);
194-
const envContext = this.getEnvContext(context);
195-
const preview = this.getPreview(tsconfigPath)(envContext);
196-
197-
const devServer = await preview.getDevServer(devServerContext)(envContext);
198-
await devServer.listen(port);
199-
}
119+
generateAppTsConfig(bitCmps, appRootPath, appTsconfigPath, tsconfigPath, depsResolver, workspace);
120+
121+
const envVars = await getEnvFile('development', appRootPath, context.envVariables as any);
122+
await serveApplication({
123+
angularOptions: {
124+
...this.options.angularBuildOptions as ApplicationBuilderOptions,
125+
tsConfig: tsconfigPath
126+
},
127+
sourceRoot: this.options.sourceRoot || 'src',
128+
workspaceRoot: appRootPath,
129+
port,
130+
logger: logger,
131+
tempFolder: tempFolder,
132+
envVars: {
133+
process: { env: envVars }
134+
}
135+
});
200136

201137
return {
202138
appName: this.name,
@@ -221,11 +157,12 @@ export class AngularApp implements Application {
221157
}
222158
const tempFolder = this.getTempFolder();
223159
const tsconfigPath = this.getTsconfigPath(tempFolder);
224-
this.generateTsConfig([capsule.component], appRootPath, appTsconfigPath, tsconfigPath, depsResolver, undefined, entryServer);
160+
generateAppTsConfig([capsule.component], appRootPath, appTsconfigPath, tsconfigPath, depsResolver, undefined, entryServer ? [entryServer] : undefined);
225161

226-
if (!this.options.bundler && Number(VERSION.major) >= 16) {
227-
const envVars = await this.getEnvFile('production', appRootPath, context.envVariables as any);
228-
await buildApplication({
162+
const errors: Error[] = [];
163+
if (!this.options.bundler) {
164+
const envVars = await getEnvFile('production', appRootPath, context.envVariables);
165+
const results = await buildApplication({
229166
angularOptions: {
230167
...appOptions,
231168
tsConfig: tsconfigPath
@@ -240,7 +177,11 @@ export class AngularApp implements Application {
240177
'process.env': envVars
241178
}
242179
});
243-
console.log('build done');
180+
for (const result of results) {
181+
if (result.error) {
182+
errors.push(new Error(result.error));
183+
}
184+
}
244185
} else {
245186
let bundler: Bundler;
246187
if (this.options.bundler) {
@@ -252,9 +193,15 @@ export class AngularApp implements Application {
252193

253194
bundler = await preview.getBundler(bundlerContext)(envContext);
254195
}
255-
await bundler.run();
196+
const results = await bundler.run();
197+
for (const result of results) {
198+
if (result.errors) {
199+
errors.push(...result.errors);
200+
}
201+
}
256202
}
257203
return {
204+
errors,
258205
artifacts: [{
259206
name: this.name,
260207
globPatterns: [outputPath],

angular/app-types/angular-app-type/component.json

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,30 @@
22
"componentId": {
33
"scope": "bitdev.angular",
44
"name": "app-types/angular-app-type",
5-
"version": "7.0.0"
5+
"version": "8.0.1"
66
},
77
"propagate": false,
88
"extensions": {
99
"teambit.dependencies/dependency-resolver": {
1010
"policy": {
1111
"dependencies": {
12-
"h3": "^1.9.0",
13-
"nitropack": "^2.8.0",
14-
"@angular-devkit/build-angular": "-",
15-
"@angular/cli": "-",
16-
"typescript": "-",
17-
"#alias": "-"
12+
"@angular/cli": "-"
1813
},
1914
"peerDependencies": {
20-
"@angular-devkit/build-angular": ">= 0.0.1",
21-
"@angular/cli": ">= 13.0.0",
22-
"esbuild": ">= 0.14.0",
23-
"typescript": ">= 3.5.3"
15+
"@angular/cli": ">= 13.0.0"
2416
}
2517
}
2618
},
27-
"bitdev.node/[email protected]": {},
28-
"teambit.envs/envs": {},
2919
"teambit.component/renaming": {
3020
"renamedFrom": {
3121
"scope": "bitdev.angular",
3222
"name": "dev-services/apps",
3323
"version": "02f8cd3d639894dea416cc38674a6887e11e939b"
3424
}
25+
},
26+
"bitdev.node/[email protected]": {},
27+
"teambit.envs/envs": {
28+
"env": "bitdev.node/node-env"
3529
}
3630
}
3731
}

angular/app-types/angular-app-type/utils.ts

Lines changed: 0 additions & 65 deletions
This file was deleted.

angular/devkit/common/component.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"componentId": {
33
"scope": "bitdev.angular",
44
"name": "dev-services/common",
5-
"version": "7.0.0"
5+
"version": "33b27185d4339685b5106095d560648a1e296ae7"
66
},
77
"propagate": false,
88
"extensions": {
@@ -19,14 +19,14 @@
1919
}
2020
}
2121
},
22-
"teambit.node/[email protected]": {},
2322
"teambit.envs/envs": {},
2423
"teambit.component/dev-files": {},
2524
"teambit.pkg/pkg": {},
2625
"teambit.preview/preview": {},
2726
"teambit.compositions/compositions": {},
2827
"teambit.docs/docs": {},
2928
"teambit.pipelines/builder": {},
30-
"teambit.semantics/schema": {}
29+
"teambit.semantics/schema": {},
30+
"teambit.node/[email protected]": {}
3131
}
3232
}

angular/devkit/common/env-options.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ import { WebpackConfigWithDevServer } from '@teambit/webpack';
33
export type WebpackConfigFactory = (opts: any) => Promise<WebpackConfigWithDevServer>;
44

55
export type AngularEnvOptions = {
6-
76
/**
87
* Use Rollup & Angular Elements to compile compositions instead of webpack.
98
* This transforms compositions into Web Components and replaces the Angular bundler by the React bundler.
109
*/
1110
useAngularElementsPreview?: boolean;
12-
jestConfigPath: string;
1311
jestModulePath: string;
1412
ngPackagrModulePath: string;
1513
angularElementsModulePath?: string;

0 commit comments

Comments
 (0)