Skip to content

Commit f4cc31d

Browse files
author
David Heinemeier Hansson
committed
FIRST!1!
0 parents  commit f4cc31d

File tree

16 files changed

+267
-0
lines changed

16 files changed

+267
-0
lines changed

MIT-LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2016 David Heinemeier Hansson, Basecamp
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Webpacker
2+
3+
Webpacker makes it easy to use the JavaScript preprocessor and bundler Webpack
4+
to manage application-like JavaScript in Rails. It coexists with the asset pipeline,
5+
as the purpose is only to use Webpack for app-like JavaScript, not images, css, or
6+
even JavaScript Sprinkles.
7+
8+
It's designed to work with Rails 5.1+ and makes use of the Yarn dependency management
9+
that's been made default from that version forward. You can either make use of Webpacker
10+
during setup of a new application with --webpack or you can uncomment the gem and run
11+
`bin/rails webpacker:install` in an existing application.
12+
13+
When Webpacker has been installed...
14+
15+
FIXME: Write the rest...
16+
17+
## License
18+
Webpacker is released under the [MIT License](http://www.opensource.org/licenses/MIT).

lib/install/bin/webpack-watcher.tt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<%= shebang %>
2+
3+
BIN_PATH = File.expand_path('.', __dir__)
4+
5+
Dir.chdir(BIN_PATH) do
6+
exec "./webpack --watch --progress --color #{ARGV.join(" ")}"
7+
end

lib/install/bin/webpack.tt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<%= shebang %>
2+
3+
RAILS_ENV = ENV['RAILS_ENV'] || 'development'
4+
WEBPACK_ENV = ENV['WEBPACK_ENV'] || RAILS_ENV
5+
6+
APP_PATH = File.expand_path('../', __dir__)
7+
VENDOR_PATH = File.expand_path('../vendor', __dir__)
8+
9+
SET_NODE_PATH = "NODE_PATH=#{VENDOR_PATH}/node_modules"
10+
WEBPACK_BIN = "./node_modules/webpack/bin/webpack.js"
11+
WEBPACK_CONFIG = "#{APP_PATH}/config/webpack/#{WEBPACK_ENV}.js"
12+
13+
Dir.chdir(VENDOR_PATH) do
14+
exec "#{SET_NODE_PATH} #{WEBPACK_BIN} --config #{WEBPACK_CONFIG} #{ARGV.join(" ")}"
15+
end

lib/install/config/development.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Note: You must restart bin/webpack-watcher for changes to take effect
2+
3+
var path = require('path')
4+
var webpack = require('webpack')
5+
var _ = require('lodash')
6+
7+
var config = module.exports = require('./shared.js')
8+
9+
config = _.merge(config, {
10+
debug: true,
11+
displayErrorDetails: true,
12+
outputPathinfo: true,
13+
devtool: 'sourcemap',
14+
})

lib/install/config/production.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Note: You must restart bin/webpack-watcher for changes to take effect
2+
3+
var path = require('path')
4+
var webpack = require('webpack')
5+
var _ = require('lodash')
6+
7+
var config = module.exports = require('./shared.js');
8+
9+
config.output = _.merge(config.output, { filename: '[name]-[hash].js' })
10+
11+
config.plugins.push(
12+
new webpack.optimize.UglifyJsPlugin(),
13+
new webpack.optimize.OccurenceOrderPlugin()
14+
)

lib/install/config/shared.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Note: You must restart bin/webpack-watcher for changes to take effect
2+
3+
var path = require('path')
4+
var glob = require('glob')
5+
var _ = require('lodash')
6+
7+
module.exports = {
8+
entry: _.keyBy(glob.sync('../app/javascript/packs/*.js'), function(entry) { return path.basename(entry, '.js') }),
9+
10+
output: { filename: '[name].js', path: '../public/packs' },
11+
12+
module: {
13+
loaders: [
14+
{ test: /\.coffee$/, loader: "coffee-loader" }
15+
]
16+
},
17+
18+
plugins: [],
19+
20+
resolve: {
21+
extensions: [ '', '.js', '.coffee' ],
22+
root: [
23+
path.resolve('../app/javascript'),
24+
path.resolve('../vendor/node_modules')
25+
]
26+
},
27+
28+
resolveLoader: {
29+
modulesDirectories: [ path.resolve('../vendor/node_modules') ]
30+
}
31+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// var React = require('react')

lib/install/template.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
INSTALL_PATH = File.dirname(__FILE__)
2+
3+
directory "#{INSTALL_PATH}/javascript", 'app/javascript'
4+
5+
directory "#{INSTALL_PATH}/bin", 'bin'
6+
chmod 'bin', 0755 & ~File.umask, verbose: false
7+
8+
directory "#{INSTALL_PATH}/config", 'config/webpack'
9+
10+
run './bin/yarn add webpack lodash'
11+
12+
environment \
13+
"# Make javascript_pack_tag lookup digest hash to enable long-term caching\n" +
14+
" config.x.webpacker[:digesting] = true\n",
15+
env: 'production'

lib/tasks/webpacker.rake

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
PACKS_PATH = Rails.root.join('public/packs')
2+
PACK_DIGESTS_PATH = PACKS_PATH.join('digests.json')
3+
4+
WEBPACKER_APP_TEMPLATE_PATH = File.expand_path('../install/template.rb', File.dirname(__FILE__))
5+
6+
namespace :webpacker do
7+
desc "compile javascript packs using webpack for production with digests"
8+
task :compile do
9+
webpack_digests_json = JSON.parse(`WEBPACK_ENV=production ./bin/webpack --json`)['assetsByChunkName'].to_json
10+
11+
FileUtils.mkdir_p(PACKS_PATH)
12+
File.open(PACK_DIGESTS_PATH, 'w+') { |file| file.write webpack_digests_json }
13+
14+
puts "Compiled digests for all packs in #{PACK_DIGESTS_PATH}: "
15+
puts webpack_digests_json
16+
end
17+
18+
desc "install webpacker in this application"
19+
task :install do
20+
exec "./bin/rails app:template LOCATION=#{WEBPACKER_APP_TEMPLATE_PATH}"
21+
end
22+
end

0 commit comments

Comments
 (0)