@teambit/typescript 1.0.108 → 1.0.110

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.
@@ -1,286 +0,0 @@
1
- import { BuildContext, BuiltTaskResult, ComponentResult } from '@teambit/builder';
2
- import { Compiler, TranspileFileParams, TranspileFileOutput } from '@teambit/compiler';
3
- import { Network } from '@teambit/isolator';
4
- import { Logger } from '@teambit/logger';
5
- import fs from 'fs-extra';
6
- import path from 'path';
7
- import ts from 'typescript';
8
- import { BitError } from '@teambit/bit-error';
9
- import { TypeScriptCompilerOptions } from './compiler-options';
10
-
11
- export class TypescriptCompiler implements Compiler {
12
- distDir: string;
13
- distGlobPatterns: string[];
14
- shouldCopyNonSupportedFiles: boolean;
15
- artifactName: string;
16
- constructor(
17
- readonly id: string,
18
- private logger: Logger,
19
- private options: TypeScriptCompilerOptions,
20
- private tsModule: typeof ts
21
- ) {
22
- this.distDir = options.distDir || 'dist';
23
- this.distGlobPatterns = options.distGlobPatterns || [`${this.distDir}/**`, `!${this.distDir}/tsconfig.tsbuildinfo`];
24
- this.shouldCopyNonSupportedFiles =
25
- typeof options.shouldCopyNonSupportedFiles === 'boolean' ? options.shouldCopyNonSupportedFiles : true;
26
- this.artifactName = options.artifactName || 'dist';
27
- this.options.tsconfig ||= {};
28
- this.options.tsconfig.compilerOptions ||= {};
29
- // mutate the outDir, otherwise, on capsules, the dists might be written to a different directory and make confusion
30
- this.options.tsconfig.compilerOptions.outDir = this.distDir;
31
- }
32
-
33
- displayName = 'TypeScript';
34
- deleteDistDir = false;
35
-
36
- displayConfig() {
37
- return this.stringifyTsconfig(this.options.tsconfig);
38
- }
39
-
40
- getDistDir() {
41
- return this.distDir;
42
- }
43
-
44
- /**
45
- * compile one file on the workspace
46
- */
47
- transpileFile(fileContent: string, options: TranspileFileParams): TranspileFileOutput {
48
- if (!this.isFileSupported(options.filePath)) {
49
- return null; // file is not supported
50
- }
51
- const compilerOptionsFromTsconfig = this.tsModule.convertCompilerOptionsFromJson(
52
- this.options.tsconfig.compilerOptions,
53
- '.'
54
- );
55
- if (compilerOptionsFromTsconfig.errors.length) {
56
- // :TODO @david replace to a more concrete error type and put in 'exceptions' directory here.
57
- const formattedErrors = this.tsModule.formatDiagnosticsWithColorAndContext(
58
- compilerOptionsFromTsconfig.errors,
59
- this.getFormatDiagnosticsHost()
60
- );
61
- throw new Error(`failed parsing the tsconfig.json.\n${formattedErrors}`);
62
- }
63
-
64
- const compilerOptions = compilerOptionsFromTsconfig.options;
65
- compilerOptions.sourceRoot = options.componentDir;
66
- compilerOptions.rootDir = '.';
67
- const result = this.tsModule.transpileModule(fileContent, {
68
- compilerOptions,
69
- fileName: options.filePath,
70
- reportDiagnostics: true,
71
- });
72
-
73
- if (result.diagnostics && result.diagnostics.length) {
74
- const formatHost = this.getFormatDiagnosticsHost();
75
- const error = this.tsModule.formatDiagnosticsWithColorAndContext(result.diagnostics, formatHost);
76
-
77
- // :TODO @david please replace to a more concrete error type and put in 'exceptions' directory here.
78
- throw new Error(error);
79
- }
80
-
81
- const outputPath = this.replaceFileExtToJs(options.filePath);
82
- const outputFiles = [{ outputText: result.outputText, outputPath }];
83
- if (result.sourceMapText) {
84
- outputFiles.push({
85
- outputText: result.sourceMapText,
86
- outputPath: `${outputPath}.map`,
87
- });
88
- }
89
- return outputFiles;
90
- }
91
-
92
- async preBuild(context: BuildContext) {
93
- const capsules = context.capsuleNetwork.seedersCapsules;
94
- const capsuleDirs = capsules.map((capsule) => capsule.path);
95
- await this.writeTsConfig(capsuleDirs);
96
- await this.writeTypes(capsuleDirs);
97
- await this.writeNpmIgnore(capsuleDirs);
98
- }
99
-
100
- /**
101
- * compile multiple components on the capsules
102
- */
103
- async build(context: BuildContext): Promise<BuiltTaskResult> {
104
- const componentsResults = await this.runTscBuild(context.capsuleNetwork);
105
-
106
- return {
107
- artifacts: this.getArtifactDefinition(),
108
- componentsResults,
109
- };
110
- }
111
-
112
- getArtifactDefinition() {
113
- return [
114
- {
115
- generatedBy: this.id,
116
- name: this.artifactName,
117
- globPatterns: this.distGlobPatterns,
118
- },
119
- ];
120
- }
121
-
122
- /**
123
- * given a source file, return its parallel in the dists. e.g. index.ts => dist/index.js
124
- */
125
- getDistPathBySrcPath(srcPath: string) {
126
- const fileWithJSExtIfNeeded = this.replaceFileExtToJs(srcPath);
127
- return path.join(this.distDir, fileWithJSExtIfNeeded);
128
- }
129
-
130
- /**
131
- * whether typescript is able to compile the given path
132
- */
133
- isFileSupported(filePath: string): boolean {
134
- const isJsAndCompile = !!this.options.compileJs && filePath.endsWith('.js');
135
- const isJsxAndCompile = !!this.options.compileJsx && filePath.endsWith('.jsx');
136
- return (
137
- (filePath.endsWith('.ts') || filePath.endsWith('.tsx') || isJsAndCompile || isJsxAndCompile) &&
138
- !filePath.endsWith('.d.ts')
139
- );
140
- }
141
-
142
- /**
143
- * we have two options here:
144
- * 1. pass all capsules-dir at the second parameter of createSolutionBuilder and then no
145
- * need to write the main tsconfig.json with all the references.
146
- * 2. write main tsconfig.json and pass the capsules root-dir.
147
- * we went with option #2 because it'll be easier for users to go to the capsule-root and run
148
- * `tsc --build` to debug issues.
149
- */
150
- private async runTscBuild(network: Network): Promise<ComponentResult[]> {
151
- const rootDir = network.capsulesRootDir;
152
- const capsules = await network.getCapsulesToCompile();
153
- if (!capsules.length) {
154
- return [];
155
- }
156
- const capsuleDirs = capsules.getAllCapsuleDirs();
157
- const formatHost = {
158
- getCanonicalFileName: (p) => p,
159
- getCurrentDirectory: () => '', // it helps to get the files with absolute paths
160
- getNewLine: () => this.tsModule.sys.newLine,
161
- };
162
- const componentsResults: ComponentResult[] = [];
163
- let currentComponentResult: Partial<ComponentResult> = { errors: [] };
164
- const reportDiagnostic = (diagnostic: ts.Diagnostic) => {
165
- const errorStr = process.stdout.isTTY
166
- ? this.tsModule.formatDiagnosticsWithColorAndContext([diagnostic], formatHost)
167
- : this.tsModule.formatDiagnostic(diagnostic, formatHost);
168
- if (!diagnostic.file) {
169
- // the error is general and not related to a specific file. e.g. tsconfig is missing.
170
- throw new BitError(errorStr);
171
- }
172
- this.logger.consoleFailure(errorStr);
173
- if (!currentComponentResult.component || !currentComponentResult.errors) {
174
- throw new Error(`currentComponentResult is not defined yet for ${diagnostic.file}`);
175
- }
176
- currentComponentResult.errors.push(errorStr);
177
- };
178
- // this only works when `verbose` is `true` in the `ts.createSolutionBuilder` function.
179
- const reportSolutionBuilderStatus = (diag: ts.Diagnostic) => {
180
- const msg = diag.messageText as string;
181
- this.logger.debug(msg);
182
- };
183
- const errorCounter = (errorCount: number) => {
184
- this.logger.info(`total error found: ${errorCount}`);
185
- };
186
- const host = this.tsModule.createSolutionBuilderHost(
187
- undefined,
188
- undefined,
189
- reportDiagnostic,
190
- reportSolutionBuilderStatus,
191
- errorCounter
192
- );
193
- await this.writeProjectReferencesTsConfig(rootDir, capsuleDirs);
194
- const solutionBuilder = this.tsModule.createSolutionBuilder(host, [rootDir], { verbose: true });
195
- let nextProject;
196
- const longProcessLogger = this.logger.createLongProcessLogger('compile typescript components', capsules.length);
197
- // eslint-disable-next-line no-cond-assign
198
- while ((nextProject = solutionBuilder.getNextInvalidatedProject())) {
199
- // regex to make sure it will work correctly for both linux and windows
200
- // it replaces both /tsconfig.json and \tsocnfig.json
201
- const capsulePath = nextProject.project.replace(/[/\\]tsconfig.json/, '');
202
- const currentComponentId = capsules.getIdByPathInCapsule(capsulePath);
203
- if (!currentComponentId) throw new Error(`unable to find component for ${capsulePath}`);
204
- longProcessLogger.logProgress(currentComponentId.toString());
205
- const capsule = capsules.getCapsule(currentComponentId);
206
- if (!capsule) throw new Error(`unable to find capsule for ${currentComponentId.toString()}`);
207
- currentComponentResult.component = capsule.component;
208
- currentComponentResult.startTime = Date.now();
209
- nextProject.done();
210
- currentComponentResult.endTime = Date.now();
211
- componentsResults.push({ ...currentComponentResult } as ComponentResult);
212
- currentComponentResult = { errors: [] };
213
- }
214
- longProcessLogger.end();
215
-
216
- return componentsResults;
217
- }
218
-
219
- private getFormatDiagnosticsHost(): ts.FormatDiagnosticsHost {
220
- return {
221
- getCanonicalFileName: (p) => p,
222
- getCurrentDirectory: this.tsModule.sys.getCurrentDirectory,
223
- getNewLine: () => this.tsModule.sys.newLine,
224
- };
225
- }
226
-
227
- private async writeTypes(dirs: string[]) {
228
- await Promise.all(
229
- this.options.types.map(async (typePath) => {
230
- const contents = await fs.readFile(typePath, 'utf8');
231
- const filename = path.basename(typePath);
232
-
233
- await Promise.all(
234
- dirs.map(async (dir) => {
235
- const filePath = path.join(dir, 'types', filename);
236
- if (!(await fs.pathExists(filePath))) {
237
- await fs.outputFile(filePath, contents);
238
- }
239
- })
240
- );
241
- })
242
- );
243
- }
244
-
245
- /**
246
- * when using project-references, typescript adds a file "tsconfig.tsbuildinfo" which is not
247
- * needed for the package.
248
- */
249
- private async writeNpmIgnore(dirs: string[]) {
250
- const NPM_IGNORE_FILE = '.npmignore';
251
- await Promise.all(
252
- dirs.map(async (dir) => {
253
- const npmIgnorePath = path.join(dir, NPM_IGNORE_FILE);
254
- const npmIgnoreEntriesStr = `\n${this.distDir}/tsconfig.tsbuildinfo\n`;
255
- await fs.appendFile(npmIgnorePath, npmIgnoreEntriesStr);
256
- })
257
- );
258
- }
259
-
260
- private async writeProjectReferencesTsConfig(rootDir: string, projects: string[]) {
261
- const files = [];
262
- const references = projects.map((project) => ({ path: project }));
263
- const tsconfig = { files, references };
264
- const tsconfigStr = this.stringifyTsconfig(tsconfig);
265
- await fs.writeFile(path.join(rootDir, 'tsconfig.json'), tsconfigStr);
266
- }
267
-
268
- private async writeTsConfig(dirs: string[]) {
269
- const tsconfigStr = this.stringifyTsconfig(this.options.tsconfig);
270
- await Promise.all(dirs.map((dir) => fs.writeFile(path.join(dir, 'tsconfig.json'), tsconfigStr)));
271
- }
272
-
273
- private stringifyTsconfig(tsconfig) {
274
- return JSON.stringify(tsconfig, undefined, 2);
275
- }
276
-
277
- private replaceFileExtToJs(filePath: string): string {
278
- if (!this.isFileSupported(filePath)) return filePath;
279
- const fileExtension = path.extname(filePath);
280
- return filePath.replace(new RegExp(`${fileExtension}$`), '.js'); // makes sure it's the last occurrence
281
- }
282
-
283
- version() {
284
- return this.tsModule.version;
285
- }
286
- }
@@ -1,247 +0,0 @@
1
- import ts, { Node, SourceFile, SyntaxKind } from 'typescript';
2
- import { getTsconfig } from 'get-tsconfig';
3
- import { SchemaExtractor, SchemaExtractorOptions } from '@teambit/schema';
4
- import { TsserverClient } from '@teambit/ts-server';
5
- import type { Workspace } from '@teambit/workspace';
6
- import { ComponentDependency, DependencyResolverMain } from '@teambit/dependency-resolver';
7
- import { SchemaNode, APISchema, ModuleSchema, UnImplementedSchema } from '@teambit/semantics.entities.semantic-schema';
8
- import { Component } from '@teambit/component';
9
- import { AbstractVinyl } from '@teambit/legacy/dist/consumer/component/sources';
10
- import { EnvContext } from '@teambit/envs';
11
- import { Formatter } from '@teambit/formatter';
12
- import { Logger } from '@teambit/logger';
13
- import AspectLoaderAspect, { AspectLoaderMain, getCoreAspectPackageName } from '@teambit/aspect-loader';
14
- import { ScopeMain } from '@teambit/scope';
15
- import pMapSeries from 'p-map-series';
16
- import { compact, flatten } from 'lodash';
17
- import { TypescriptMain, SchemaTransformerSlot, APITransformerSlot } from './typescript.main.runtime';
18
- import { TransformerNotFound } from './exceptions';
19
- import { SchemaExtractorContext } from './schema-extractor-context';
20
- import { Identifier } from './identifier';
21
- import { IdentifierList } from './identifier-list';
22
- import { ExtractorOptions } from './extractor-options';
23
- import { TypescriptAspect } from './typescript.aspect';
24
-
25
- export class TypeScriptExtractor implements SchemaExtractor {
26
- constructor(
27
- private tsconfig: any,
28
- private schemaTransformerSlot: SchemaTransformerSlot,
29
- private apiTransformerSlot: APITransformerSlot,
30
- private tsMain: TypescriptMain,
31
- private rootTsserverPath: string,
32
- private rootContextPath: string,
33
- private depResolver: DependencyResolverMain,
34
- private workspace: Workspace | undefined,
35
- private scope: ScopeMain,
36
- private aspectLoader: AspectLoaderMain,
37
- private logger: Logger
38
- ) {}
39
-
40
- parseSourceFile(file: AbstractVinyl): SourceFile {
41
- const sourceFile = ts.createSourceFile(
42
- file.path,
43
- file.contents.toString('utf8'),
44
- ts.ScriptTarget.Latest,
45
- true
46
- /** don't pass the scriptKind, it'll be determined automatically by typescript by the filepath */
47
- );
48
- // leave this commented out, it's helpful when there are issues with ASTs. consider throwing in this case.
49
- // console.log("sourceFile Errors", file.path, sourceFile.parseDiagnostics);
50
- return sourceFile;
51
- }
52
-
53
- /**
54
- * extract a component schema.
55
- */
56
- async extract(component: Component, options: SchemaExtractorOptions = {}): Promise<APISchema> {
57
- // override the rootTsserverPath and rootContextPath if passed via options
58
- if (options.tsserverPath) {
59
- this.rootTsserverPath = options.tsserverPath;
60
- }
61
- if (options.contextPath) {
62
- this.rootContextPath = options.contextPath;
63
- }
64
- const tsserver = await this.getTsServer();
65
- const mainFile = component.mainFile;
66
- const compatibleExts = ['.tsx', '.ts'];
67
- const internalFiles = component.filesystem.files.filter(
68
- (file) => compatibleExts.includes(file.extname) && file.path !== mainFile.path
69
- );
70
- const allFiles = [mainFile, ...internalFiles];
71
-
72
- const context = await this.createContext(tsserver, component, options.formatter);
73
-
74
- await pMapSeries(allFiles, async (file) => {
75
- const ast = this.parseSourceFile(file);
76
- const identifiers = await this.computeIdentifiers(ast, context); // compute for every file
77
- const cacheKey = context.getIdentifierKeyForNode(ast);
78
- context.setIdentifiers(cacheKey, new IdentifierList(identifiers));
79
- });
80
-
81
- const mainAst = this.parseSourceFile(mainFile);
82
- const moduleSchema = (await this.computeSchema(mainAst, context)) as ModuleSchema;
83
- moduleSchema.flatExportsRecursively();
84
- const apiScheme = moduleSchema;
85
- const internals = await this.computeInternalModules(context, internalFiles);
86
-
87
- const location = context.getLocation(mainAst);
88
-
89
- return new APISchema(location, apiScheme, internals, component.id as any);
90
- }
91
-
92
- async computeInternalModules(context: SchemaExtractorContext, internalFiles: AbstractVinyl[]) {
93
- const internals = compact(
94
- await Promise.all(
95
- [...context.internalIdentifiers.entries()].map(async ([filePath]) => {
96
- const file = internalFiles.find((internalFile) => internalFile.path === filePath);
97
- if (!file) return undefined;
98
- const fileAst = this.parseSourceFile(file);
99
- const moduleSchema = (await this.computeSchema(fileAst, context)) as ModuleSchema;
100
- moduleSchema.flatExportsRecursively();
101
- return moduleSchema;
102
- })
103
- )
104
- );
105
- return internals;
106
- }
107
-
108
- dispose() {
109
- if (!this.tsserver) return;
110
- this.tsserver.killTsServer();
111
- }
112
-
113
- async computeIdentifiers(node: Node, context: SchemaExtractorContext) {
114
- const transformer = this.getTransformer(node, context);
115
- let identifiers: Identifier[] = [];
116
- if (!transformer || !transformer.getIdentifiers) {
117
- this.logger.debug(new TransformerNotFound(node, context.component, context.getLocation(node)).toString());
118
- } else {
119
- identifiers = await transformer.getIdentifiers(node, context);
120
- }
121
- return identifiers;
122
- }
123
-
124
- private async createContext(
125
- tsserver: TsserverClient,
126
- component: Component,
127
- formatter?: Formatter
128
- ): Promise<SchemaExtractorContext> {
129
- const componentDeps = await this.getComponentDeps(component);
130
- return new SchemaExtractorContext(
131
- tsserver,
132
- component,
133
- this,
134
- componentDeps,
135
- this.rootContextPath,
136
- this.workspace?.path || this.scope.path,
137
- formatter
138
- );
139
- }
140
-
141
- private async getComponentDeps(component: Component): Promise<ComponentDependency[]> {
142
- const deps = await this.depResolver.getDependencies(component);
143
- const componentDeps = deps.getComponentDependencies();
144
- return componentDeps;
145
- }
146
-
147
- private tsserver: TsserverClient | undefined = undefined;
148
-
149
- private async getTsServer() {
150
- if (!this.tsserver) {
151
- const tsserver = this.tsMain.getTsserverClient();
152
- if (tsserver) {
153
- this.tsserver = tsserver;
154
- return tsserver;
155
- }
156
-
157
- this.tsserver = await this.tsMain.initTsserverClient(this.rootTsserverPath);
158
- return this.tsserver;
159
- }
160
-
161
- return this.tsserver;
162
- }
163
-
164
- async computeSchema(node: Node, context: SchemaExtractorContext): Promise<SchemaNode> {
165
- const transformer = this.getTransformer(node, context);
166
-
167
- if (!transformer) {
168
- return new UnImplementedSchema(context.getLocation(node), node.getText(), SyntaxKind[node.kind]);
169
- }
170
-
171
- const schemaNode = await transformer.transform(node, context);
172
-
173
- return this.transformAPI(schemaNode, context);
174
- }
175
-
176
- async transformAPI(schema: SchemaNode, context: SchemaExtractorContext): Promise<SchemaNode> {
177
- const apiTransformer = this.getAPITransformer(schema);
178
- return apiTransformer ? apiTransformer.transform(schema, context) : schema;
179
- }
180
-
181
- async getComponentIDByPath(file: string) {
182
- const coreAspectIds = this.aspectLoader.getCoreAspectIds();
183
- const matchingCoreAspect = coreAspectIds.find((c) => file === getCoreAspectPackageName(c));
184
-
185
- if (matchingCoreAspect) {
186
- return (this.workspace || this.scope).resolveComponentId(matchingCoreAspect);
187
- }
188
-
189
- if (this.workspace) {
190
- return this.workspace.getComponentIdByPath(file);
191
- }
192
- return null;
193
- }
194
-
195
- /**
196
- * select the correct transformer for a node.
197
- */
198
- getTransformer(node: Node, context: SchemaExtractorContext) {
199
- const transformers = flatten(this.schemaTransformerSlot.values());
200
- const transformer = transformers.find((singleTransformer) => {
201
- return singleTransformer.predicate(node);
202
- });
203
- if (!transformer) {
204
- this.logger.debug(new TransformerNotFound(node, context.component, context.getLocation(node)).toString());
205
- return undefined;
206
- }
207
-
208
- return transformer;
209
- }
210
-
211
- getAPITransformer(node: SchemaNode) {
212
- const transformers = flatten(this.apiTransformerSlot.values());
213
- const transformer = transformers.find((singleTransformer) => {
214
- return singleTransformer.predicate(node);
215
- });
216
- if (!transformer) {
217
- return undefined;
218
- }
219
-
220
- return transformer;
221
- }
222
-
223
- static from(options: ExtractorOptions) {
224
- return (context: EnvContext) => {
225
- const tsconfig = getTsconfig(options.tsconfig)?.config || { compilerOptions: options.compilerOptions };
226
- const tsMain = context.getAspect<TypescriptMain>(TypescriptAspect.id);
227
- const aspectLoaderMain = context.getAspect<AspectLoaderMain>(AspectLoaderAspect.id);
228
-
229
- // When loading the env from a scope you don't have a workspace
230
- const rootPath = tsMain.workspace?.path || tsMain.scope.path || '';
231
-
232
- return new TypeScriptExtractor(
233
- tsconfig,
234
- tsMain.schemaTransformerSlot,
235
- tsMain.apiTransformerSlot,
236
- tsMain,
237
- rootPath,
238
- rootPath,
239
- tsMain.depResolver,
240
- tsMain.workspace,
241
- tsMain.scope,
242
- aspectLoaderMain,
243
- context.createLogger(options.name)
244
- );
245
- };
246
- }
247
- }