Skip to content

Commit 499173b

Browse files
alan-agius4dgp1130
authored andcommittedFeb 7, 2023
fix(@schematics/angular): remove bootstrapping wrapping in universal schematic
The bootstrapping wrapping is no longer necessary as of 15.1.3. See: angular/angular#48868 (cherry picked from commit 88fddc0)
1 parent 14e317d commit 499173b

File tree

2 files changed

+1
-110
lines changed

2 files changed

+1
-110
lines changed
 

‎packages/schematics/angular/universal/index.ts

+1-80
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
getPackageJsonDependency,
3030
} from '../utility/dependencies';
3131
import { latestVersions } from '../utility/latest-versions';
32-
import { findBootstrapModuleCall, findBootstrapModulePath } from '../utility/ng-ast-utils';
32+
import { findBootstrapModulePath } from '../utility/ng-ast-utils';
3333
import { relativePathToWorkspaceRoot } from '../utility/paths';
3434
import { targetBuildNotFoundError } from '../utility/project-targets';
3535
import { getWorkspace, updateWorkspace } from '../utility/workspace';
@@ -109,84 +109,6 @@ function findBrowserModuleImport(host: Tree, modulePath: string): ts.Node {
109109
return browserModuleNode;
110110
}
111111

112-
function wrapBootstrapCall(mainFile: string): Rule {
113-
return (host: Tree) => {
114-
const mainPath = normalize('/' + mainFile);
115-
let bootstrapCall: ts.Node | null = findBootstrapModuleCall(host, mainPath);
116-
if (bootstrapCall === null) {
117-
throw new SchematicsException('Bootstrap module not found.');
118-
}
119-
120-
let bootstrapCallExpression: ts.Node | null = null;
121-
let currentCall = bootstrapCall;
122-
while (bootstrapCallExpression === null && currentCall.parent) {
123-
currentCall = currentCall.parent;
124-
if (ts.isExpressionStatement(currentCall) || ts.isVariableStatement(currentCall)) {
125-
bootstrapCallExpression = currentCall;
126-
}
127-
}
128-
bootstrapCall = currentCall;
129-
130-
// In case the bootstrap code is a variable statement
131-
// we need to determine it's usage
132-
if (bootstrapCallExpression && ts.isVariableStatement(bootstrapCallExpression)) {
133-
const declaration = bootstrapCallExpression.declarationList.declarations[0];
134-
const bootstrapVar = (declaration.name as ts.Identifier).text;
135-
const sf = bootstrapCallExpression.getSourceFile();
136-
bootstrapCall = findCallExpressionNode(sf, bootstrapVar) || currentCall;
137-
}
138-
139-
// indent contents
140-
const triviaWidth = bootstrapCall.getLeadingTriviaWidth();
141-
const beforeText =
142-
`function bootstrap() {\n` + ' '.repeat(triviaWidth > 2 ? triviaWidth + 1 : triviaWidth);
143-
const afterText =
144-
`\n${triviaWidth > 2 ? ' '.repeat(triviaWidth - 1) : ''}};\n` +
145-
`
146-
147-
if (document.readyState === 'complete') {
148-
bootstrap();
149-
} else {
150-
document.addEventListener('DOMContentLoaded', bootstrap);
151-
}
152-
`;
153-
154-
// in some cases we need to cater for a trailing semicolon such as;
155-
// bootstrap().catch(err => console.log(err));
156-
const lastToken = bootstrapCall.parent.getLastToken();
157-
let endPos = bootstrapCall.getEnd();
158-
if (lastToken && lastToken.kind === ts.SyntaxKind.SemicolonToken) {
159-
endPos = lastToken.getEnd();
160-
}
161-
162-
const recorder = host.beginUpdate(mainPath);
163-
recorder.insertLeft(bootstrapCall.getStart(), beforeText);
164-
recorder.insertRight(endPos, afterText);
165-
host.commitUpdate(recorder);
166-
};
167-
}
168-
169-
function findCallExpressionNode(node: ts.Node, text: string): ts.Node | null {
170-
if (
171-
ts.isCallExpression(node) &&
172-
ts.isIdentifier(node.expression) &&
173-
node.expression.text === text
174-
) {
175-
return node;
176-
}
177-
178-
let foundNode: ts.Node | null = null;
179-
ts.forEachChild(node, (childNode) => {
180-
foundNode = findCallExpressionNode(childNode, text);
181-
182-
if (foundNode) {
183-
return true;
184-
}
185-
});
186-
187-
return foundNode;
188-
}
189-
190112
function addServerTransition(
191113
options: UniversalOptions,
192114
mainFile: string,
@@ -292,7 +214,6 @@ export default function (options: UniversalOptions): Rule {
292214
mergeWith(rootSource),
293215
addDependencies(),
294216
updateConfigFile(options, tsConfigDirectory),
295-
wrapBootstrapCall(clientBuildOptions.main),
296217
addServerTransition(options, clientBuildOptions.main, clientProject.root),
297218
]);
298219
};

‎packages/schematics/angular/universal/index_spec.ts

-30
Original file line numberDiff line numberDiff line change
@@ -191,36 +191,6 @@ describe('Universal Schematic', () => {
191191
expect(contents).not.toContain(`withServerTransition({ appId: 'foo' })`);
192192
});
193193

194-
it('should wrap the bootstrap call in a DOMContentLoaded event handler', async () => {
195-
const tree = await schematicRunner.runSchematic('universal', defaultOptions, appTree);
196-
const filePath = '/projects/bar/src/main.ts';
197-
const contents = tree.readContent(filePath);
198-
expect(contents).toContain(`document.addEventListener('DOMContentLoaded', bootstrap);`);
199-
});
200-
201-
it('should wrap the bootstrap declaration in a DOMContentLoaded event handler', async () => {
202-
const filePath = '/projects/bar/src/main.ts';
203-
appTree.overwrite(
204-
filePath,
205-
`
206-
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
207-
import { AppModule } from './app/app.module';
208-
import { environment } from './environments/environment';
209-
import { hmrBootstrap } from './hmr';
210-
211-
const bootstrap = () => platformBrowserDynamic().bootstrapModule(AppModule);
212-
213-
if (!hmrBootstrap) {
214-
bootstrap().catch(err => console.log(err));
215-
}
216-
`,
217-
);
218-
219-
const tree = await schematicRunner.runSchematic('universal', defaultOptions, appTree);
220-
const contents = tree.readContent(filePath);
221-
expect(contents).toContain(`document.addEventListener('DOMContentLoaded', bootstrap);`);
222-
});
223-
224194
it('should install npm dependencies', async () => {
225195
await schematicRunner.runSchematic('universal', defaultOptions, appTree);
226196
expect(schematicRunner.tasks.length).toBe(1);

0 commit comments

Comments
 (0)
Please sign in to comment.