cssbundling-rails 1.1.2 → 1.3.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: 45868eda1ae67158b652d75c9ad8d89bc55bf67ac62e0539b8a30b91122d0186
4
- data.tar.gz: d6b0d91eaf919c44628aa7c9edbdf3a56d3d34e0ace6651dd42aca2091acb30e
3
+ metadata.gz: 80c2c969114694a1020f70a4f5025ddaaddd9814c802646e768631942a1adbdf
4
+ data.tar.gz: d5a828cf779270bf0f05257fcc2e9ee33fbbae7b23e5f668bcad26779a0131f0
5
5
  SHA512:
6
- metadata.gz: 517e8067a0fabc88db726ca0d89c4ec488a3815aef2c6c5cf5e4df254c2706ed6826693610062e4fe0385b3d42688217f46de5005bd8b631b8ba8e8f10720346
7
- data.tar.gz: f81ead37fb5b36fbf09a1d8751ae67fbeb3e28d82baa80d177cd0779d8c3bc36ed62b686021ce3fba5cb817f1c431d585346a6e067502e33e3483b283b501b80
6
+ metadata.gz: a42545bdef51ea41c6c386067a643a84c862fe996b0f1d519891ada561db3ad067796a66172ee628bcfc993540644529dfda0c6b938410a121d727c04e111ada
7
+ data.tar.gz: 1ed4b170f44f51e8db2d04e7d963702bacef1eab17099eac42a9df6166f1aa4ad480b60caa11437973e219ab1c35c3a6292eb08252db42ccf26c88ddc08ac4bb
data/README.md CHANGED
@@ -8,9 +8,7 @@ Whenever the bundler detects changes to any of the stylesheet files in your proj
8
8
 
