Skip to content

Commit f866eab

Browse files
filipesilvamgechev
authored andcommitted
fix(@ngtools/webpack): only load test helpers on tests
Fix #16858
1 parent 89b1fc0 commit f866eab

14 files changed

+151
-142
lines changed

packages/ngtools/webpack/src/transformers/ast_helpers.ts

Lines changed: 0 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import { virtualFs } from '@angular-devkit/core';
9-
import { readFileSync, readdirSync } from 'fs';
10-
import { dirname, join } from 'path';
118
import * as ts from 'typescript';
12-
import { WebpackCompilerHost } from '../compiler_host';
139

1410

1511
// Find all nodes from the AST in the subtree of node of SyntaxKind kind.
@@ -45,129 +41,3 @@ export function getLastNode(sourceFile: ts.SourceFile): ts.Node | null {
4541

4642
return null;
4743
}
48-
49-
50-
// Test transform helpers.
51-
const basePath = '/project/src/';
52-
const fileName = basePath + 'test-file.ts';
53-
const typeScriptLibFiles = loadTypeScriptLibFiles();
54-
const tsLibFiles = loadTsLibFiles();
55-
56-
export function createTypescriptContext(
57-
content: string,
58-
additionalFiles?: Record<string, string>,
59-
useLibs = false,
60-
extraCompilerOptions: ts.CompilerOptions = {},
61-
) {
62-
// Set compiler options.
63-
const compilerOptions: ts.CompilerOptions = {
64-
noEmitOnError: useLibs,
65-
allowJs: true,
66-
newLine: ts.NewLineKind.LineFeed,
67-
moduleResolution: ts.ModuleResolutionKind.NodeJs,
68-
module: ts.ModuleKind.ESNext,
69-
target: ts.ScriptTarget.ESNext,
70-
skipLibCheck: true,
71-
sourceMap: false,
72-
importHelpers: true,
73-
experimentalDecorators: true,
74-
...extraCompilerOptions,
75-
};
76-
77-
// Create compiler host.
78-
const compilerHost = new WebpackCompilerHost(
79-
compilerOptions,
80-
basePath,
81-
new virtualFs.SimpleMemoryHost(),
82-
false,
83-
);
84-
85-
// Add a dummy file to host content.
86-
compilerHost.writeFile(fileName, content, false);
87-
88-
if (useLibs) {
89-
// Write the default libs.
90-
// These are needed for tests that use import(), because it relies on a Promise being there.
91-
const compilerLibFolder = dirname(compilerHost.getDefaultLibFileName(compilerOptions));
92-
for (const [k, v] of Object.entries(typeScriptLibFiles)) {
93-
compilerHost.writeFile(join(compilerLibFolder, k), v, false);
94-
}
95-
}
96-
97-
if (compilerOptions.importHelpers) {
98-
for (const [k, v] of Object.entries(tsLibFiles)) {
99-
compilerHost.writeFile(k, v, false);
100-
}
101-
}
102-
103-
if (additionalFiles) {
104-
for (const key in additionalFiles) {
105-
compilerHost.writeFile(basePath + key, additionalFiles[key], false);
106-
}
107-
}
108-
109-
// Create the TypeScript program.
110-
const program = ts.createProgram([fileName], compilerOptions, compilerHost);
111-
112-
return { compilerHost, program };
113-
}
114-
115-
export function transformTypescript(
116-
content: string | undefined,
117-
transformers: ts.TransformerFactory<ts.SourceFile>[],
118-
program?: ts.Program,
119-
compilerHost?: WebpackCompilerHost,
120-
): string | undefined {
121-
// Use given context or create a new one.
122-
if (content !== undefined) {
123-
const typescriptContext = createTypescriptContext(content);
124-
if (!program) {
125-
program = typescriptContext.program;
126-
}
127-
if (!compilerHost) {
128-
compilerHost = typescriptContext.compilerHost;
129-
}
130-
} else if (!program || !compilerHost) {
131-
throw new Error('transformTypescript needs either `content` or a `program` and `compilerHost');
132-
}
133-
134-
// Emit.
135-
const { emitSkipped, diagnostics } = program.emit(
136-
undefined, undefined, undefined, undefined, { before: transformers },
137-
);
138-
139-
// Throw error with diagnostics if emit wasn't successfull.
140-
if (emitSkipped) {
141-
throw new Error(ts.formatDiagnostics(diagnostics, compilerHost));
142-
}
143-
144-
// Return the transpiled js.
145-
return compilerHost.readFile(fileName.replace(/\.tsx?$/, '.js'));
146-
}
147-
148-
function loadTypeScriptLibFiles(): Record<string, string> {
149-
const libFolderPath = dirname(require.resolve('typescript/lib/lib.d.ts'));
150-
const libFolderFiles = readdirSync(libFolderPath);
151-
const libFileNames = libFolderFiles.filter(f => f.startsWith('lib.') && f.endsWith('.d.ts'));
152-
153-
// Return a map of the lib names to their content.
154-
const libs: Record<string, string> = {};
155-
for (const f of libFileNames) {
156-
libs[f] = readFileSync(join(libFolderPath, f), 'utf-8');
157-
}
158-
159-
return libs;
160-
}
161-
162-
function loadTsLibFiles(): Record<string, string> {
163-
const libFolderPath = dirname(require.resolve('tslib/package.json'));
164-
const libFolderFiles = readdirSync(libFolderPath);
165-
166-
// Return a map of the lib names to their content.
167-
const libs: Record<string, string> = {};
168-
for (const f of libFolderFiles) {
169-
libs[join('node_modules/tslib', f)] = readFileSync(join(libFolderPath, f), 'utf-8');
170-
}
171-
172-
return libs;
173-
}

