Skip to content

Commit 2ec3306

Browse files
committed
refactor(@schematics/angular): standardize Tailwind import and detection
The Tailwind CSS import statement is updated to use single quotes, aligning with the project's coding style for consistency. The regular expression for detecting an existing Tailwind CSS configuration has been improved to recognize both single and double-quoted import paths. This enhancement makes the schematic more resilient to variations in user code style.
1 parent 30bd75c commit 2ec3306

File tree

4 files changed

+10
-10
lines changed

4 files changed

+10
-10
lines changed

packages/schematics/angular/application/index_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ describe('Application Schematic', () => {
868868
expect(packageJson.devDependencies['@tailwindcss/postcss']).toBeDefined();
869869

870870
const stylesContent = tree.readContent('/projects/foo/src/styles.css');
871-
expect(stylesContent).toContain('@import "tailwindcss";');
871+
expect(stylesContent).toContain(`@import 'tailwindcss';`);
872872
});
873873

874874
describe(`fileNameStyleGuide: '2016'`, () => {

packages/schematics/angular/ng-new/index_spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ describe('Ng New Schematic', () => {
126126
expect(packageJson.devDependencies['@tailwindcss/postcss']).toBeDefined();
127127

128128
const stylesContent = tree.readContent('/bar/src/styles.css');
129-
expect(stylesContent).toContain('@import "tailwindcss";');
129+
expect(stylesContent).toContain(`@import 'tailwindcss';`);
130130
});
131131

132132
it(`should create files with file name style guide '2016'`, async () => {

packages/schematics/angular/tailwind/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function addTailwindStyles(options: { project: string }, project: ProjectDefinit
5555

5656
if (!stylesheetPath) {
5757
const newStylesheetPath = join(project.sourceRoot ?? 'src', 'tailwind.css');
58-
tree.create(newStylesheetPath, '@import "tailwindcss";\n');
58+
tree.create(newStylesheetPath, `@import 'tailwindcss';\n`);
5959

6060
return updateWorkspace((workspace) => {
6161
const project = workspace.projects.get(options.project);
@@ -82,8 +82,8 @@ function addTailwindStyles(options: { project: string }, project: ProjectDefinit
8282
});
8383
} else {
8484
let stylesheetContent = tree.readText(stylesheetPath);
85-
if (!stylesheetContent.includes('@import "tailwindcss";')) {
86-
stylesheetContent += '\n@import "tailwindcss";\n';
85+
if (!/@import ["']tailwindcss["'];/.test(stylesheetContent)) {
86+
stylesheetContent += `\n@import 'tailwindcss';\n`;
8787
tree.overwrite(stylesheetPath, stylesheetContent);
8888
}
8989
}

packages/schematics/angular/tailwind/index_spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,17 @@ describe('Tailwind Schematic', () => {
6464
it('should add tailwind imports to styles.css', async () => {
6565
const tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
6666
const stylesContent = tree.readContent('/projects/bar/src/styles.css');
67-
expect(stylesContent).toContain('@import "tailwindcss";');
67+
expect(stylesContent).toContain(`@import 'tailwindcss';`);
6868
});
6969

7070
it('should not add duplicate tailwind imports to styles.css', async () => {
7171
let tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
7272
const stylesContent = tree.readContent('/projects/bar/src/styles.css');
73-
expect(stylesContent.match(/@import "tailwindcss";/g)?.length).toBe(1);
73+
expect(stylesContent.match(/@import 'tailwindcss';/g)?.length).toBe(1);
7474

7575
tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, tree);
7676
const stylesContentAfter = tree.readContent('/projects/bar/src/styles.css');
77-
expect(stylesContentAfter.match(/@import "tailwindcss";/g)?.length).toBe(1);
77+
expect(stylesContentAfter.match(/@import 'tailwindcss';/g)?.length).toBe(1);
7878
});
7979

8080
describe('with scss styles', () => {
@@ -86,7 +86,7 @@ describe('Tailwind Schematic', () => {
8686
const tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
8787
expect(tree.exists('/projects/bar/src/tailwind.css')).toBe(true);
8888
const stylesContent = tree.readContent('/projects/bar/src/tailwind.css');
89-
expect(stylesContent).toContain('@import "tailwindcss";');
89+
expect(stylesContent).toContain(`@import 'tailwindcss';`);
9090
});
9191

9292
it('should add tailwind.css to angular.json', async () => {
@@ -99,7 +99,7 @@ describe('Tailwind Schematic', () => {
9999
it('should not add tailwind imports to styles.scss', async () => {
100100
const tree = await schematicRunner.runSchematic('tailwind', { project: 'bar' }, appTree);
101101
const stylesContent = tree.readContent('/projects/bar/src/styles.scss');
102-
expect(stylesContent).not.toContain('@import "tailwindcss";');
102+
expect(stylesContent).not.toContain(`@import 'tailwindcss';`);
103103
});
104104
});
105105

0 commit comments

Comments
 (0)