| 27 | } |
| 28 | |
| 29 | export function optimize( |
| 30 | input: string, |
| 31 | { file = class="st">'input.css', minify = false, map }: OptimizeOptions = {}, |
| 32 | ): TransformResult { |
| 33 | function optimize(code: Buffer | Uint8Array, map: string | undefined) { |
| 34 | return transform({ |
| 35 | filename: file, |
| 36 | code, |
| 37 | minify, |
| 38 | sourceMap: typeof map !== class="st">'undefined', |
| 39 | inputSourceMap: map, |
| 40 | drafts: { |
| 41 | customMedia: true, |
| 42 | }, |
| 43 | nonStandard: { |
| 44 | deepSelectorCombinator: true, |
| 45 | }, |
| 46 | include: Features.Nesting | Features.MediaQueries, |
| 47 | exclude: Features.LogicalProperties | Features.DirSelector | Features.LightDark, |
| 48 | targets: { |
| 49 | safari: (16 << 16) | (4 << 8), |
| 50 | ios_saf: (16 << 16) | (4 << 8), |
| 51 | firefox: 128 << 16, |
| 52 | chrome: 111 << 16, |
| 53 | }, |
| 54 | errorRecovery: true, |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | class="cm">// Running Lightning CSS twice to ensure that adjacent rules are merged after |
| 59 | class="cm">// nesting is applied. This creates a more optimized output. |
| 60 | let result = optimize(Buffer.from(input), map) |
| 61 | map = result.map?.toString() |
| 62 | |
| 63 | result.warnings = result.warnings.filter((warning) => { |
| 64 | class="cm">// Ignore warnings about unknown pseudo-classes as they are likely caused |
| 65 | class="cm">// by the use of `:deep()`, `:slotted()`, and `:global()` which are not |
| 66 | class="cm">// standard CSS but are commonly used in frameworks like Vue. |
| 67 | if (/class="st">'(deep|slotted|global)' is not recognized as a valid pseudo-/.test(warning.message)) { |
| 68 | return false |
| 69 | } |
| 70 | |
| 71 | class="cm">// Ignore warnings about unknown at-rules. |
| 72 | class="cm">// |
| 73 | class="cm">// TODO: drop `@position-try` once https://github.com/parcel-bundler/lightningcss/pull/1238 lands |
| 74 | if (/Unknown at rule: @position-try/.test(warning.message)) { |
| 75 | return false |
| 76 | } |
| 77 | |
| 78 | return true |
| 79 | }) |
| 80 | |
| 81 | class="cm">// Because of `errorRecovery: true`, there could be warnings, so let's let the |
| 82 | class="cm">// user know about them. |
| 83 | if (process.env.NODE_ENV !== class="st">'test' && result.warnings.length > 0) { |
| 84 | let lines = input.split(class="st">'\n') |
| 85 | |
| 86 | let output = [ |