lightningcss_rb 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LightningcssRb
4
+ class UnknownError < Error
5
+ attr_reader :context
6
+
7
+ def initialize(message, context = nil)
8
+ super(message)
9
+
10
+ @context = context
11
+ end
12
+
13
+ def pretty_print
14
+ <<~PP
15
+ #{self.class.name}: #{message}
16
+ #{JSON.pretty_generate(context)}
17
+ PP
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LightningcssRb
4
+ VERSION = "0.0.1"
5
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require_relative "lightningcss_rb/version"
5
+ require_relative "lightningcss_rb/error"
6
+ require_relative "lightningcss_rb/unknown_error"
7
+ require_relative "lightningcss_rb/ast"
8
+
9
+ # Load the native extension. Precompiled gems ship a build per Ruby ABI under
10
+ # lib/lightningcss_rb/<major.minor>/; source installs compile to lib/lightningcss_rb/.
11
+ begin
12
+ RUBY_VERSION =~ /(\d+\.\d+)/
13
+ require_relative "lightningcss_rb/#{Regexp.last_match(1)}/lightningcss_rb"
14
+ rescue LoadError
15
+ require_relative "lightningcss_rb/lightningcss_rb"
16
+ end
17
+
18
+ # Ruby bindings for the LightningCSS parser, transformer, and minifier.
19
+ #
20
+ # The native extension exposes the underlying LightningCSS AST as JSON; the
21
+ # methods here wrap those primitives with a friendlier Ruby interface.
22
+ module LightningcssRb
23
+ # Error is defined above so it is available before ast.rb loads.
24
+
25
+ module_function
26
+
27
+ # Parse CSS into the LightningCSS AST as a Ruby Hash (with symbol keys).
28
+ #
29
+ # @param css [String] the CSS source to parse
30
+ # @return [Hash] the parsed stylesheet AST
31
+ # @raise [LightningcssRb::Error] if the CSS cannot be parsed
32
+ def parse(css)
33
+ JSON.parse(_parse(css), symbolize_names: true)
34
+ end
35
+
36
+ # Serialize an AST (as returned by {parse}) back to CSS.
37
+ #
38
+ # @param ast [Hash, String] an AST Hash from {parse}, or a raw AST JSON String
39
+ # @param minify [Boolean] remove whitespace and shorten the output
40
+ # @return [String] the serialized CSS
41
+ # @raise [LightningcssRb::Error] if the AST cannot be serialized
42
+ def to_css(ast, minify: false)
43
+ json = ast.is_a?(String) ? ast : JSON.generate(ast)
44
+ _to_css(json, minify)
45
+ end
46
+
47
+ # Parse, optimize, and re-print CSS in one step.
48
+ #
49
+ # @param css [String] the CSS source
50
+ # @param minify [Boolean] remove whitespace and shorten the output
51
+ # @return [String] the transformed CSS
52
+ # @raise [LightningcssRb::Error] if the CSS cannot be processed
53
+ def transform(css, minify: true)
54
+ _transform(css, minify)
55
+ end
56
+
57
+ # Parse, optimize, and minify CSS. Shorthand for `transform(css, minify: true)`.
58
+ #
59
+ # @param css [String] the CSS source
60
+ # @return [String] the minified CSS
61
+ # @raise [LightningcssRb::Error] if the CSS cannot be processed
62
+ def minify(css)
63
+ _transform(css, true)
64
+ end
65
+ end
data/lib/tasks/.keep ADDED
File without changes
@@ -0,0 +1,16 @@
1
+ module LightningcssRb
2
+ VERSION: String
3
+
4
+ class Error < StandardError
5
+ end
6
+
7
+ def self.parse: (String css) -> Hash[Symbol, untyped]
8
+ def self.to_css: (Hash[Symbol, untyped] | String ast, ?minify: bool) -> String
9
+ def self.transform: (String css, ?minify: bool) -> String
10
+ def self.minify: (String css) -> String
11
+
12
+ # Native primitives defined by the extension.
13
+ def self._parse: (String css) -> String
14
+ def self._to_css: (String ast_json, bool minify) -> String
15
+ def self._transform: (String css, bool minify) -> String
16
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lightningcss_rb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Marc Heiligers
8
+ bindir: exe
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rb_sys
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: 0.9.91
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: 0.9.91
26
+ description: lightningcss_rb wraps the LightningCSS Rust library via a native extension,
27
+ exposing CSS parsing (to an AST), transformation, and minification to Ruby.
28
+ email:
29
+ - marc@fascination.works
30
+ executables: []
31
+ extensions:
32
+ - ext/lightningcss_rb/extconf.rb
33
+ extra_rdoc_files: []
34
+ files:
35
+ - CHANGELOG.md
36
+ - Cargo.lock
37
+ - Cargo.toml
38
+ - LICENSE.txt
39
+ - NOTICE.md
40
+ - README.md
41
+ - Rakefile
42
+ - ext/lightningcss_rb/Cargo.toml
43
+ - ext/lightningcss_rb/extconf.rb
44
+ - ext/lightningcss_rb/src/lib.rs
45
+ - lib/lightningcss_rb.rb
46
+ - lib/lightningcss_rb/ast.rb
47
+ - lib/lightningcss_rb/ast/selectors.rb
48
+ - lib/lightningcss_rb/ast/selectors/attribute.rb
49
+ - lib/lightningcss_rb/ast/selectors/base.rb
50
+ - lib/lightningcss_rb/ast/selectors/class_selector.rb
51
+ - lib/lightningcss_rb/ast/selectors/combinator.rb
52
+ - lib/lightningcss_rb/ast/selectors/id.rb
53
+ - lib/lightningcss_rb/ast/selectors/namespace.rb
54
+ - lib/lightningcss_rb/ast/selectors/nesting.rb
55
+ - lib/lightningcss_rb/ast/selectors/pseudo_class.rb
56
+ - lib/lightningcss_rb/ast/selectors/pseudo_element.rb
57
+ - lib/lightningcss_rb/ast/selectors/type_selector.rb
58
+ - lib/lightningcss_rb/ast/selectors/universal.rb
59
+ - lib/lightningcss_rb/error.rb
60
+ - lib/lightningcss_rb/unknown_error.rb
61
+ - lib/lightningcss_rb/version.rb
62
+ - lib/tasks/.keep
63
+ - sig/lightningcss_rb.rbs
64
+ homepage: https://github.com/marcheiligers/lightningcss_rb
65
+ licenses:
66
+ - MIT
67
+ metadata:
68
+ allowed_push_host: https://rubygems.org
69
+ homepage_uri: https://github.com/marcheiligers/lightningcss_rb
70
+ source_code_uri: https://github.com/marcheiligers/lightningcss_rb
71
+ changelog_uri: https://github.com/marcheiligers/lightningcss_rb/blob/main/CHANGELOG.md
72
+ rubygems_mfa_required: 'true'
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 3.4.0
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: 3.3.11
86
+ requirements: []
87
+ rubygems_version: 4.0.6
88
+ specification_version: 4
89
+ summary: Ruby bindings for LightningCSS, a fast CSS parser, transformer, and minifier.
90
+ test_files: []