Skip to content

Commit 2e561ed

Browse files
authored
feat(nx-plugin): update templates to use Tailwind version 4.x (#1692)
1 parent 548b755 commit 2e561ed

File tree

17 files changed

+144
-95
lines changed

17 files changed

+144
-95
lines changed

packages/create-analog/files/postcss.config.cjs

Lines changed: 0 additions & 6 deletions
This file was deleted.

packages/create-analog/files/styles.css

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
/* Tailwind directives */
2-
@tailwind base;
3-
@tailwind components;
4-
@tailwind utilities;
1+
@import 'tailwindcss';
52

63
/* You can add global styles to this file, and also import other style files */
74
:root {

packages/create-analog/files/tailwind.config.ts

Lines changed: 0 additions & 9 deletions
This file was deleted.

packages/create-analog/index.js

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,6 @@ async function init() {
211211
}
212212

213213
if (!skipTailwind) {
214-
addTailwindConfig(write, filesDir);
215-
addPostCssConfig(write, filesDir);
216214
addTailwindDirectives(write, filesDir);
217215
}
218216

@@ -230,7 +228,7 @@ async function init() {
230228
}
231229

232230
if (!skipTailwind) {
233-
addTailwindDevDependencies(pkg);
231+
addTailwindDependencies(pkg);
234232
}
235233

236234
if (pkgManager === 'yarn') {
@@ -370,27 +368,10 @@ function addTailwindDirectives(write, filesDir) {
370368
);
371369
}
372370

373-
function addPostCssConfig(write, filesDir) {
374-
write(
375-
'postcss.config.cjs',
376-
fs.readFileSync(path.join(filesDir, `postcss.config.cjs`), 'utf-8'),
377-
);
378-
}
379-
380-
function addTailwindConfig(write, filesDir) {
381-
write(
382-
'tailwind.config.ts',
383-
fs.readFileSync(path.join(filesDir, `tailwind.config.ts`), 'utf-8'),
384-
);
385-
}
386-
387-
function addTailwindDevDependencies(pkg) {
388-
['tailwindcss@^3.3.1', 'postcss@^8.4.21', 'autoprefixer@^10.4.14'].forEach(
389-
(packageName) => {
390-
const [name, version] = packageName.split('@');
391-
pkg.devDependencies[name] = version;
392-
},
393-
);
371+
function addTailwindDependencies(pkg) {
372+
pkg.dependencies['tailwindcss'] = '^4.1.4';
373+
pkg.dependencies['postcss'] = '^8.5.3';
374+
pkg.dependencies['@tailwindcss/vite'] = '^4.1.4';
394375
}
395376

396377
function addYarnDevDependencies(pkg, template) {

packages/nx-plugin/src/generators/app/files/template-angular-v16/vite.config.ts__template__

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import analog from '@analogjs/platform';
44
import { defineConfig, Plugin, splitVendorChunkPlugin } from 'vite';
55
import tsConfigPaths from 'vite-tsconfig-paths';
6+
<% if (addTailwind) { %>
7+
// @ts-expect-error @tailwindcss/vite currently uses mts. TypeScript is complaining this, but it works as expected.
8+
import tailwindcss from '@tailwindcss/vite';
9+
<% } %>
610

711
// https://vitejs.dev/config/
812
export default defineConfig(({ mode }) => {
@@ -17,6 +21,9 @@ export default defineConfig(({ mode }) => {
1721
target: ['es2020'],
1822
},
1923
plugins: [
24+
<% if (addTailwind) { %>
25+
tailwindcss(),
26+
<% } %>
2027
<% if (addTRPC) { %>
2128
analog({
2229
nitro: {

packages/nx-plugin/src/generators/app/files/template-angular-v17/vite.config.ts__template__

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import analog from '@analogjs/platform';
44
import { defineConfig, Plugin, splitVendorChunkPlugin } from 'vite';
55
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
6+
<% if (addTailwind) { %>
7+
// @ts-expect-error @tailwindcss/vite currently uses mts. TypeScript is complaining this, but it works as expected.
8+
import tailwindcss from '@tailwindcss/vite';
9+
<% } %>
610

711
// https://vitejs.dev/config/
812
export default defineConfig(({ mode }) => {
@@ -17,15 +21,18 @@ export default defineConfig(({ mode }) => {
1721
<% } %>
1822
build: {
1923
outDir: '<%= offsetFromRoot %>dist/<%= projectRoot %>/client',
20-
reportCompressedSize: true,
24+
reportCompressedSize: true,
2125
target: ['es2020'],
2226
},
2327
server: {
2428
fs: {
2529
allow: ['.'],
2630
},
27-
},
31+
},
2832
plugins: [
33+
<% if (addTailwind) { %>
34+
tailwindcss(),
35+
<% } %>
2936
<% if (addTRPC) { %>
3037
analog({
3138
nitro: {

packages/nx-plugin/src/generators/app/files/template-angular-v18/vite.config.ts__template__

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import analog from '@analogjs/platform';
44
import { defineConfig, Plugin, splitVendorChunkPlugin } from 'vite';
55
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
6+
<% if (addTailwind) { %>
7+
// @ts-expect-error @tailwindcss/vite currently uses mts. TypeScript is complaining this, but it works as expected.
8+
import tailwindcss from '@tailwindcss/vite';
9+
<% } %>
610

711
// https://vitejs.dev/config/
812
export default defineConfig(({ mode }) => {
@@ -16,15 +20,18 @@ export default defineConfig(({ mode }) => {
1620
<% } %>
1721
build: {
1822
outDir: '<%= offsetFromRoot %>dist/<%= projectRoot %>/client',
19-
reportCompressedSize: true,
23+
reportCompressedSize: true,
2024
target: ['es2020'],
2125
},
2226
server: {
2327
fs: {
2428
allow: ['.'],
2529
},
26-
},
30+
},
2731
plugins: [
32+
<% if (addTailwind) { %>
33+
tailwindcss(),
34+
<% } %>
2835
<% if (addTRPC) { %>
2936
analog({
3037
nitro: {

packages/nx-plugin/src/generators/app/files/template-angular-v19/vite.config.ts__template__

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import analog from '@analogjs/platform';
44
import { defineConfig, Plugin } from 'vite';
55
import { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
6+
<% if (addTailwind) { %>
7+
// @ts-expect-error @tailwindcss/vite currently uses mts. TypeScript is complaining this, but it works as expected.
8+
import tailwindcss from '@tailwindcss/vite';
9+
<% } %>
610

711
// https://vitejs.dev/config/
812
export default defineConfig(({ mode }) => {
@@ -16,15 +20,18 @@ export default defineConfig(({ mode }) => {
1620
<% } %>
1721
build: {
1822
outDir: '<%= offsetFromRoot %>dist/<%= projectRoot %>/client',
19-
reportCompressedSize: true,
23+
reportCompressedSize: true,
2024
target: ['es2020'],
2125
},
2226
server: {
2327
fs: {
2428
allow: ['.'],
2529
},
26-
},
30+
},
2731
plugins: [
32+
<% if (addTailwind) { %>
33+
tailwindcss(),
34+
<% } %>
2835
<% if (addTRPC) { %>
2936
analog({
3037
nitro: {

packages/nx-plugin/src/generators/app/generator.spec.ts

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import {
77
} from '@nx/devkit';
88
import { createTreeWithEmptyWorkspace } from '@nx/devkit/testing';
99
import { test } from 'vitest';
10+
import { lt } from 'semver';
1011

1112
import generator from './generator';
1213
import { AnalogNxApplicationGeneratorOptions } from './schema';
14+
import { checkAndCleanWithSemver } from '@nx/devkit/src/utils/semver';
1315

1416
describe('nx-plugin generator', () => {
1517
const setup = async (
@@ -205,17 +207,42 @@ describe('nx-plugin generator', () => {
205207

206208
const verifyTailwindIsSetUp = (
207209
tree: Tree,
208-
devDependencies: Record<string, string>,
210+
dependencies: Record<string, string>,
209211
) => {
210-
expect(devDependencies['tailwindcss']).toBeDefined();
211-
const hasTailwindConfigFile = tree.exists(
212-
'apps/tailwind-app/tailwind.config.ts',
213-
);
214-
const hasPostCSSConfigFile = tree.exists(
215-
'apps/tailwind-app/postcss.config.cjs',
212+
expect(dependencies['tailwindcss']).toBeDefined();
213+
214+
const version = checkAndCleanWithSemver(
215+
'tailwindcss',
216+
dependencies['tailwindcss'],
216217
);
217-
expect(hasTailwindConfigFile).toBeTruthy();
218-
expect(hasPostCSSConfigFile).toBeTruthy();
218+
219+
if (lt(version, '4.0.0')) {
220+
const hasTailwindConfigFile = tree.exists(
221+
'apps/tailwind-app/tailwind.config.ts',
222+
);
223+
const hasPostCSSConfigFile = tree.exists(
224+
'apps/tailwind-app/postcss.config.cjs',
225+
);
226+
expect(hasTailwindConfigFile).toBeTruthy();
227+
expect(hasPostCSSConfigFile).toBeTruthy();
228+
} else {
229+
expect(dependencies['@tailwindcss/vite']).toBeDefined();
230+
231+
console.log(tree.read('apps/tailwind-app/src/styles.css').toString());
232+
233+
const hasCorrectCssImplementation = tree
234+
.read('apps/tailwind-app/src/styles.css')
235+
.includes(`@import 'tailwindcss';`);
236+
237+
const regex = /plugins: \[.*\btailwindcss\(\)/s;
238+
239+
const viteConfig = tree
240+
.read('apps/tailwind-app/vite.config.ts')
241+
.toString();
242+
243+
expect(regex.test(viteConfig)).toBeTruthy();
244+
expect(hasCorrectCssImplementation).toBeTruthy();
245+
}
219246
};
220247

221248
const verifyTrpcIsSetUp = (
@@ -309,7 +336,7 @@ describe('nx-plugin generator', () => {
309336

310337
verifyHomePageExists(tree, analogAppName);
311338

312-
verifyTailwindIsSetUp(tree, devDependencies);
339+
verifyTailwindIsSetUp(tree, dependencies);
313340
});
314341

315342
it('creates an analogjs app in the source directory with trpc set up', async () => {
@@ -372,7 +399,7 @@ describe('nx-plugin generator', () => {
372399

373400
verifyHomePageExists(tree, analogAppName);
374401

375-
verifyTailwindIsSetUp(tree, devDependencies);
402+
verifyTailwindIsSetUp(tree, dependencies);
376403
});
377404

378405
it('creates an analogjs app in the source directory with trpc set up', async () => {
@@ -438,7 +465,7 @@ describe('nx-plugin generator', () => {
438465

439466
verifyHomePageExists(tree, analogAppName);
440467

441-
verifyTailwindIsSetUp(tree, devDependencies);
468+
verifyTailwindIsSetUp(tree, dependencies);
442469
});
443470

444471
it('creates an analogjs app in the source directory with trpc set up', async () => {
@@ -496,7 +523,7 @@ describe('nx-plugin generator', () => {
496523

497524
verifyHomePageExists(tree, analogAppName);
498525

499-
verifyTailwindIsSetUp(tree, devDependencies);
526+
verifyTailwindIsSetUp(tree, dependencies);
500527
});
501528

502529
it('creates an analogjs app in the source directory without trpc due to unsupported Nx version', async () => {

0 commit comments

Comments
 (0)