Skip to content

Support JS version of sass --embedded #256

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ext/sass/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
/dart-sass/
/embedded_sass.proto
/embedded_sass_pb.rb
/package-lock.json
/node_modules/
/protoc.exe
/ruby/
74 changes: 50 additions & 24 deletions ext/sass/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ end

CLEAN.include %w[protoc.exe ruby *.proto *.tar.gz *.zip]

CLOBBER.include %w[dart-sass cli.rb embedded_sass_pb.rb]
CLOBBER.include %w[dart-sass cli.rb embedded_sass_pb.rb node_modules package-lock.json]

file 'protoc.exe' do |t|
fetch(ENV.fetch('PROTOC_BIN') { SassConfig.default_protoc }, t.name)
chmod 'a+x', t.name
end

file 'dart-sass' do |t|
file 'dart-sass/sass' do |t|
raise if ENV.key?('DART_SASS')

gem_install 'sass-embedded', SassConfig.gem_version, SassConfig.gem_platform do |dir|
Expand All @@ -29,34 +29,58 @@ rescue StandardError
rm archive
end

file 'node_modules/sass' do
sh 'npm', 'install'
end

task 'dart-sass' do
Rake::Task['dart-sass/sass'].invoke
rescue NotImplementedError
Rake::Task['node_modules/sass'].invoke
end

file 'cli.rb' => %w[dart-sass] do |t|
require_relative '../../lib/sass/elf'

exe = 'dart-sass/sass'
exe = "#{exe}#{['', '.bat', '.exe'].find { |ext| File.exist?("#{exe}#{ext}") }}"
begin
exe = 'dart-sass/sass'
exe = "#{exe}#{['', '.bat', '.exe'].find { |ext| File.exist?("#{exe}#{ext}") }}"

raise "#{exe} not found" unless File.exist?(exe)
raise Errno::ENOENT, exe unless File.exist?(exe)

runtime = 'dart-sass/src/dart'
runtime = "#{runtime}#{['', '.exe'].find { |ext| File.exist?("#{runtime}#{ext}") }}"
snapshot = 'dart-sass/src/sass.snapshot'
runtime = 'dart-sass/src/dart'
runtime = "#{runtime}#{['', '.exe'].find { |ext| File.exist?("#{runtime}#{ext}") }}"
snapshot = 'dart-sass/src/sass.snapshot'

command = if File.exist?(runtime) && File.exist?(snapshot)
[runtime, snapshot]
else
[exe]
end
command = if File.exist?(runtime) && File.exist?(snapshot)
[runtime, snapshot]
else
[exe]
end

interpreter = File.open(command[0], 'rb') do |file|
Sass.const_get(:ELF).new(file).interpreter
rescue ArgumentError
nil
end
interpreter = File.open(command[0], 'rb') do |file|
Sass.const_get(:ELF).new(file).interpreter
rescue ArgumentError
nil
end

command_source = command.map do |argument|
"File.absolute_path('#{argument}', __dir__).freeze"
end.join(',
')
rescue Errno::ENOENT
package = 'node_modules/sass'

script = File.join(package, SassConfig.package_json(package)['bin']['sass'])

command_source = command.map do |argument|
"File.absolute_path('#{argument}', __dir__).freeze"
end.join(',
interpreter = nil

command_source = [
"'node'",
"File.absolute_path('#{script}', __dir__).freeze"
].join(',
')
end

if interpreter.nil?
File.write(t.name, <<~CLI_RB)
Expand Down Expand Up @@ -290,12 +314,14 @@ module SassConfig

module_function

def dart_sass_version
def package_json(path = '.')
require 'json'

spec = JSON.parse(File.read(File.absolute_path('package.json', __dir__)))
JSON.parse(File.read(File.absolute_path('package.json', path)))
end

spec['dependencies']['sass']
def dart_sass_version
package_json['dependencies']['sass']
end

def default_dart_sass
Expand Down
2 changes: 1 addition & 1 deletion lib/sass/compiler/host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def version_request

case version_response.implementation_name
when 'dart-sass'
info << '[Dart]'
info << (CLI::COMMAND.first == 'node' ? '[JavaScript]' : '[Dart]')
end

info
Expand Down