Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move browserslist and babel config into package.json #2633

Merged
merged 1 commit into from
Aug 17, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
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