Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(vite-plugin-angular): cache style URLs by matched styleUrls expression #571

Merged
merged 1 commit into from
Jul 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 37 additions & 23 deletions packages/vite-plugin-angular/src/lib/component-resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ export function hasStyleUrls(code: string) {
}

interface StyleUrlsCacheEntry {
code: string;
matchedStyleUrls: string;
styleUrls: string[];
}

const EMPTY_ARRAY: any[] = [];

export class StyleUrlsResolver {
// These resolvers may be called multiple times during the same
// compilation for the same files. Caching is required because these
Expand All @@ -20,32 +22,44 @@ export class StyleUrlsResolver {
private readonly styleUrlsCache = new Map<string, StyleUrlsCacheEntry>();

resolve(code: string, id: string): string[] {
const entry = this.styleUrlsCache.get(id);
if (entry?.code === code) {
return entry.styleUrls;
}
const styleUrlsExecArray = styleUrlsRE.exec(code);

const styleUrlsGroup = styleUrlsRE.exec(code);

if (Array.isArray(styleUrlsGroup) && styleUrlsGroup[0]) {
const styleUrls = styleUrlsGroup[0].replace(
/(styleUrls|\:|\s|\[|\]|"|')/g,
''
);
const styleUrlPaths = styleUrls?.split(',') || [];

const newEntry = {
code,
styleUrls: styleUrlPaths.map((styleUrlPath) => {
return `${styleUrlPath}|${resolve(dirname(id), styleUrlPath)}`;
}),
};
if (styleUrlsExecArray === null) {
return EMPTY_ARRAY;
}

this.styleUrlsCache.set(id, newEntry);
return newEntry.styleUrls;
// Given the code is the following:
// @Component({
// styleUrls: [
// './app.component.scss'
// ]
// })
// The `matchedStyleUrls` would result in: `styleUrls: [\n './app.component.scss'\n ]`.
const [matchedStyleUrls] = styleUrlsExecArray;
const entry = this.styleUrlsCache.get(id);
// We're using `matchedStyleUrls` as a key because the code may be changing continuously,
// resulting in the resolver being called multiple times. While the code changes, the
// `styleUrls` may remain constant, which means we should always return the previously
// resolved style URLs.
if (entry?.matchedStyleUrls === matchedStyleUrls) {
return entry.styleUrls;
}

return [];
// The `styleUrls` property is an array, which means we may have a list of
// CSS files provided there. Let `matchedStyleUrls` be equal to the following:
// "styleUrls: [\n './app.component.scss',\n '../global.scss'\n ]"
const styleUrlPaths = matchedStyleUrls
.replace(/(styleUrls|\:|\s|\[|\]|"|')/g, '')
// The above replace will result in the following:
// "./app.component.scss,../global.scss"
.split(',');

const styleUrls = styleUrlPaths.map((styleUrlPath) => {
return `${styleUrlPath}|${resolve(dirname(id), styleUrlPath)}`;
});

this.styleUrlsCache.set(matchedStyleUrls, { styleUrls, matchedStyleUrls });
return styleUrls;
}
}

Expand Down