packages/ngtools/webpack/src/transformers/ctor-parameters_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies
9-
import { createTypescriptContext, transformTypescript } from './ast_helpers';
109
import { downlevelConstructorParameters } from './ctor-parameters';
10+
import { createTypescriptContext, transformTypescript } from './spec_helpers';
1111

1212
function transform(input: string, additionalFiles?: Record<string, string>) {
1313
const { program, compilerHost } = createTypescriptContext(input, additionalFiles);

packages/ngtools/webpack/src/transformers/elide_imports_spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@
88
// tslint:disable:no-big-function
99
import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies
1010
import * as ts from 'typescript';
11-
import { createTypescriptContext, getLastNode, transformTypescript } from './ast_helpers';
11+
import { getLastNode } from './ast_helpers';
1212
import { RemoveNodeOperation } from './interfaces';
1313
import { makeTransform } from './make_transform';
14+
import { createTypescriptContext, transformTypescript } from './spec_helpers';
1415

1516
describe('@ngtools/webpack transformers', () => {
1617
describe('elide_imports', () => {

packages/ngtools/webpack/src/transformers/export_lazy_module_map_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies
9-
import { transformTypescript } from './ast_helpers';
109
import { exportLazyModuleMap } from './export_lazy_module_map';
10+
import { transformTypescript } from './spec_helpers';
1111

1212
describe('@ngtools/webpack transformers', () => {
1313
describe('export_lazy_module_map', () => {

packages/ngtools/webpack/src/transformers/export_ngfactory_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies
9-
import { transformTypescript } from './ast_helpers';
109
import { exportNgFactory } from './export_ngfactory';
10+
import { transformTypescript } from './spec_helpers';
1111

1212
describe('@ngtools/webpack transformers', () => {
1313
describe('export_ngfactory', () => {

packages/ngtools/webpack/src/transformers/import_factory_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { tags } from '@angular-devkit/core';
9-
import { createTypescriptContext, transformTypescript } from './ast_helpers';
109
import { importFactory } from './import_factory';
10+
import { createTypescriptContext, transformTypescript } from './spec_helpers';
1111

1212
describe('@ngtools/webpack transformers', () => {
1313
describe('import_factory', () => {

packages/ngtools/webpack/src/transformers/multiple_transformers_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies
9-
import { createTypescriptContext, transformTypescript } from './ast_helpers';
109
import { exportLazyModuleMap } from './export_lazy_module_map';
1110
import { exportNgFactory } from './export_ngfactory';
1211
import { removeDecorators } from './remove_decorators';
1312
import { replaceBootstrap } from './replace_bootstrap';
13+
import { createTypescriptContext, transformTypescript } from './spec_helpers';
1414

1515

1616
describe('@ngtools/webpack transformers', () => {

packages/ngtools/webpack/src/transformers/register_locale_data_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies
9-
import { transformTypescript } from './ast_helpers';
109
import { registerLocaleData } from './register_locale_data';
10+
import { transformTypescript } from './spec_helpers';
1111

1212
describe('@ngtools/webpack transformers', () => {
1313
describe('register_locale_data', () => {

packages/ngtools/webpack/src/transformers/remove-ivy-jit-support-calls_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*/
88
import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies
99
import * as ts from 'typescript';
10-
import { createTypescriptContext, transformTypescript } from './ast_helpers';
1110
import { removeIvyJitSupportCalls } from './remove-ivy-jit-support-calls';
11+
import { createTypescriptContext, transformTypescript } from './spec_helpers';
1212

1313
function transform(
1414
input: string,

packages/ngtools/webpack/src/transformers/remove_decorators_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*/
88
// tslint:disable:no-big-function
99
import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies
10-
import { createTypescriptContext, transformTypescript } from './ast_helpers';
1110
import { removeDecorators } from './remove_decorators';
11+
import { createTypescriptContext, transformTypescript } from './spec_helpers';
1212

1313
describe('@ngtools/webpack transformers', () => {
1414
describe('remove_decorators', () => {

packages/ngtools/webpack/src/transformers/replace_bootstrap_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies
9-
import { createTypescriptContext, transformTypescript } from './ast_helpers';
109
import { replaceBootstrap } from './replace_bootstrap';
10+
import { createTypescriptContext, transformTypescript } from './spec_helpers';
1111

1212
describe('@ngtools/webpack transformers', () => {
1313
describe('replace_bootstrap', () => {

packages/ngtools/webpack/src/transformers/replace_resources_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies
9-
import { createTypescriptContext, transformTypescript } from './ast_helpers';
109
import { replaceResources } from './replace_resources';
10+
import { createTypescriptContext, transformTypescript } from './spec_helpers';
1111

1212
function transform(
1313
input: string,

packages/ngtools/webpack/src/transformers/replace_server_bootstrap_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
*/
88
// tslint:disable:no-big-function
99
import { tags } from '@angular-devkit/core'; // tslint:disable-line:no-implicit-dependencies
10-
import { createTypescriptContext, transformTypescript } from './ast_helpers';
1110
import { replaceServerBootstrap } from './replace_server_bootstrap';
11+
import { createTypescriptContext, transformTypescript } from './spec_helpers';
1212

1313
describe('@ngtools/webpack transformers', () => {
1414
describe('replace_server_bootstrap', () => {

0 commit comments

Comments
 (0)