Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ gem "rubocop", ">= 0.49", require: false

group :test do
gem "minitest", "~> 5.0"
gem "byebug"
end
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ GEM
arel (7.1.4)
ast (2.3.0)
builder (3.2.3)
byebug (9.0.6)
concurrent-ruby (1.0.5)
erubis (2.7.0)
globalid (0.3.7)
Expand Down Expand Up @@ -128,6 +129,7 @@ PLATFORMS

DEPENDENCIES
bundler (~> 1.12)
byebug
minitest (~> 5.0)
rails
rake (>= 11.1)
Expand Down
4 changes: 2 additions & 2 deletions lib/install/angular.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
puts "Copying angular loader to config/webpack/loaders"
copy_file "#{__dir__}/config/loaders/installers/angular.js", "config/webpack/loaders/angular.js"

puts "Copying angular example entry file to #{Webpacker::Configuration.entry_path}"
copy_file "#{__dir__}/examples/angular/hello_angular.js", "#{Webpacker::Configuration.entry_path}/hello_angular.js"
puts "Copying angular example entry file to #{Webpacker::Configuration.source_entry_path}"
copy_file "#{__dir__}/examples/angular/hello_angular.js", "#{Webpacker::Configuration.source_entry_path}/hello_angular.js"

puts "Copying hello_angular app to #{Webpacker::Configuration.source_path}"
directory "#{__dir__}/examples/angular/hello_angular", "#{Webpacker::Configuration.source_path}/hello_angular"
Expand Down
13 changes: 6 additions & 7 deletions lib/install/elm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,23 @@
copy_file "#{__dir__}/config/loaders/installers/elm.js",
"config/webpack/loaders/elm.js"

puts "Copying elm example entry file to #{Webpacker::Configuration.entry_path}"
copy_file "#{__dir__}/examples/elm/Main.elm", "#{Webpacker::Configuration.entry_path}/Main.elm"
puts "Copying elm example entry file to #{Webpacker::Configuration.source_entry_path}"
copy_file "#{__dir__}/examples/elm/Main.elm", "#{Webpacker::Configuration.source_entry_path}/Main.elm"

puts "Copying elm app file to #{Webpacker::Configuration.entry_path}"
puts "Copying elm app file to #{Webpacker::Configuration.source_entry_path}"
copy_file "#{__dir__}/examples/elm/hello_elm.js",
"#{Webpacker::Configuration.entry_path}/hello_elm.js"
"#{Webpacker::Configuration.source_entry_path}/hello_elm.js"

puts "Installing all elm dependencies"
run "yarn add elm elm-webpack-loader"
run "yarn add --dev elm-hot-loader"
run "yarn run elm package install -- --yes"

puts "Updating Webpack paths to include Elm file extension"
insert_into_file Webpacker::Configuration.file_path, " - .elm\n", after: /extensions:\n/
insert_into_file Webpacker::Configuration.config_path, " - .elm\n", after: /extensions:\n/

puts "Updating elm source location"
source_path = File.join(Webpacker::Configuration.source, Webpacker::Configuration.fetch(:source_entry_path))
gsub_file "elm-package.json", /\"\.\"\n/, %("#{source_path}"\n)
gsub_file "elm-package.json", /\"\.\"\n/, %("#{Webpacker::Configuration.source_entry_path}"\n)

puts "Updating .gitignore to include elm-stuff folder"
insert_into_file ".gitignore", "/elm-stuff\n", before: "/node_modules\n"
Expand Down
4 changes: 2 additions & 2 deletions lib/install/react.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
puts "Copying react loader to config/webpack/loaders"
copy_file "#{__dir__}/config/loaders/installers/react.js", "config/webpack/loaders/react.js"

puts "Copying react example entry file to #{Webpacker::Configuration.entry_path}"
copy_file "#{__dir__}/examples/react/hello_react.jsx", "#{Webpacker::Configuration.entry_path}/hello_react.jsx"
puts "Copying react example entry file to #{Webpacker::Configuration.source_entry_path}"
copy_file "#{__dir__}/examples/react/hello_react.jsx", "#{Webpacker::Configuration.source_entry_path}/hello_react.jsx"

puts "Installing all react dependencies"
run "yarn add react react-dom babel-preset-react prop-types"
Expand Down
2 changes: 1 addition & 1 deletion lib/install/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
copy_file "#{__dir__}/config/.babelrc", ".babelrc"

