diff --git a/packages/schematics/angular/web-worker/index.ts b/packages/schematics/angular/web-worker/index.ts index 45e1f39ad34c..9718f0407c39 100644 --- a/packages/schematics/angular/web-worker/index.ts +++ b/packages/schematics/angular/web-worker/index.ts @@ -37,12 +37,12 @@ function addSnippet(options: WebWorkerOptions): Rule { return; } - const fileRegExp = new RegExp(`^${options.name}.*\\.ts`); + const fileRegExp = new RegExp(`^${options.name}(?:\\..+)?\\.ts$`); 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..0f59ed82f0b9 100644 --- a/packages/schematics/angular/web-worker/index_spec.ts +++ b/packages/schematics/angular/web-worker/index_spec.ts @@ -95,6 +95,46 @@ 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 () => { + appTree.create('/projects/bar/src/app/app.config.server.ts', 'export const config = {};\n'); + const tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree); + + 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 file whose name only starts with the worker name', async () => { + appTree.create('/projects/bar/src/app/app-header.ts', 'export class AppHeader {}\n'); + const tree = await schematicRunner.runSchematic('web-worker', defaultOptions, appTree); + + expect(tree.readContent('/projects/bar/src/app/app-header.ts')).not.toContain('new Worker'); + expect(tree.readContent('/projects/bar/src/app/app.ts')).toContain( + `new Worker(new URL('./${defaultOptions.name}.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' };