Michaël De Boey 8e8a2d6efc
update @ampproject/remapping to @jridgewell/remapping (#18716)
Even though
[`@ampproject/remapping`](https://npm.im/@ampproject/remapping) isn't
deprecated on npm (yet), it's
[repo](https://github.com/ampproject/remapping) is archived, so people
should move to
[`@jridgewell/remapping`](https://npm.im/@jridgewell/remapping)

> Development moved to
[monorepo](https://github.com/jridgewell/sourcemaps)
> See
https://github.com/jridgewell/sourcemaps/tree/main/packages/remapping
for latest code.

---------

Co-authored-by: Jordan Pittman <jordan@cryptica.me>
2025-08-12 09:52:35 -04:00

91 lines
2.3 KiB
TypeScript

import remapping from '@jridgewell/remapping'
import { Features, transform } from 'lightningcss'
import MagicString from 'magic-string'
export interface OptimizeOptions {
/**
* The file being transformed
*/
file?: string
/**
* Enabled minified output
*/
minify?: boolean
/**
* The output source map before optimization
*
* If omitted a resulting source map will not be available
*/
map?: string
}
export interface TransformResult {
code: string
map: string | undefined
}
export function optimize(
input: string,
{ file = 'input.css', minify = false, map }: OptimizeOptions = {},
): TransformResult {
function optimize(code: Buffer | Uint8Array, map: string | undefined) {
return transform({
filename: file,
code,
minify,
sourceMap: typeof map !== 'undefined',
inputSourceMap: map,
drafts: {
customMedia: true,
},
nonStandard: {
deepSelectorCombinator: true,
},
include: Features.Nesting | Features.MediaQueries,
exclude: Features.LogicalProperties | Features.DirSelector | Features.LightDark,
targets: {
safari: (16 << 16) | (4 << 8),
ios_saf: (16 << 16) | (4 << 8),
firefox: 128 << 16,
chrome: 111 << 16,
},
errorRecovery: true,
})
}
// Running Lightning CSS twice to ensure that adjacent rules are merged after
// nesting is applied. This creates a more optimized output.
let result = optimize(Buffer.from(input), map)
map = result.map?.toString()
result = optimize(result.code, map)
map = result.map?.toString()
let code = result.code.toString()
// Work around an issue where the media query range syntax transpilation
// generates code that is invalid with `@media` queries level 3.
let magic = new MagicString(code)
magic.replaceAll('@media not (', '@media not all and (')
// We have to use a source-map-preserving method of replacing the content
// which requires the use of Magic String + remapping(…) to make sure
// the resulting map is correct
if (map !== undefined && magic.hasChanged()) {
let magicMap = magic.generateMap({ source: 'original', hires: 'boundary' }).toString()
let remapped = remapping([magicMap, map], () => null)
map = remapped.toString()
}
code = magic.toString()
return {
code,
map,
}
}