rmosolgo / graphiql-rails

Mount the GraphiQL query editor in a Rails app
MIT License
447 stars 137 forks source link

Cannot compile assets with RAILS_ENV=production #44

Closed guingois closed 8 months ago

guingois commented 6 years ago
FROM ruby:2.5.1-alpine3.7

RUN apk add --no-cache build-base sqlite-dev tzdata nodejs=8.9.3-r1

ENV RAILS_VERSION 5.2.0

RUN gem install rails --version "$RAILS_VERSION"

RUN rails new --skip-bundle --database=sqlite3 dummy_app \
&& cd dummy_app \
&& echo 'gem "graphiql-rails", "1.4.10"' >> Gemfile \
&& bundle

WORKDIR /dummy_app
docker build --tag giql .
docker run --rm --interactive --tty giql sh
/dummy_app # RAILS_ENV=development bundle exec rake assets:precompile
Yarn executable was not detected in the system.
Download Yarn at https://yarnpkg.com/en/docs/install
I, [2018-04-18T10:16:37.489067 #35]  INFO -- : Writing /dummy_app/public/assets/application-f360afb1ab93d9c96eeba633778fb50e4860f856ce8e22b383401daaddc917e8.js
I, [2018-04-18T10:16:37.501698 #35]  INFO -- : Writing /dummy_app/public/assets/application-f360afb1ab93d9c96eeba633778fb50e4860f856ce8e22b383401daaddc917e8.js.gz
I, [2018-04-18T10:16:37.505899 #35]  INFO -- : Writing /dummy_app/public/assets/application-f0d704deea029cf000697e2c0181ec173a1b474645466ed843eb5ee7bb215794.css
I, [2018-04-18T10:16:37.506153 #35]  INFO -- : Writing /dummy_app/public/assets/application-f0d704deea029cf000697e2c0181ec173a1b474645466ed843eb5ee7bb215794.css.gz
I, [2018-04-18T10:16:37.984407 #35]  INFO -- : Writing /dummy_app/public/assets/graphiql/rails/application-09ecfeeb9885e7c9d9529f06c4360e92312b9c15f455df0e6d29ed6098ae85ee.js
I, [2018-04-18T10:16:37.984569 #35]  INFO -- : Writing /dummy_app/public/assets/graphiql/rails/application-09ecfeeb9885e7c9d9529f06c4360e92312b9c15f455df0e6d29ed6098ae85ee.js.gz
I, [2018-04-18T10:16:38.003801 #35]  INFO -- : Writing /dummy_app/public/assets/graphiql/rails/application-2fbb932618633e58c4f72482da2714cb25f2c9fc91e26fb33df63a1e5b47f74c.css
I, [2018-04-18T10:16:38.003948 #35]  INFO -- : Writing /dummy_app/public/assets/graphiql/rails/application-2fbb932618633e58c4f72482da2714cb25f2c9fc91e26fb33df63a1e5b47f74c.css.gz
/dummy_app # RAILS_ENV=production bundle exec rake assets:precompile
Yarn executable was not detected in the system.
Download Yarn at https://yarnpkg.com/en/docs/install
I, [2018-04-18T10:15:09.415186 #8]  INFO -- : Writing /dummy_app/public/assets/application-3d9f5ba74dcfc600bc2f9dc7e80aa5ac058cb0eecd52dec7490cb3dda09307d5.js
I, [2018-04-18T10:15:09.428073 #8]  INFO -- : Writing /dummy_app/public/assets/application-3d9f5ba74dcfc600bc2f9dc7e80aa5ac058cb0eecd52dec7490cb3dda09307d5.js.gz
I, [2018-04-18T10:15:09.436581 #8]  INFO -- : Writing /dummy_app/public/assets/application-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css
I, [2018-04-18T10:15:09.436894 #8]  INFO -- : Writing /dummy_app/public/assets/application-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css.gz
rake aborted!
Uglifier::Error: In strict mode code, functions can only be declared at top level or immediately within another function.
/usr/local/bundle/gems/uglifier-4.1.9/lib/uglifier.rb:234:in `parse_result'
/usr/local/bundle/gems/uglifier-4.1.9/lib/uglifier.rb:216:in `run_uglifyjs'
/usr/local/bundle/gems/uglifier-4.1.9/lib/uglifier.rb:168:in `compile'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/uglifier_compressor.rb:53:in `call'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/uglifier_compressor.rb:28:in `call'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:75:in `call_processor'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:57:in `block in call_processors'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:56:in `reverse_each'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/processor_utils.rb:56:in `call_processors'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/loader.rb:134:in `load_from_unloaded'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/loader.rb:60:in `block in load'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/loader.rb:317:in `fetch_asset_from_dependency_cache'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/loader.rb:44:in `load'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/cached_environment.rb:20:in `block in initialize'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/cached_environment.rb:47:in `load'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/base.rb:66:in `find_asset'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/base.rb:73:in `find_all_linked_assets'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/manifest.rb:142:in `block in find'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:114:in `block (2 levels) in logical_paths'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:228:in `block in stat_tree'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:212:in `block in stat_directory'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:209:in `each'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:209:in `stat_directory'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:227:in `stat_tree'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:231:in `block in stat_tree'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:212:in `block in stat_directory'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:209:in `each'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:209:in `stat_directory'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:227:in `stat_tree'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:231:in `block in stat_tree'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:212:in `block in stat_directory'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:209:in `each'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:209:in `stat_directory'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/path_utils.rb:227:in `stat_tree'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:105:in `each'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:105:in `block in logical_paths'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:104:in `each'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/legacy.rb:104:in `logical_paths'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/manifest.rb:140:in `find'
/usr/local/bundle/gems/sprockets-3.7.1/lib/sprockets/manifest.rb:185:in `compile'
/usr/local/bundle/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:68:in `block (3 levels) in define'
/usr/local/bundle/gems/sprockets-3.7.1/lib/rake/sprocketstask.rb:147:in `with_logger'
/usr/local/bundle/gems/sprockets-rails-3.2.1/lib/sprockets/rails/task.rb:67:in `block (2 levels) in define'
/usr/local/bundle/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
/usr/local/bundle/bin/bundle:104:in `load'
/usr/local/bundle/bin/bundle:104:in `<main>'
Tasks: TOP => assets:precompile
(See full trace by running task with --trace)

The error doesn't happen if I remove gem "graphiql-rails" in the Gemfile.

rmosolgo commented 6 years ago

Sorry to hear it! It looks like uglify is choking on some of the GraphiQL assets. You could debug further by trying to figure out which minified code is causing problems. Also, you could try adjusting the version/settings of uglify, or by updating our GraphiQL version (if a new one is available?) to see if the newer version doesn't have that trouble.

guingois commented 6 years ago

I've tried updating GraphiQL (using the dedicated Rake task), and the issue remains.

I've also tried requiring different combinations of assets in graphiql-rails' application.js. There isn't a single asset responsible for the failure: it happens only when the compilation requires some specific combinations, and each asset taken separately can be compiled without errors. Therefore I'll try to focus the debugging on Uglifier, rather than on graphiql-rails or GraphiQL.

Thanks for your help, feel free to close the issue if you think it's no longer relevant to this project.

rebeccajfriedman commented 6 years ago

I'm also experiencing this issue ^

eiapopeia commented 6 years ago

It works for me until version 1.4.8 of graphiql-rails and fails for 1.4.9 and 1.4.10. So some changes in the path graphiql-rails/app/assets/javascripts/graphiql/rails/ may cause the issue. Ruby: 2.5.1 Rails: 5.2.0 uglifier: 4.1.10

denisahearn commented 6 years ago

I encountered the same problem with graphiql-rails 1.4.10. Falling back to 1.4.8 works around the problem for me.

longkt90 commented 6 years ago

TLDR: change config/environments/production.rb (and config/environments/staging.rb) from config.assets.js_compressor = :uglifier to config.assets.js_compressor = Uglifier.new(harmony: true) fix this issue

More details: From https://github.com/rmosolgo/graphiql-rails/commit/3eb2491475823d332c314611e73288200998c566, we changed reactjs from 16.0.0 to 16.2.0 which cause this https://github.com/facebook/react/issues/12434

I tested with @savybrandt comment to change production.rb from config.assets.js_compressor = :uglifier to config.assets.js_compressor = Uglifier.new(harmony: true) and it is working fine.

Thanks @erwanthomas for the Dockerfile, very helpful to debug and change the gem version around, this is the modified version to show it works.

FROM ruby:2.5.1-alpine3.7

RUN apk add --no-cache build-base sqlite-dev tzdata nodejs=8.9.3-r1

ENV RAILS_VERSION 5.2.0

RUN gem install rails --version "$RAILS_VERSION"

ENV RAILS_ENV production

RUN rails new --database=sqlite3 dummy_app && cd dummy_app

WORKDIR /dummy_app

RUN echo 'gem "graphiql-rails", "1.4.10"' >> Gemfile && bundle

RUN sed -i -e 's/config.assets.js_compressor = :uglifier/config.assets.js_compressor = Uglifier.new(harmony: true) /g' config/environments/production.rb

RUN bundle exec rake assets:precompile 
bjeanes commented 6 years ago

Setting config.assets.js_compressor = Uglifier.new(harmony: true) also worked from me.

alex-andreiev commented 4 years ago

It works for me until version 1.4.8 of graphiql-rails and fails for 1.4.9 and 1.4.10. So some changes in the path graphiql-rails/app/assets/javascripts/graphiql/rails/ may cause the issue. Ruby: 2.5.1 Rails: 5.2.0 uglifier: 4.1.10

It works for me!

rmosolgo commented 8 months ago

Hey, I hope that the fix recommended above worked. If anyone encounters this on a new version, please open a new issue!