Skip to content

Commit 7c3bab9

Browse files
committed
refactor(@angular/build): use new encapsulate style helper for HMR
The `@angular/compiler` package now has an updated `encapsulateStyle` helper that allows directly providing the component identifier as an argument. This avoids needing to perform an additional regular expression replace with the internal marker text. Also, the component identifier validity check has been updated to support unicode characters in the event they happen to be used for the application identifier.
1 parent dd49949 commit 7c3bab9

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

packages/angular/build/src/tools/vite/middlewares/assets-middleware.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import type { Connect, ViteDevServer } from 'vite';
1212
import { loadEsmModule } from '../../../utils/load-esm';
1313
import { AngularMemoryOutputFiles, pathnameWithoutBasePath } from '../utils';
1414

15-
const COMPONENT_REGEX = /%COMP%/g;
16-
1715
export function createAngularAssetsMiddleware(
1816
server: ViteDevServer,
1917
assets: Map<string, string>,
@@ -90,12 +88,13 @@ export function createAngularAssetsMiddleware(
9088
// Shim the stylesheet if a component ID is provided
9189
if (componentId.length > 0) {
9290
// Validate component ID
93-
if (/[_.-A-Za-z0-9]+-c\d{9}$/.test(componentId)) {
91+
if (/^[_.\-\p{Letter}\d]+-c\d{9}$/u.test(componentId)) {
9492
loadEsmModule<typeof import('@angular/compiler')>('@angular/compiler')
9593
.then((compilerModule) => {
96-
const encapsulatedData = compilerModule
97-
.encapsulateStyle(new TextDecoder().decode(data))
98-
.replaceAll(COMPONENT_REGEX, componentId);
94+
const encapsulatedData = compilerModule.encapsulateStyle(
95+
new TextDecoder().decode(data),
96+
componentId,
97+
);
9998

10099
res.setHeader('Content-Type', 'text/css');
101100
res.setHeader('Cache-Control', 'no-cache');

0 commit comments

Comments
 (0)