jass-react-jsx 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e31ef797726107cd51b539bafc78294404ef819c4786fa70d32abc00ccc69307
4
+ data.tar.gz: 69064f9603959af47b297a7f62aafccc35a6d1ac9d6b5648296a208926258691
5
+ SHA512:
6
+ metadata.gz: 5439f946ec3b164a9964adb77b0b54b1e291aa51081b599915b613f3ac68ab3353a7fcaf6947d6cbaed167b321f1c8754c4f4f8ec5bc9548ee5be9773909ebf4
7
+ data.tar.gz: 6c7b195644fecc9cb5855a52f9a0053fe13e173ee845b398a0d2a141d6dea6301914ebdb2820288c3a1168b5f6fc7ed6527f352ccbcae2524a516afb5d6ccf57
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Matthias Grosser
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ [![Gem Version](https://badge.fury.io/rb/jass-react-jsx.svg)](http://badge.fury.io/rb/jass-react-jsx)
2
+ [![build](https://github.com/mtgrosser/jass-react-jsx/actions/workflows/build.yml/badge.svg)](https://github.com/mtgrosser/jass-react-jsx/actions/workflows/build.yml)
3
+
4
+ # Jass::React::JSX – React JSX support for the Rails asset pipeline
5
+
6
+ `Jass::React::JSX` enables Sprockets and the Rails asset pipeline to compile [React JSX components](https://reactjs.org/docs/introducing-jsx.html).
7
+
8
+ JSX components will be compiled to ES modules, which can be imported using the new Rails [Import Maps](https://github.com/rails/importmap-rails) or
9
+ regular `<script module>` tags.
10
+
11
+ ## Motivation
12
+
13
+ Modern browsers support native loading of ES modules using the `import` statement.
14
+ By leveraging the new Rails Import Maps, modular JS applications can be built
15
+ without having to integrate a complex and tedious JS build pipeline.
16
+
17
+ However, framework-specific component formats like the React JSX format could not
18
+ be used this way till now.
19
+
20
+ `Jass::React::JSX` enables the asset pipeline to compile `.jsx` files to ES modules,
21
+ allowing to build modular React applications in a clear and straightforward way,
22
+ without the necessity of external build tools.
23
+
24
+ ## Installation
25
+
26
+ ### Gemfile
27
+
28
+ ```ruby
29
+ gem 'jass-react-jsx'
30
+ ```
31
+
32
+ ### JS dependencies
33
+
34
+ Add the Babel transformers to your JS dependencies:
35
+ ```sh
36
+ $ yarn add @babel/core @babel/plugin-transform-react-jsx
37
+ ```
38
+
39
+ ### Node.js
40
+
41
+ `Jass::React::JSX` depends on [Nodo](https://github.com/mtgrosser/nodo), which requires a working Node.js installation.
42
+
43
+ ## Usage
44
+
45
+ Place your `.jsx` components inside your regular `app/javascript` path.
46
+
47
+ In `app/javascript/components/HelloWorld.jsx`:
48
+
49
+ ```jsx
50
+ import { Component } from "react";
51
+
52
+ export default class HelloWorld extends Component {
53
+ render() {
54
+ return (
55
+ <div>
56
+ <h1>Hello, world!</h1>
57
+ <h2>The current UNIX date is {Date.now()}.</h2>
58
+ </div>
59
+ );
60
+ }
61
+ }
62
+ ```
63
+
64
+ In your HTML code, load the component as a module:
65
+
66
+ ```erb
67
+ <%= javascript_include_tag 'components/HelloWorld.js', module: true %>
68
+ ```
69
+
70
+ ### Components with imports
71
+
72
+ In order to use module `import`s within your components, you need to pin them in your Rails importmap:
73
+
74
+ ```ruby
75
+ # config/importmap.rb
76
+ pin "react", to: "https://cdn.esm.sh/v45/react@17.0.2/es2021/react.js"
77
+ pin "react-dom", to: "https://cdn.esm.sh/v45/react-dom@17.0.2/es2021/react-dom.js"
78
+ pin "react/jsx-runtime", to: "https://cdn.esm.sh/v45/react@17.0.2/es2021/jsx-runtime.js"
79
+
80
+ pin "components/HelloWorld.jsx", to: "components/HelloWorld.js"
81
+ ```
82
+
83
+ Then just use them in your JS code:
84
+
85
+ ```js
86
+ import { render } from "react-dom";
87
+ import HelloWorld from "components/HelloWorld.jsx";
88
+ ```
89
+
90
+ ## Limitations
91
+
92
+ Currently, the following things are not (yet) supported:
93
+
94
+ - source maps
@@ -0,0 +1,26 @@
1
+ class Jass::React::JSX::Compiler < Nodo::Core
2
+ require babelCore: '@babel/core',
3
+ pluginTransformReactJSX: '@babel/plugin-transform-react-jsx'
4
+
5
+ class_function def compile(source, filename)
6
+ filename = File.basename(filename)
7
+ compile_component(source, filename)
8
+ end
9
+
10
+ function :compile_component, <<~'JS'
11
+ (source, filename) => {
12
+ let code = '';
13
+ nodo.debug(`Compiling component ${filename}`);
14
+
15
+ const result = babelCore.transformSync(source,
16
+ { plugins: [
17
+ ["@babel/plugin-transform-react-jsx", { "runtime": "automatic" } ]
18
+ ]
19
+ }
20
+ );
21
+
22
+ return { code: result.code, map: result.map };
23
+ }
24
+ JS
25
+
26
+ end
@@ -0,0 +1,25 @@
1
+ module Jass::React::JSX::Processor
2
+ VERSION = '1'
3
+
4
+ class << self
5
+
6
+ def cache_key
7
+ @cache_key ||= [name, VERSION].freeze
8
+ end
9
+
10
+ def call(input)
11
+ data = input[:data]
12
+
13
+ js, map = input[:cache].fetch(cache_key + [input[:filename]] + [data]) do
14
+ result = Jass::React::JSX::Compiler.compile(data, input[:filename])
15
+ result.values_at('code', 'map')
16
+ end
17
+
18
+ #map = Sprockets::SourceMapUtils.format_source_map(map, input)
19
+ #map = Sprockets::SourceMapUtils.combine_source_maps(input[:metadata][:map], map)
20
+
21
+ { data: js } #, map: map }
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,7 @@
1
+ module Jass
2
+ module React
3
+ module JSX
4
+ VERSION = '0.3.0'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,17 @@
1
+ require 'nodo'
2
+
3
+ require_relative 'jsx/version'
4
+ require_relative 'jsx/compiler'
5
+
6
+ begin
7
+ require 'sprockets'
8
+ rescue LoadError
9
+ # Sprockets not available
10
+ end
11
+
12
+ if defined?(Sprockets)
13
+ require_relative 'jsx/processor'
14
+
15
+ Sprockets.register_mime_type 'text/jsx', extensions: %w[.jsx], charset: :unicode
16
+ Sprockets.register_transformer 'text/jsx', 'application/javascript', Jass::React::JSX::Processor
17
+ end
@@ -0,0 +1 @@
1
+ require_relative 'jass/react/jsx'
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jass-react-jsx
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthias Grosser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-10-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: nodo
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.6.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.6.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - mtgrosser@gmx.net
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - LICENSE
91
+ - README.md
92
+ - lib/jass-react-jsx.rb
93
+ - lib/jass/react/jsx.rb
94
+ - lib/jass/react/jsx/compiler.rb
95
+ - lib/jass/react/jsx/processor.rb
96
+ - lib/jass/react/jsx/version.rb
97
+ homepage: https://github.com/mtgrosser/jass-react-jsx
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: 2.3.0
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubygems_version: 3.1.4
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Compile React JSX for Sprockets
120
+ test_files: []