Skip to content

Commit 89c86cc

Browse files
committed
fix(@schematics/angular): avoid adding web worker snippet to server configuration files
In an application with SSR enabled, `ng g web-worker app` appends the worker creation snippet to `app.config.server.ts` instead of the component file. The sibling file exclusion pattern is end-anchored, so `app.config.server.ts` and `app.routes.server.ts` end in `server.ts` rather than `config.ts`/`routes.ts` and are not excluded, and the alphabetical sort places `app.config.server.ts` before `app.ts`. Exclude `.server.ts` variants and require a dot-separated type suffix so that component files such as `observer.ts` remain eligible. A new spec with a server application fails without this change.
1 parent be15160 commit 89c86cc

2 files changed

Lines changed: 22 additions & 2 deletions

File tree

‎packages/schematics/angular/web-worker/index.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ function addSnippet(options: WebWorkerOptions): Rule {
4141
const siblingModules = host
4242
.getDir(options.path)
4343
.subfiles // Find all files that start with the same name, are ts files,
44-
// and aren't spec or module files.
45-
.filter((f) => fileRegExp.test(f) && !/(module|spec|config|routes)\.ts$/.test(f))
44+
// and aren't spec, module, or configuration files (including their server variants).
45+
.filter((f) => fileRegExp.test(f) && !/\.(module|spec|config|routes|server)\.ts$/.test(f))
4646
// Sort alphabetically for consistency.
4747
.sort();
4848

‎packages/schematics/angular/web-worker/index_spec.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,26 @@ describe('Web Worker Schematic', () => {
9595
expect(appComponent).toContain('console.log(`page got message: ${data}`)');
9696
});
9797

98+
it('should add snippet to the component file and not a server configuration file', async () => {
99+
appTree = await schematicRunner.runSchematic('server', { project: 'bar' }, appTree);
100+
const tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree);
101+
102+
const appComponent = tree.readContent('/projects/bar/src/app/app.ts');
103+
expect(appComponent).toContain(`new Worker(new URL('./${defaultOptions.name}.worker`);
104+
105+
const serverConfig = tree.readContent('/projects/bar/src/app/app.config.server.ts');
106+
expect(serverConfig).not.toContain('new Worker');
107+
});
108+
109+
it(`should add snippet to a sibling file whose name ends with 'server'`, async () => {
110+
appTree.create('/projects/bar/src/app/observer.ts', 'export class Observer {}\n');
111+
const options = { ...defaultOptions, name: 'observer' };
112+
const tree = await schematicRunner.runSchematic('web-worker', options, appTree);
113+
114+
const content = tree.readContent('/projects/bar/src/app/observer.ts');
115+
expect(content).toContain(`new Worker(new URL('./observer.worker`);
116+
});
117+
98118
it(`should add 'tsconfig.worker.json' outside of 'src' directory in root app`, async () => {
99119
const rootAppOptions = { ...appOptions, projectRoot: '', name: 'foo' };
100120
const workerOptions = { ...defaultOptions, project: 'foo' };

0 commit comments

Comments
 (0)