Skip to content

Commit

Permalink
feat(vite-plugin-angular): add support for styleUrl in component deco…
Browse files Browse the repository at this point in the history
…rator (#784)
  • Loading branch information
brandonroberts authored Dec 7, 2023
1 parent c80d05f commit 4ae30a9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
19 changes: 18 additions & 1 deletion packages/vite-plugin-angular/src/lib/component-resolvers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const thePathsAreEqual = (actual: string[], expected: string[]) => {
return true;
};

describe('component-resolvers styleUrls', () => {
describe('component-resolvers', () => {
const id = '/path/to/src/app.component.ts';

describe('matcher', () => {
Expand All @@ -55,6 +55,23 @@ describe('component-resolvers styleUrls', () => {
expect(thePathsAreEqual(resolvedPaths, actualPaths));
});

it('should handle single line styleUrl', () => {
const code = `
@Component({
styleUrl: './app.component.css'
})
export class MyComponent {}
`;

const actualPaths = [
'./app.component.css|/path/to/src/app.component.css',
];
const templateUrlsResolver = new TemplateUrlsResolver();
const resolvedPaths = templateUrlsResolver.resolve(code, id);

expect(thePathsAreEqual(resolvedPaths, actualPaths));
});

it('should handle multi-line styleUrls', () => {
const code = `
@Component({
Expand Down
3 changes: 2 additions & 1 deletion packages/vite-plugin-angular/src/lib/component-resolvers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { dirname, resolve } from 'path';

const styleUrlsRE = /styleUrls\s*:\s*\[([^\[]*?)\]/;
const styleUrlsRE = /styleUrls\s*:\s*\[([^\[]*?)\]|styleUrl:\s*["'](.*?)["']/;
const templateUrlRE = /templateUrl:\s*["'](.*?)["']/g;

export function hasStyleUrls(code: string) {
Expand Down Expand Up @@ -50,6 +50,7 @@ export class StyleUrlsResolver {
// "styleUrls: [\n './app.component.scss',\n '../global.scss'\n ]"
const styleUrlPaths = matchedStyleUrls
.replace(/(styleUrls|\:|\s|\[|\]|"|')/g, '')
.replace(/(styleUrl|:\s*["'](.*?)["'])/g, '')
// The above replace will result in the following:
// "./app.component.scss,../global.scss"
.split(',');
Expand Down

0 comments on commit 4ae30a9

Please sign in to comment.