jass-rollup 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 366d04569adca5967e9fdfe78513f5d5002def5f08d271f8ba87a52cde06ae4f
4
+ data.tar.gz: 25490df65797582e189ff087ad8ec8e3a639280ee8f5d87b24b5c7e5ad6fb6a5
5
+ SHA512:
6
+ metadata.gz: 44823decd0e6d4da71e854e465d2f8a71e50b13186bdb57085e19580fe02c62e390ec406d04664841aa9e54d32bf40c80260a5564b8792a38dc9236aefbba62e
7
+ data.tar.gz: f7c0a706a9341fc9c079c4c7a802210065b5c63b6787818daaee4ea7918cc404725281a9460cf7944cbe3a449f75e812e55795fa4a2f12d295a20b37b2846bda
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,91 @@
1
+ [![Gem Version](https://badge.fury.io/rb/jass-rollup.svg)](http://badge.fury.io/rb/nodo) [![build](https://github.com/mtgrosser/jass-rollup/actions/workflows/build.yml/badge.svg)](https://github.com/mtgrosser/jass-rollup/actions/workflows/build.yml)
2
+
3
+ # Jass::Rollup – Rollup for Sprockets and the Rails asset pipeline
4
+
5
+ `Jass::Rollup` integrates the [Rollup](https://rollupjs.org) JS bundler with Sprockets and the Rails asset pipeline.
6
+
7
+ ## 💡 Motivation
8
+
9
+ JavaScript build pipelines tend to be a complicated mess consisting of dev servers,
10
+ an infinite number of `npm` dependencies and other "opinionated" conventions (and lack thereof).
11
+
12
+ The `Jass` gem series provide a straightforward way of integrating modern JS tooling
13
+ with the existing Rails asset pipeline, while adhering to established workflows
14
+ for asset processing in Rails applications.
15
+
16
+ ## 📦 Installation
17
+
18
+ ### Gemfile
19
+
20
+ ```ruby
21
+ gem 'jass-rollup'
22
+ ```
23
+
24
+ ### JS dependencies
25
+
26
+ Add `rollup` to your JS dependencies:
27
+
28
+ ```sh
29
+ $ yarn add rollup @rollup/plugin-commonjs @rollup/plugin-node-resolve
30
+ ```
31
+
32
+ ### Node.js
33
+
34
+ `Jass::Rollup` depends on [Nodo](https://github.com/mtgrosser/nodo), which requires a working Node.js installation.
35
+
36
+ ## ⚡️ Usage
37
+
38
+ `Jass::Rollup` provides two new directives to use in your JS assets, as well as
39
+ a new Sprockets file extension for rollup bundle config files.
40
+
41
+ ### rollup
42
+
43
+ The `rollup` directive will invoke `Rollup` with the given entry point
44
+ relative to your `Rails.root`.
45
+
46
+ To bundle an entry point as ES module:
47
+
48
+ ```js
49
+ //= rollup app/javascript/entry.js
50
+ ```
51
+
52
+ To bundle a `npm` module as ES module:
53
+
54
+ ```js
55
+ //= rollup vendor/node_modules/rxjs/dist/esm5/index.js
56
+ ```
57
+
58
+ ### rollup_esm
59
+
60
+ The `require_esm` directive will bundle a `npm` module referenced by its
61
+ name as ES module:
62
+
63
+ ```js
64
+ //= rollup_esm currency.js
65
+ ```
66
+
67
+ ### Bundling with custom rollup configurations
68
+
69
+ If your Rollup bundle requires extra configuration options, e.g. plugins, the file
70
+ extension `.rollup` can be used:
71
+
72
+ ```js
73
+ // assets/javascript/main.js.rollup
74
+ export default {
75
+ input: 'app/javascript/entry.js',
76
+ output: {
77
+ format: 'es'
78
+ }
79
+ };
80
+ ```
81
+
82
+ This asset will be bundled to `main.js`.
83
+
84
+
85
+ ## 💎 Other Jass gems for the asset pipeline
86
+
87
+ [Jass::Esbuild](https://github.com/mtgrosser/jass-esbuild) – esbuild support for Sprockets
88
+
89
+ [Jass::Vue::SFC](https://github.com/mtgrosser/jass-vue-sfc) – Vue Single File Component support for Sprockets
90
+
91
+ [Jass::React::JSX](https://github.com/mtgrosser/jass-react-jsx) – React JSX support for Sprockets
@@ -0,0 +1,74 @@
1
+ class Jass::Rollup::Compiler < Nodo::Core
2
+ require :rollup,
3
+ commonjs: '@rollup/plugin-commonjs',
4
+ nodeResolve: '@rollup/plugin-node-resolve'
5
+
6
+ # workaround for https://github.com/rollup/rollup/issues/4251
7
+ script <<~'JS'
8
+ const loadConfigFile = require(path.join(path.dirname(require.resolve('rollup')), 'loadConfigFile'));
9
+ JS
10
+
11
+ class_function def compile(config)
12
+ result = bundle(config)
13
+ result.fetch('output').first.slice('code', 'map')
14
+ end
15
+
16
+ class_function def compile_esm(mod)
17
+ result = bundle_esm(mod)
18
+ result.fetch('output').first.slice('code', 'map')
19
+ end
20
+
21
+ class_function def compile_entry(entry)
22
+ result = bundle_entry(entry)
23
+ result.fetch('output').first.slice('code', 'map')
24
+ end
25
+
26
+ function :default_plugins, <<~'JS'
27
+ () => [
28
+ nodeResolve.nodeResolve({ moduleDirectories: [process.env.NODE_PATH] }),
29
+ commonjs({ requireReturnsDefault: 'namespace', defaultIsModuleExports: true })
30
+ ]
31
+ JS
32
+
33
+ function :bundle_esm, <<~'JS'
34
+ async (mod) => {
35
+ const entry = require.resolve(mod);
36
+ return await bundle_entry(entry);
37
+ }
38
+ JS
39
+
40
+ function :bundle, <<~'JS'
41
+ async (config) => {
42
+ const { options, warnings } = await loadConfigFile(config, { format: 'es' });
43
+
44
+ warnings.flush();
45
+
46
+ let inputOptions = options[0];
47
+ let outputOptions = options[0].output[0];
48
+ let inputPlugins = inputOptions.plugins;
49
+
50
+ if (inputPlugins && inputPlugins.length == 1 && inputPlugins[0].name == 'stdin') {
51
+ inputPlugins.push(...default_plugins().reverse());
52
+ }
53
+
54
+ const bundle = await rollup.rollup(inputOptions);
55
+ return await bundle.generate(outputOptions);
56
+ }
57
+ JS
58
+
59
+ function :bundle_entry, <<~'JS'
60
+ async (entry) => {
61
+ const inputOptions = {
62
+ input: entry,
63
+ plugins: default_plugins()
64
+ };
65
+ const outputOptions = {
66
+ format: 'es',
67
+ exports: 'named'
68
+ };
69
+ const bundle = await rollup.rollup(inputOptions);
70
+ return await bundle.generate(outputOptions);
71
+ }
72
+ JS
73
+
74
+ end
@@ -0,0 +1,45 @@
1
+ module Jass
2
+ module Rollup
3
+ class DirectiveProcessor
4
+ DIRECTIVES = %w[rollup rollup_esm require_npm].freeze
5
+
6
+ class << self
7
+ def instance
8
+ @instance ||= new
9
+ end
10
+
11
+ def call(input)
12
+ instance.call(input)
13
+ end
14
+ end
15
+
16
+ def call(input)
17
+ data = "#{input[:data] || ''}"
18
+ data.gsub!(%r{^//=\s*(?<directive>#{DIRECTIVES.join('|')})\s+(?<args>.+)$}) do
19
+ match = Regexp.last_match
20
+ process_matching_directive(match[:directive], match[:args].squish.split(' '))
21
+ end
22
+ { data: data }
23
+ end
24
+
25
+ def process_matching_directive(name, args)
26
+ "#{send("process_#{name}_directive", *args)}\n"
27
+ rescue => e
28
+ e.set_backtrace(["#{@filename}:0"] + e.backtrace)
29
+ raise e
30
+ end
31
+
32
+ def process_rollup_esm_directive(mod)
33
+ Compiler.compile_esm(mod).fetch('code')
34
+ end
35
+
36
+ def process_rollup_directive(entry, *args)
37
+ Compiler.compile_entry(entry).fetch('code')
38
+ end
39
+
40
+ def process_require_npm_directive(path, *args)
41
+ File.read(File.expand_path(path, Nodo.modules_root))
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,22 @@
1
+ module Jass::Rollup::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
+ code, map = input[:cache].fetch([self.cache_key, data]) do
14
+ result = Jass::Rollup::Compiler.compile(input[:filename])
15
+ [result['code'], nil]
16
+ end
17
+
18
+ { data: code }
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ module Jass
2
+ module Rollup
3
+ VERSION = '0.6.0'
4
+ end
5
+ end
@@ -0,0 +1,19 @@
1
+ require 'nodo'
2
+
3
+ require_relative 'rollup/version'
4
+ require_relative 'rollup/compiler'
5
+
6
+ begin
7
+ require 'sprockets'
8
+ rescue LoadError
9
+ # Sprockets not available
10
+ end
11
+
12
+ if defined?(Sprockets)
13
+ require_relative 'rollup/processor'
14
+ require_relative 'rollup/directive_processor'
15
+
16
+ Sprockets.register_mime_type 'application/javascript+rollup-config', extensions: %w[.js.rollup .rollup], charset: :unicode
17
+ Sprockets.register_transformer 'application/javascript+rollup-config', 'application/javascript', Jass::Rollup::Processor
18
+ Sprockets.register_preprocessor 'application/javascript', Jass::Rollup::DirectiveProcessor
19
+ end
@@ -0,0 +1 @@
1
+ require_relative 'jass/rollup'
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jass-rollup
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
+ platform: ruby
6
+ authors:
7
+ - Matthias Grosser
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-10-11 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.5.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.5.4
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-rollup.rb
93
+ - lib/jass/rollup.rb
94
+ - lib/jass/rollup/compiler.rb
95
+ - lib/jass/rollup/directive_processor.rb
96
+ - lib/jass/rollup/processor.rb
97
+ - lib/jass/rollup/version.rb
98
+ homepage: https://github.com/mtgrosser/jass-rollup
99
+ licenses:
100
+ - MIT
101
+ metadata: {}
102
+ post_install_message:
103
+ rdoc_options: []
104
+ require_paths:
105
+ - lib
106
+ required_ruby_version: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 2.3.0
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ requirements: []
117
+ rubygems_version: 3.1.4
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Rollup for Sprockets and the Rails asset pipeline
121
+ test_files: []