diff --git a/packages/schematics/angular/web-worker/index.ts b/packages/schematics/angular/web-worker/index.ts index 45e1f39ad34c..e37754aa48e9 100644 --- a/packages/schematics/angular/web-worker/index.ts +++ b/packages/schematics/angular/web-worker/index.ts @@ -41,8 +41,8 @@ function addSnippet(options: WebWorkerOptions): Rule { const siblingModules = host .getDir(options.path) .subfiles // Find all files that start with the same name, are ts files, - // and aren't spec or module files. - .filter((f) => fileRegExp.test(f) && !/(module|spec|config|routes)\.ts$/.test(f)) + // and aren't spec, module, declaration, or configuration files (including their server variants). + .filter((f) => fileRegExp.test(f) && !/\.(module|spec|config|routes|server|d)\.ts$/.test(f)) // Sort alphabetically for consistency. .sort(); diff --git a/packages/schematics/angular/web-worker/index_spec.ts b/packages/schematics/angular/web-worker/index_spec.ts index a922a6d3cee6..4117f06e32e8 100644 --- a/packages/schematics/angular/web-worker/index_spec.ts +++ b/packages/schematics/angular/web-worker/index_spec.ts @@ -95,6 +95,36 @@ describe('Web Worker Schematic', () => { expect(appComponent).toContain('console.log(`page got message: ${data}`)'); }); + it('should add snippet to the component file and not a server configuration file', async () => { + const serverAppTree = await schematicRunner.runSchematic('server', { project: 'bar' }, appTree); + const tree = await schematicRunner.runSchematic('web-worker', defaultOptions, serverAppTree); + + const appComponent = tree.readContent('/projects/bar/src/app/app.ts'); + expect(appComponent).toContain(`new Worker(new URL('./${defaultOptions.name}.worker`); + + const serverConfig = tree.readContent('/projects/bar/src/app/app.config.server.ts'); + expect(serverConfig).not.toContain('new Worker'); + }); + + it(`should add snippet to a sibling file whose name ends with 'server'`, async () => { + appTree.create('/projects/bar/src/app/observer.ts', 'export class Observer {}\n'); + const options = { ...defaultOptions, name: 'observer' }; + const tree = await schematicRunner.runSchematic('web-worker', options, appTree); + + const content = tree.readContent('/projects/bar/src/app/observer.ts'); + expect(content).toContain(`new Worker(new URL('./observer.worker`); + }); + + it('should not add snippet to a declaration file', async () => { + appTree.create('/projects/bar/src/app/app.d.ts', 'declare const foo: string;\n'); + const tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree); + + expect(tree.readContent('/projects/bar/src/app/app.d.ts')).not.toContain('new Worker'); + expect(tree.readContent('/projects/bar/src/app/app.ts')).toContain( + `new Worker(new URL('./${defaultOptions.name}.worker`, + ); + }); + it(`should add 'tsconfig.worker.json' outside of 'src' directory in root app`, async () => { const rootAppOptions = { ...appOptions, projectRoot: '', name: 'foo' }; const workerOptions = { ...defaultOptions, project: 'foo' };