diff --git a/lib/generators/suspenders/styles_generator.rb b/lib/generators/suspenders/styles_generator.rb index a4703a223..1c7895e3e 100644 --- a/lib/generators/suspenders/styles_generator.rb +++ b/lib/generators/suspenders/styles_generator.rb @@ -3,6 +3,7 @@ module Generators class StylesGenerator < Rails::Generators::Base include Suspenders::Generators::APIAppUnsupported + source_root File.expand_path("../../templates/styles", __FILE__) desc <<~MARKDOWN Configures application to use PostCSS via cssbundling-rails. @@ -39,6 +40,14 @@ def configure_application_stylesheet TEXT end end + + def install_postcss_url + run "yarn add postcss-url" + end + + def configures_postcss + copy_file "postcss.config.js", "postcss.config.js" + end end end end diff --git a/lib/generators/templates/styles/postcss.config.js b/lib/generators/templates/styles/postcss.config.js new file mode 100644 index 000000000..6c5a992f7 --- /dev/null +++ b/lib/generators/templates/styles/postcss.config.js @@ -0,0 +1,11 @@ +module.exports = { + plugins: [ + require('postcss-import'), + require('postcss-nesting'), + require('autoprefixer'), + require('postcss-url')({ + url: 'copy', + assetsPath: 'static' + }) + ], +} diff --git a/test/fixtures/files/postcss.config.js b/test/fixtures/files/postcss.config.js new file mode 100644 index 000000000..6c5a992f7 --- /dev/null +++ b/test/fixtures/files/postcss.config.js @@ -0,0 +1,11 @@ +module.exports = { + plugins: [ + require('postcss-import'), + require('postcss-nesting'), + require('autoprefixer'), + require('postcss-url')({ + url: 'copy', + assetsPath: 'static' + }) + ], +} diff --git a/test/generators/suspenders/styles_generator_test.rb b/test/generators/suspenders/styles_generator_test.rb index 684d909c4..7dd70a0d5 100644 --- a/test/generators/suspenders/styles_generator_test.rb +++ b/test/generators/suspenders/styles_generator_test.rb @@ -75,6 +75,22 @@ class StylesGeneratorTest < Rails::Generators::TestCase end end + test "installs postcss-url" do + output = run_generator + + assert_match(/add\s*postcss-url/, output) + end + + test "configures postcss.config.js" do + expected = file_fixture("postcss.config.js").read + + run_generator + + assert_file app_root("postcss.config.js") do |file| + assert_equal expected, file + end + end + test "generator has a custom description" do assert_no_match(/Description/, generator_class.desc) end @@ -94,6 +110,7 @@ def restore_destination remove_file_if_exists "app/assets/stylesheets/base.css" remove_file_if_exists "app/assets/stylesheets/components.css" remove_file_if_exists "app/assets/stylesheets/utilities.css" + remove_file_if_exists "postcss.config.js" end end end