jsbundling-rails 1.1.2 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -8
- data/lib/install/Procfile.dev +1 -1
- data/lib/install/bun/Procfile.dev +2 -0
- data/lib/install/bun/bun.config.js +37 -0
- data/lib/install/bun/install.rb +28 -0
- data/lib/install/dev +3 -0
- data/lib/install/install.rb +0 -10
- data/lib/install/install_node.rb +9 -0
- data/lib/jsbundling/version.rb +1 -1
- data/lib/tasks/jsbundling/build.rake +34 -13
- data/lib/tasks/jsbundling/install.rake +13 -3
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d27f6cc62feb84d7b81b8eba5aa68fec1232775b88ca5d16b6b2a5d2efb0183d
|
4
|
+
data.tar.gz: 1d5e167c6802db6fe99ce8df513313597f42d586bd19bed864645e3db4682728
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a1f15e3bfe2fbfdf113990d126cc1e54e494e2666acaedd4969ee590446c1e4f5aa7395dd4da537e4d8fef7ef0c3e52a27513dc184b9950d821f98d942458081
|
7
|
+
data.tar.gz: 8789b31503404c6f88fea2cf98517e01e31126004d061373242730a1d9de3dcc24bb470577cb6586117115ff79da63f37e7e51dd0d751fa9b37479f4a4048113
|
data/README.md
CHANGED
@@ -1,36 +1,41 @@
|
|
1
1
|
# JavaScript Bundling for Rails
|
2
2
|
|
3
|
-
Use [esbuild](https://esbuild.github.io), [rollup.js](https://rollupjs.org), or [Webpack](https://webpack.js.org) to bundle your JavaScript, then deliver it via the asset pipeline in Rails. This gem provides installers to get you going with the bundler of your choice in a new Rails application, and a convention to use `app/assets/builds` to hold your bundled output as artifacts that are not checked into source control (the installer adds this directory to `.gitignore` by default).
|
3
|
+
Use [Bun](https://bun.sh), [esbuild](https://esbuild.github.io), [rollup.js](https://rollupjs.org), or [Webpack](https://webpack.js.org) to bundle your JavaScript, then deliver it via the asset pipeline in Rails. This gem provides installers to get you going with the bundler of your choice in a new Rails application, and a convention to use `app/assets/builds` to hold your bundled output as artifacts that are not checked into source control (the installer adds this directory to `.gitignore` by default).
|
4
4
|
|
5
5
|
You develop using this approach by running the bundler in watch mode in a terminal with `yarn build --watch` (and your Rails server in another, if you're not using something like [puma-dev](https://github.com/puma/puma-dev)). You can also use `./bin/dev`, which will start both the Rails server and the JS build watcher (along with a CSS build watcher, if you're also using `cssbundling-rails`).
|
6
6
|
|
7
7
|
Whenever the bundler detects changes to any of the JavaScript files in your project, it'll bundle `app/javascript/application.js` into `app/assets/builds/application.js` (and all other entry points configured). You can refer to the build output in your layout using the standard asset pipeline approach with `<%= javascript_include_tag "application", defer: true %>`.
|
8
8
|
|
9
|
-
When you deploy your application to production, the `javascript:build` task attaches to the `assets:precompile` task to ensure that all your package dependencies from `package.json` have been installed via yarn, and then runs
|
9
|
+
When you deploy your application to production, the `javascript:build` task attaches to the `assets:precompile` task to ensure that all your package dependencies from `package.json` have been installed via your javascript package manager (bun or yarn), and then runs the build script defined in `package.json` to process all the entry points, as it would in development. The latter files are then picked up by the asset pipeline, digested, and copied into public/assets, as any other asset pipeline file.
|
10
10
|
|
11
11
|
This also happens in testing where the bundler attaches to the `test:prepare` task to ensure the JavaScript has been bundled before testing commences. If your testing library of choice does not call the `test:prepare` Rake task, ensure that your test suite runs `javascript:build` to bundle JavaScript before testing commences.
|
12
12
|
|
13
13
|
That's it!
|
14
14
|
|
15
|
-
You can configure your bundler options in the `build` script in `package.json` or via the installer-generated `rollup.config.js` for rollup.js or `webpack.config.json` for Webpack (esbuild does not have a default configuration format, and we don't intend to use esbuild as an API in order to hack around it).
|
15
|
+
You can configure your bundler options in the `build` script in `package.json` or via the installer-generated `bun.config.js` for Bun, `rollup.config.js` for rollup.js or `webpack.config.json` for Webpack (esbuild does not have a default configuration format, and we don't intend to use esbuild as an API in order to hack around it).
|
16
16
|
|
17
17
|
If you're already using [`webpacker`](https://github.com/rails/webpacker) and you're wondering if you should migrate to `jsbundling-rails`, have a look at [the high-level comparison](./docs/comparison_with_webpacker.md). If you're looking to migrate from webpacker, see the [migration guide](https://github.com/rails/jsbundling-rails/blob/main/docs/switch_from_webpacker.md).
|
18
18
|
|
19
19
|
If you want to use webpack features like [code splitting](https://webpack.js.org/guides/code-splitting/) and [hot module reloading](https://webpack.js.org/concepts/hot-module-replacement/), consider using the official fork of `webpacker`, [`shakapacker`](https://github.com/shakacode/shakapacker).
|
20
20
|
|
21
21
|
## Installation
|
22
|
+
If you are installing esbuild, rollup, or webpack, you must already have node
|
23
|
+
and yarn installed on your system. You will also need npx version 7.1.0 or later.
|
22
24
|
|
23
|
-
|
25
|
+
If you are using Bun, then you must have the Bun runtime already installed on
|
26
|
+
your system.
|
27
|
+
|
28
|
+
To get started run:
|
24
29
|
|
25
30
|
```
|
26
31
|
./bin/bundle add jsbundling-rails
|
27
32
|
```
|
28
33
|
|
29
34
|
```
|
30
|
-
./bin/rails javascript:install:[esbuild|rollup|webpack]
|
35
|
+
./bin/rails javascript:install:[bun|esbuild|rollup|webpack]
|
31
36
|
```
|
32
37
|
|
33
|
-
Or, in Rails 7+, you can preconfigure your new application to use a specific bundler with `rails new myapp -j [esbuild|rollup|webpack]`.
|
38
|
+
Or, in Rails 7+, you can preconfigure your new application to use a specific bundler with `rails new myapp -j [bun|esbuild|rollup|webpack]`.
|
34
39
|
|
35
40
|
|
36
41
|
## FAQ
|
@@ -39,9 +44,9 @@ Or, in Rails 7+, you can preconfigure your new application to use a specific bun
|
|
39
44
|
|
40
45
|
The default build script for esbuild relies on the `app/javascript/*.*` glob pattern to compile multiple entrypoints automatically. This glob pattern is not available by default on Windows, so you need to change the build script in `package.json` to manually list the entrypoints you wish to compile.
|
41
46
|
|
42
|
-
### Why does esbuild overwrite my application.css?
|
47
|
+
### Why does bun/esbuild overwrite my application.css?
|
43
48
|
|
44
|
-
If you [import CSS](https://esbuild.github.io/content-types/#css-from-js) in your application.js while using esbuild, you'll be creating both an `app/assets/builds/application.js` _and_ `app/assets/builds/application.css` file when bundling. The latter can conflict with the `app/assets/builds/application.css` produced by [cssbundling-rails](https://github.com/rails/cssbundling-rails). The solution is to either change the output file for esbuild (and the references for that) or for cssbundling. Both are specified in `package.json`.
|
49
|
+
If you [import CSS](https://esbuild.github.io/content-types/#css-from-js) in your application.js while using esbuild or Bun, you'll be creating both an `app/assets/builds/application.js` _and_ `app/assets/builds/application.css` file when bundling. The latter can conflict with the `app/assets/builds/application.css` produced by [cssbundling-rails](https://github.com/rails/cssbundling-rails). The solution is to either change the output file for bun/esbuild (and the references for that) or for cssbundling. Both are specified in `package.json`.
|
45
50
|
|
46
51
|
### How can I reference static assets in JavaScript code?
|
47
52
|
|
data/lib/install/Procfile.dev
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
web:
|
1
|
+
web: env RUBY_DEBUG_OPEN=true bin/rails server
|
2
2
|
js: yarn build --watch
|
@@ -0,0 +1,37 @@
|
|
1
|
+
import path from 'path';
|
2
|
+
import fs from 'fs';
|
3
|
+
|
4
|
+
const config = {
|
5
|
+
sourcemap: "external",
|
6
|
+
entrypoints: ["app/javascript/application.js"],
|
7
|
+
outdir: path.join(process.cwd(), "app/assets/builds"),
|
8
|
+
};
|
9
|
+
|
10
|
+
const build = async (config) => {
|
11
|
+
const result = await Bun.build(config);
|
12
|
+
|
13
|
+
if (!result.success) {
|
14
|
+
if (process.argv.includes('--watch')) {
|
15
|
+
console.error("Build failed");
|
16
|
+
for (const message of result.logs) {
|
17
|
+
console.error(message);
|
18
|
+
}
|
19
|
+
return;
|
20
|
+
} else {
|
21
|
+
throw new AggregateError(result.logs, "Build failed");
|
22
|
+
}
|
23
|
+
}
|
24
|
+
};
|
25
|
+
|
26
|
+
(async () => {
|
27
|
+
await build(config);
|
28
|
+
|
29
|
+
if (process.argv.includes('--watch')) {
|
30
|
+
fs.watch(path.join(process.cwd(), "app/javascript"), { recursive: true }, (eventType, filename) => {
|
31
|
+
console.log(`File changed: ${filename}. Rebuilding...`);
|
32
|
+
build(config);
|
33
|
+
});
|
34
|
+
} else {
|
35
|
+
process.exit(0);
|
36
|
+
}
|
37
|
+
})();
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
if Rails.root.join("Procfile.dev").exist?
|
4
|
+
append_to_file "Procfile.dev", "js: bun run build --watch\n"
|
5
|
+
else
|
6
|
+
say "Add default Procfile.dev"
|
7
|
+
copy_file "#{__dir__}/Procfile.dev", "Procfile.dev"
|
8
|
+
|
9
|
+
say "Ensure foreman is installed"
|
10
|
+
run "gem install foreman"
|
11
|
+
end
|
12
|
+
|
13
|
+
say "Add default bun.config.js"
|
14
|
+
copy_file "#{__dir__}/bun.config.js", "bun.config.js"
|
15
|
+
|
16
|
+
say "Add build script to package.json"
|
17
|
+
package_json = JSON.parse(File.read("package.json"))
|
18
|
+
package_json["scripts"] ||= {}
|
19
|
+
package_json["scripts"]["build"] = "bun bun.config.js"
|
20
|
+
File.write("package.json", JSON.pretty_generate(package_json))
|
21
|
+
|
22
|
+
say "Add ability to diff lockb to .gitattributes"
|
23
|
+
if Rails.root.join(".gitattributes").exist?
|
24
|
+
append_to_file ".gitattributes", "\n# See https://bun.sh/docs/install/lockfile\n*.lockb diff=lockb\n"
|
25
|
+
else
|
26
|
+
copy_file "#{__dir__}/.gitattributes", ".gitattributes"
|
27
|
+
end
|
28
|
+
say %(Run `git config diff.lockb.textconv bun && git config diff.lockb.binary true` to enable pretty diffs for Bun's .lockb file), :green
|
data/lib/install/dev
CHANGED
data/lib/install/install.rb
CHANGED
@@ -31,16 +31,6 @@ unless Rails.root.join("package.json").exist?
|
|
31
31
|
copy_file "#{__dir__}/package.json", "package.json"
|
32
32
|
end
|
33
33
|
|
34
|
-
if Rails.root.join("Procfile.dev").exist?
|
35
|
-
append_to_file "Procfile.dev", "js: yarn build --watch\n"
|
36
|
-
else
|
37
|
-
say "Add default Procfile.dev"
|
38
|
-
copy_file "#{__dir__}/Procfile.dev", "Procfile.dev"
|
39
|
-
|
40
|
-
say "Ensure foreman is installed"
|
41
|
-
run "gem install foreman"
|
42
|
-
end
|
43
|
-
|
44
34
|
say "Add bin/dev to start foreman"
|
45
35
|
copy_file "#{__dir__}/dev", "bin/dev"
|
46
36
|
chmod "bin/dev", 0755, verbose: false
|
@@ -0,0 +1,9 @@
|
|
1
|
+
if Rails.root.join("Procfile.dev").exist?
|
2
|
+
append_to_file "Procfile.dev", "js: yarn build --watch\n"
|
3
|
+
else
|
4
|
+
say "Add default Procfile.dev"
|
5
|
+
copy_file "#{__dir__}/Procfile.dev", "Procfile.dev"
|
6
|
+
|
7
|
+
say "Ensure foreman is installed"
|
8
|
+
run "gem install foreman"
|
9
|
+
end
|
data/lib/jsbundling/version.rb
CHANGED
@@ -1,28 +1,49 @@
|
|
1
1
|
namespace :javascript do
|
2
2
|
desc "Install JavaScript dependencies"
|
3
3
|
task :install do
|
4
|
-
|
5
|
-
|
4
|
+
command = install_command
|
5
|
+
unless system(command)
|
6
|
+
raise "jsbundling-rails: Command install failed, ensure #{command.split.first} is installed"
|
6
7
|
end
|
7
8
|
end
|
8
9
|
|
9
10
|
desc "Build your JavaScript bundle"
|
10
11
|
build_task = task :build do
|
11
|
-
|
12
|
-
|
12
|
+
command = build_command
|
13
|
+
unless system(command)
|
14
|
+
raise "jsbundling-rails: Command build failed, ensure `#{command}` runs without errors"
|
13
15
|
end
|
14
16
|
end
|
15
|
-
|
17
|
+
|
18
|
+
build_task.prereqs << :install unless ENV["SKIP_YARN_INSTALL"] || ENV["SKIP_BUN_INSTALL"]
|
19
|
+
end
|
20
|
+
|
21
|
+
def install_command
|
22
|
+
return "bun install" if File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock'))
|
23
|
+
return "yarn install" if File.exist?('yarn.lock') || tool_exists?('yarn')
|
24
|
+
raise "jsbundling-rails: No suitable tool found for installing JavaScript dependencies"
|
25
|
+
end
|
26
|
+
|
27
|
+
def build_command
|
28
|
+
return "bun run build" if File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock'))
|
29
|
+
return "yarn build" if File.exist?('yarn.lock') || tool_exists?('yarn')
|
30
|
+
raise "jsbundling-rails: No suitable tool found for building JavaScript"
|
16
31
|
end
|
17
32
|
|
18
|
-
|
19
|
-
|
33
|
+
def tool_exists?(tool)
|
34
|
+
system "command -v #{tool} > /dev/null"
|
20
35
|
end
|
21
36
|
|
22
|
-
|
23
|
-
Rake::Task
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
Rake::Task
|
37
|
+
unless ENV["SKIP_JS_BUILD"]
|
38
|
+
if Rake::Task.task_defined?("assets:precompile")
|
39
|
+
Rake::Task["assets:precompile"].enhance(["javascript:build"])
|
40
|
+
end
|
41
|
+
|
42
|
+
if Rake::Task.task_defined?("test:prepare")
|
43
|
+
Rake::Task["test:prepare"].enhance(["javascript:build"])
|
44
|
+
elsif Rake::Task.task_defined?("spec:prepare")
|
45
|
+
Rake::Task["spec:prepare"].enhance(["javascript:build"])
|
46
|
+
elsif Rake::Task.task_defined?("db:test:prepare")
|
47
|
+
Rake::Task["db:test:prepare"].enhance(["javascript:build"])
|
48
|
+
end
|
28
49
|
end
|
@@ -5,18 +5,28 @@ namespace :javascript do
|
|
5
5
|
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/install.rb", __dir__)}"
|
6
6
|
end
|
7
7
|
|
8
|
+
desc "Install node-specific elements for bundlers that use node/yarn."
|
9
|
+
task :node_shared do
|
10
|
+
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/install_node.rb", __dir__)}"
|
11
|
+
end
|
12
|
+
|
13
|
+
desc "Install Bun"
|
14
|
+
task bun: "javascript:install:shared" do
|
15
|
+
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/bun/install.rb", __dir__)}"
|
16
|
+
end
|
17
|
+
|
8
18
|
desc "Install esbuild"
|
9
|
-
task esbuild: "javascript:install:shared" do
|
19
|
+
task esbuild: ["javascript:install:shared", "javascript:install:node_shared"] do
|
10
20
|
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/esbuild/install.rb", __dir__)}"
|
11
21
|
end
|
12
22
|
|
13
23
|
desc "Install rollup.js"
|
14
|
-
task rollup: "javascript:install:shared" do
|
24
|
+
task rollup: ["javascript:install:shared", "javascript:install:node_shared"] do
|
15
25
|
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/rollup/install.rb", __dir__)}"
|
16
26
|
end
|
17
27
|
|
18
28
|
desc "Install Webpack"
|
19
|
-
task webpack: "javascript:install:shared" do
|
29
|
+
task webpack: ["javascript:install:shared", "javascript:install:node_shared"] do
|
20
30
|
system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/webpack/install.rb", __dir__)}"
|
21
31
|
end
|
22
32
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsbundling-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- David Heinemeier Hansson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-
|
11
|
+
date: 2023-09-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -34,9 +34,13 @@ files:
|
|
34
34
|
- README.md
|
35
35
|
- lib/install/Procfile.dev
|
36
36
|
- lib/install/application.js
|
37
|
+
- lib/install/bun/Procfile.dev
|
38
|
+
- lib/install/bun/bun.config.js
|
39
|
+
- lib/install/bun/install.rb
|
37
40
|
- lib/install/dev
|
38
41
|
- lib/install/esbuild/install.rb
|
39
42
|
- lib/install/install.rb
|
43
|
+
- lib/install/install_node.rb
|
40
44
|
- lib/install/package.json
|
41
45
|
- lib/install/rollup/install.rb
|
42
46
|
- lib/install/rollup/rollup.config.js
|