-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CLI command to update webpack config (#270)
- Loading branch information
Showing
15 changed files
with
250 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# frozen_string_literal: true | ||
|
||
module Bridgetown | ||
module Commands | ||
class Webpack < Thor::Group | ||
include Thor::Actions | ||
include ConfigurationOverridable | ||
extend Summarizable | ||
|
||
Registrations.register do | ||
register(Webpack, "webpack", "webpack ACTION", Webpack.summary) | ||
end | ||
|
||
def self.banner | ||
"bridgetown webpack ACTION" | ||
end | ||
summary "Perform actions on the bridgetown webpack configuration" | ||
|
||
def self.exit_on_failure? | ||
true | ||
end | ||
|
||
def webpack | ||
logger = Bridgetown.logger | ||
return show_actions if args.empty? | ||
|
||
action = args.first | ||
if supported_actions.include?(action) | ||
perform action | ||
else | ||
logger.error "Error:".red, "🚨 Please enter a valid action." | ||
say "\n" | ||
show_actions | ||
end | ||
end | ||
|
||
def self.source_root | ||
File.expand_path("./webpack", __dir__) | ||
end | ||
|
||
def self.destination_root | ||
site.root_dir | ||
end | ||
|
||
def site | ||
@site ||= Bridgetown::Site.new(configuration_with_overrides(quiet: true)) | ||
end | ||
|
||
protected | ||
|
||
def perform(action) | ||
automation = find_in_source_paths("#{action}.rb") | ||
inside(New.created_site_dir || Dir.pwd) do | ||
apply automation, verbose: false | ||
end | ||
end | ||
|
||
def show_actions | ||
say "Available actions:\n".bold | ||
|
||
longest_action = supported_actions.keys.max_by(&:size).size | ||
supported_actions.each do |action, description| | ||
say action.ljust(longest_action).to_s.bold.blue + "\t" + "# #{description}" | ||
end | ||
end | ||
|
||
def supported_actions | ||
{ | ||
setup: "Sets up a webpack integration with Bridgetown in your project", | ||
update: "Updates the Bridgetown webpack defaults to the latest available version", | ||
"enable-postcss": "Configures PostCSS in your project", | ||
}.with_indifferent_access | ||
end | ||
end | ||
end | ||
end |
4 changes: 4 additions & 0 deletions
4
bridgetown-core/lib/bridgetown-core/commands/webpack/enable-postcss.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
create_file "postcss.config.js" | ||
template "webpack.defaults.js.erb", "webpack.defaults.js", force: true |
4 changes: 4 additions & 0 deletions
4
bridgetown-core/lib/bridgetown-core/commands/webpack/setup.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# frozen_string_literal: true | ||
|
||
template "webpack.defaults.js.erb", "webpack.defaults.js" | ||
copy_file "webpack.config.js", force: true |
3 changes: 3 additions & 0 deletions
3
bridgetown-core/lib/bridgetown-core/commands/webpack/update.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# frozen_string_literal: true | ||
|
||
template "webpack.defaults.js.erb", "webpack.defaults.js", force: true |
18 changes: 18 additions & 0 deletions
18
bridgetown-core/lib/bridgetown-core/commands/webpack/webpack.config.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const config = require("./webpack.defaults.js") | ||
|
||
// Add any overrides to the default webpack config here: | ||
|
||
// Eg: | ||
// | ||
// ``` | ||
// const path = require("path") | ||
// config.resolve.modules.push(path.resolve(__dirname, 'frontend', 'components')) | ||
// config.resolve.alias.frontendComponents = path.resolve(__dirname, 'frontend', 'components') | ||
// ``` | ||
|
||
|
||
|
||
|
||
//////////////////////////////////////////////////////// | ||
|
||
module.exports = config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# frozen_string_literal: true | ||
|
||
require "helper" | ||
|
||
class TestWebpackCommand < BridgetownUnitTest | ||
def webpack_defaults | ||
File.join(@full_path, "webpack.defaults.js") | ||
end | ||
|
||
def webpack_config | ||
File.join(@full_path, "webpack.config.js") | ||
end | ||
|
||
context "the webpack command" do | ||
setup do | ||
@path = "new-site" | ||
@full_path = File.expand_path(@path, Dir.pwd) | ||
|
||
capture_stdout { Bridgetown::Commands::Base.start(["new", @path]) } | ||
@cmd = Bridgetown::Commands::Webpack.new | ||
end | ||
|
||
teardown do | ||
FileUtils.rm_r @full_path if File.directory?(@full_path) | ||
end | ||
|
||
should "list all available actions when invoked without args" do | ||
output = capture_stdout do | ||
@cmd.webpack | ||
end | ||
assert_match %r!setup!, output | ||
assert_match %r!update!, output | ||
assert_match %r!enable-postcss!, output | ||
end | ||
|
||
should "show error when action doesn't exist" do | ||
output = capture_stdout do | ||
@cmd.invoke(:webpack, ["qwerty"]) | ||
end | ||
|
||
assert_match %r!Please enter a valid action!, output | ||
end | ||
|
||
should "setup webpack defaults and config" do | ||
File.delete webpack_defaults # Delete the file created during setup | ||
|
||
@cmd.inside(@full_path) do | ||
capture_stdout { @cmd.invoke(:webpack, ["setup"]) } | ||
end | ||
|
||
assert_exist webpack_defaults | ||
assert_exist webpack_config | ||
end | ||
|
||
should "update webpack config" do | ||
webpack_defaults = File.join(@full_path, "webpack.defaults.js") | ||
File.write(webpack_defaults, "OLD_VERSION") | ||
|
||
@cmd.inside(@full_path) do | ||
capture_stdout { @cmd.invoke(:webpack, ["update"]) } | ||
end | ||
|
||
assert_file_contains %r!module.exports!, webpack_defaults | ||
refute_file_contains %r!OLD_VERSION!, webpack_defaults | ||
end | ||
|
||
# TODO: Figure out how to make this test pass | ||
# Info: https://github.com/bridgetownrb/bridgetown/pull/270#issuecomment-841709898 | ||
|
||
# should "enable postcss in webpack config" do | ||
# webpack_defaults = File.join(@full_path, "webpack.defaults.js") | ||
# refute_file_contains %r!postcss-loader!, webpack_defaults | ||
|
||
# @cmd.inside(@full_path) do | ||
# capture_stdout { @cmd.invoke(:webpack, ["enable-postcss"]) } | ||
# end | ||
|
||
# assert_file_contains %r!postcss-loader!, webpack_defaults | ||
# end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.