diff --git a/Rakefile b/Rakefile index 912ee38..d6c5113 100644 --- a/Rakefile +++ b/Rakefile @@ -7,4 +7,4 @@ Rake::TestTask.new(:test) do |t| t.test_files = FileList['test/**/*_test.rb'] end -task :default => :spec +task :default => :test diff --git a/js/babel-plugin-sprockets-commoner-internal/index.js b/js/babel-plugin-sprockets-commoner-internal/index.js index f9929bf..4247ec0 100644 --- a/js/babel-plugin-sprockets-commoner-internal/index.js +++ b/js/babel-plugin-sprockets-commoner-internal/index.js @@ -242,7 +242,7 @@ module.exports = function (context) { opts = { globalNamespaces: [], // We can get these from Sprockets - extensions: ['.js', '.json', '.coffee', '.js.erb', '.coffee.erb'] + extensions: ['.js', '.jsx', '.json', '.coffee', '.js.erb', '.jsx.erb', '.coffee.erb'] }; Object.assign(opts, state.opts, { basedir: dirname(state.file.opts.filename) }); diff --git a/lib/sprockets/commoner.rb b/lib/sprockets/commoner.rb index 2051bd1..d9449a9 100644 --- a/lib/sprockets/commoner.rb +++ b/lib/sprockets/commoner.rb @@ -1,5 +1,6 @@ require 'sprockets' require 'sprockets/commoner/json_processor' +require 'sprockets/commoner/jsx_processor' require 'sprockets/commoner/processor' require 'sprockets/commoner/bundle' @@ -10,8 +11,10 @@ def self.sprockets4? end end + register_mime_type "application/jsx", extensions: [".js.jsx",".jsx"], charset: :unicode register_postprocessor 'application/javascript', ::Sprockets::Commoner::Processor register_transformer 'application/json', 'application/javascript', ::Sprockets::Commoner::JSONProcessor + register_transformer 'application/jsx', 'application/javascript', ::Sprockets::Commoner::JSXProcessor register_bundle_metadata_reducer 'application/javascript', :commoner_enabled, false, :| register_bundle_metadata_reducer 'application/javascript', :commoner_required, Set.new, :+ register_bundle_metadata_reducer 'application/javascript', :commoner_used_helpers, Set.new, :+ diff --git a/lib/sprockets/commoner/jsx_processor.rb b/lib/sprockets/commoner/jsx_processor.rb new file mode 100644 index 0000000..477c2be --- /dev/null +++ b/lib/sprockets/commoner/jsx_processor.rb @@ -0,0 +1,9 @@ +module Sprockets + module Commoner + class JSXProcessor + def self.call(input) + { data: input[:data] } + end + end + end +end diff --git a/lib/sprockets/commoner/processor.rb b/lib/sprockets/commoner/processor.rb index e0b65e7..7e1e3cf 100644 --- a/lib/sprockets/commoner/processor.rb +++ b/lib/sprockets/commoner/processor.rb @@ -11,7 +11,7 @@ class Processor < Schmooze::Base BABELRC_FILE = '.babelrc'.freeze PACKAGE_JSON = 'package.json'.freeze JS_PACKAGE_PATH = File.expand_path('../../../js', __dir__) - ALLOWED_EXTENSIONS = /\.js(?:on)?(?:\.erb)?\z/ + ALLOWED_EXTENSIONS = /\.js(?:on|x)?(?:\.erb)?\z/ dependencies babel: 'babel-core', commoner: 'babel-plugin-sprockets-commoner-internal' diff --git a/test/allowed_extensions_test.rb b/test/allowed_extensions_test.rb new file mode 100644 index 0000000..8a822f0 --- /dev/null +++ b/test/allowed_extensions_test.rb @@ -0,0 +1,23 @@ +require 'test_helper' + +class AllowedExtensionsTest < MiniTest::Test + def setup + @env = Sprockets::Environment.new(File.join(__dir__, 'fixtures')) + @processor = Sprockets::Commoner::Processor.new(@env.root) + end + + def test_allowed_extensions + assert @processor.send('should_process?', "#{@env.root}/my-lib.js") + assert @processor.send('should_process?', "#{@env.root}/my-lib.js.erb") + assert @processor.send('should_process?', "#{@env.root}/my-lib.json") + assert @processor.send('should_process?', "#{@env.root}/my-lib.json.erb") + assert @processor.send('should_process?', "#{@env.root}/myComponent.jsx") + assert @processor.send('should_process?', "#{@env.root}/myComponent.jsx.erb") + end + + def test_forbidden_extensions + assert !@processor.send('should_process?', "#{@env.root}/my-lib.jso") + assert !@processor.send('should_process?', "#{@env.root}/my-lib.jsxon") + assert !@processor.send('should_process?', "#{@env.root}/my-lib.jsonx") + end +end diff --git a/test/fixtures/package.json b/test/fixtures/package.json index b021826..2a6381f 100644 --- a/test/fixtures/package.json +++ b/test/fixtures/package.json @@ -2,6 +2,7 @@ "private": true, "dependencies": { "babel-core": "6.9.1", - "babel-preset-es2015": "6.9.0" + "babel-preset-es2015": "6.9.0", + "babel-preset-react": "^6.16.0" } } diff --git a/test/fixtures/react/.babelrc b/test/fixtures/react/.babelrc new file mode 100644 index 0000000..3391242 --- /dev/null +++ b/test/fixtures/react/.babelrc @@ -0,0 +1,3 @@ +{ + presets: ["react"] +} diff --git a/test/fixtures/react/index.js b/test/fixtures/react/index.js new file mode 100644 index 0000000..41f9e9d --- /dev/null +++ b/test/fixtures/react/index.js @@ -0,0 +1 @@ +require('./render'); diff --git a/test/fixtures/react/render.jsx b/test/fixtures/react/render.jsx new file mode 100644 index 0000000..d0f87a5 --- /dev/null +++ b/test/fixtures/react/render.jsx @@ -0,0 +1,4 @@ +ReactDOM.render( +