Need
I need both Sass and TailwindCSS to work in parallel for our project (Rails 7 deployed on Heroku).
Current setup
# Procfile
release: rake db:migrate
web: bundle exec puma -C config/puma.rb
worker: bundle exec sidekiq -C config/sidekiq.yml
css: yarn build:css
sass: yarn build:sass
js: yarn build
// package.json
{
// ...
"scripts": {
"build": "...",
"build:css": "tailwindcss -i ./path/to/input.css -o ./path/to/output1.css",
"build:sass": "sass ./path/to/input.sass.scss:./path/to/output2.css"
}
}
<!-- application.html.erb -->
<%= stylesheet_link_tag 'tailwind', 'data-turbo-track': 'reload' %>
<%= stylesheet_link_tag 'application', 'data-turbo-track': 'reload' %>
Problem
Locally, everything works just fine. But when I try to push this to production, my Sass code disappears.
I found out in cssbundling gem's docs that it was due to:
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.
Solution(?)
So I changed for only one script calling the other ones:
// package.json
{
// ...
"scripts": {
"build": "...",
"build:css": "yarn build:sass & yarn build:tailwind",
"build:css-watch": "yarn build:sass --watch & yarn build:tailwind --watch"
"build:tailwind": "tailwindcss -i ./path/to/input.css -o ./path/to/output1.css",
"build:sass": "sass ./path/to/input.sass.scss:./path/to/output2.css"
}
}
Which now works in production, but for development environment, if I want to use the --watch option, the only solution I found is to create another script: build:css-watch.
Question(s)
Is my solution the best one for my problem?
If so, the build:css-watch script looks really weird to me, is there any other solution?
Related
Extremly odd error, asset precompilation works fine in dev mode, but as soon as i do production mode, i get error:
root#53407ec15126:/app# RAILS_ENV=production rails assets:precompile
yarn install v1.22.18
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.04s.
yarn run v1.22.18
error Command "build" not found.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
rails aborted!
jsbundling-rails: Command build failed, ensure yarn is installed and `yarn build` runs without errors
Tasks: TOP => assets:precompile => javascript:build
(See full trace by running task with --trace)
root#53407ec15126:/chinese-flashcards-app#
And the weirdest thing is, i dont even use yarn in my project(i use importmaps).
Well.. I figured out how to do a monkey patch(Sigh..).
Add this to package.json:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "echo 1",
"build:css": "echo 1"
},
This does not resolve the issue of rails prod preprocessor using yarn/npm tho(i dont need them).
I built a new rails app and when I have configured the .travis.yml file it exits with one all the time.
Here is my travis configuration:
language: ruby
node_js:
- 12
cache:
bundler: true
yarn: true
services:
- redis-server
sudo: required
before_install:
- sudo apt-get update
- sudo apt-get install google-chrome-stable
addons:
postgresql: '9.6'
before_script:
- psql -c 'create database product_hunt_clone_test;' -U postgres
script:
- bundle install
- bundle exec rake db:schema:load
- bundle exec rake db:test:prepare
- SECRET_KEY_BASE=a RAILS_ENV=production bundle exec rake assets:precompile
- bundle exec rake test
- bundle exec rake test:system
and here are the errors travis is giving me:
screenshot from travis console
You have apparently create the initial application using the --skip-sprockets parameter. As such, you have disable the use of the assets pipeline in your application and with that have disable the need (and possibility) to run rake assets:precompile.
If you do need assets pipeline (and thus the rails-sprockets gem), you can re-enable it by adding
gem "sprockets-rails"
to your Gemfile and uncommenting the line
# require "sprockets/railtie"
in your config/application.rb file. You would also need to create the asset files as necessary. Please refer to the documentation for the assets pipeline for details.
If you actually want to skip the assets pipeline, you can just remove the call to the assets:precompile task from your .travis.yml.
Are you correctly installing your gems (with bundle install) before executing your rake commands? Seems like you would need sprockets before compiling the assets.
env
Rails 6.0.0
Ruby 2.6.0
Amazon Linux2
What
When I deploy my rails app this error happen
ActionView::Template::Error (Webpacker can't find application in /home/web/www/eloop-regular/public/packs/manifest.json. Possible causes:
1. You want to set webpacker.yml value of compile to true for your environment
unless you are using the `webpack -w` or the webpack-dev-server.
2. webpack has not yet re-run to reflect updates.
3. You have misconfigured Webpacker's config/webpacker.yml file.
4. Your webpack configuration is not creating a manifest.
1. You want to set webpacker.yml value of compile to true for your environment unless you are using the webpack -w or the webpack-dev-server.
my config/webpacker.yml contains
production:
<<: *default
# Production depends on precompilation of packs prior to booting for performance.
compile: true
# Extract and emit a css file
extract_css: true
# Cache manifest.json for performance
cache_manifest: true
2. webpack has not yet re-run to reflect updates.
I run
$ RAILS_ENV=production bundle exec rails webpacker:compile
3. You have misconfigured Webpacker's config/webpacker.yml file.
in development environment webpacker works correctly.
4. Your webpack configuration is not creating a manifest.
manifest.json is created
I have the same issue when I run pre-built rails 6 app.Found out it is because of Webpack version inconsistency in yarn.lock file.
Then when I run
yarn add #rails/webpacker
bundle update webpacker
as found in this comment , issue's solved.
Remove Webpacker:
rm -rf bin/webpack*
Install Webpacker:
rails webpacker:install
Compile by Webpacker in production mode:
RAILS_ENV=production rails webpacker:compile
About this
I'd the same problem. Just run in development environment:
rails webpacker:install
If you use Docker, run:
docker exec rails-app-name rails webpacker:install
If the manifest.json does not exist in your filesystem create it with rake assets:precompile.
For me what worked was to replace
<%= stylesheet_pack_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
with <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
In the application.html.erb file
I'm trying to run jest tests alongside my rails application in Travis CI. This is what pops up in the Travis CI logs.
$ yarn test
yarn run v1.9.4
$ jest
/bin/sh: jest: not found
error Command failed with exit code 127.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Here's my .travis.yml
sudo: required
services:
- docker
- redis-server
language: ruby
cache:
bundler: true
directories:
- node_modules
yarn: true
addons:
postgresql: "9.6"
chrome: stable
rvm:
- 2.5.1
before_script:
- cp config/database.yml{.sample,}
- bundle exec rake db:create db:test:prepare assets:precompile
install:
- bundle install
- nvm install node
- node -v
- npm i -g yarn
- yarn
script:
- yarn test
- bundle exec rake test
Here's my package.json > scripts and devDependencies section
"scripts": { "test": "jest" },
"devDependencies": {
"#vue/test-utils": "^1.0.0-beta.25",
"babel-jest": "^23.6.0",
"babel-preset-es2015": "^6.24.1",
"eslint": "^5.5.0",
"eslint-plugin-jest": "^21.22.0",
"eslint-plugin-vue": "^4.7.1",
"jest": "^23.6.0",
"jest-canvas-mock": "^1.1.0",
"stylelint": "^9.5.0",
"stylelint-config-standard": "^18.2.0",
"vue-jest": "^2.6.0",
"webpack-dev-server": "2.11.2"
},
"jest": {
"roots": [
It fails on yarn test. When I debug the job and re-run the steps one-by-one it succeeds and it can find and run jest successfully. I've also tried clearing caches for the repo.
Am I missing something on my setup?
I figured it out, the offending step is:
before_script:
- cp config/database.yml{.sample,}
- bundle exec rake db:create db:test:prepare assets:precompile
install:
- bundle install
- nvm install node
- node -v
- npm i -g yarn
- yarn
script:
- yarn test
- bundle exec rake test
The bundle exec rake assets:precompile compiles the webpack bundle for production and removes devDependencies. So when it gets to the script step it cannot find jest anymore. The short term fix was removing assets:precompile in the before_script. Long-term would be to make a separate job testing for asset precompilation itself.
I have installed bower in the app server and I am able to run bundle exec rake bower:install in any release or in current directory. But when I'm trying the same while cap production deploy, the following error is repeated.
Command:
cd /home/deploy/apps/xxxx/releases/20150910064532 && ~/.rvm/bin/rvm default do bundle exec rake bower:install CI=true
Bower not found! You can install Bower using Node and npm:
DEBUG [68147ae5] $ npm install bower -g
DEBUG [68147ae5] For more info see http://bower.io/
config in deploy.rb
namespace :bower do
desc 'Install bower'
task :install do
on roles(:web) do
within release_path do
execute :rake, 'bower:install CI=true'
end
end
end
end
before 'deploy:compile_assets', 'bower:install'