diff --git a/README.md b/README.md index 8661e83b..8cdd2dcd 100644 --- a/README.md +++ b/README.md @@ -57,190 +57,11 @@ An extremely fast CSS parser, transformer, and minifier written in Rust. Use it - Opt-in support for locally scoped CSS variables and other dashed identifiers. - `:local()` and `:global()` selectors - The `composes` property +- **Custom transforms** – The Lightning CSS visitor API can be used to implement custom transform plugins. ## Documentation -Lightning CSS can be used from [Parcel](https://parceljs.org), as a standalone library from JavaScript or Rust, using a standalone CLI, or wrapped as a plugin within any other tool. - -### From Node - -See the [TypeScript definitions](https://github.com/parcel-bundler/lightningcss/blob/master/node/index.d.ts) for full API docs. - -Here is a simple example that compiles the input CSS for Safari 13.2, and minifies the output. - -```js -const css = require('lightningcss'); - -let {code, map} = css.transform({ - filename: 'style.css', - code: Buffer.from('.foo { color: red }'), - minify: true, - sourceMap: true, - targets: { - // Semver versions are represented using a single 24-bit number, with one component per byte. - // e.g. to represent 13.2.0, the following could be used. - safari: (13 << 16) | (2 << 8) - } -}); -``` - -You can also convert the results of running `browserslist` into targets which can be passed to Lightning CSS: - -```js -const browserslist = require('browserslist'); -const css = require('lightningcss'); - -let targets = css.browserslistToTargets(browserslist('>= 0.25%')); -``` - -Bundling is also possible by using the `bundle` API. This processes `@import` rules and inlines them. This API requires filesystem access, so it does not accept `code` directly via the API. - -```js -let {code, map} = css.bundle({ - filename: 'style.css', - minify: true -}); -``` - -The `bundleAsync` API is an asynchronous version of `bundle`, which also accepts a custom `resolver` object. This allows you to provide custom JavaScript functions for resolving `@import` specifiers to file paths, and reading files from the file system (or another source). The `read` and `resolve` functions are both optional, and may either return a string synchronously, or a Promise for asynchronous resolution. - -```js -let {code, map} = await css.bundleAsync({ - filename: 'style.css', - minify: true, - resolver: { - read(filePath) { - return fs.readFileSync(filePath, 'utf8'); - }, - resolve(specifier, from) { - return path.resolve(path.dirname(from), specifier); - } - } -}); -``` - -Note that using a custom resolver can slow down bundling significantly, especially when reading files asynchronously. Use `readFileSync` rather than `readFile` if possible for better performance, or omit either of the methods if you don't need to override the default behavior. - -### From Rust - -See the Rust API docs on [docs.rs](https://docs.rs/lightningcss). - -### With Parcel - -Parcel includes Lightning CSS as the default CSS transformer. You should also add a `browserslist` property to your `package.json`, which defines the target browsers that your CSS will be compiled for. - -While Lightning CSS handles the most commonly used PostCSS plugins like `autoprefixer`, `postcss-preset-env`, and CSS modules, you may still need PostCSS for more custom plugins like TailwindCSS. If that's the case, your PostCSS config will be picked up automatically. You can remove the plugins listed above from your PostCSS config, and they'll be handled by Lightning CSS. - -You can also configure Lightning CSS in the `package.json` in the root of your project. Currently, three options are supported: `drafts`, which can be used to enable CSS nesting and custom media queries, `pseudoClasses`, which allows replacing some pseudo classes like `:focus-visible` with normal classes that can be applied via JavaScript (e.g. polyfills), and `cssModules`, which enables CSS modules globally rather than only for files ending in `.module.css`, or accepts an options object. - -```json -{ - "@parcel/transformer-css": { - "cssModules": true, - "drafts": { - "nesting": true, - "customMedia": true - }, - "pseudoClasses": { - "focusVisible": "focus-ring" - } - } -} -``` - -See the [Parcel docs](https://parceljs.org/languages/css) for more details. - -### From Deno or in browser - -The `lightningcss-wasm` package can be used in Deno or directly in browsers. This uses a WebAssembly build of Lightning CSS. Use `TextEncoder` and `TextDecoder` convert code from a string to a typed array and back. - -```js -import init, {transform} from 'https://unpkg.com/lightningcss-wasm'; - -await init(); - -let {code, map} = transform({ - filename: 'style.css', - code: new TextEncoder().encode('.foo { color: red }'), - minify: true, -}); - -console.log(new TextDecoder().decode(code)); -``` - -### With webpack - -css-minimizer-webpack-plugin has builtin support for Lightning CSS. Install Lightning CSS in your project, and configure the plugin as documented [in its README](https://github.com/webpack-contrib/css-minimizer-webpack-plugin#using-custom-minifier-lightningcss-previously-parcelcss). - -### From the CLI - -Lightning CSS includes a standalone CLI that can be used to compile, minify, and bundle CSS files. It can be used when you only need to compile CSS, and don't need more advanced functionality from a larger build tool such as code splitting and support for other languages. - -To use the CLI, install the `lightningcss-cli` package with an npm compatible package manager: - -```shell -npm install lightningcss-cli -``` - -Then, you can run the `lightningcss` command via `npx`, `yarn`, or by setting up a script in your package.json. - -```json -{ - "scripts": { - "build": "lightningcss --minify --nesting --bundle --targets '>= 0.25%' --sourcemap input.css -o output.css" - } -} -``` - -To see all of the available options, use the `--help` argument: - -```shell -npx lightningcss --help -``` - -#### Browserslist configuration - -If the `--browserslist` option is provided, then `lightningcss` finds browserslist configuration, -selects queries by environment and loads the resulting queries as targets. - -Configuration discovery and targets resolution is modeled after the original `browserslist` nodeJS package. -The configuration is resolved in the following order: - -- If a `BROWSERSLIST` environment variable is present, then load targets from its value. This is analog to the `--targets` CLI option. - _Example:_ `BROWSERSLIST="firefox ESR" lightningcss [OPTIONS] ` -- If a `BROWSERSLIST_CONFIG` environment variable is present, then resolve the file at the provided path. - Then parse and use targets from `package.json` or any browserslist configuration file pointed to by the environment variable. - _Example:_ `BROWSERSLIST_CONFIG="../config/browserslist" lightningcss [OPTIONS] ` -- If none of the above apply, then find, parse and use targets from the first `browserslist`, `.browserslistrc` - or `package.json` configuration file in any parent directory. - -Browserslist configuration files may contain sections denoted by angular brackets `[]`. -Use these to specify different targets for different environments. -Targets which are not placed in a section are added to `defaults` and used if no section matches the environment. - -_Example:_ - -``` -# Defaults, applied when no other section matches the provided environment. -firefox ESR - -[staging] -# Targets applied only to the staging environment. -samsung >= 4 -``` - -When using parsed configuration from `browserslist`, `.browserslistrc` or `package.json` configuration files, -the environment determined by - -- the `BROWSERSLIST_ENV` environment variable if present, -- otherwise the `NODE_ENV` environment variable if present, -- otherwise `production` is used. - -If no targets are found for the resulting environment, then the `defaults` configuration section is used. - -### Error recovery - -By default, Lightning CSS is strict, and will error when parsing an invalid rule or declaration. However, sometimes you may encounter a third party library that you can't easily modify, which unintentionally contains invalid syntax, or IE-specific hacks. In these cases, you can enable the `errorRecovery` option (or `--error-recovery` CLI flag). This will skip over invalid rules and declarations, omitting them in the output, and producing a warning instead of an error. You should also open an issue or PR to fix the issue in the library if possible. +Lightning CSS can be used from [Parcel](https://parceljs.org), as a standalone library from JavaScript or Rust, using a standalone CLI, or wrapped as a plugin within any other tool. See the [Lightning CSS website](https://lightningcss.dev/docs.html) for documentation. ## Benchmarks diff --git a/node/ast.d.ts b/node/ast.d.ts index eecaac3e..f3121024 100644 --- a/node/ast.d.ts +++ b/node/ast.d.ts @@ -6857,7 +6857,7 @@ export type DefaultAtRule = null; * * // Serialize it to a string. let res = stylesheet.to_css(PrinterOptions::default()).unwrap(); assert_eq!(res.code, ".foo, .bar {\n color: red;\n}\n"); ``` */ -export interface StyleSheetParser { +export interface StyleSheet { /** * A list of top-level rules within the style sheet. */ diff --git a/package.json b/package.json index 2e07558d..deefb376 100644 --- a/package.json +++ b/package.json @@ -48,25 +48,33 @@ "@mdn/browser-compat-data": "^5.1.6", "@napi-rs/cli": "^2.6.2", "autoprefixer": "^10.4.8", - "caniuse-lite": "^1.0.30001373", "codemirror": "^6.0.1", "cssnano": "^5.0.8", "esbuild": "^0.13.10", "flowgen": "^1.21.0", "jest-diff": "^27.4.2", "json-schema-to-typescript": "^11.0.2", + "markdown-it-anchor": "^8.6.6", + "markdown-it-prism": "^2.3.0", + "markdown-it-table-of-contents": "^0.6.0", "napi-wasm": "^1.0.1", "node-fetch": "^3.1.0", - "parcel": "^2.7.0", + "parcel": "^2.8.2", "patch-package": "^6.5.0", "path-browserify": "^1.0.1", "postcss": "^8.3.11", + "posthtml-include": "^1.7.4", + "posthtml-markdownit": "^1.3.1", + "posthtml-prism": "^1.0.4", "process": "^0.11.10", "puppeteer": "^12.0.1", - "sharp": "^0.29.1", + "sharp": "^0.31.1", "util": "^0.12.4", "uvu": "^0.5.6" }, + "resolutions": { + "lightningcss": "link:." + }, "scripts": { "prepare": "patch-package", "build": "node scripts/build.js && node scripts/build-flow.js", @@ -74,8 +82,8 @@ "prepublishOnly": "node scripts/build-flow.js", "wasm:build": "cargo build --target wasm32-unknown-unknown -p lightningcss_node && cp target/wasm32-unknown-unknown/debug/lightningcss_node.wasm wasm/. && node scripts/build-wasm.js", "wasm:build-release": "cargo build --target wasm32-unknown-unknown -p lightningcss_node --release && cp target/wasm32-unknown-unknown/release/lightningcss_node.wasm wasm/. && node scripts/build-wasm.js", - "website:start": "parcel website/index.html website/playground/index.html", - "website:build": "yarn wasm:build-release && parcel build website/index.html website/playground/index.html", + "website:start": "parcel 'website/*.html' website/playground/index.html", + "website:build": "yarn wasm:build-release && parcel build 'website/*.html' website/playground/index.html", "build-ast": "cargo run --example schema --features jsonschema && node scripts/build-ast.js", "test": "uvu node/test" } diff --git a/src/stylesheet.rs b/src/stylesheet.rs index 7b34809a..25e410e2 100644 --- a/src/stylesheet.rs +++ b/src/stylesheet.rs @@ -68,7 +68,10 @@ pub use crate::printer::PseudoClasses; #[cfg_attr( feature = "jsonschema", derive(schemars::JsonSchema), - schemars(bound = "T: schemars::JsonSchema, T::AtRule: schemars::JsonSchema") + schemars( + rename = "StyleSheet", + bound = "T: schemars::JsonSchema, T::AtRule: schemars::JsonSchema" + ) )] pub struct StyleSheet<'i, 'o, T: AtRuleParser<'i> = DefaultAtRuleParser> { /// A list of top-level rules within the style sheet. diff --git a/website/.posthtmlrc b/website/.posthtmlrc new file mode 100644 index 00000000..def9e84b --- /dev/null +++ b/website/.posthtmlrc @@ -0,0 +1,28 @@ +{ + "plugins": { + "posthtml-include": {}, + "posthtml-markdownit": { + "markdownit": { + "html": true + }, + "plugins": [ + { + "plugin": "markdown-it-anchor" + }, + { + "plugin": "markdown-it-table-of-contents", + "options": { + "containerHeaderHtml": "