puts "Creating javascript app source directory"
directory "#{__dir__}/javascript", "#{Webpacker::Configuration.source}"
directory "#{__dir__}/javascript", "app/javascript"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone re-runs webpacker:install this will re-create app/javascriptif the source path is not the default we ship

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I see. We can revert that then.


puts "Copying binstubs"
directory "#{__dir__}/bin", "bin"
Expand Down
8 changes: 4 additions & 4 deletions lib/install/vue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
puts "Copying vue loader to config/webpack/loaders"
copy_file "#{__dir__}/config/loaders/installers/vue.js", "config/webpack/loaders/vue.js"

puts "Copying the example entry file to #{Webpacker::Configuration.entry_path}"
copy_file "#{__dir__}/examples/vue/hello_vue.js", "#{Webpacker::Configuration.entry_path}/hello_vue.js"
puts "Copying the example entry file to #{Webpacker::Configuration.source_entry_path}"
copy_file "#{__dir__}/examples/vue/hello_vue.js", "#{Webpacker::Configuration.source_entry_path}/hello_vue.js"

puts "Copying vue app file to #{Webpacker::Configuration.entry_path}"
copy_file "#{__dir__}/examples/vue/app.vue", "#{Webpacker::Configuration.entry_path}/app.vue"
puts "Copying vue app file to #{Webpacker::Configuration.source_entry_path}"
copy_file "#{__dir__}/examples/vue/app.vue", "#{Webpacker::Configuration.source_entry_path}/app.vue"

puts "Installing all vue dependencies"
run "yarn add vue vue-loader vue-template-compiler"
Expand Down
5 changes: 2 additions & 3 deletions lib/tasks/webpacker/clobber.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ require "webpacker/configuration"
namespace :webpacker do
desc "Remove the webpack compiled output directory"
task clobber: ["webpacker:verify_install", :environment] do
output_path = Webpacker::Configuration.output_path
FileUtils.rm_r(output_path) if File.exist?(output_path)
$stdout.puts "Removed webpack output path directory #{output_path}"
Webpacker.clobber
$stdout.puts "Removed webpack output path directory #{Webpacker.config.public_output_path}"
end
end

Expand Down
12 changes: 10 additions & 2 deletions lib/tasks/webpacker/compile.rake
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
$stdout.sync = true

def ensure_log_goes_to_stdout
old_logger = Webpacker.logger
Webpacker.logger = ActiveSupport::Logger.new(STDOUT)
yield
ensure
Webpacker.logger = old_logger
end

namespace :webpacker do
desc "Compile javascript packs using webpack for production with digests"
task compile: ["webpacker:verify_install", :environment] do
Webpacker.ensure_log_goes_to_stdout do
if Webpacker::Compiler.compile
ensure_log_goes_to_stdout do
if Webpacker.compile
# Successful compilation!
else
# Failed compilation
Expand Down
4 changes: 2 additions & 2 deletions lib/tasks/webpacker/verify_install.rake
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ require "webpacker/configuration"
namespace :webpacker do
desc "Verifies if webpacker is installed"
task verify_install: [:check_node, :check_yarn, :check_binstubs] do
if File.exist?(Webpacker::Configuration.file_path)
if Webpacker.config.config_path.exist?
$stdout.puts "Webpacker is installed 🎉 🍰"
$stdout.puts "Using #{Webpacker::Configuration.file_path} file for setting up webpack paths"
$stdout.puts "Using #{Webpacker.config.config_path} file for setting up webpack paths"
else
$stderr.puts "Configuration config/webpacker.yml file not found. \n"\
"Make sure webpacker:install is run successfully before " \
Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/webpacker/yarn_install.rake
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
namespace :webpacker do
desc "Support for older Rails versions.Install all JavaScript dependencies as specified via Yarn"
desc "Support for older Rails versions. Install all JavaScript dependencies as specified via Yarn"
task :yarn_install, [:arg1, :arg2] do |task, args|
system "yarn #{args[:arg1]} #{args[:arg2]}"
end
Expand Down
28 changes: 15 additions & 13 deletions lib/webpacker.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,27 @@
require "active_support/core_ext/module/attribute_accessors"
require "active_support/logger"
require "active_support/tagged_logging"

module Webpacker
extend self

def bootstrap
Webpacker::Env.load
Webpacker::Configuration.load
Webpacker::Manifest.load

def instance=(instance)
@instance = instance
end