9
9
  When you deploy your application to production, the `css: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 `yarn build:css` to process your stylesheet entrypoint, as it would in development. This output is then picked up by the asset pipeline, digested, and copied into public/assets, as any other asset pipeline file.
10
10
 
11
- This also happens in testing where the bundler attaches to the `test:prepare` task to ensure the stylesheets have 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`).
12
-
13
- If your test framework does not define a `test:prepare` Rake task, ensure that your test framework runs `css:build` to bundle stylesheets before testing commences. If your setup uses [jsbundling-rails](https://github.com/rails/jsbundling-rails) (ie, esbuild + tailwind), you will also need to run `javascript:build`.
11
+ This also happens in testing where the bundler attaches to the `test:prepare` task to ensure the stylesheets have been bundled before testing commences. If your test framework does not call the `test:prepare` Rake task, ensure that your test framework runs `css:build` to bundle stylesheets before testing commences. If your setup uses [jsbundling-rails](https://github.com/rails/jsbundling-rails) (ie, esbuild + tailwind), you will also need to run `javascript:build`.
14
12
 
15
13
  That's it!
16
14
 
@@ -45,6 +43,14 @@ Some CSS packages use new CSS features that are not supported by the default Sas
45
43
 
46
44
  A common issue is that your repository does not contain the output directory used by the build commands. You must have `app/assets/builds` available. Add the directory with a `.gitkeep` file, and you'll ensure it's available in production.
47
45
 
46
+ ### Why isn't Rails using my updated css files?
47
+
48
+ Watch out - if you precompile your files locally, those will be served over the dynamically created ones you expect. The solution:
49
+
50
+ ```shell
51
+ rails assets:clobber
52
+ ```
53
+
48
54
  ## License
49
55
 
50
56
  CSS Bundling for Rails is released under the [MIT License](https://opensource.org/licenses/MIT).
@@ -1,3 +1,3 @@
1
1
  module Cssbundling
2
- VERSION = "1.1.2"
2
+ VERSION = "1.3.0"
3
3
  end
@@ -0,0 +1,2 @@
1
+ web: env RUBY_DEBUG_OPEN=true bin/rails server
2
+ css: bun run build:css --watch
@@ -0,0 +1,2 @@
1
+ web: env RUBY_DEBUG_OPEN=true bin/rails server
2
+ css: yarn build:css --watch
@@ -1,7 +1,10 @@
1
- say "Install Bootstrap with Bootstrap Icons and Popperjs/core"
1
+ require_relative "../helpers"
2
+ self.extend Helpers
3
+
4
+ say "Install Bootstrap with Bootstrap Icons, Popperjs/core and Autoprefixer"
2
5
  copy_file "#{__dir__}/application.bootstrap.scss",
3
6
  "app/assets/stylesheets/application.bootstrap.scss"
4
- run "yarn add sass bootstrap bootstrap-icons @popperjs/core"
7
+ run "#{bundler_cmd} add sass bootstrap bootstrap-icons @popperjs/core postcss postcss-cli autoprefixer nodemon"
5
8
 
6
9
  inject_into_file "config/initializers/assets.rb", after: /.*Rails.application.config.assets.paths.*\n/ do
7
10
  <<~RUBY
@@ -16,16 +19,14 @@ else
16
19
  say %(Add import * as bootstrap from "bootstrap" to your entry point JavaScript file), :red
17
20
  end
18
21
 
19
- say "Add build:css script"
20
- build_script = "sass ./app/assets/stylesheets/application.bootstrap.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules"
22
+ add_package_json_script("build:css:compile", "sass ./app/assets/stylesheets/application.bootstrap.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules")
23
+ add_package_json_script("build:css:prefix", "postcss ./app/assets/builds/application.css --use=autoprefixer --output=./app/assets/builds/application.css")
24
+ add_package_json_script("build:css", "#{bundler_run_cmd} build:css:compile && #{bundler_run_cmd} build:css:prefix")
25
+ add_package_json_script("watch:css", "nodemon --watch ./app/assets/stylesheets/ --ext scss --exec \"#{bundler_run_cmd} build:css\"", false)
21
26
 
22
- case `npx -v`.to_f
23
- when 7.1...8.0
24
- run %(npm set-script build:css "#{build_script}")
25
- run %(yarn build:css)
26
- when (8.0..)
27
- run %(npm pkg set scripts.build:css="#{build_script}")
28
- run %(yarn build:css)
29
- else
30
- say %(Add "scripts": { "build:css": "#{build_script}" } to your package.json), :green
31
- end
27
+ gsub_file "Procfile.dev", "build:css --watch", "watch:css"
28
+
29
+ package_json = JSON.parse(File.read("package.json"))
30
+ package_json["browserslist"] ||= {}
31
+ package_json["browserslist"] = ["defaults"]
32
+ File.write("package.json", JSON.pretty_generate(package_json))
@@ -1,18 +1,11 @@
1
+ require_relative "../helpers"
2
+ self.extend Helpers
3
+
1
4
  say "Install Bulma"
2
5
  copy_file "#{__dir__}/application.bulma.scss",
3
6
  "app/assets/stylesheets/application.bulma.scss"
4
- run "yarn add sass bulma"
7
+ run "#{bundler_cmd} add sass bulma"
5
8
 
6
9
  say "Add build:css script"
7
- build_script = "sass ./app/assets/stylesheets/application.bulma.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules"
8
-
9
- case `npx -v`.to_f
10
- when 7.1...8.0
11
- run %(npm set-script build:css "#{build_script}")
12
- run %(yarn build:css)
13
- when (8.0..)
14
- run %(npm pkg set scripts.build:css="#{build_script}")
15
- run %(yarn build:css)
16
- else
17
- say %(Add "scripts": { "build:css": "#{build_script}" } to your package.json), :green
18
- end
10
+ add_package_json_script "build:css",
11
+ "sass ./app/assets/stylesheets/application.bulma.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules"
data/lib/install/dev CHANGED
@@ -5,4 +5,7 @@ if ! gem list foreman -i --silent; then
5
5
  gem install foreman
6
6
  fi
7
7
 
8
+ # Default to port 3000 if not specified
9
+ export PORT="${PORT:-3000}"
10
+
8
11
  exec foreman start -f Procfile.dev "$@"
@@ -0,0 +1,42 @@
1
+ require 'json'
2
+
3
+ module Helpers
4
+ def bundler_cmd
5
+ using_bun? ? "bun" : "yarn"
6
+ end
7
+
8
+ def bundler_run_cmd
9
+ using_bun? ? "bun run" : "yarn"
10
+ end
11
+
12
+ def using_bun?
13
+ File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock'))
14
+ end
15
+
16
+ def tool_exists?(tool)
17
+ system "command -v #{tool} > /dev/null"
18
+ end
19
+
20
+ def add_package_json_script(name, script, run_script=true)
21
+ if using_bun?
22
+ package_json = JSON.parse(File.read("package.json"))
23
+ package_json["scripts"] ||= {}
24
+ package_json["scripts"][name] = script
25
+ File.write("package.json", JSON.pretty_generate(package_json))
26
+ run %(bun run #{name}) if run_script
27
+ else
28
+ case `npx -v`.to_f
29
+ when 7.1...8.0
30
+ say "Add #{name} script"
31
+ run %(npm set-script #{name} "#{script}")
32
+ run %(yarn #{name}) if run_script
33
+ when (8.0..)
34
+ say "Add #{name} script"
35
+ run %(npm pkg set scripts.#{name}="#{script}")
36
+ run %(yarn #{name}) if run_script
37
+ else
38
+ say %(Add "scripts": { "#{name}": "#{script}" } to your package.json), :green
39
+ end
40
+ end
41
+ end
42
+ end
@@ -1,3 +1,6 @@
1
+ require_relative "helpers"
2
+ self.extend Helpers
3
+
1
4
  say "Build into app/assets/builds"
2
5
  empty_directory "app/assets/builds"
3
6
  keep_file "app/assets/builds"
@@ -41,10 +44,10 @@ unless Rails.root.join("package.json").exist?
41
44
  end
42
45
 
43
46
  if Rails.root.join("Procfile.dev").exist?
44
- append_to_file "Procfile.dev", "css: yarn build:css --watch\n"
47
+ append_to_file "Procfile.dev", "css: #{bundler_run_cmd} build:css --watch\n"
45
48
  else
46
49
  say "Add default Procfile.dev"
47
- copy_file "#{__dir__}/Procfile.dev", "Procfile.dev"
50
+ copy_file "#{__dir__}/#{using_bun? ? "Procfile_for_bun" : "Procfile_for_node"}", "Procfile.dev"
48
51
 
49
52
  say "Ensure foreman is installed"
50
53
  run "gem install foreman"
@@ -1,18 +1,11 @@
1
+ require_relative "../helpers"
2
+ self.extend Helpers
3
+
1
4
  say "Install PostCSS w/ nesting and autoprefixer"
2
5
  copy_file "#{__dir__}/postcss.config.js", "postcss.config.js"
3
6
  copy_file "#{__dir__}/application.postcss.css", "app/assets/stylesheets/application.postcss.css"
4
- run "yarn add postcss postcss-cli postcss-nesting autoprefixer"
7
+ run "#{bundler_cmd} add postcss postcss-cli postcss-import postcss-nesting autoprefixer"
5
8
 
6
9
  say "Add build:css script"
7
- build_script = "postcss ./app/assets/stylesheets/application.postcss.css -o ./app/assets/builds/application.css"
8
-
9
- case `npx -v`.to_f
10
- when 7.1...8.0
11
- run %(npm set-script build:css "#{build_script}")
12
- run %(yarn build:css)
13
- when (8.0..)
14
- run %(npm pkg set scripts.build:css="#{build_script}")
15
- run %(yarn build:css)
16
- else
17
- say %(Add "scripts": { "build:css": "#{build_script}" } to your package.json), :green
18
- end
10
+ add_package_json_script "build:css",
11
+ "postcss ./app/assets/stylesheets/application.postcss.css -o ./app/assets/builds/application.css"
@@ -1,5 +1,6 @@
1
1
  module.exports = {
2
2
  plugins: [
3
+ require('postcss-import'),
3
4
  require('postcss-nesting'),
4
5
  require('autoprefixer'),
5
6
  ],
@@ -1,17 +1,10 @@
1
+ require_relative "../helpers"
2
+ self.extend Helpers
3
+
1
4
  say "Install Sass"
2
5
  copy_file "#{__dir__}/application.sass.scss", "app/assets/stylesheets/application.sass.scss"
3
- run "yarn add sass"
6
+ run "#{bundler_cmd} add sass"
4
7
 
5
8
  say "Add build:css script"
6
- build_script = "sass ./app/assets/stylesheets/application.sass.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules"
7
-
8
- case `npx -v`.to_f
9
- when 7.1...8.0
10
- run %(npm set-script build:css "#{build_script}")
11
- run %(yarn build:css)
12
- when (8.0..)
13
- run %(npm pkg set scripts.build:css="#{build_script}")
14
- run %(yarn build:css)
15
- else
16
- say %(Add "scripts": { "build:css": "#{build_script}" } to your package.json), :green
17
- end
9
+ add_package_json_script "build:css",
10
+ "sass ./app/assets/stylesheets/application.sass.scss:./app/assets/builds/application.css --no-source-map --load-path=node_modules"
@@ -1,18 +1,11 @@
1
+ require_relative "../helpers"
2
+ self.extend Helpers
3
+
1
4
  say "Install Tailwind (+PostCSS w/ autoprefixer)"
2
5
  copy_file "#{__dir__}/tailwind.config.js", "tailwind.config.js"
3
6
  copy_file "#{__dir__}/application.tailwind.css", "app/assets/stylesheets/application.tailwind.css"
4
- run "yarn add tailwindcss@latest postcss@latest autoprefixer@latest"
7
+ run "#{bundler_cmd} add tailwindcss@latest postcss@latest autoprefixer@latest"
5
8
 
6
9
  say "Add build:css script"
7
- build_script = "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify"
8
-
9
- case `npx -v`.to_f
10
- when 7.1...8.0
11
- run %(npm set-script build:css "#{build_script}")
12
- run %(yarn build:css)
13
- when (8.0..)
14
- run %(npm pkg set scripts.build:css="#{build_script}")
15
- run %(yarn build:css)
16
- else
17
- say %(Add "scripts": { "build:css": "#{build_script}" } to your package.json), :green
18
- end
10
+ add_package_json_script "build:css",
11
+ "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify"
@@ -1,18 +1,48 @@
1
1
  namespace :css do
2
+ desc "Install JavaScript dependencies"
3
+ task :install do
4
+ command = install_command
5
+ unless system(command)
6
+ raise "cssbundling-rails: Command install failed, ensure #{command.split.first} is installed"
7
+ end
8
+ end
9
+
2
10
  desc "Build your CSS bundle"
3
- task :build do
4
- unless system "yarn install && yarn build:css"
5
- raise "cssbundling-rails: Command css:build failed, ensure yarn is installed and `yarn build:css` runs without errors"
11
+ build_task = task :build do
12
+ command = build_command
13
+ unless system(command)
14
+ raise "cssbundling-rails: Command build failed, ensure `#{command}` runs without errors"
6
15
  end
7
16
  end
17
+ build_task.prereqs << :install unless ENV["SKIP_YARN_INSTALL"] || ENV["SKIP_BUN_INSTALL"]
18
+ end
19
+
20
+ def install_command
21
+ return "bun install" if File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock'))
22
+ return "yarn install" if File.exist?('yarn.lock') || tool_exists?('yarn')
23
+ raise "cssbundling-rails: No suitable tool found for installing JavaScript dependencies"
8
24
  end
9
25
 
10
- if Rake::Task.task_defined?("assets:precompile")
11
- Rake::Task["assets:precompile"].enhance(["css:build"])
26
+ def build_command
27
+ return "bun run build:css" if File.exist?('bun.lockb') || (tool_exists?('bun') && !File.exist?('yarn.lock'))
28
+ return "yarn build:css" if File.exist?('yarn.lock') || tool_exists?('yarn')
29
+ raise "cssbundling-rails: No suitable tool found for building CSS"
12
30
  end
13
31
 
14
- if Rake::Task.task_defined?("test:prepare")
15
- Rake::Task["test:prepare"].enhance(["css:build"])
16
- elsif Rake::Task.task_defined?("db:test:prepare")
17
- Rake::Task["db:test:prepare"].enhance(["css:build"])
32
+ def tool_exists?(tool)
33
+ system "command -v #{tool} > /dev/null"
34
+ end
35
+
36
+ unless ENV["SKIP_CSS_BUILD"]
37
+ if Rake::Task.task_defined?("assets:precompile")
38
+ Rake::Task["assets:precompile"].enhance(["css:build"])
39
+ end
40
+
41
+ if Rake::Task.task_defined?("test:prepare")
42
+ Rake::Task["test:prepare"].enhance(["css:build"])
43
+ elsif Rake::Task.task_defined?("spec:prepare")
44
+ Rake::Task["spec:prepare"].enhance(["css:build"])
45
+ elsif Rake::Task.task_defined?("db:test:prepare")
46
+ Rake::Task["db:test:prepare"].enhance(["css:build"])
47
+ end
18
48
  end
@@ -1,7 +1,7 @@
1
1
  namespace :css do
2
2
  desc "Remove CSS builds"
3
3
  task :clobber do
4
- rm_rf Dir["app/assets/builds/**/[^.]*.css"], verbose: false
4
+ rm_rf Dir["app/assets/builds/**/[^.]*.{css,css.map}"], verbose: false
5
5
  end
6
6
  end
7
7
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cssbundling-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.2
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-12-14 00:00:00.000000000 Z
12
+ date: 2023-09-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
@@ -36,12 +36,14 @@ files:
36
36
  - lib/cssbundling-rails.rb
37
37
  - lib/cssbundling/engine.rb
38
38
  - lib/cssbundling/version.rb
39
- - lib/install/Procfile.dev
39
+ - lib/install/Procfile_for_bun.dev
40
+ - lib/install/Procfile_for_node.dev
40
41
  - lib/install/bootstrap/application.bootstrap.scss
41
42
  - lib/install/bootstrap/install.rb
42
43
  - lib/install/bulma/application.bulma.scss
43
44
  - lib/install/bulma/install.rb
44
45
  - lib/install/dev
46
+ - lib/install/helpers.rb
45
47
  - lib/install/install.rb
46
48
  - lib/install/package.json
47
49
  - lib/install/postcss/application.postcss.css
@@ -75,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
77
  - !ruby/object:Gem::Version
76
78
  version: '0'
77
79
  requirements: []
78
- rubygems_version: 3.3.26
80
+ rubygems_version: 3.4.10
79
81
  signing_key:
80
82
  specification_version: 4
81
83
  summary: Bundle and process CSS with Tailwind, Bootstrap, PostCSS, Sass in Rails via
@@ -1,2 +0,0 @@
1
- web: unset PORT && bin/rails server
2
- css: yarn build:css --watch