index.js.flow 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. // @flow
  2. type Exclude<A, B> = A;
  3. // see https://gist.github.com/thecotne/6e5969f4aaf8f253985ed36b30ac9fe0
  4. type $FlowGen$If<X: boolean, Then, Else = empty> = $Call<
  5. ((true, Then, Else) => Then) & ((false, Then, Else) => Else),
  6. X,
  7. Then,
  8. Else
  9. >;
  10. type $FlowGen$Assignable<A, B> = $Call<
  11. ((...r: [B]) => true) & ((...r: [A]) => false),
  12. A
  13. >;
  14. import type {
  15. Angle,
  16. CssColor,
  17. Rule,
  18. CustomProperty,
  19. EnvironmentVariable,
  20. Function,
  21. Image,
  22. LengthValue,
  23. MediaQuery,
  24. Declaration,
  25. Ratio,
  26. Resolution,
  27. Selector,
  28. SupportsCondition,
  29. Time,
  30. Token,
  31. TokenOrValue,
  32. UnknownAtRule,
  33. Url,
  34. Variable,
  35. StyleRule,
  36. DeclarationBlock,
  37. ParsedComponent,
  38. Multiplier,
  39. StyleSheet,
  40. Location2,
  41. } from "./ast.js.flow";
  42. import { Targets, Features } from "./targets.js.flow";
  43. declare export * from "./ast.js.flow";
  44. declare export { Targets, Features };
  45. export type TransformOptions<C: CustomAtRules> = {|
  46. /**
  47. * The filename being transformed. Used for error messages and source maps.
  48. */
  49. filename: string,
  50. /**
  51. * The source code to transform.
  52. */
  53. code: Uint8Array,
  54. /**
  55. * Whether to enable minification.
  56. */
  57. minify?: boolean,
  58. /**
  59. * Whether to output a source map.
  60. */
  61. sourceMap?: boolean,
  62. /**
  63. * An input source map to extend.
  64. */
  65. inputSourceMap?: string,
  66. /**
  67. * An optional project root path, used as the source root in the output source map.
  68. * Also used to generate relative paths for sources used in CSS module hashes.
  69. */
  70. projectRoot?: string,
  71. /**
  72. * The browser targets for the generated code.
  73. */
  74. targets?: Targets,
  75. /**
  76. * Features that should always be compiled, even when supported by targets.
  77. */
  78. include?: number,
  79. /**
  80. * Features that should never be compiled, even when unsupported by targets.
  81. */
  82. exclude?: number,
  83. /**
  84. * Whether to enable parsing various draft syntax.
  85. */
  86. drafts?: Drafts,
  87. /**
  88. * Whether to enable various non-standard syntax.
  89. */
  90. nonStandard?: NonStandard,
  91. /**
  92. * Whether to compile this file as a CSS module.
  93. */
  94. cssModules?: boolean | CSSModulesConfig,
  95. /**
  96. * Whether to analyze dependencies (e.g. string).
  97. * When enabled, string dependencies
  98. * are replaced with hashed placeholders that can be replaced with the final
  99. * urls later (after bundling). Dependencies are returned as part of the result.
  100. */
  101. analyzeDependencies?: boolean | DependencyOptions,
  102. /**
  103. * Replaces user action pseudo classes with class names that can be applied from JavaScript.
  104. * This is useful for polyfills, for example.
  105. */
  106. pseudoClasses?: PseudoClasses,
  107. /**
  108. * A list of class names, ids, and custom identifiers (e.g. @keyframes) that are known
  109. * to be unused. These will be removed during minification. Note that these are not
  110. * selectors but individual names (without any . or # prefixes).
  111. */
  112. unusedSymbols?: string[],
  113. /**
  114. * Whether to ignore invalid rules and declarations rather than erroring.
  115. * When enabled, warnings are returned, and the invalid rule or declaration is
  116. * omitted from the output code.
  117. */
  118. errorRecovery?: boolean,
  119. /**
  120. * An AST visitor object. This allows custom transforms or analysis to be implemented in JavaScript.
  121. * Multiple visitors can be composed into one using the string function.
  122. * For optimal performance, visitors should be as specific as possible about what types of values
  123. * they care about so that JavaScript has to be called as little as possible.
  124. */
  125. visitor?: Visitor<C> | VisitorFunction<C>,
  126. /**
  127. * Defines how to parse custom CSS at-rules. Each at-rule can have a prelude, defined using a CSS
  128. * [syntax string](https://drafts.css-houdini.org/css-properties-values-api/#syntax-strings), and
  129. * a block body. The body can be a declaration list, rule list, or style block as defined in the
  130. * [css spec](https://drafts.csswg.org/css-syntax/#declaration-rule-list).
  131. */
  132. customAtRules?: C,
  133. |};
  134. declare type PropertyStart =
  135. | "-"
  136. | "_"
  137. | "a"
  138. | "b"
  139. | "c"
  140. | "d"
  141. | "e"
  142. | "f"
  143. | "g"
  144. | "h"
  145. | "i"
  146. | "j"
  147. | "k"
  148. | "l"
  149. | "m"
  150. | "n"
  151. | "o"
  152. | "p"
  153. | "q"
  154. | "r"
  155. | "s"
  156. | "t"
  157. | "u"
  158. | "v"
  159. | "w"
  160. | "x"
  161. | "y"
  162. | "z";
  163. export type ReturnedDeclaration =
  164. | Declaration
  165. | {|
  166. /**
  167. * The property name.
  168. */
  169. property: string,
  170. /**
  171. * The raw string value for the declaration.
  172. */
  173. raw: string,
  174. |};
  175. export type ReturnedMediaQuery =
  176. | MediaQuery
  177. | {|
  178. /**
  179. * The raw string value for the media query.
  180. */
  181. raw: string,
  182. |};
  183. declare type FindByType<Union, Name> = $FlowGen$If<
  184. $FlowGen$Assignable<
  185. Union,
  186. {|
  187. type: Name,
  188. |}
  189. >,
  190. Union,
  191. empty
  192. >;
  193. export type ReturnedRule = Rule<ReturnedDeclaration, ReturnedMediaQuery>;
  194. declare type RequiredValue<Rule> = $FlowGen$If<
  195. $FlowGen$Assignable<
  196. Rule,
  197. {|
  198. value: { [key: string]: any },
  199. |}
  200. >,
  201. $FlowGen$If<
  202. $FlowGen$Assignable<$PropertyType<Rule, "value">, StyleRule>,
  203. {|
  204. ...Rule,
  205. ...{|
  206. value: {|
  207. ...Required<StyleRule>,
  208. ...{|
  209. declarations: Required<DeclarationBlock>,
  210. |},
  211. |},
  212. |},
  213. |},
  214. {|
  215. ...Rule,
  216. ...{|
  217. value: Required<$PropertyType<Rule, "value">>,
  218. |},
  219. |}
  220. >,
  221. Rule
  222. >;
  223. declare type RuleVisitor<R = RequiredValue<Rule>> = (
  224. rule: R
  225. ) => ReturnedRule | ReturnedRule[] | void;
  226. declare type MappedRuleVisitors = $ObjMapi<
  227. { [k: Exclude<$PropertyType<Rule, "type">, "unknown" | "custom">]: any },
  228. <Name>(Name) => RuleVisitor<RequiredValue<FindByType<Rule, Name>>>
  229. >;
  230. declare type UnknownVisitors<T> = {
  231. [name: string]: RuleVisitor<T>,
  232. };
  233. declare type CustomVisitors<T: CustomAtRules> = $ObjMapi<
  234. T,
  235. <Name>(Name) => RuleVisitor<CustomAtRule<Name, $ElementType<T, Name>>>
  236. >;
  237. declare type AnyCustomAtRule<C: CustomAtRules> = $ElementType<
  238. $ObjMapi<C, <Key>(Key) => CustomAtRule<Key, $ElementType<C, Key>>>,
  239. $Keys<C>
  240. >;
  241. declare type RuleVisitors<C: CustomAtRules> = {|
  242. ...MappedRuleVisitors,
  243. ...{|
  244. unknown?:
  245. | UnknownVisitors<UnknownAtRule>
  246. | $Diff<
  247. RuleVisitor<UnknownAtRule>,
  248. { [key: $Keys<CallableFunction>]: any }
  249. >,
  250. custom?:
  251. | CustomVisitors<C>
  252. | $Diff<
  253. RuleVisitor<AnyCustomAtRule<C>>,
  254. { [key: $Keys<CallableFunction>]: any }
  255. >,
  256. |},
  257. |};
  258. declare type PreludeTypes = Exclude<
  259. $PropertyType<ParsedComponent, "type">,
  260. "literal" | "repeated" | "token"
  261. >;
  262. declare type SyntaxString = string | string;
  263. declare type ComponentTypes = $ObjMapi<
  264. { [k: PreludeTypes]: any },
  265. <Key>(Key) => FindByType<ParsedComponent, Key>
  266. >;
  267. declare type Repetitions = $ObjMapi<
  268. { [k: PreludeTypes]: any },
  269. <Key>(Key) => {|
  270. type: "repeated",
  271. value: {|
  272. components: FindByType<ParsedComponent, Key>[],
  273. multiplier: Multiplier,
  274. |},
  275. |}
  276. >;
  277. declare type MappedPrelude = {| ...ComponentTypes, ...Repetitions |};
  278. declare type MappedBody<P: $PropertyType<CustomAtRuleDefinition, "body">> =
  279. $FlowGen$If<$FlowGen$Assignable<P, "style-block">, "rule-list", P>;
  280. declare type CustomAtRule<N, R: CustomAtRuleDefinition> = {|
  281. name: N,
  282. prelude: $FlowGen$If<
  283. $FlowGen$Assignable<$PropertyType<R, "prelude">, $Keys<MappedPrelude>>,
  284. $ElementType<MappedPrelude, $PropertyType<R, "prelude">>,
  285. ParsedComponent
  286. >,
  287. body: FindByType<CustomAtRuleBody, MappedBody<$PropertyType<R, "body">>>,
  288. loc: Location2,
  289. |};
  290. declare type CustomAtRuleBody =
  291. | {|
  292. type: "declaration-list",
  293. value: Required<DeclarationBlock>,
  294. |}
  295. | {|
  296. type: "rule-list",
  297. value: RequiredValue<Rule>[],
  298. |};
  299. declare type FindProperty<Union, Name> = $FlowGen$If<
  300. $FlowGen$Assignable<
  301. Union,
  302. {|
  303. property: Name,
  304. |}
  305. >,
  306. Union,
  307. empty
  308. >;
  309. declare type DeclarationVisitor<P = Declaration> = (
  310. property: P
  311. ) => ReturnedDeclaration | ReturnedDeclaration[] | void;
  312. declare type MappedDeclarationVisitors = $ObjMapi<
  313. {
  314. [k: Exclude<
  315. $PropertyType<Declaration, "property">,
  316. "unparsed" | "custom"
  317. >]: any,
  318. },
  319. <Name>(
  320. Name
  321. ) => DeclarationVisitor<
  322. FindProperty<Declaration, Name> | FindProperty<Declaration, "unparsed">
  323. >
  324. >;
  325. declare type CustomPropertyVisitors = {
  326. [name: string]: DeclarationVisitor<CustomProperty>,
  327. };
  328. declare type DeclarationVisitors = {|
  329. ...MappedDeclarationVisitors,
  330. ...{|
  331. custom?: CustomPropertyVisitors | DeclarationVisitor<CustomProperty>,
  332. |},
  333. |};
  334. declare type RawValue = {|
  335. /**
  336. * A raw string value which will be parsed like CSS.
  337. */
  338. raw: string,
  339. |};
  340. declare type TokenReturnValue = TokenOrValue | TokenOrValue[] | RawValue | void;
  341. declare type TokenVisitor = (token: Token) => TokenReturnValue;
  342. declare type VisitableTokenTypes =
  343. | "ident"
  344. | "at-keyword"
  345. | "hash"
  346. | "id-hash"
  347. | "string"
  348. | "number"
  349. | "percentage"
  350. | "dimension";
  351. declare type TokenVisitors = $ObjMapi<
  352. { [k: VisitableTokenTypes]: any },
  353. <Name>(Name) => (token: FindByType<Token, Name>) => TokenReturnValue
  354. >;
  355. declare type FunctionVisitor = (fn: Function) => TokenReturnValue;
  356. declare type EnvironmentVariableVisitor = (
  357. env: EnvironmentVariable
  358. ) => TokenReturnValue;
  359. declare type EnvironmentVariableVisitors = {
  360. [name: string]: EnvironmentVariableVisitor,
  361. };
  362. export type Visitor<C: CustomAtRules> = {|
  363. StyleSheet?: (
  364. stylesheet: StyleSheet
  365. ) => StyleSheet<ReturnedDeclaration, ReturnedMediaQuery> | void,
  366. StyleSheetExit?: (
  367. stylesheet: StyleSheet
  368. ) => StyleSheet<ReturnedDeclaration, ReturnedMediaQuery> | void,
  369. Rule?: RuleVisitor<> | RuleVisitors<C>,
  370. RuleExit?: RuleVisitor<> | RuleVisitors<C>,
  371. Declaration?: DeclarationVisitor<> | DeclarationVisitors,
  372. DeclarationExit?: DeclarationVisitor<> | DeclarationVisitors,
  373. Url?: (url: Url) => Url | void,
  374. Color?: (color: CssColor) => CssColor | void,
  375. Image?: (image: Image) => Image | void,
  376. ImageExit?: (image: Image) => Image | void,
  377. Length?: (length: LengthValue) => LengthValue | void,
  378. Angle?: (angle: Angle) => Angle | void,
  379. Ratio?: (ratio: Ratio) => Ratio | void,
  380. Resolution?: (resolution: Resolution) => Resolution | void,
  381. Time?: (time: Time) => Time | void,
  382. CustomIdent?: (ident: string) => string | void,
  383. DashedIdent?: (ident: string) => string | void,
  384. MediaQuery?: (
  385. query: MediaQuery
  386. ) => ReturnedMediaQuery | ReturnedMediaQuery[] | void,
  387. MediaQueryExit?: (
  388. query: MediaQuery
  389. ) => ReturnedMediaQuery | ReturnedMediaQuery[] | void,
  390. SupportsCondition?: (condition: SupportsCondition) => SupportsCondition,
  391. SupportsConditionExit?: (condition: SupportsCondition) => SupportsCondition,
  392. Selector?: (selector: Selector) => Selector | Selector[] | void,
  393. Token?: TokenVisitor | TokenVisitors,
  394. Function?:
  395. | FunctionVisitor
  396. | {
  397. [name: string]: FunctionVisitor,
  398. },
  399. FunctionExit?:
  400. | FunctionVisitor
  401. | {
  402. [name: string]: FunctionVisitor,
  403. },
  404. Variable?: (variable: Variable) => TokenReturnValue,
  405. VariableExit?: (variable: Variable) => TokenReturnValue,
  406. EnvironmentVariable?:
  407. | EnvironmentVariableVisitor
  408. | EnvironmentVariableVisitors,
  409. EnvironmentVariableExit?:
  410. | EnvironmentVariableVisitor
  411. | EnvironmentVariableVisitors,
  412. |};
  413. export type VisitorDependency = FileDependency | GlobDependency;
  414. export type VisitorOptions = {|
  415. addDependency: (dep: VisitorDependency) => void,
  416. |};
  417. export type VisitorFunction<C: CustomAtRules> = (
  418. options: VisitorOptions
  419. ) => Visitor<C>;
  420. export type CustomAtRules = {|
  421. [name: string]: CustomAtRuleDefinition,
  422. |};
  423. export type CustomAtRuleDefinition = {|
  424. /**
  425. * Defines the syntax for a custom at-rule prelude. The value should be a
  426. * CSS [syntax string](https://drafts.css-houdini.org/css-properties-values-api/#syntax-strings)
  427. * representing the types of values that are accepted. This property may be omitted or
  428. * set to null to indicate that no prelude is accepted.
  429. */
  430. prelude?: SyntaxString | null,
  431. /**
  432. * Defines the type of body contained within the at-rule block.
  433. * - declaration-list: A CSS declaration list, as in a style rule.
  434. * - rule-list: A list of CSS rules, as supported within a non-nested
  435. * at-rule such as string.
  436. * - style-block: Both a declaration list and rule list, as accepted within
  437. * a nested at-rule within a style rule (e.g. string inside a style rule
  438. * with directly nested declarations).
  439. */
  440. body?: "declaration-list" | "rule-list" | "style-block" | null,
  441. |};
  442. export type DependencyOptions = {|
  443. /**
  444. * Whether to preserve string rules rather than removing them.
  445. */
  446. preserveImports?: boolean,
  447. |};
  448. export type BundleOptions<C: CustomAtRules> = $Diff<
  449. TransformOptions<C>,
  450. {| code: any |}
  451. >;
  452. export type BundleAsyncOptions<C: CustomAtRules> = {|
  453. ...$Exact<BundleOptions<C>>,
  454. resolver?: Resolver,
  455. |};
  456. /**
  457. * Custom resolver to use when loading CSS files.
  458. */
  459. export type Resolver = {|
  460. /**
  461. * Read the given file and return its contents as a string.
  462. */
  463. read?: (file: string) => string | Promise<string>,
  464. /**
  465. * Resolve the given CSS import specifier from the provided originating file to a
  466. * path which gets passed to string.
  467. */
  468. resolve?: (
  469. specifier: string,
  470. originatingFile: string
  471. ) => string | Promise<string>,
  472. |};
  473. export type Drafts = {|
  474. /**
  475. * Whether to enable @custom-media rules.
  476. */
  477. customMedia?: boolean,
  478. |};
  479. export type NonStandard = {|
  480. /**
  481. * Whether to enable the non-standard >>> and /deep/ selector combinators used by Angular and Vue.
  482. */
  483. deepSelectorCombinator?: boolean,
  484. |};
  485. export type PseudoClasses = {|
  486. hover?: string,
  487. active?: string,
  488. focus?: string,
  489. focusVisible?: string,
  490. focusWithin?: string,
  491. |};
  492. export type TransformResult = {|
  493. /**
  494. * The transformed code.
  495. */
  496. code: Uint8Array,
  497. /**
  498. * The generated source map, if enabled.
  499. */
  500. map: Uint8Array | void,
  501. /**
  502. * CSS module exports, if enabled.
  503. */
  504. exports: CSSModuleExports | void,
  505. /**
  506. * CSS module references, if string is enabled.
  507. */
  508. references: CSSModuleReferences,
  509. /**
  510. * string dependencies, if enabled.
  511. */
  512. dependencies: Dependency[] | void,
  513. /**
  514. * Warnings that occurred during compilation.
  515. */
  516. warnings: Warning[],
  517. |};
  518. export type Warning = {|
  519. message: string,
  520. type: string,
  521. value?: any,
  522. loc: ErrorLocation,
  523. |};
  524. export type CSSModulesConfig = {|
  525. /**
  526. * The pattern to use when renaming class names and other identifiers. Default is string.
  527. */
  528. pattern?: string,
  529. /**
  530. * Whether to rename dashed identifiers, e.g. custom properties.
  531. */
  532. dashedIdents?: boolean,
  533. /**
  534. * Whether to enable hashing for string.
  535. */
  536. animation?: boolean,
  537. /**
  538. * Whether to enable hashing for CSS grid identifiers.
  539. */
  540. grid?: boolean,
  541. /**
  542. * Whether to enable hashing for string names.
  543. */
  544. container?: boolean,
  545. /**
  546. * Whether to enable hashing for custom identifiers.
  547. */
  548. customIdents?: boolean,
  549. /**
  550. * Whether to require at least one class or id selector in each rule.
  551. */
  552. pure?: boolean,
  553. |};
  554. export type CSSModuleExports = {
  555. /**
  556. * Maps exported (i.e. original) names to local names.
  557. */
  558. [name: string]: CSSModuleExport,
  559. };
  560. export type CSSModuleExport = {|
  561. /**
  562. * The local (compiled) name for this export.
  563. */
  564. name: string,
  565. /**
  566. * Whether the export is referenced in this file.
  567. */
  568. isReferenced: boolean,
  569. /**
  570. * Other names that are composed by this export.
  571. */
  572. composes: CSSModuleReference[],
  573. |};
  574. export type CSSModuleReferences = {
  575. /**
  576. * Maps placeholder names to references.
  577. */
  578. [name: string]: DependencyCSSModuleReference,
  579. };
  580. export type CSSModuleReference =
  581. | LocalCSSModuleReference
  582. | GlobalCSSModuleReference
  583. | DependencyCSSModuleReference;
  584. export type LocalCSSModuleReference = {|
  585. type: "local",
  586. /**
  587. * The local (compiled) name for the reference.
  588. */
  589. name: string,
  590. |};
  591. export type GlobalCSSModuleReference = {|
  592. type: "global",
  593. /**
  594. * The referenced global name.
  595. */
  596. name: string,
  597. |};
  598. export type DependencyCSSModuleReference = {|
  599. type: "dependency",
  600. /**
  601. * The name to reference within the dependency.
  602. */
  603. name: string,
  604. /**
  605. * The dependency specifier for the referenced file.
  606. */
  607. specifier: string,
  608. |};
  609. export type Dependency =
  610. | ImportDependency
  611. | UrlDependency
  612. | FileDependency
  613. | GlobDependency;
  614. export type ImportDependency = {|
  615. type: "import",
  616. /**
  617. * The url of the string dependency.
  618. */
  619. url: string,
  620. /**
  621. * The media query for the string rule.
  622. */
  623. media: string | null,
  624. /**
  625. * The string rule.
  626. */
  627. supports: string | null,
  628. /**
  629. * The source location where the string rule was found.
  630. */
  631. loc: SourceLocation,
  632. /**
  633. * The placeholder that the import was replaced with.
  634. */
  635. placeholder: string,
  636. |};
  637. export type UrlDependency = {|
  638. type: "url",
  639. /**
  640. * The url of the dependency.
  641. */
  642. url: string,
  643. /**
  644. * The source location where the string was found.
  645. */
  646. loc: SourceLocation,
  647. /**
  648. * The placeholder that the url was replaced with.
  649. */
  650. placeholder: string,
  651. |};
  652. export type FileDependency = {|
  653. type: "file",
  654. filePath: string,
  655. |};
  656. export type GlobDependency = {|
  657. type: "glob",
  658. glob: string,
  659. |};
  660. export type SourceLocation = {|
  661. /**
  662. * The file path in which the dependency exists.
  663. */
  664. filePath: string,
  665. /**
  666. * The start location of the dependency.
  667. */
  668. start: Location,
  669. /**
  670. * The end location (inclusive) of the dependency.
  671. */
  672. end: Location,
  673. |};
  674. export type Location = {|
  675. /**
  676. * The line number (1-based).
  677. */
  678. line: number,
  679. /**
  680. * The column number (0-based).
  681. */
  682. column: number,
  683. |};
  684. export type ErrorLocation = {|
  685. ...$Exact<Location>,
  686. filename: string,
  687. |};
  688. /**
  689. * Compiles a CSS file, including optionally minifying and lowering syntax to the given
  690. * targets. A source map may also be generated, but this is not enabled by default.
  691. */
  692. declare export function transform<C: CustomAtRules>(
  693. options: TransformOptions<C>
  694. ): TransformResult;
  695. export type TransformAttributeOptions = {|
  696. /**
  697. * The filename in which the style attribute appeared. Used for error messages and dependencies.
  698. */
  699. filename?: string,
  700. /**
  701. * The source code to transform.
  702. */
  703. code: Uint8Array,
  704. /**
  705. * Whether to enable minification.
  706. */
  707. minify?: boolean,
  708. /**
  709. * The browser targets for the generated code.
  710. */
  711. targets?: Targets,
  712. /**
  713. * Whether to analyze string dependencies.
  714. * When enabled, string dependencies are replaced with hashed placeholders
  715. * that can be replaced with the final urls later (after bundling).
  716. * Dependencies are returned as part of the result.
  717. */
  718. analyzeDependencies?: boolean,
  719. /**
  720. * Whether to ignore invalid rules and declarations rather than erroring.
  721. * When enabled, warnings are returned, and the invalid rule or declaration is
  722. * omitted from the output code.
  723. */
  724. errorRecovery?: boolean,
  725. /**
  726. * An AST visitor object. This allows custom transforms or analysis to be implemented in JavaScript.
  727. * Multiple visitors can be composed into one using the string function.
  728. * For optimal performance, visitors should be as specific as possible about what types of values
  729. * they care about so that JavaScript has to be called as little as possible.
  730. */
  731. visitor?: Visitor<empty> | VisitorFunction<empty>,
  732. |};
  733. export type TransformAttributeResult = {|
  734. /**
  735. * The transformed code.
  736. */
  737. code: Uint8Array,
  738. /**
  739. * string dependencies, if enabled.
  740. */
  741. dependencies: Dependency[] | void,
  742. /**
  743. * Warnings that occurred during compilation.
  744. */
  745. warnings: Warning[],
  746. |};
  747. /**
  748. * Compiles a single CSS declaration list, such as an inline style attribute in HTML.
  749. */
  750. declare export function transformStyleAttribute(
  751. options: TransformAttributeOptions
  752. ): TransformAttributeResult;
  753. /**
  754. * Converts a browserslist result into targets that can be passed to lightningcss.
  755. * @param browserslist the result of calling string
  756. */
  757. declare export function browserslistToTargets(browserslist: string[]): Targets;
  758. /**
  759. * Bundles a CSS file and its dependencies, inlining @import rules.
  760. */
  761. declare export function bundle<C: CustomAtRules>(
  762. options: BundleOptions<C>
  763. ): TransformResult;
  764. /**
  765. * Bundles a CSS file and its dependencies asynchronously, inlining @import rules.
  766. */
  767. declare export function bundleAsync<C: CustomAtRules>(
  768. options: BundleAsyncOptions<C>
  769. ): Promise<TransformResult>;
  770. /**
  771. * Composes multiple visitor objects into a single one.
  772. */
  773. declare export function composeVisitors<C: CustomAtRules>(
  774. visitors: (Visitor<C> | VisitorFunction<C>)[]
  775. ): Visitor<C> | VisitorFunction<C>;