Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpackerとvue.jsの導入 #7

Merged
merged 5 commits into from
Mar 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"presets": [
["env", {
"modules": false,
"targets": {
"browsers": "> 1%",
"uglify": true
},
"useBuiltIns": true
}]
],

"plugins": [
"syntax-dynamic-import",
"transform-object-rest-spread",
["transform-class-properties", { "spec": true }]
]
}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,8 @@
config/settings.local.yml
config/settings/*.local.yml
config/environments/*.local.yml
/public/packs
/public/packs-test
/node_modules
yarn-debug.log*
.yarn-integrity
3 changes: 3 additions & 0 deletions .postcssrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
plugins:
postcss-import: {}
postcss-cssnext: {}
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ gem 'pg', '~> 0.18'
gem 'puma', '~> 3.7'

# Frontend
# gem 'webpacker'
gem 'webpacker'

# Backend
gem 'jbuilder', '~> 2.5'
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ GEM
rack (2.0.4)
rack-mini-profiler (0.10.7)
rack (>= 1.2.0)
rack-proxy (0.6.4)
rack
rack-test (0.8.3)
rack (>= 1.0, < 3)
rails (5.1.5)
Expand Down Expand Up @@ -235,6 +237,10 @@ GEM
activemodel (>= 5.0)
bindex (>= 0.4.0)
railties (>= 5.0)
webpacker (3.3.0)
activesupport (>= 4.2)
rack-proxy (>= 0.6.1)
railties (>= 4.2)
websocket-driver (0.6.5)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
Expand Down Expand Up @@ -268,6 +274,7 @@ DEPENDENCIES
timecop
tzinfo-data
web-console (>= 3.3.0)
webpacker

BUNDLED WITH
1.16.1
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@
git clone git@github.com:kimuray/dic_memo.git
cd dic_memo
bundle install --path vendor/bundle
yarn
bin/webpack-dev-server
rails s
```

### 追記
`yarn` : javascriptのパッケージマネージャー(Rubyでいうところのbundler的なもの)
`bin/webpack-dev-server` : 開発中にjavascriptの変更を検知して開発環境用に自動でビルドしてくれるもの

## Development Flow

1. `git checkout develop`
Expand Down
Empty file added app/frontend/images/.keep
Empty file.
15 changes: 15 additions & 0 deletions app/frontend/javascripts/application.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint no-console:0 */
// This file is automatically compiled by Webpack, along with any other files
// present in this directory. You're encouraged to place your actual application logic in
// a relevant structure within app/javascript and only use these pack files to reference
// that code so it'll be compiled.
//
// To reference this file, add <%= javascript_pack_tag 'application' %> to the appropriate
// layout file, like app/views/layouts/application.html.erb

import 'stylesheets/application';
import Rails from 'rails-ujs';

require.context('images', true, /\.(png|jpg|jpeg|svg)$/)

Rails.start();
Empty file.
4 changes: 2 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
<title>DicMemo</title>
<%= csrf_meta_tags %>

<%= stylesheet_link_tag 'application', media: 'all' %>
<%= javascript_include_tag 'application' %>
<%= stylesheet_pack_tag 'application', media: 'all' %>
<%= javascript_pack_tag 'application' %>
</head>

<body>
Expand Down
4 changes: 1 addition & 3 deletions app/views/users/dashboard.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
<p>Find me in app/views/users/dashboard.html.erb</p>

<% if user_signed_in? %>
<!-- リンクを踏むとdeleteではなくgetメソッドが飛びRouting Errorが発生。Webpack導入後に method: :delete に戻す -->
<p><%= link_to "log out", destroy_user_session_path, method: :get %></p>
<!-- <p><%#= link_to "log out", destroy_user_session_path, method: :delete %></p> -->
<p><%= link_to "log out", destroy_user_session_path, method: :delete %></p>
<% else %>
<p><%= link_to "user login", user_session_path, method: :get %></p>
<% end %>
15 changes: 15 additions & 0 deletions bin/webpack
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env ruby

ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
ENV["NODE_ENV"] ||= ENV["RAILS_ENV"]

require "pathname"
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)

require "rubygems"
require "bundler/setup"

require "webpacker"
require "webpacker/webpack_runner"
Webpacker::WebpackRunner.run(ARGV)
15 changes: 15 additions & 0 deletions bin/webpack-dev-server
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/usr/bin/env ruby

