jsbundling-rails 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f0ef32480b80f6c274f0bb59e0785cf22aa157b19e688fcbebbe4544d195c81e
4
+ data.tar.gz: 131dffe9aa231f4a456452a3b9ba67f0d91d2079ebd3102a43c30c63bdc5f2a0
5
+ SHA512:
6
+ metadata.gz: 22aa53c9b71baa3704f447ea717f9ccd2d7b489dbb17eb0885dd65fb94bb3d678ec15a5d74589c4c5c5ec8bbc89603cc2963b978f38047d28ba02b15b45d9638
7
+ data.tar.gz: db1a29cb7d04cf0e61cfdc0b03f44ffe766d12936ef7c5147cd704297726724f7317fa7aa89737b6978c628b157c0454675b98e4530e3067ec14d54ee0f755f0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2021 David Heinemeier Hansson
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # JavaScript Bundling for Rails
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/bundles` to hold your bundled output as artifacts that are not checked into source control (the installer adds this directory to `.gitignore` by default).
4
+
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)). Whenever the bundler detects changes to any of the JavaScript files in your project, it'll bundle `app/javascript/application.js` into `app/assets/bundles/javascript.js` (and all other entry points placed in the root of `app/javascript`). You can refer to the build output in your layout using the standard asset pipeline approach with `<%= javascript_include_tag "application" %>`.
6
+
7
+ When you deploy your application to production, the bundler attaches to the `assets:precompile` task to ensure that all your package dependencies from `package.json` have been installed via npm, and then runs `yarn build` 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.
8
+
9
+ This also happens in testing where the bundler attaches to the `test:prepare` task to ensure the JavaScript has been bundled before testing commences. (Note that this currently only applies to rails `test:*` tasks (like `test:all` or `test:controllers`), not "rails test", as that doesn't load `test:prepare`).
10
+
11
+ That's it!
12
+
13
+ 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).
14
+
15
+
16
+ ## Installation
17
+
18
+ You must already have node and yarn installed on your system. Then:
19
+
20
+ 1. Add `jsbundling-rails` to your Gemfile with `gem 'jsbundling-rails'`
21
+ 2. Run `./bin/bundle install`
22
+ 3. Run `./bin/rails javascript:[rollup|esbuild|webpack]:install`
23
+
24
+ Or, in Rails 7+, you can preconfigure your new application to use a specific bundler with `rails new myapp -j [rollup|esbuild|webpack]`.
25
+
26
+
27
+ ## License
28
+
29
+ rollup.js for Rails is released under the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1 @@
1
+ // Entry point for the build script in your package.json
@@ -0,0 +1,3 @@
1
+ say "Create default package.json and install esbuild"
2
+ copy_file "#{__dir__}/package.json", "package.json"
3
+ run "yarn add esbuild"
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "app",
3
+ "private": "true",
4
+ "scripts": {
5
+ "build": "esbuild app/javascript/*.* --bundle --outdir=app/assets/builds"
6
+ }
7
+ }
@@ -0,0 +1,23 @@
1
+ say "Compile into app/assets/builds"
2
+ empty_directory "app/assets/builds"
3
+ keep_file "app/assets/builds"
4
+ append_to_file "app/assets/config/manifest.js", %(//= link_tree ../builds .js\n)
5
+
6
+ if Rails.root.join(".gitignore").exist?
7
+ append_to_file ".gitignore", %(/app/assets/builds\n)
8
+ end
9
+
10
+ if (app_layout_path = Rails.root.join("app/views/layouts/application.html.erb")).exist?
11
+ say "Add JavaScript include tag in application layout"
12
+ insert_into_file app_layout_path.to_s,
13
+ %(\n <%= javascript_include_tag "application", "data-track-turbo": "true", defer: true %>), before: /\s*<\/head>/
14
+ else
15
+ say "Default application.html.erb is missing!", :red
16
+ say %( Add <%= javascript_include_tag "application", "data-track-turbo": "true", defer: true %> within the <head> tag in your custom layout.)
17
+ end
18
+
19
+ unless (app_js_entrypoint_path = Rails.root.join("app/javascript/application.js")).exist?
20
+ say "Create default entrypoint in app/javascript/application.js"
21
+ empty_directory app_js_entrypoint_path.parent.to_s
22
+ copy_file "#{__dir__}/application.js", app_js_entrypoint_path
23
+ end
@@ -0,0 +1,4 @@
1
+ say "Create default package.json and install rollup with config"
2
+ copy_file "#{__dir__}/package.json", "package.json"
3
+ copy_file "#{__dir__}/rollup.config.js", "rollup.config.js"
4
+ run "yarn add rollup @rollup/plugin-node-resolve"
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "app",
3
+ "private": "true",
4
+ "scripts": {
5
+ "build": "rollup -c rollup.config.js"
6
+ }
7
+ }
@@ -0,0 +1,13 @@
1
+ import resolve from "@rollup/plugin-node-resolve"
2
+
3
+ export default {
4
+ input: "app/javascript/application.js",
5
+ output: {
6
+ file: "app/assets/builds/application.js",
7
+ format: "es",
8
+ inlineDynamicImports: true
9
+ },
10
+ plugins: [
11
+ resolve()
12
+ ]
13
+ }
@@ -0,0 +1,4 @@
1
+ say "Create default package.json and install Webpack with config"
2
+ copy_file "#{__dir__}/package.json", "package.json"
3
+ copy_file "#{__dir__}/webpack.config.js", "webpack.config.js"
4
+ run "yarn add webpack webpack-cli"
@@ -0,0 +1,7 @@
1
+ {
2
+ "name": "app",
3
+ "private": "true",
4
+ "scripts": {
5
+ "build": "webpack --config webpack.config.js"
6
+ }
7
+ }
@@ -0,0 +1,16 @@
1
+ const path = require("path")
2
+ const webpack = require('webpack')
3
+
4
+ module.exports = {
5
+ mode: "production",
6
+ entry: "./app/javascript/application.js",
7
+ output: {
8
+ filename: "application.js",
9
+ path: path.resolve(__dirname, "app/assets/builds"),
10
+ },
11
+ plugins: [
12
+ new webpack.optimize.LimitChunkCountPlugin({
13
+ maxChunks: 1
14
+ })
15
+ ]
16
+ }
@@ -0,0 +1,4 @@
1
+ module Jsbundling
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module Jsbundling
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,5 @@
1
+ module Jsbundling
2
+ end
3
+
4
+ require "jsbundling/version"
5
+ require "jsbundling/engine"
@@ -0,0 +1,9 @@
1
+ namespace :javascript do
2
+ desc "Build your JavaScript bundle"
3
+ task :build do
4
+ system "yarn install && yarn build"
5
+ end
6
+ end
7
+
8
+ Rake::Task["assets:precompile"].enhance(["javascript:build"])
9
+ Rake::Task["test:prepare"].enhance(["javascript:build"])
@@ -0,0 +1,23 @@
1
+ namespace :javascript do
2
+ namespace :install do
3
+ desc "Install shared elements for all bundlers"
4
+ task :shared do
5
+ system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/install.rb", __dir__)}"
6
+ end
7
+
8
+ desc "Install esbuild"
9
+ task esbuild: "javascript:install:shared" do
10
+ system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/esbuild/install.rb", __dir__)}"
11
+ end
12
+
13
+ desc "Install rollup.js"
14
+ task rollup: "javascript:install:shared" do
15
+ system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/rollup/install.rb", __dir__)}"
16
+ end
17
+
18
+ desc "Install Webpack"
19
+ task webpack: "javascript:install:shared" do
20
+ system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{File.expand_path("../../install/webpack/install.rb", __dir__)}"
21
+ end
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jsbundling-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Heinemeier Hansson
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-09-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 6.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 6.0.0
27
+ description:
28
+ email: david@loudthinking.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - MIT-LICENSE
34
+ - README.md
35
+ - lib/install/application.js
36
+ - lib/install/esbuild/install.rb
37
+ - lib/install/esbuild/package.json
38
+ - lib/install/install.rb
39
+ - lib/install/rollup/install.rb
40
+ - lib/install/rollup/package.json
41
+ - lib/install/rollup/rollup.config.js
42
+ - lib/install/webpack/install.rb
43
+ - lib/install/webpack/package.json
44
+ - lib/install/webpack/webpack.config.js
45
+ - lib/jsbundling-rails.rb
46
+ - lib/jsbundling/engine.rb
47
+ - lib/jsbundling/version.rb
48
+ - lib/tasks/jsbundling/build.rake
49
+ - lib/tasks/jsbundling/install.rake
50
+ homepage: https://github.com/rails/jsbundling-rails
51
+ licenses:
52
+ - MIT
53
+ metadata: {}
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.1.4
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Bundle and transpile JavaScript in Rails with esbuild, rollup.js, or Webpack.
73
+ test_files: []