From a0b0ec42a2d80a26a2da94559dd5542fe26e9fac Mon Sep 17 00:00:00 2001 From: paramagicdev Date: Sun, 26 Jul 2020 04:16:55 -0400 Subject: [PATCH] feat: add babel and postcss configs to templates --- examples/railsapp/Gemfile.lock | 228 --------------------- lib/snowpacker/runner.rb | 52 ----- lib/snowpacker/templates/babel.config.js | 72 +++++++ lib/snowpacker/templates/postcss.config.js | 12 ++ 4 files changed, 84 insertions(+), 280 deletions(-) delete mode 100644 examples/railsapp/Gemfile.lock delete mode 100644 lib/snowpacker/runner.rb create mode 100644 lib/snowpacker/templates/babel.config.js create mode 100644 lib/snowpacker/templates/postcss.config.js diff --git a/examples/railsapp/Gemfile.lock b/examples/railsapp/Gemfile.lock deleted file mode 100644 index 0872a34..0000000 --- a/examples/railsapp/Gemfile.lock +++ /dev/null @@ -1,228 +0,0 @@ -PATH - remote: ../.. - specs: - snowpacker (0.0.2) - rack-proxy (~> 0.6.4) - -GEM - remote: https://rubygems.org/ - specs: - actioncable (6.0.3.2) - actionpack (= 6.0.3.2) - nio4r (~> 2.0) - websocket-driver (>= 0.6.1) - actionmailbox (6.0.3.2) - actionpack (= 6.0.3.2) - activejob (= 6.0.3.2) - activerecord (= 6.0.3.2) - activestorage (= 6.0.3.2) - activesupport (= 6.0.3.2) - mail (>= 2.7.1) - actionmailer (6.0.3.2) - actionpack (= 6.0.3.2) - actionview (= 6.0.3.2) - activejob (= 6.0.3.2) - mail (~> 2.5, >= 2.5.4) - rails-dom-testing (~> 2.0) - actionpack (6.0.3.2) - actionview (= 6.0.3.2) - activesupport (= 6.0.3.2) - rack (~> 2.0, >= 2.0.8) - rack-test (>= 0.6.3) - rails-dom-testing (~> 2.0) - rails-html-sanitizer (~> 1.0, >= 1.2.0) - actiontext (6.0.3.2) - actionpack (= 6.0.3.2) - activerecord (= 6.0.3.2) - activestorage (= 6.0.3.2) - activesupport (= 6.0.3.2) - nokogiri (>= 1.8.5) - actionview (6.0.3.2) - activesupport (= 6.0.3.2) - builder (~> 3.1) - erubi (~> 1.4) - rails-dom-testing (~> 2.0) - rails-html-sanitizer (~> 1.1, >= 1.2.0) - activejob (6.0.3.2) - activesupport (= 6.0.3.2) - globalid (>= 0.3.6) - activemodel (6.0.3.2) - activesupport (= 6.0.3.2) - activerecord (6.0.3.2) - activemodel (= 6.0.3.2) - activesupport (= 6.0.3.2) - activestorage (6.0.3.2) - actionpack (= 6.0.3.2) - activejob (= 6.0.3.2) - activerecord (= 6.0.3.2) - marcel (~> 0.3.1) - activesupport (6.0.3.2) - concurrent-ruby (~> 1.0, >= 1.0.2) - i18n (>= 0.7, < 2) - minitest (~> 5.1) - tzinfo (~> 1.1) - zeitwerk (~> 2.2, >= 2.2.2) - addressable (2.7.0) - public_suffix (>= 2.0.2, < 5.0) - bindex (0.8.1) - bootsnap (1.4.6) - msgpack (~> 1.0) - builder (3.2.4) - byebug (11.1.3) - capybara (3.33.0) - addressable - mini_mime (>= 0.1.3) - nokogiri (~> 1.8) - rack (>= 1.6.0) - rack-test (>= 0.6.3) - regexp_parser (~> 1.5) - xpath (~> 3.2) - childprocess (3.0.0) - concurrent-ruby (1.1.6) - crass (1.0.6) - erubi (1.9.0) - ffi (1.13.1) - globalid (0.4.2) - activesupport (>= 4.2.0) - i18n (1.8.3) - concurrent-ruby (~> 1.0) - jbuilder (2.10.0) - activesupport (>= 5.0.0) - listen (3.2.1) - rb-fsevent (~> 0.10, >= 0.10.3) - rb-inotify (~> 0.9, >= 0.9.10) - loofah (2.6.0) - crass (~> 1.0.2) - nokogiri (>= 1.5.9) - mail (2.7.1) - mini_mime (>= 0.1.1) - marcel (0.3.3) - mimemagic (~> 0.3.2) - method_source (1.0.0) - mimemagic (0.3.5) - mini_mime (1.0.2) - mini_portile2 (2.4.0) - minitest (5.14.1) - msgpack (1.3.3) - nio4r (2.5.2) - nokogiri (1.10.10) - mini_portile2 (~> 2.4.0) - public_suffix (4.0.5) - puma (4.3.5) - nio4r (~> 2.0) - rack (2.2.3) - rack-cors (1.1.1) - rack (>= 2.0.0) - rack-proxy (0.6.5) - rack - rack-test (1.1.0) - rack (>= 1.0, < 3) - rails (6.0.3.2) - actioncable (= 6.0.3.2) - actionmailbox (= 6.0.3.2) - actionmailer (= 6.0.3.2) - actionpack (= 6.0.3.2) - actiontext (= 6.0.3.2) - actionview (= 6.0.3.2) - activejob (= 6.0.3.2) - activemodel (= 6.0.3.2) - activerecord (= 6.0.3.2) - activestorage (= 6.0.3.2) - activesupport (= 6.0.3.2) - bundler (>= 1.3.0) - railties (= 6.0.3.2) - sprockets-rails (>= 2.0.0) - rails-dom-testing (2.0.3) - activesupport (>= 4.2.0) - nokogiri (>= 1.6) - rails-html-sanitizer (1.3.0) - loofah (~> 2.3) - railties (6.0.3.2) - actionpack (= 6.0.3.2) - activesupport (= 6.0.3.2) - method_source - rake (>= 0.8.7) - thor (>= 0.20.3, < 2.0) - rake (13.0.1) - rb-fsevent (0.10.4) - rb-inotify (0.10.1) - ffi (~> 1.0) - regexp_parser (1.7.1) - rubyzip (2.3.0) - sass-rails (6.0.0) - sassc-rails (~> 2.1, >= 2.1.1) - sassc (2.4.0) - ffi (~> 1.9) - sassc-rails (2.1.2) - railties (>= 4.0.0) - sassc (>= 2.0) - sprockets (> 3.0) - sprockets-rails - tilt - selenium-webdriver (3.142.7) - childprocess (>= 0.5, < 4.0) - rubyzip (>= 1.2.2) - spring (2.1.0) - spring-watcher-listen (2.0.1) - listen (>= 2.7, < 4.0) - spring (>= 1.2, < 3.0) - sprockets (4.0.2) - concurrent-ruby (~> 1.0) - rack (> 1, < 3) - sprockets-rails (3.2.1) - actionpack (>= 4.0) - activesupport (>= 4.0) - sprockets (>= 3.0.0) - sqlite3 (1.4.2) - thor (1.0.1) - thread_safe (0.3.6) - tilt (2.0.10) - turbolinks (5.2.1) - turbolinks-source (~> 5.2) - turbolinks-source (5.2.0) - tzinfo (1.2.7) - thread_safe (~> 0.1) - web-console (4.0.4) - actionview (>= 6.0.0) - activemodel (>= 6.0.0) - bindex (>= 0.4.0) - railties (>= 6.0.0) - webdrivers (4.4.1) - nokogiri (~> 1.6) - rubyzip (>= 1.3.0) - selenium-webdriver (>= 3.0, < 4.0) - websocket-driver (0.7.3) - websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.5) - xpath (3.2.0) - nokogiri (~> 1.8) - zeitwerk (2.4.0) - -PLATFORMS - ruby - -DEPENDENCIES - bootsnap (>= 1.4.2) - byebug - capybara (>= 2.15) - jbuilder (~> 2.7) - listen (~> 3.2) - puma (~> 4.1) - rack-cors (~> 1.1) - rails (~> 6.0.3, >= 6.0.3.2) - sass-rails (>= 6) - selenium-webdriver - snowpacker (~> 0.0.2)! - spring - spring-watcher-listen (~> 2.0.0) - sqlite3 (~> 1.4) - turbolinks (~> 5) - tzinfo-data - web-console (>= 3.3.0) - webdrivers - -RUBY VERSION - ruby 2.6.3p62 - -BUNDLED WITH - 2.1.4 diff --git a/lib/snowpacker/runner.rb b/lib/snowpacker/runner.rb deleted file mode 100644 index eac42d8..0000000 --- a/lib/snowpacker/runner.rb +++ /dev/null @@ -1,52 +0,0 @@ -# frozen_string_literal: true - -require 'socket' -require 'snowpacker/env' - -module Snowpacker - module Rails - class Runner - attr_reader :config_file - - def initialize - @config_file = Snowpacker.config.config_file - @env = Env.set_env_variables - - detect_port! - - rescue Errno::ENOENT, NoMethodError - $stdout.puts "Snowpacker configuration not found in #{Snowpacker.config_location}" - $stdout.puts "Please run bundle exec rails generate snowpacker to install Snowpacker" - exit! - end - - # Build for production - def build - snowpacker_command(env: :production, cmd: :build) - end - - # Serve for development - def dev - snowpacker_command(env: :development, cmd: :dev) - end - - private - - def snowpacker_command(env: '', cmd: '') - command = "NODE_ENV=#{env} yarn run snowpack #{cmd} --config #{@config_file}" - exec(command) - end - - def detect_port! - hostname = Snowpacker.config.hostname - port = Snowpacker.config.port - server = TCPServer.new(hostname, port) - server.close - rescue Errno::EADDRINUSE - $stdout.puts "Another program is running on port #{port}.\n - Set a new port in #{Snowpacker.config_path} for devOptions: { port: \"#{port}\" }" - exit! - end - end - end -end diff --git a/lib/snowpacker/templates/babel.config.js b/lib/snowpacker/templates/babel.config.js new file mode 100644 index 0000000..38ba281 --- /dev/null +++ b/lib/snowpacker/templates/babel.config.js @@ -0,0 +1,72 @@ +module.exports = function() { + const validEnv = ['development', 'test', 'production'] + const currentEnv = process.env["NODE_ENV"] + const isDevelopmentEnv = (currentEnv === "development") + const isProductionEnv = (currentEnv === 'production') + const isTestEnv = (currentEnv === 'test') + + if (!validEnv.includes(currentEnv)) { + throw new Error( + 'Please specify a valid `NODE_ENV` or ' + + '`BABEL_ENV` environment variables. Valid values are "development", ' + + '"test", and "production". Instead, received: ' + + JSON.stringify(currentEnv) + + '.' + ) + } + + return { + presets: [ + isTestEnv && [ + '@babel/preset-env', + { + targets: { + node: 'current' + } + } + ], + (isProductionEnv || isDevelopmentEnv) && [ + '@babel/preset-env', + { + forceAllTransforms: true, + useBuiltIns: 'entry', + corejs: 3, + modules: false, + exclude: ['transform-typeof-symbol'] + } + ] + ].filter(Boolean), + plugins: [ + 'babel-plugin-macros', + '@babel/plugin-syntax-dynamic-import', + isTestEnv && 'babel-plugin-dynamic-import-node', + '@babel/plugin-transform-destructuring', + [ + '@babel/plugin-proposal-class-properties', + { + loose: true + } + ], + [ + '@babel/plugin-proposal-object-rest-spread', + { + useBuiltIns: true + } + ], + [ + '@babel/plugin-transform-runtime', + { + helpers: false, + regenerator: true, + corejs: false + } + ], + [ + '@babel/plugin-transform-regenerator', + { + async: false + } + ] + ].filter(Boolean) + } +} diff --git a/lib/snowpacker/templates/postcss.config.js b/lib/snowpacker/templates/postcss.config.js new file mode 100644 index 0000000..aa5998a --- /dev/null +++ b/lib/snowpacker/templates/postcss.config.js @@ -0,0 +1,12 @@ +module.exports = { + plugins: [ + require('postcss-import'), + require('postcss-flexbugs-fixes'), + require('postcss-preset-env')({ + autoprefixer: { + flexbox: 'no-2009' + }, + stage: 3 + }) + ] +}