Skip to content

Commit

Permalink
move browserslist and babel config into package
Browse files Browse the repository at this point in the history
- running webpacker:install will now add browserslist and babel
configuration into package.json instead of copying files
    - webpacker:install:react also updated to use preset in package.json

- use babel/preset.js in npm package as a preset, providing updates and
less complexity to apps
    - babel/preset-react.js can be used for react applications

- additionally, updated rubocop.yml to target ruby 2.4 since it is now
the minimum supported version
  • Loading branch information
skipkayhil committed Aug 17, 2020
1 parent 15b701b commit dc6c147
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require: rubocop-performance
AllCops:
TargetRubyVersion: 2.2
TargetRubyVersion: 2.4
# RuboCop has a bunch of cops enabled by default. This setting tells RuboCop
# to ignore them, so only the ones explicitly set in this file are enabled.
DisabledByDefault: true
Expand Down
1 change: 0 additions & 1 deletion lib/install/config/.browserslistrc

This file was deleted.

12 changes: 7 additions & 5 deletions lib/install/react.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require "webpacker/configuration"
require "fileutils"
require "json"

replace_babel_config = FileUtils.compare_file(Rails.root.join("babel.config.js"), "#{__dir__}/config/babel.config.js")

say "Copying babel.config.js to app root directory"
copy_file "#{__dir__}/examples/react/babel.config.js", "babel.config.js", force: replace_babel_config
say "Adding react-preset to babel configuration in package.json"
old_package = JSON.parse(File.read('package.json'))
old_package["babel"] = {
"presets": ["./node_modules/@rails/webpacker/package/babel/preset-react.js"]
}
File.write('package.json', JSON.dump(old_package))

say "Copying react example entry file to #{Webpacker.config.source_entry_path}"
copy_file "#{__dir__}/examples/react/hello_react.jsx", "#{Webpacker.config.source_entry_path}/hello_react.jsx"
Expand Down
18 changes: 12 additions & 6 deletions lib/install/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@
say "Copying postcss.config.js to app root directory"
copy_file "#{__dir__}/config/postcss.config.js", "postcss.config.js"

say "Copying babel.config.js to app root directory"
copy_file "#{__dir__}/config/babel.config.js", "babel.config.js"

say "Copying .browserslistrc to app root directory"
copy_file "#{__dir__}/config/.browserslistrc", ".browserslistrc"

if Dir.exists?(Webpacker.config.source_path)
say "The JavaScript app source directory already exists"
else
Expand Down Expand Up @@ -45,6 +39,18 @@
say "Installing dev server for live reloading"
run "yarn add --dev webpack-dev-server"

insert_into_file Rails.root.join("package.json").to_s, before: /\n}\n*$/ do
<<~JSON.chomp
,
"babel": {
"presets": ["./node_modules/@rails/webpacker/package/babel/preset.js"]
},
"browserslist": [
"defaults"
]
JSON
end

if Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR > 1
say "You need to allow webpack-dev-server host as allowed origin for connect-src.", :yellow
say "This can be done in Rails 5.2+ for development environment in the CSP initializer", :yellow
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
module.exports = function(api) {
var validEnv = ['development', 'test', 'production']
var currentEnv = api.env()
var isDevelopmentEnv = api.env('development')
var isProductionEnv = api.env('production')
var isTestEnv = api.env('test')
module.exports = function config(api) {
const validEnv = ['development', 'test', 'production']
const currentEnv = api.env()
const isDevelopmentEnv = api.env('development')
const isProductionEnv = api.env('production')
const isTestEnv = api.env('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) +
'.'
`Please specify a valid NODE_ENV or BABEL_ENV environment variable. Valid values are "development", "test", and "production". Instead, received: "${JSON.stringify(currentEnv)}".`
)
}

Expand All @@ -24,8 +20,7 @@ module.exports = function(api) {
node: 'current'
},
modules: 'commonjs'
},
'@babel/preset-react'
}
],
(isProductionEnv || isDevelopmentEnv) && [
'@babel/preset-env',
Expand Down
18 changes: 7 additions & 11 deletions lib/install/config/babel.config.js → package/babel/preset.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
module.exports = function(api) {
var validEnv = ['development', 'test', 'production']
var currentEnv = api.env()
var isDevelopmentEnv = api.env('development')
var isProductionEnv = api.env('production')
var isTestEnv = api.env('test')
module.exports = function config(api) {
const validEnv = ['development', 'test', 'production']
const currentEnv = api.env()
const isDevelopmentEnv = api.env('development')
const isProductionEnv = api.env('production')
const isTestEnv = api.env('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) +
'.'
`Please specify a valid NODE_ENV or BABEL_ENV environment variable. Valid values are "development", "test", and "production". Instead, received: "${JSON.stringify(currentEnv)}".`
)
}

Expand Down

0 comments on commit dc6c147

Please sign in to comment.