rspack_rails 0.3.0 → 0.4.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d38b564f0260e86ab95775b11d6435853d520922028c59c3acce6c7ad1af639b
4
- data.tar.gz: 062aff2e71411f542808ec77ed5054935040ec6018a1a3839ba374b2cfd10dc2
3
+ metadata.gz: d5c0f485458181960a59600c16b354bbb260d5856d37200ffaf416e4d6c88222
4
+ data.tar.gz: a06bf0ec56b0850342ea39c4d461f4d165e8b3c1f046c7c41d6149b5412c5fbe
5
5
  SHA512:
6
- metadata.gz: 0d1ab868d62a2ce92bb0cf1f330f4113e311658fea8e0174f50ef73c001fa7f67ccb580094bdb7f805606026536b7a3580ece740dd4f88d3b946baa32e84883a
7
- data.tar.gz: c919c924784fb9885e4b8c9b8ae5db4255475adb9fd915a71e1a7a882a20bd02d6cf6c0eb2120928d7c89a0b214676ec3ddf92b1e785f48a89059e5e9ed02bd5
6
+ metadata.gz: 70886ccbe82932f10548639cb3190b4a06ad032fa854c5677c87553c93d4a32feb4a9968a5b72881fa5cfb94833ca02994168a541771c95a33222d51422677f4
7
+ data.tar.gz: 989663562bc6cd2e8a72ef05872319117c5f21e9b43697fc57874ea6c725370a86118ec3e01bd950f854dddbc57d48a8e9f10b675f2ef0686c1126df93573203
data/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.4.0] - 2026-11-13
4
+
5
+ - `rspack:migrate`: moves an importmap-rails or jsbundling-rails (esbuild/webpack/rollup) app to rspack. Requires a clean git tree (or `FORCE=1`)
6
+ - `rspack.config.mjs` resolves bare imports from `app/javascript` (`resolve.modules`), so importmap-style `import "controllers/application"` keeps working
7
+ - `rspack:install:stimulus` also rewrites a bare `import "controllers"`
8
+ - Installers stop when a package install fails instead of carrying on
9
+
3
10
  ## [0.3.0] - 2026-11-13
4
11
 
5
12
  - Multiple entry points: every `app/javascript/entrypoints/*.{js,ts}` becomes a bundle named after the file; `application.ts` works as the main entry
data/README.md CHANGED
@@ -1,8 +1,24 @@
1
1
  # rspack_rails
2
2
 
