application-xxxxxxx.jsがNot Foundになる
rails6.0でgem 'webpacker', gem 'react-rails'
を用いてreact.jsを導入しました。
開発環境では問題なく使えていたjavascirptが、本番環境ではaplication.jsが読み込まれず、js部分が一切適応されなくなりました。
public/assetsは問題なく作成されていて、cssも効いています。
README通りに、以下のコマンドを実行して、本番環境のAWS EC2にもreactを導入したのち、
$ bundle install $ rails webpacker:install $ rails webpacker:install:react $ rails generate react:install
cssファイルをprecompileするために、次のコードを実行した。
$ rails assets:precompile RAILS_ENV=production
react関連のjavascriptをprecompileするために webpack-dev-serverを起動しました。
[takaya@ip-10-0-0-222 rails_app] $ bin/webpack-dev-server ℹ 「wds」: Project is running at http://localhost:3035/ ℹ 「wds」: webpack output is served from /packs/ ℹ 「wds」: Content not from webpack is served from /var/www/rails/rails_app/public/packs ℹ 「wds」: 404s will fallback to /index.html ℹ 「wdm」: Hash: f3006a028632deca77e4 Version: webpack 4.44.2 Time: 10272ms Built at: 12/01/2020 12:50:27 PM Asset Size Chunks Chunk Names js/application-b87f0d4fd4c3fba00f5a.js 13.3 MiB application [emitted] [immutable] application js/application-b87f0d4fd4c3fba00f5a.js.map 13.4 MiB application [emitted] [dev] application js/hello_react-35afa24b0b3d6cd6614c.js 1.47 MiB hello_react [emitted] [immutable] hello_react js/hello_react-35afa24b0b3d6cd6614c.js.map 1.69 MiB hello_react [emitted] [dev] hello_react js/server_rendering-93ddcf26a452421c2437.js 12.9 MiB server_rendering [emitted] [immutable] server_rendering js/server_rendering-93ddcf26a452421c2437.js.map 12.9 MiB server_rendering [emitted] [dev] server_rendering manifest.json 1.05 KiB [emitted] ℹ 「wdm」: Compiled successfully.
webpack-dev-serverでのcompileは成功しているのですが、pullic/packs/jsの中は一切からのままで、packsフォルダーの中にmanifest.jsonがあり、precompileされたfileの名前だけが残されています。
[takaya@ip-10-0-0-222 public]$ cd packs [takaya@ip-10-0-0-222 packs]$ ls js manifest.json nohup.out [takaya@ip-10-0-0-222 packs]$ cd js [takaya@ip-10-0-0-222 js]$ ls [takaya@ip-10-0-0-222 js]$
こちらはmanifest.jsonの内容になります。
json:manifest.json
1{ 2 "application.js": "/packs/js/application-b87f0d4fd4c3fba00f5a.js", 3 "application.js.map": "/packs/js/application-b87f0d4fd4c3fba00f5a.js.map", 4 "entrypoints": { 5 "application": { 6 "js": [ 7 "/packs/js/application-b87f0d4fd4c3fba00f5a.js" 8 ], 9 "js.map": [ 10 "/packs/js/application-b87f0d4fd4c3fba00f5a.js.map" 11 ] 12 }, 13 "hello_react": { 14 "js": [ 15 "/packs/js/hello_react-35afa24b0b3d6cd6614c.js" 16 ], 17 "js.map": [ 18 "/packs/js/hello_react-35afa24b0b3d6cd6614c.js.map" 19 ] 20 }, 21 "server_rendering": { 22 "js": [ 23 "/packs/js/server_rendering-93ddcf26a452421c2437.js" 24 ], 25 "js.map": [ 26 "/packs/js/server_rendering-93ddcf26a452421c2437.js.map" 27 ] 28 } 29 }, 30 "hello_react.js": "/packs/js/hello_react-35afa24b0b3d6cd6614c.js", 31 "hello_react.js.map": "/packs/js/hello_react-35afa24b0b3d6cd6614c.js.map", 32 "server_rendering.js": "/packs/js/server_rendering-93ddcf26a452421c2437.js", 33 "server_rendering.js.map": "/packs/js/server_rendering-93ddcf26a452421c2437.js.map"}
production.rbのエラー内容はこちらです。
ruby:
1 ActionController::RoutingError (No route matches [GET] "/packs/js/application-b87f0d4fd4c3fba00f5a.js"): 2[370489fe-7bb5-4a00-aad7-e4e2903e0a03]
##主な問題
webpack-dev-serverでcompileされたjs fileがpublic/packs/js内に出力されず、
その結果application.jsがNot foundで適応されていないです。
もしわかる人がいましたら、正しいjs fileのprecompileの仕方を教えて頂きたいです。
各設定ファイル
app/javascipt/packs/application.js
javascript:application.js
1// This file is automatically compiled by Webpack, along with any other files 2// present in this directory. You're encouraged to place your actual application logic in 3// a relevant structure within app/javascript and only use these pack files to reference 4// that code so it'll be compiled. 5 6require("@rails/ujs").start() 7require("@rails/activestorage").start() 8require("channels") 9require("jquery")//jquery追加用 10 11// Uncomment to copy all static images under ../images to the output folder and reference 12// them with the image_pack_tag helper in views (e.g <%= image_pack_tag 'rails.png' %>) 13// or the `imagePath` JavaScript helper below. 14// 15// const images = require.context('../images', true) 16// const imagePath = (name) => images(name, true) 17// Support component names relative to this directory: 18var componentRequireContext = require.context("components", true); 19var ReactRailsUJS = require("react_ujs"); 20ReactRailsUJS.useContext(componentRequireContext);
package.json
json
1{ 2 "name": "app", 3 "private": true, 4 "dependencies": { 5 "@babel/preset-react": "^7.12.5", 6 "@rails/actioncable": "^6.0.0", 7 "@rails/activestorage": "^6.0.0", 8 "@rails/ujs": "^6.0.0", 9 "@rails/webpacker": "4.3.0", 10 "babel-plugin-transform-react-remove-prop-types": "^0.4.24", 11 "dotenv": "^8.2.0", 12 "google-map-react": "^2.1.9", 13 "jquery": "^3.5.1", 14 "prop-types": "^15.7.2", 15 "react": "^17.0.1", 16 "react-awesome-stars-rating": "^0.2.5", 17 "react-dom": "^17.0.1", 18 "react-icons": "^3.11.0", 19 "react-responsive": "^8.1.1", 20 "react_ujs": "^2.6.1" 21 }, 22 "version": "0.1.0", 23 "devDependencies": { 24 "webpack-dev-server": "^3.11.0" 25 } 26}
config/environments/produciton.rb
ruby
1const { environment } = require('@rails/webpacker') 2//追記 3const webpack = require('webpack') 4environment.plugins.prepend('Provide', 5 new webpack.ProvidePlugin({ 6 $: 'jquery/src/jquery', 7 jQuery: 'jquery/src/jquery' 8 }) 9) 10//ここまで 11module.exports = environment
webpacker.yml
yml
1# Note: You must restart bin/webpack-dev-server for changes to take effect 2 3default: &default 4 source_path: app/javascript 5 source_entry_path: packs 6 public_root_path: public 7 public_output_path: packs 8 cache_path: tmp/cache/webpacker 9 check_yarn_integrity: false 10 webpack_compile_output: true 11 12 # Additional paths webpack should lookup modules 13 # ['app/assets', 'engine/foo/app/assets'] 14 resolved_paths: [] 15 16 # Reload manifest.json on all requests so we reload latest compiled packs 17 cache_manifest: false 18 19 # Extract and emit a css file 20 extract_css: false 21 22 static_assets_extensions: 23 - .jpg 24 - .jpeg 25 - .png 26 - .gif 27 - .tiff 28 - .ico 29 - .svg 30 - .eot 31 - .otf 32 - .ttf 33 - .woff 34 - .woff2 35 36 extensions: 37 - .jsx 38 - .mjs 39 - .js 40 - .sass 41 - .scss 42 - .css 43 - .module.sass 44 - .module.scss 45 - .module.css 46 - .png 47 - .svg 48 - .gif 49 - .jpeg 50 - .jpg 51 52development: 53 <<: *default 54 compile: true 55 56 # Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock, and node_modules 57 check_yarn_integrity: true 58 59 # Reference: https://webpack.js.org/configuration/dev-server/ 60 dev_server: 61 https: false 62 host: localhost 63 port: 3035 64 public: localhost:3035 65 hmr: false 66 # Inline should be set to true if using HMR 67 inline: true 68 overlay: true 69 compress: true 70 disable_host_check: true 71 use_local_ip: false 72 quiet: false 73 pretty: false 74 headers: 75 'Access-Control-Allow-Origin': '*' 76 watch_options: 77 ignored: '**/node_modules/**' 78 79 80test: 81 <<: *default 82 compile: true 83 84 # Compile test packs to a separate directory 85 public_output_path: packs-test 86 87production: 88 <<: *default 89 90 # Production depends on precompilation of packs prior to booting for performance. 91 compile: true 92 93 # Extract and emit a css file 94 extract_css: true 95 96 # Cache manifest.json for performance 97 cache_manifest: true 98
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。