On this page

", + "includeLevel": [ + 2, + 3 + ] + } + }, + { + "plugin": "markdown-it-prism" + } + ] + } + } +} \ No newline at end of file diff --git a/website/bundling.html b/website/bundling.html new file mode 100644 index 00000000..67be3901 --- /dev/null +++ b/website/bundling.html @@ -0,0 +1 @@ + diff --git a/website/css-modules.html b/website/css-modules.html new file mode 100644 index 00000000..b9b53a8c --- /dev/null +++ b/website/css-modules.html @@ -0,0 +1 @@ + diff --git a/website/docs.css b/website/docs.css new file mode 100644 index 00000000..d2a4bc48 --- /dev/null +++ b/website/docs.css @@ -0,0 +1,290 @@ +@import "synthwave.css"; + +html { + color-scheme: dark; + background: #111; + font-family: system-ui; + --gold: lch(80% 82.34 80.104); + --gold-text: lch(85% 82.34 80.104); + --gold-shadow: lch(80% 82.34 80.104 / .7); +} + +@font-face { + font-family:"din-1451-lt-pro-engschrift"; + src:url("https://use.typekit.net/af/7fa6e1/00000000000000007735bbcd/30/l?primer=388f68b35a7cbf1ee3543172445c23e26935269fadd3b392a13ac7b2903677eb&fvd=n4&v=3") format("woff2"),url("https://use.typekit.net/af/7fa6e1/00000000000000007735bbcd/30/d?primer=388f68b35a7cbf1ee3543172445c23e26935269fadd3b392a13ac7b2903677eb&fvd=n4&v=3") format("woff"),url("https://use.typekit.net/af/7fa6e1/00000000000000007735bbcd/30/a?primer=388f68b35a7cbf1ee3543172445c23e26935269fadd3b392a13ac7b2903677eb&fvd=n4&v=3") format("opentype"); + font-display:auto;font-style:normal;font-weight:400;font-stretch:normal; +} + +@font-face { + font-family:"urbane-rounded"; + src:url("https://use.typekit.net/af/916187/00000000000000007735bfa0/30/l?primer=81a69539b194230396845be9681d114557adfb35f4cccc679c164afb4aa47365&fvd=n6&v=3") format("woff2"),url("https://use.typekit.net/af/916187/00000000000000007735bfa0/30/d?primer=81a69539b194230396845be9681d114557adfb35f4cccc679c164afb4aa47365&fvd=n6&v=3") format("woff"),url("https://use.typekit.net/af/916187/00000000000000007735bfa0/30/a?primer=81a69539b194230396845be9681d114557adfb35f4cccc679c164afb4aa47365&fvd=n6&v=3") format("opentype"); + font-display:auto;font-style:normal;font-weight:600;font-stretch:normal; +} + +header { + max-width: 800px; + width: 100%; + margin: 0 auto; + padding: 50px 0; + font-size: 16px; + background: radial-gradient(closest-side, lch(80% 82.34 80.104 / .25), transparent); + display: grid; + column-gap: 30px; + grid-area: header; + grid-template-areas: "logo header" + "logo subheader" + ". links"; +} + +header svg { + filter: drop-shadow(0 0 5px var(--gold-shadow)) drop-shadow(0 0 15px var(--gold-shadow)); + grid-area: logo; + place-self: center end; + width: 50px; +} + +header svg .outer { + stroke-width: 30px; + stroke: var(--gold); +} + +header svg .inner { + fill: lch(100% 82.34 80.104); +} + +header .title { + font-family: urbane-rounded, ui-rounded; + font-size: 60px; + font-weight: 600; + -webkit-text-stroke: 2px var(--gold-text); + color: transparent; + filter: drop-shadow(0 0 3px var(--gold-shadow)) drop-shadow(0 0 10px var(--gold)); + margin: 0; + letter-spacing: -0.02em; + text-decoration: none; +} + +h1, h2, h3 { + font-family: urbane-rounded, ui-rounded; + font-weight: 600; + color: lch(65% 85 35); + margin: 2em 0 .5em 0; + letter-spacing: -0.02em; +} + +h1 { + margin-top: 0; +} + +header p { + grid-area: links; + margin: 0; +} + +header p a { + font-family: urbane-rounded, ui-rounded; + font-weight: 600; + font-size: 1em; + color: lch(90% 50.34 80.104); + filter: drop-shadow(0 0 8px lch(90% 50.34 80.104 / .7)); + text-decoration-color: lch(90% 50.34 80.104 / 0); + text-decoration-style: wavy; + text-decoration-thickness: 2px; + text-underline-offset: 2px; + text-decoration-skip-ink: none; + transition: text-decoration-color 150ms; +} + +header a:hover { + text-decoration-color: lch(90% 50.34 80.104); +} + +@media (width < 500px) { + header { + grid-template-areas: "logo" + "header" + "subheader" + "links"; + place-items: center; + text-align: center; + gap: 8px; + } + header .title { + font-size: 38px; + -webkit-text-stroke-width: 1.5px; + padding: 0; + } + + header h2 { + font-size: 14px; + } + + header p a { + font-size: 13px; + } + + header svg { + place-self: center; + } +} + +body { + --body-padding: 20px; + padding: 0 var(--body-padding); + margin: 0 auto; + width: fit-content; + display: grid; + grid-template-columns: 180px 1fr; + gap: 40px; + grid-template-areas: "header header" + "nav main" + "footer footer"; +} + +main { + max-width: 800px; + padding-right: 240px; + grid-area: main; + position: relative; +} + +p, li { + line-height: 1.5em; +} + +p:empty { + display: none; +} + +a { + color: lch(85% 58 205); +} + +nav { + grid-area: nav; + text-align: end; + padding-right: 20px; + border-right: 1px solid lch(90% 50.34 80.104 / .1); + height: fit-content; + position: sticky; + top: 40px; +} + +nav h3, +.table-of-contents h3 { + margin-top: 0; +} + +main > aside { + position: sticky; + top: 40px; +} + +.table-of-contents { + position: absolute; + left: 100%; + margin-left: 40px; + border-left: 1px solid lch(90% 50.34 80.104 / .1); + padding-left: 20px; + overflow: auto; + max-height: calc(100vh - 80px); +} + +.table-of-contents ul, +nav ul { + list-style: none; + padding-left: 2ch; +} + +.table-of-contents > ul { + margin: 0; + padding: 0; + width: 180px; +} + +nav > ul { + margin: 0; + padding: 0; +} + +.table-of-contents li, +nav li { + margin: 6px 0; + line-height: 1em; +} + +.table-of-contents a, +nav a { + color: lch(90% 50.34 80.104); + text-decoration: none; + font-family: urbane-rounded; + font-size: 14px; +} + +.table-of-contents a:hover, +.table-of-contents a[aria-current], +nav a:hover, +nav a[aria-current] { + color: var(--gold-text); +} + +a[aria-current] { + text-decoration: underline; +} + +@media (width < 1040px) { + .table-of-contents { + display: none; + } + + main { + padding-right: 0; + } +} + +@media (width < 600px) { + body { + grid-template-areas: "header" "nav" "main" "footer"; + grid-template-columns: 1fr; + } + + nav { + text-align: start; + border-right: none; + border-bottom: 1px solid lch(90% 50.34 80.104 / .1); + padding-bottom: 20px; + position: static; + } +} + +.warning { + border: 4px solid lch(70% 82.34 80.104); + background: lch(80% 82.34 80.104 / .15); + padding: 20px; + border-radius: 8px; + margin: 20px 0; +} + +.warning > :first-child { + margin-top: 0; +} + +.warning > :last-child { + margin-bottom: 0; +} + +.warning pre { + background: rgb(0 0 0 / .65); +} + +.warning :is(h1, h2, h3) { + color: white; +} + +footer { + font-size: 12px; + color: #666; + text-align: center; + padding-bottom: 20px; + grid-area: footer; +} diff --git a/website/docs.html b/website/docs.html new file mode 100644 index 00000000..f6d60f40 --- /dev/null +++ b/website/docs.html @@ -0,0 +1 @@ + diff --git a/website/docs.js b/website/docs.js new file mode 100644 index 00000000..802d17da --- /dev/null +++ b/website/docs.js @@ -0,0 +1,38 @@ +// Mark the current section in the table of contents with aria-current when scrolled into view. +let tocLinks = document.querySelectorAll('.table-of-contents a'); +let headers = new Map(); +for (let link of tocLinks) { + let headerId = link.hash.slice(1); + let header = document.getElementById(headerId); + headers.set(header, link); +} + +let intersectingHeaders = new Set(); +let observer = new IntersectionObserver(entries => { + for (let entry of entries) { + if (entry.isIntersecting) { + intersectingHeaders.add(entry.target); + } else { + intersectingHeaders.delete(entry.target); + } + } + + if (intersectingHeaders.size > 0) { + let current = document.querySelector('.table-of-contents a[aria-current]'); + if (current) { + current.removeAttribute('aria-current'); + } + let first; + for (let [header, link] of headers) { + if (intersectingHeaders.has(header)) { + first = link; + break; + } + } + first.setAttribute('aria-current', 'location'); + } +}); + +for (let header of headers.keys()) { + observer.observe(header); +} diff --git a/website/include/layout.html b/website/include/layout.html new file mode 100644 index 00000000..8172c877 --- /dev/null +++ b/website/include/layout.html @@ -0,0 +1,51 @@ + + + + + + {{ title }} – Lightning CSS + + + + + + + + + + + + + + + + +
+ + + + + Lightning CSS +

PlaygroundDocsRust docsnpmGitHub

+
+ +
+ +
+
+ Copyright © 2022 Devon Govett and Parcel Contributors. +
+ + + diff --git a/website/index.html b/website/index.html index f9b8cac2..fb42639d 100644 --- a/website/index.html +++ b/website/index.html @@ -28,13 +28,14 @@

Lightning CSS

An extremely fast CSS parser, transformer, bundler, and minifier.

-

GitHubRust docsnpmPlayground

+

PlaygroundDocsRust docsnpmGitHub

Light speed

Lightning CSS is over 100x faster than comparable JavaScript-based tools. It can minify over 2.7 million lines of code per second on a single thread.

Lightning CSS is written in Rust, a native systems programming language. It was built with performance in mind from the start, designed to make efficient use of memory, and limit AST passes.

+

Get started →