def compile
Webpacker::Compiler.compile
Webpacker::Manifest.load
def instance
@instance ||= Webpacker::Instance.new
end

def env
Webpacker::Env.current
end
delegate :logger, :logger=, :env, to: :instance
delegate :config, :compiler, :manifest, :commands, to: :instance
delegate :bootstrap, :clobber, :compile, to: :commands
end

require "webpacker/logger"
require "webpacker/env"
require "webpacker/instance"
require "webpacker/configuration"
require "webpacker/manifest"
require "webpacker/compiler"
require "webpacker/commands"

require "webpacker/railtie" if defined?(Rails)
22 changes: 22 additions & 0 deletions lib/webpacker/commands.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Webpacker::Commands
delegate :config, :compiler, :manifest, to: :@webpacker

def initialize(webpacker)
@webpacker = webpacker
end

def clobber
config.public_output_path.rmtree if config.public_output_path.exist?
config.cache_path.rmtree if config.cache_path.exist?
end

def bootstrap
config.refresh
manifest.refresh
end

def compile
compiler.compile
manifest.refresh
end
end
65 changes: 31 additions & 34 deletions lib/webpacker/compiler.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
require "open3"
require "webpacker/env"
require "webpacker/configuration"

module Webpacker::Compiler
extend self

delegate :cache_path, :output_path, :source, to: Webpacker::Configuration
delegate :logger, to: Webpacker

class Webpacker::Compiler
# Additional paths that test compiler needs to watch
# Webpacker::Compiler.watched_paths << 'bower_components'
mattr_accessor(:watched_paths) { [] }

delegate :config, :logger, :env, to: :@webpacker

def initialize(webpacker)
@webpacker = webpacker
end

def compile
if stale?
cache_source_timestamp
record_compilation_timestamp
run_webpack
else
logger.debug "No compiling needed as everything is fresh"
Expand All @@ -23,38 +22,27 @@ def compile

# Returns true if all the compiled packs are up to date with the underlying asset files.
def fresh?
if cached_timestamp_path.exist? && output_path.exist?
cached_timestamp_path.read == current_source_timestamp
else
false
end
source_last_changed_at == source_compiled_from_last_change_at
end

# Returns true if the compiled packs are out of date with the underlying asset files.
def stale?
!fresh?
end

# FIXME: Deprecate properly
alias_method :compile?, :fresh?

def default_watched_paths
["#{source}/**/*", "yarn.lock", "package.json", "config/webpack/**/*"].freeze
end

private
def current_source_timestamp
files = Dir[*default_watched_paths, *watched_paths].reject { |f| File.directory?(f) }
files.map { |f| File.mtime(f).utc.to_i }.max.to_s
def source_compiled_from_last_change_at
compilation_timestamp_path.read if compilation_timestamp_path.exist? && config.public_manifest_path.exist?
end

def cache_source_timestamp
cache_path.mkpath
cached_timestamp_path.write(current_source_timestamp)
def source_last_changed_at
files = Dir[*default_watched_paths, *watched_paths].reject { |f| File.directory?(f) }
files.map { |f| File.mtime(f).utc.to_i }.max.to_s
end

def cached_timestamp_path
cache_path.join(".compiler-timestamp")
def record_compilation_timestamp
config.cache_path.mkpath
compilation_timestamp_path.write(source_last_changed_at)
end

def run_webpack
Expand All @@ -63,15 +51,24 @@ def run_webpack
sterr, stdout, status = Open3.capture3(webpack_env, "#{RbConfig.ruby} ./bin/webpack")

if status.success?
logger.info "Compiled all packs in #{Webpacker::Configuration.entry_path}"
true
logger.info "Compiled all packs in #{config.source_entry_path}"
else
logger.error "Compilation Failed:\n#{sterr}\n#{stdout}"
false
logger.error "Compilation failed:\n#{sterr}\n#{stdout}"
end

status.success?
end


def default_watched_paths
["#{config.source_path}/**/*", "yarn.lock", "package.json", "config/webpack/**/*"].freeze
end

def compilation_timestamp_path
config.cache_path.join(".compiled-from-last-change-at")
end

def webpack_env
{ "NODE_ENV" => Webpacker.env, "ASSET_HOST" => ActionController::Base.helpers.compute_asset_host }
{ "NODE_ENV" => env, "ASSET_HOST" => ActionController::Base.helpers.compute_asset_host }
end
end
Loading