Skip to content
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

Support shakapacker #20

Merged
merged 1 commit into from
Nov 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
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning].

## [Unreleased]

### Added

- Support Shakapacker in the installation script. ([@skryukov])

### Fixed

- Fix installation script interactive mode. ([@skryukov])
- Fix installation script dependencies installation. ([@skryukov])

## [0.4.0] - 2024-11-03

### Added
Expand Down
6 changes: 6 additions & 0 deletions lib/generators/turbo_mount/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ def js_destination_root
def js_entrypoint
if vite?
js_file_path "entrypoints/application.js"
elsif shakapacker?
js_file_path "packs/application.js"
else
js_file_path "application.js"
end
Expand All @@ -41,6 +43,10 @@ def vite?
file?("config/vite.json") && Dir.glob(file_path("vite.config.*")).any?
end

def shakapacker?
file?("config/shakapacker.yml") || file?("config/webpacker.yml")
end

# Interactivity Helpers
def ask(*)
unless options[:interactive]
Expand Down
34 changes: 21 additions & 13 deletions lib/generators/turbo_mount/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class InstallGenerator < Rails::Generators::Base
enum: JSPackageManager.package_managers,
desc: "The package manager you want to use to install Turbo Mount"

class_option :interactive, type: :boolean, default: true,
desc: "Whether to prompt for optional installations"

class_option :verbose, type: :boolean, default: false,
desc: "Run the generator in verbose mode"

Expand All @@ -33,6 +36,7 @@ def install

package_manager.validate!

create_initializer
if package_manager.importmap?
install_importmap
else
Expand All @@ -44,43 +48,47 @@ def install

private

def install_nodejs
package_manager.add_dependencies("turbo-mount", FRAMEWORKS[framework][:npm_packages])

def create_initializer
say "Creating Turbo Mount initializer"
template "turbo-mount.js", js_file_path("turbo-mount.js")

needs_alias = shakapacker? || package_manager.importmap?
initializer_path = needs_alias ? "turbo-mount-initializer.js" : "turbo-mount.js"
initializer_import = needs_alias ? %(import "turbo-mount-initializer"\n) : %(import "./turbo-mount"\n)

template "turbo-mount.js", js_file_path(initializer_path)
begin
append_to_file js_entrypoint, %(import "./turbo-mount"\n)
append_to_file js_entrypoint, initializer_import
rescue
say 'Could not find the application entrypoint, please add `import "./turbo-mount"` manually.', :yellow
say "Could not find the application entrypoint, please add `#{initializer_import.strip}` manually.", :yellow
end
end

def install_nodejs
package_manager.add_dependencies("turbo-mount", FRAMEWORKS[framework]["npm_packages"])

warn_about_vite_plugin if vite?
end

def install_importmap
say "Creating Turbo Mount initializer"
template "turbo-mount.js", js_file_path("turbo-mount-initializer.js")
append_to_file "app/javascript/application.js", %(import "turbo-mount-initializer"\n)

say "Pinning Turbo Mount to the importmap"
append_to_file "config/importmap.rb", %(pin "turbo-mount", to: "turbo-mount.min.js"\n)
append_to_file "config/importmap.rb", %(pin "turbo-mount/#{framework}", to: "turbo-mount/#{framework}.min.js"\n)
append_to_file "config/importmap.rb", %(pin "turbo-mount-initializer"\n)

say "Pinning framework dependencies to the importmap"
package_manager.add_dependencies(FRAMEWORKS[framework][:pins])
package_manager.add_dependencies(FRAMEWORKS[framework]["pins"])
end

def warn_about_vite_plugin
say "Make sure to install and add #{FRAMEWORKS[framework][:vite_plugin]} to your Vite config", :yellow
say "Make sure to install and add #{FRAMEWORKS[framework]["vite_plugin"]} to your Vite config", :yellow
end

def package_manager
@package_manager ||= JSPackageManager.new(self)
end

def extension
FRAMEWORKS[framework][:extension]
FRAMEWORKS[framework]["extension"]
end

def framework
Expand Down
Empty file.
Empty file.
43 changes: 38 additions & 5 deletions spec/lib/generators/install/install_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@

subject(:generator) { run_generator(args) }

before do
prepare_destination
FileUtils.cp_r(Dir["spec/fixtures/with_vite/*"], destination_root) if package_manager != :importmap
FileUtils.cp_r(Dir["spec/fixtures/with_importmap/*"], destination_root) if package_manager == :importmap
end
before { prepare_destination }

context "with --package-manager=importmap" do
before { FileUtils.cp_r(Dir["spec/fixtures/with_importmap/*"], destination_root) }

context "with --framework=svelte" do
let(:framework) { :svelte }

Expand Down Expand Up @@ -106,7 +104,37 @@
end
end

context "with shakapacker" do
before { FileUtils.cp_r(Dir["spec/fixtures/with_shakapacker/*"], destination_root) }

let(:package_manager) { :npm }

context "with --framework=react" do
let(:framework) { :react }

it "builds the correct structure" do
expect { generator }.not_to raise_error

expect(destination_root).to(have_structure do
file("app/javascript/packs/application.js") do
contains('import "turbo-mount-initializer"')
end
file("app/javascript/turbo-mount-initializer.js") do
contains('import { registerComponent } from "turbo-mount/react"')
end
file("package.json") do
contains('"turbo-mount":')
contains('"react":')
contains('"react-dom":')
end
end)
end
end
end

context "with --package-manager=npm" do
before { FileUtils.cp_r(Dir["spec/fixtures/with_vite/*"], destination_root) }

let(:package_manager) { :npm }

context "with --framework=svelte" do
Expand All @@ -124,6 +152,7 @@
end
file("package.json") do
contains('"turbo-mount":')
contains('"svelte":')
end
end)
end
Expand All @@ -144,6 +173,7 @@
end
file("package.json") do
contains('"turbo-mount":')
contains('"svelte":')
end
end)
end
Expand All @@ -164,6 +194,8 @@
end
file("package.json") do
contains('"turbo-mount":')
contains('"react":')
contains('"react-dom":')
end
end)
end
Expand All @@ -184,6 +216,7 @@
end
file("package.json") do
contains('"turbo-mount":')
contains('"vue":')
end
end)
end
Expand Down