3
- Bundle your Rails app's JavaScript with [Rspack](https://rspack.rs), the Rust-powered, webpack-compatible bundler.
3
+ [![Gem Version](https://img.shields.io/gem/v/rspack_rails)](https://rubygems.org/gems/rspack_rails)
4
+ [![CI](https://github.com/tungnt1203/rspack_rails/actions/workflows/main.yml/badge.svg)](https://github.com/tungnt1203/rspack_rails/actions/workflows/main.yml)
4
5
 
5
- rspack_rails follows the `jsbundling-rails` model: rspack writes bundles to `app/assets/builds`, and Propshaft (or Sprockets) serves and fingerprints them.
6
+ Bundle your Rails app's JavaScript and CSS with [Rspack](https://rspack.rs), the Rust-powered, webpack-compatible bundler, and get **hot module replacement** in development.
7
+
8
+ ![Editing CSS and a Stimulus controller while the page keeps its state](docs/hmr.gif)
9
+
10
+ *Above: CSS and a Stimulus controller are edited while the page is open. Both update in place, and the counter keeps its value because the page never reloads.*
11
+
12
+ - **One command setup**: `bin/rails rspack:install`, then `bin/dev`
13
+ - **HMR for JavaScript, CSS and Stimulus controllers**, with an automatic fallback to the asset pipeline when the dev server is not running
14
+ - **Stimulus autoloading**: drop a `*_controller.js` (or `.ts`) in `app/javascript/controllers`, no manifest to maintain
15
+ - **TypeScript and CSS modules** out of the box, through Rspack's built-in SWC and native CSS
16
+ - **Multiple entry points** from `app/javascript/entrypoints/`
17
+ - **`rspack:migrate`** moves an importmap or jsbundling app over in one command
18
+ - **`rspack:doctor`** checks Node.js, packages, config and layout, and says what to fix
19
+ - Works with **Propshaft and Sprockets**; CI runs a real app on Rails 7.2 and 8.1
20
+
21
+ rspack_rails follows the `jsbundling-rails` model: rspack writes bundles to `app/assets/builds`, and Propshaft (or Sprockets) serves and fingerprints them in production.
6
22
 
7
23
  ## Installation
8
24
 
@@ -19,6 +35,28 @@ The installer:
19
35
  - adds a `js:` process running the dev server to `Procfile.dev` and sets up `bin/dev`
20
36
  - installs `@rspack/cli`, `@rspack/core` and `@rspack/dev-server` with your package manager (bun, yarn, pnpm or npm, detected from the lockfile)
21
37
 
38
+ ## Migrating from importmap or jsbundling
39
+
40
+ ```bash
41
+ git commit -am "Before rspack" # rspack:migrate refuses to run on a dirty tree (FORCE=1 to override)
42
+ bin/rails rspack:migrate
43
+ git diff # review
44
+ bin/dev
45
+ ```
46
+
47
+ **From importmap-rails**
48
+ - Every `pin` in `config/importmap.rb` becomes an npm dependency. Versions recorded by importmap (`# @1.2.3` or jspm URLs) are kept. Gem-served pins such as Turbo install the latest npm release.
49
+ - `javascript_importmap_tags` is replaced with `rspack_javascript_tag`, and `import "controllers"` with the Stimulus autoloader.
50
+ - `config/importmap.rb`, `bin/importmap`, vendored packages, the `importmap-rails` gem, `stale_when_importmap_changes` and the `bin/importmap audit` CI step are removed.
51
+ - Bare imports of app code (`import "controllers/application"`) keep working through `resolve.modules`.
52
+
53
+ **From jsbundling-rails** (esbuild, webpack, rollup)
54
+ - The `jsbundling-rails` gem, the old bundler package and its config file are removed. `Procfile.dev` runs the rspack dev server.
55
+ - `javascript_include_tag "application"` becomes `rspack_javascript_tag`, and the generated `controllers/index.js` gives way to the autoloader.
56
+ - Extra entry files in the root of `app/javascript/` are listed so you can move them to `app/javascript/entrypoints/`.
57
+
58
+ The task finishes by listing any remaining lines that mention importmap or jsbundling. Shakapacker/Webpacker apps are not supported yet.
59
+
22
60
  ## Usage
23
61
 
24
62
  Run `bin/dev` to start Rails and the rspack dev server (with hot module replacement) together.
@@ -112,6 +150,7 @@ npm run typecheck
112
150
  |---|---|
113
151
  | `bin/rails rspack:install:stimulus` | Sets up Stimulus with controller autoloading |
114
152
  | `bin/rails rspack:install:typescript` | Adds tsconfig.json, typescript and a `typecheck` script |
153
+ | `bin/rails rspack:migrate` | Moves an importmap or jsbundling app to rspack |
115
154
  | `bin/rails rspack:doctor` | Checks Node.js, packages, config, layout, Procfile and dev server |
116
155
  | `bin/rails rspack:build` | Installs JS dependencies, then runs `build` |
117
156
  | `bin/rails rspack:clobber` | Removes rspack output from `app/assets/builds` |
@@ -143,6 +182,7 @@ bin/setup
143
182
  bundle exec rake # specs + rubocop
144
183
  bin/integration # new Rails app + every installer, build, doctor and precompile
145
184
  bin/integration "~> 7.2.0" # against a specific Rails version
185
+ bin/integration-migrate importmap # rails new + rspack:migrate (or: jsbundling)
146
186
  ```
147
187
 
148
188
  ## License
@@ -68,6 +68,6 @@ unless File.exist?("bin/dev") && File.read("bin/dev").include?("Procfile.dev")
68
68
  end
69
69
 
70
70
  say "Installing @rspack/cli, @rspack/core and @rspack/dev-server"
71
- run pm.add_dev_command("@rspack/cli", "@rspack/core", "@rspack/dev-server")
71
+ run pm.add_dev_command("@rspack/cli", "@rspack/core", "@rspack/dev-server"), abort_on_failure: true
72
72
 
73
73
  say "rspack_rails installed. Run bin/dev to start developing.", :green
@@ -0,0 +1,103 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Rails application template run by `bin/rails rspack:migrate`.
4
+ # Moves an app from importmap-rails or jsbundling-rails to rspack_rails.
5
+ require "json"
6
+
7
+ migration = RspackRails::Migration.new(Rails.root)
8
+ pm = RspackRails::PackageManager.detect(Rails.root)
9
+ layout = "app/views/layouts/application.html.erb"
10
+ stimulus = File.exist?("app/javascript/controllers/application.js")
11
+
12
+ def remove_gem(name)
13
+ gem_line = /^[ \t]*gem[ \t]+["']#{Regexp.escape(name)}["'].*\n/
14
+ gsub_file "Gemfile", /^[ \t]*#.*\n(?=#{gem_line.source})/, "" # the comment describing it
15
+ gsub_file "Gemfile", gem_line, ""
16
+ end
17
+
18
+ case migration.source
19
+ when :rspack
20
+ say "This app already uses rspack_rails. Run `bin/rails rspack:doctor` to check it.", :green
21
+ exit
22
+ when :shakapacker
23
+ say "Shakapacker/Webpacker apps are not supported by rspack:migrate yet.", :red
24
+ exit 1
25
+ when :none
26
+ say "No importmap-rails or jsbundling-rails setup found. Running rspack:install instead."
27
+ when :importmap
28
+ say "Migrating from importmap-rails"
29
+ packages = migration.npm_install_args
30
+ vendored = migration.vendored_files
31
+
32
+ gsub_file layout, /^\s*<%= javascript_importmap_tags.*%>\n/, "" if File.exist?(layout)
33
+ if File.exist?("app/assets/config/manifest.js")
34
+ gsub_file "app/assets/config/manifest.js", %r{^//= link_tree \.\./\.\./javascript .js\n}, ""
35
+ gsub_file "app/assets/config/manifest.js", %r{^//= link_tree \.\./\.\./\.\./vendor/javascript .js\n}, ""
36
+ end
37
+ if File.exist?("app/javascript/application.js")
38
+ gsub_file "app/javascript/application.js", %r{^// Configure your import map in config/importmap.rb.*\n}, ""
39
+ end
40
+ Dir.glob("app/controllers/**/*.rb").each do |controller|
41
+ next unless File.read(controller).include?("stale_when_importmap_changes")
42
+
43
+ gsub_file controller, /^[ \t]*# Changes to the importmap will invalidate the etag.*\n/, ""
44
+ gsub_file controller, /^[ \t]*stale_when_importmap_changes.*\n/, ""
45
+ end
46
+ gsub_file "config/ci.rb", %r{^[ \t]*step .*bin/importmap audit.*\n}, "" if File.exist?("config/ci.rb")
47
+ remove_file "config/importmap.rb"
48
+ remove_file "bin/importmap"
49
+ vendored.each { |path| remove_file path }
50
+ remove_gem "importmap-rails"
51
+ when :jsbundling
52
+ say "Migrating from jsbundling-rails"
53
+ bundlers = migration.bundler_packages
54
+ migration.bundler_configs.each { |path| remove_file path }
55
+ gsub_file "Procfile.dev", /^js: .*\n/, "js: #{pm.run_command("dev")}\n" if File.exist?("Procfile.dev")
56
+ remove_gem "jsbundling-rails"
57
+ end
58
+
59
+ apply "#{__dir__}/install.rb"
60
+
61
+ if migration.source == :importmap && packages.any?
62
+ say "Installing packages that were pinned in config/importmap.rb: #{packages.join(", ")}"
63
+ run pm.add_command(*packages), abort_on_failure: true
64
+ end
65
+
66
+ if migration.source == :jsbundling && bundlers.any?
67
+ say "Removing the previous bundler: #{bundlers.join(", ")}"
68
+ package = JSON.parse(File.read("package.json"))
69
+ bundlers.each do |name|
70
+ package["dependencies"]&.delete(name)
71
+ package["devDependencies"]&.delete(name)
72
+ end
73
+ File.write("package.json", "#{JSON.pretty_generate(package)}\n")
74
+ end
75
+
76
+ apply "#{__dir__}/stimulus.rb" if stimulus
77
+
78
+ controllers_index = "app/javascript/controllers/index.js"
79
+ if migration.source == :importmap && File.exist?(controllers_index) &&
80
+ File.read(controllers_index).include?("@hotwired/stimulus-loading")
81
+ remove_file controllers_index # replaced by controllers/autoload.js
82
+ end
83
+
84
+ if migration.source == :jsbundling
85
+ migration.extra_root_entries.each do |path|
86
+ say "#{path} was an entry point under jsbundling. Move it to app/javascript/entrypoints/ " \
87
+ "so rspack builds it.", :yellow
88
+ end
89
+ end
90
+
91
+ leftovers = Dir.glob("{app,config,lib}/**/*.{rb,erb,js,ts,yml}").flat_map do |path|
92
+ File.readlines(path).each_with_index.filter_map do |line, index|
93
+ next if line.strip.start_with?("#", "//", "<%#")
94
+
95
+ "#{path}:#{index + 1}: #{line.strip}" if line.match?(/importmap|jsbundling/i)
96
+ end
97
+ end
98
+ if leftovers.any?
99
+ say "These lines still mention importmap/jsbundling and may need a manual change:", :yellow
100
+ leftovers.each { |line| say " #{line}", :yellow }
101
+ end
102
+
103
+ say "Migration finished. Review the changes with `git diff`, then run `bin/rails rspack:doctor`.", :green
@@ -47,6 +47,9 @@ export default defineConfig({
47
47
  },
48
48
  resolve: {
49
49
  extensions: ["...", ".ts"],
50
+ // Bare imports of app code work as they did with importmap
51
+ // (import "controllers/application", import "lib/utils").
52
+ modules: ["node_modules", path.resolve(root, "app/javascript")],
50
53
  },
51
54
  module: {
52
55
  rules: [
@@ -16,9 +16,9 @@ end
16
16
 
17
17
  if !File.exist?(entry)
18
18
  create_file entry, %(import "./controllers/autoload"\n)
19
- elsif File.read(entry).match?(%r{^import ["']\./controllers(/index(\.js)?)?["'];?$})
20
- say "Replacing the controllers/index.js import with controllers/autoload"
21
- gsub_file entry, %r{^import ["']\./controllers(/index(\.js)?)?["'];?$}, %(import "./controllers/autoload")
19
+ elsif File.read(entry).match?(%r{^import ["'](?:\./)?controllers(/index(\.js)?)?["'];?$})
20
+ say "Replacing the controllers import with controllers/autoload"
21
+ gsub_file entry, %r{^import ["'](?:\./)?controllers(/index(\.js)?)?["'];?$}, %(import "./controllers/autoload")
22
22
  elsif !File.read(entry).include?("./controllers/autoload")
23
23
  append_to_file entry, %(import "./controllers/autoload"\n)
24
24
  end
@@ -28,6 +28,6 @@ if File.exist?("app/javascript/controllers/index.js")
28
28
  end
29
29
 
30
30
  say "Installing @hotwired/stimulus"
31
- run pm.add_command("@hotwired/stimulus")
31
+ run pm.add_command("@hotwired/stimulus"), abort_on_failure: true
32
32
 
33
33
  say "Stimulus installed. Controllers in app/javascript/controllers are registered automatically.", :green
@@ -14,6 +14,6 @@ package = JSON.parse(File.read("package.json"))
14
14
  File.write("package.json", "#{JSON.pretty_generate(package)}\n")
15
15
 
16
16
  say "Installing typescript"
17
- run pm.add_dev_command("typescript")
17
+ run pm.add_dev_command("typescript"), abort_on_failure: true
18
18
 
19
19
  say "TypeScript ready. Rspack compiles .ts files; run `#{pm.run_command("typecheck")}` to type-check.", :green
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "pathname"
5
+
6
+ module RspackRails
7
+ # Detects how an app bundles JavaScript today and works out what
8
+ # `rspack:migrate` has to change. Pure inspection: nothing here writes files.
9
+ class Migration
10
+ Pin = Struct.new(:name, :to, :version, keyword_init: true)
11
+
12
+ # importmap-only helpers with no npm equivalent.
13
+ IMPORTMAP_ONLY = %w[@hotwired/stimulus-loading].freeze
14
+
15
+ BUNDLER_PACKAGES = %w[esbuild webpack webpack-cli rollup @rollup/plugin-node-resolve
16
+ @rollup/plugin-commonjs @rollup/plugin-terser].freeze
17
+ BUNDLER_CONFIGS = %w[webpack.config.js rollup.config.js rollup.config.mjs].freeze
18
+
19
+ attr_reader :root
20
+
21
+ def initialize(root)
22
+ @root = Pathname(root)
23
+ end
24
+
25
+ # :rspack, :shakapacker, :importmap, :jsbundling or :none
26
+ def source
27
+ @source ||= if file?("rspack.config.mjs") then :rspack
28
+ elsif file?("config/shakapacker.yml") || file?("config/webpacker.yml") then :shakapacker
29
+ elsif file?("config/importmap.rb") then :importmap
30
+ elsif jsbundling? then :jsbundling
31
+ else
32
+ :none
33
+ end
34
+ end
35
+
36
+ def pins
37
+ @pins ||= parse_pins(read("config/importmap.rb").to_s)
38
+ end
39
+
40
+ # { "package" => "version or nil" } for every pin that comes from npm. The version is
41
+ # known only when importmap recorded it (vendored "# @1.2.3" pins or jspm URLs); gem-served
42
+ # pins such as turbo-rails install the latest npm release.
43
+ def npm_packages
44
+ pins.reject { |pin| local_pin?(pin) || IMPORTMAP_ONLY.include?(pin.name) }
45
+ .to_h { |pin| [package_name(pin.name), pin.version] }
46
+ end
47
+
48
+ # The argument list for `npm install` / `yarn add`, versions pinned when known.
49
+ def npm_install_args
50
+ npm_packages.map { |name, version| version ? "#{name}@#{version}" : name }
51
+ end
52
+
53
+ # Vendored copies of packages that now come from npm.
54
+ def vendored_files
55
+ pins.reject { |pin| local_pin?(pin) }.filter_map do |pin|
56
+ path = "vendor/javascript/#{pin.to || "#{pin.name}.js"}"
57
+ path if file?(path)
58
+ end
59
+ end
60
+
61
+ def bundler_packages
62
+ dev = package_json.fetch("devDependencies", {}).keys + package_json.fetch("dependencies", {}).keys
63
+ dev & BUNDLER_PACKAGES
64
+ end
65
+
66
+ def bundler_configs
67
+ BUNDLER_CONFIGS.select { |path| file?(path) }
68
+ end
69
+
70
+ # jsbundling's esbuild default builds every file in app/javascript; rspack
71
+ # only builds application.* and entrypoints/*.
72
+ def extra_root_entries
73
+ Dir.glob("app/javascript/*.{js,ts}", base: root).sort.grep_v(%r{/application\.[jt]s\z})
74
+ end
75
+
76
+ def parse_pins(source)
77
+ source.each_line.filter_map do |line|
78
+ match = line.match(/^\s*pin\s+["']([^"']+)["'](.*)$/)
79
+ next unless match
80
+
81
+ rest = match[2]
82
+ to = rest[/to:\s*["']([^"']+)["']/, 1]
83
+ version = rest[/#\s*@(\S+)/, 1] || to.to_s[%r{npm:(?:@[^/]+/)?[^@/]+@([^/]+)/}, 1]
84
+ Pin.new(name: match[1], to: to, version: version)
85
+ end
86
+ end
87
+
88
+ private
89
+
90
+ def jsbundling?
91
+ read("Gemfile").to_s.match?(/^\s*gem\s+["']jsbundling-rails["']/) ||
92
+ package_json.dig("scripts", "build").to_s.match?(/\b(esbuild|webpack|rollup|bun build)\b/)
93
+ end
94
+
95
+ # A pin is app code when it, or the file it points to, lives in app/javascript.
96
+ def local_pin?(pin)
97
+ return true if pin.name == "application"
98
+
99
+ target = pin.to || "#{pin.name}.js"
100
+ return false if target.match?(%r{\Ahttps?://})
101
+
102
+ file?("app/javascript/#{target}")
103
+ end
104
+
105
+ # "@rails/actioncable/src" -> "@rails/actioncable", "lodash/debounce" -> "lodash"
106
+ def package_name(specifier)
107
+ parts = specifier.split("/")
108
+ specifier.start_with?("@") ? parts.first(2).join("/") : parts.first
109
+ end
110
+
111
+ def package_json
112
+ @package_json ||= file?("package.json") ? JSON.parse(read("package.json")) : {}
113
+ rescue JSON::ParserError
114
+ @package_json = {}
115
+ end
116
+
117
+ def file?(path)
118
+ root.join(path).file?
119
+ end
120
+
121
+ def read(path)
122
+ root.join(path).read if file?(path)
123
+ end
124
+ end
125
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RspackRails
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/rspack_rails.rb CHANGED
@@ -5,6 +5,7 @@ require_relative "rspack_rails/package_manager"
5
5
  require_relative "rspack_rails/configuration"
6
6
  require_relative "rspack_rails/dev_server"
7
7
  require_relative "rspack_rails/entrypoints"
8
+ require_relative "rspack_rails/migration"
8
9
 
9
10
  # Rspack integration for Rails: rake tasks, installer and view helpers.
10
11
  module RspackRails
@@ -24,6 +24,20 @@ namespace :rspack do
24
24
  end
25
25
  end
26
26
 
27
+ desc "Migrate from importmap-rails or jsbundling-rails to rspack (FORCE=1 to skip the clean git check)"
28
+ task :migrate do
29
+ dirty = system("git", "rev-parse", "--is-inside-work-tree", out: File::NULL, err: File::NULL) &&
30
+ !`git status --porcelain`.strip.empty?
31
+ if dirty && ENV["FORCE"].to_s.empty?
32
+ raise RspackRails::Error,
33
+ "rspack:migrate removes and rewrites files. Commit or stash your changes first, or run with FORCE=1."
34
+ end
35
+
36
+ template = File.expand_path("../install/migrate.rb", __dir__)
37
+ system(RbConfig.ruby, "./bin/rails", "app:template", "LOCATION=#{template}") ||
38
+ raise(RspackRails::Error, "rspack:migrate failed")
39
+ end
40
+
27
41
  desc "Check the rspack setup of this app"
28
42
  task doctor: :environment do
29
43
  require "rspack_rails/doctor"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspack_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tungnt
@@ -45,6 +45,7 @@ files:
45
45
  - lib/install/application.js
46
46
  - lib/install/dev
47
47
  - lib/install/install.rb
48
+ - lib/install/migrate.rb
48
49
  - lib/install/package.json
49
50
  - lib/install/rspack.config.mjs
50
51
  - lib/install/stimulus.rb
@@ -59,6 +60,7 @@ files:
59
60
  - lib/rspack_rails/doctor.rb
60
61
  - lib/rspack_rails/entrypoints.rb
61
62
  - lib/rspack_rails/helper.rb
63
+ - lib/rspack_rails/migration.rb
62
64
  - lib/rspack_rails/node_version.rb
63
65
  - lib/rspack_rails/package_manager.rb
64
66
  - lib/rspack_rails/railtie.rb