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

JSX Support #53

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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 Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Rake::TestTask.new(:test) do |t|
t.test_files = FileList['test/**/*_test.rb']
end

task :default => :spec
task :default => :test
2 changes: 1 addition & 1 deletion js/babel-plugin-sprockets-commoner-internal/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) });
Expand Down
3 changes: 3 additions & 0 deletions lib/sprockets/commoner.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'sprockets'
require 'sprockets/commoner/json_processor'
require 'sprockets/commoner/jsx_processor'
require 'sprockets/commoner/processor'
require 'sprockets/commoner/bundle'

Expand All @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this for?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not much for.. it just tells the sprockets that 'application/jsx' can be transformed into 'application/javascript' so it can be later included in the 'js' files.

Or we could just tell it that '.jsx' files are '.js' files stright away , by:

register_mime_type "application/javascript", extensions: [".js.jsx",".jsx"], charset: :unicode

Or maybe there's some another better way to do it. I'm not sure, I'm not really familiar with sprockets yet

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think just adding the .jsx extension as an acceptable application/javascript extension is better

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, :+
Expand Down
9 changes: 9 additions & 0 deletions lib/sprockets/commoner/jsx_processor.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Sprockets
module Commoner
class JSXProcessor
def self.call(input)
{ data: input[:data] }
end
end
end
end
2 changes: 1 addition & 1 deletion lib/sprockets/commoner/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
23 changes: 23 additions & 0 deletions test/allowed_extensions_test.rb
Original file line number Diff line number Diff line change
@@ -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 [email protected]('should_process?', "#{@env.root}/my-lib.jso")
assert [email protected]('should_process?', "#{@env.root}/my-lib.jsxon")
assert [email protected]('should_process?', "#{@env.root}/my-lib.jsonx")
end
end
3 changes: 2 additions & 1 deletion test/fixtures/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
3 changes: 3 additions & 0 deletions test/fixtures/react/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
presets: ["react"]
}
1 change: 1 addition & 0 deletions test/fixtures/react/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
require('./render');
4 changes: 4 additions & 0 deletions test/fixtures/react/render.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ReactDOM.render(
<h1>Hello, world!</h1>,
document.getElementById('root')
);
30 changes: 30 additions & 0 deletions test/react_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'test_helper'

class ReactTest < MiniTest::Test
def setup
@env = Sprockets::Environment.new(File.join(__dir__, 'fixtures'))
@env.append_path File.join(__dir__, 'fixtures')
end

def test_react
assert asset = @env['react.js']
assert_equal <<-JS.chomp, asset.to_s.chomp
!function(global) {
var __commoner_initialize_module__ = function(f) {
var module = {exports: {}};
f.call(module.exports, module, module.exports);
return module.exports;
};

var __commoner_module__react$render_jsx = __commoner_initialize_module__(function (module, exports) {
ReactDOM.render(React.createElement(
'h1',
null,
'Hello, world!'
), document.getElementById('root'));
});
var __commoner_module__react$index_js = {};
}(typeof global != 'undefined' ? global : typeof window != 'undefined' ? window : this);
JS
end
end