Skip to content

Commit

Permalink
handle math.div
Browse files Browse the repository at this point in the history
  • Loading branch information
AprilArcus committed Feb 22, 2022
1 parent b34765d commit 9e0111f
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions tasks/converter/less_conversion.rb
Expand Up @@ -173,6 +173,7 @@ def convert_to_scss(file)
file = replace_calculation_semantics(file)
file = replace_file_imports(file)
file = wrap_at_groups_with_at_root(file)
file = replace_division(file)
file
end

Expand All @@ -182,6 +183,26 @@ def wrap_at_groups_with_at_root(file)
}
end

# The `/` operator is not supported in dart-sass 1.33+, but
# sassc/libass do not support its replacement, `math.div()`.
def replace_division(less)
re = /(?<!\w)\(\s*([^(]+?)\s+\/\s+([^)]+?)\s*\)/
return less if less !~ re
# In the future, we could publish a sassc compatible build to gems
# and a dart-sass compatible build to npm, or we could develop a
# dart-sass wrapper gem of the kind envisioned by DHH
# https://discuss.rubyonrails.org/t/transition-to-the-sass-npm-gem/76566/7
# For now, stick to libsass compatible output.
dart_sass = false
if dart_sass
# use dart-sass's math.div() global function
"@use \"sass:math\";\n" + less.gsub(re, 'math.div(\1, \2)')
else # sassc / libsass
# multiply by the reciprocal
less.gsub re { "(#{$1} * #{1 / $2.to_f})" }
end
end

def sass_fn_exists(fn)
%Q{(#{fn}("") != unquote('#{fn}("")'))}
end
Expand Down

0 comments on commit 9e0111f

Please sign in to comment.