Skip to content

Commit

Permalink
inline optimizeCss in leaf packages
Browse files Browse the repository at this point in the history
Instead of introducing a custom `@tailwindcss/optimize` package
  • Loading branch information
RobinMalfait committed Mar 13, 2024
1 parent 48ec021 commit 2937d6b
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 174 deletions.
2 changes: 1 addition & 1 deletion packages/@tailwindcss-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
},
"dependencies": {
"@parcel/watcher": "^2.4.1",
"@tailwindcss/optimize": "workspace:^",
"@tailwindcss/oxide": "workspace:^",
"lightningcss": "^1.24.0",
"mri": "^1.2.0",
"picocolors": "^1.0.0",
"postcss": "8.4.24",
Expand Down
26 changes: 25 additions & 1 deletion packages/@tailwindcss-cli/src/commands/build/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import watcher from '@parcel/watcher'
import { optimizeCss } from '@tailwindcss/optimize'
import { IO, Parsing, scanDir, scanFiles, type ChangedContent } from '@tailwindcss/oxide'
import { Features, transform } from 'lightningcss'
import { existsSync } from 'node:fs'
import fs from 'node:fs/promises'
import path from 'node:path'
Expand Down Expand Up @@ -254,3 +254,27 @@ function handleImports(
),
])
}

function optimizeCss(
input: string,
{ file = 'input.css', minify = false }: { file?: string; minify?: boolean } = {},
) {
return transform({
filename: file,
code: Buffer.from(input),
minify,
sourceMap: false,
drafts: {
customMedia: true,
},
nonStandard: {
deepSelectorCombinator: true,
},
include: Features.Nesting,
exclude: Features.LogicalProperties,
targets: {
safari: (16 << 16) | (4 << 8),
},
errorRecovery: true,
}).code.toString()
}
40 changes: 0 additions & 40 deletions packages/@tailwindcss-optimize/README.md

This file was deleted.

39 changes: 0 additions & 39 deletions packages/@tailwindcss-optimize/package.json

This file was deleted.

32 changes: 0 additions & 32 deletions packages/@tailwindcss-optimize/src/index.test.ts

This file was deleted.

25 changes: 0 additions & 25 deletions packages/@tailwindcss-optimize/src/index.ts

This file was deleted.

3 changes: 0 additions & 3 deletions packages/@tailwindcss-optimize/tsconfig.json

This file was deleted.

2 changes: 1 addition & 1 deletion packages/@tailwindcss-postcss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
}
},
"dependencies": {
"@tailwindcss/optimize": "workspace:^",
"@tailwindcss/oxide": "workspace:^",
"lightningcss": "^1.24.0",
"postcss-import": "^16.0.0",
"tailwindcss": "workspace:^"
},
Expand Down
26 changes: 25 additions & 1 deletion packages/@tailwindcss-postcss/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { optimizeCss } from '@tailwindcss/optimize'
import { scanDir } from '@tailwindcss/oxide'
import fs from 'fs'
import { Features, transform } from 'lightningcss'
import postcss, { type AcceptedPlugin, type PluginCreator } from 'postcss'
import postcssImport from 'postcss-import'
import { compile } from 'tailwindcss'
Expand Down Expand Up @@ -151,4 +151,28 @@ function tailwindcss(opts: PluginOptions = {}): AcceptedPlugin {
}
}

function optimizeCss(
input: string,
{ file = 'input.css', minify = false }: { file?: string; minify?: boolean } = {},
) {
return transform({
filename: file,
code: Buffer.from(input),
minify,
sourceMap: false,
drafts: {
customMedia: true,
},
nonStandard: {
deepSelectorCombinator: true,
},
include: Features.Nesting,
exclude: Features.LogicalProperties,
targets: {
safari: (16 << 16) | (4 << 8),
},
errorRecovery: true,
}).code.toString()
}

export default Object.assign(tailwindcss, { postcss: true }) as PluginCreator<PluginOptions>
2 changes: 1 addition & 1 deletion packages/@tailwindcss-vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
}
},
"dependencies": {
"@tailwindcss/optimize": "workspace:^",
"@tailwindcss/oxide": "workspace:^",
"lightningcss": "^1.24.0",
"tailwindcss": "workspace:^"
},
"devDependencies": {
Expand Down
26 changes: 25 additions & 1 deletion packages/@tailwindcss-vite/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { optimizeCss } from '@tailwindcss/optimize'
import { IO, Parsing, scanFiles } from '@tailwindcss/oxide'
import { Features, transform } from 'lightningcss'
import path from 'path'
import { compile } from 'tailwindcss'
import type { Plugin, Update, ViteDevServer } from 'vite'
Expand Down Expand Up @@ -182,3 +182,27 @@ export default function tailwindcss(): Plugin[] {
},
] satisfies Plugin[]
}

function optimizeCss(
input: string,
{ file = 'input.css', minify = false }: { file?: string; minify?: boolean } = {},
) {
return transform({
filename: file,
code: Buffer.from(input),
minify,
sourceMap: false,
drafts: {
customMedia: true,
},
nonStandard: {
deepSelectorCombinator: true,
},
include: Features.Nesting,
exclude: Features.LogicalProperties,
targets: {
safari: (16 << 16) | (4 << 8),
},
errorRecovery: true,
}).code.toString()
}
4 changes: 2 additions & 2 deletions packages/tailwindcss/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
"utilities.css"
],
"devDependencies": {
"@tailwindcss/optimize": "workspace:^",
"@tailwindcss/oxide": "workspace:^",
"@types/node": "^20.10.8"
"@types/node": "^20.10.8",
"lightningcss": "^1.24.0"
}
}
26 changes: 25 additions & 1 deletion packages/tailwindcss/src/test-utils/run.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { optimizeCss } from '@tailwindcss/optimize'
import { Features, transform } from 'lightningcss'
import { compile } from '..'

export function compileCss(css: string, candidates: string[] = []) {
Expand All @@ -8,3 +8,27 @@ export function compileCss(css: string, candidates: string[] = []) {
export function run(candidates: string[]) {
return optimizeCss(compile('@tailwind utilities;').build(candidates)).trim()
}

export function optimizeCss(
input: string,
{ file = 'input.css', minify = false }: { file?: string; minify?: boolean } = {},
) {
return transform({
filename: file,
code: Buffer.from(input),
minify,
sourceMap: false,
drafts: {
customMedia: true,
},
nonStandard: {
deepSelectorCombinator: true,
},
include: Features.Nesting,
exclude: Features.LogicalProperties,
targets: {
safari: (16 << 16) | (4 << 8),
},
errorRecovery: true,
}).code.toString()
}
2 changes: 1 addition & 1 deletion packages/tailwindcss/tests/ui.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { expect, test, type Page } from '@playwright/test'
import { optimizeCss } from '@tailwindcss/optimize'
import { IO, Parsing, scanFiles } from '@tailwindcss/oxide'
import fs from 'fs'
import path from 'path'
import { compile } from '../src'
import { optimizeCss } from '../src/test-utils/run'

const html = String.raw
const css = String.raw
Expand Down
Loading

0 comments on commit 2937d6b

Please sign in to comment.