ENV["RAILS_ENV"] ||= ENV["RACK_ENV"] || "development"
ENV["NODE_ENV"] ||= ENV["RAILS_ENV"]

require "pathname"
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)

require "rubygems"
require "bundler/setup"

require "webpacker"
require "webpacker/dev_server_runner"
Webpacker::DevServerRunner.run(ARGV)
3 changes: 3 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Rails.application.configure do
# Verifies that versions and hashed value of the package contents in the project's package.json
config.webpacker.check_yarn_integrity = true

# Settings specified here will take precedence over those in config/application.rb.

# In the development environment your application's code is reloaded on
Expand Down
3 changes: 3 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Rails.application.configure do
# Verifies that versions and hashed value of the package contents in the project's package.json
config.webpacker.check_yarn_integrity = false

# Settings specified here will take precedence over those in config/application.rb.

# Code is not reloaded between requests.
Expand Down
4 changes: 1 addition & 3 deletions config/initializers/devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,7 @@
# config.navigational_formats = ['*/*', :html]

# The default HTTP method used to sign out a resource. Default is :delete.
# リンクを踏むとdeleteではなくgetメソッドが飛びRouting Errorが発生。Webpack導入後に config.sign_out_via = :delete に戻す
config.sign_out_via = :get
# config.sign_out_via = :delete
config.sign_out_via = :delete

# ==> OmniAuth
# Add a new OmniAuth provider. Check the wiki for more information on setting
Expand Down
3 changes: 3 additions & 0 deletions config/webpack/development.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const environment = require('./environment')

module.exports = environment.toWebpackConfig()
5 changes: 5 additions & 0 deletions config/webpack/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { environment } = require('@rails/webpacker')
const vue = require('./loaders/vue')

environment.loaders.append('vue', vue)
module.exports = environment
13 changes: 13 additions & 0 deletions config/webpack/loaders/vue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const { dev_server: devServer } = require('@rails/webpacker').config

const isProduction = process.env.NODE_ENV === 'production'
const inDevServer = process.argv.find(v => v.includes('webpack-dev-server'))
const extractCSS = !(inDevServer && (devServer && devServer.hmr)) || isProduction

module.exports = {
test: /\.vue(\.erb)?$/,
use: [{
loader: 'vue-loader',
options: { extractCSS }
}]
}
3 changes: 3 additions & 0 deletions config/webpack/production.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const environment = require('./environment')

module.exports = environment.toWebpackConfig()
3 changes: 3 additions & 0 deletions config/webpack/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const environment = require('./environment')

module.exports = environment.toWebpackConfig()
69 changes: 69 additions & 0 deletions config/webpacker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Note: You must restart bin/webpack-dev-server for changes to take effect

default: &default
source_path: app/frontend
source_entry_path: javascripts
public_output_path: packs
cache_path: tmp/cache/webpacker

# Additional paths webpack should lookup modules
# ['app/assets', 'engine/foo/app/assets']
resolved_paths: []

# Reload manifest.json on all requests so we reload latest compiled packs
cache_manifest: false

extensions:
- .vue
- .js
- .sass
- .scss
- .css
- .module.sass
- .module.scss
- .module.css
- .png
- .svg
- .gif
- .jpeg
- .jpg

development:
<<: *default
compile: true

# Reference: https://webpack.js.org/configuration/dev-server/
dev_server:
https: false
host: localhost
port: 3035
public: localhost:3035
hmr: false
# Inline should be set to true if using HMR
inline: true
overlay: true
compress: true
disable_host_check: true
use_local_ip: false
quiet: false
headers:
'Access-Control-Allow-Origin': '*'
watch_options:
ignored: /node_modules/


test:
<<: *default
compile: true

# Compile test packs to a separate directory
public_output_path: packs-test

production:
<<: *default

# Production depends on precompilation of packs prior to booting for performance.
compile: false

# Cache manifest.json for performance
cache_manifest: true
12 changes: 11 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
{
"name": "dic_memo",
"private": true,
"dependencies": {}
"dependencies": {
"@rails/webpacker": "^3.3.0",
"rails-ujs": "^5.1.5",
"vue": "^2.5.14",
"vue-loader": "^14.2.1",
"vue-template-compiler": "^2.5.14"
},
"devDependencies": {
"webpack-cli": "^2.0.11",
"webpack-dev-server": "^2.11.1"
}
}
Loading