Skip to content

Commit 982c3dc

Browse files
MeAkibalan-agius4
authored andcommitted
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. (cherry picked from commit 6588204)
1 parent e9ae950 commit 982c3dc

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ function addSnippet(options: WebWorkerOptions): Rule {
3737
return;
3838
}
3939

40-
const fileRegExp = new RegExp(`^${options.name}.*\\.ts`);
40+
const fileRegExp = new RegExp(`^${options.name}(?:\\..+)?\\.ts$`);
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, declaration, or configuration files (including their server variants).
45+
.filter((f) => fileRegExp.test(f) && !/\.(module|spec|config|routes|server|d)\.ts$/.test(f))
4646
// Sort alphabetically for consistency.
4747
.sort();
4848

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,46 @@ 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.create('/projects/bar/src/app/app.config.server.ts', 'export const config = {};\n');
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+
118+
it('should not add snippet to a file whose name only starts with the worker name', async () => {
119+
appTree.create('/projects/bar/src/app/app-header.ts', 'export class AppHeader {}\n');
120+
const tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree);
121+
122+
expect(tree.readContent('/projects/bar/src/app/app-header.ts')).not.toContain('new Worker');
123+
expect(tree.readContent('/projects/bar/src/app/app.ts')).toContain(
124+
`new Worker(new URL('./${defaultOptions.name}.worker`,
125+
);
126+
});
127+
128+
it('should not add snippet to a declaration file', async () => {
129+
appTree.create('/projects/bar/src/app/app.d.ts', 'declare const foo: string;\n');
130+
const tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree);
131+
132+
expect(tree.readContent('/projects/bar/src/app/app.d.ts')).not.toContain('new Worker');
133+
expect(tree.readContent('/projects/bar/src/app/app.ts')).toContain(
134+
`new Worker(new URL('./${defaultOptions.name}.worker`,
135+
);
136+
});
137+
98138
it(`should add 'tsconfig.worker.json' outside of 'src' directory in root app`, async () => {
99139
const rootAppOptions = { ...appOptions, projectRoot: '', name: 'foo' };
100140
const workerOptions = { ...defaultOptions, project: 'foo' };

0 commit comments

Comments
 (0)