|
9 | 9 | import { lookup as lookupMimeType } from 'mrmime';
|
10 | 10 | import { extname } from 'node:path';
|
11 | 11 | import type { Connect, ViteDevServer } from 'vite';
|
12 |
| -import { loadEsmModule } from '../../../utils/load-esm'; |
13 | 12 | import { AngularMemoryOutputFiles, pathnameWithoutBasePath } from '../utils';
|
14 | 13 |
|
15 | 14 | export function createAngularAssetsMiddleware(
|
16 | 15 | server: ViteDevServer,
|
17 | 16 | assets: Map<string, string>,
|
18 | 17 | outputFiles: AngularMemoryOutputFiles,
|
19 | 18 | usedComponentStyles: Map<string, Set<string>>,
|
| 19 | + encapsulateStyle: (style: Uint8Array, componentId: string) => string, |
20 | 20 | ): Connect.NextHandleFunction {
|
21 | 21 | return function angularAssetsMiddleware(req, res, next) {
|
22 | 22 | if (req.url === undefined || res.writableEnded) {
|
@@ -73,7 +73,7 @@ export function createAngularAssetsMiddleware(
|
73 | 73 | if (extension !== '.js' && extension !== '.html') {
|
74 | 74 | const outputFile = outputFiles.get(pathname);
|
75 | 75 | if (outputFile?.servable) {
|
76 |
| - const data = outputFile.contents; |
| 76 | + let data: Uint8Array | string = outputFile.contents; |
77 | 77 | if (extension === '.css') {
|
78 | 78 | // Inject component ID for view encapsulation if requested
|
79 | 79 | const componentId = new URL(req.url, 'http://localhost').searchParams.get('ngcomp');
|
@@ -108,22 +108,15 @@ export function createAngularAssetsMiddleware(
|
108 | 108 | return;
|
109 | 109 | }
|
110 | 110 |
|
111 |
| - loadEsmModule<typeof import('@angular/compiler')>('@angular/compiler') |
112 |
| - .then((compilerModule) => { |
113 |
| - const encapsulatedData = compilerModule.encapsulateStyle( |
114 |
| - new TextDecoder().decode(data), |
115 |
| - componentId, |
116 |
| - ); |
| 111 | + data = encapsulateStyle(data, componentId); |
| 112 | + } |
117 | 113 |
|
118 |
| - res.setHeader('Content-Type', 'text/css'); |
119 |
| - res.setHeader('Cache-Control', 'no-cache'); |
120 |
| - res.setHeader('ETag', etag); |
121 |
| - res.end(encapsulatedData); |
122 |
| - }) |
123 |
| - .catch((e) => next(e)); |
| 114 | + res.setHeader('Content-Type', 'text/css'); |
| 115 | + res.setHeader('Cache-Control', 'no-cache'); |
| 116 | + res.setHeader('ETag', etag); |
| 117 | + res.end(data); |
124 | 118 |
|
125 |
| - return; |
126 |
| - } |
| 119 | + return; |
127 | 120 | }
|
128 | 121 | }
|
129 | 122 |
|
|
0 commit comments