bridgetown-lit-renderer 1.0.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: 0b433fc8ed4d01b4ffb96f041a894f48443331dcec6b9d95a892e2c302090642
4
+ data.tar.gz: c9cdc7615999205a2b559ba20d1b0da57aa80c10469ee39a96edc1fa7d7b3301
5
+ SHA512:
6
+ metadata.gz: 802f0cb11030c562403b66caf8748450c0e69fd1ec1aa2da6ffbb22be1e9b01801e340e398ea1ce674f2b3f56f252627cb47f30f5efccf05afa478ae873bfe28
7
+ data.tar.gz: 54d46e263d8b89aca439f8bdd54e512fe27abed9e523a86dfaf1912d88d8e99d90907f3609b05c865ed578de6f347a77fb2f311d861082a7073c0f47908ce4a2
data/.gitignore ADDED
@@ -0,0 +1,38 @@
1
+ /vendor
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
16
+ *.gem
17
+ Gemfile.lock
18
+ .bundle
19
+ .ruby-version
20
+
21
+ # Node
22
+ node_modules
23
+ .npm
24
+ .node_repl_history
25
+
26
+ # Yarn
27
+ yarn-error.log
28
+ yarn-debug.log*
29
+ .pnp/
30
+ .pnp.js
31
+
32
+ # Yarn Integrity file
33
+ .yarn-integrity
34
+
35
+ test/dest
36
+ .bridgetown-metadata
37
+ .bridgetown-cache
38
+ .bridgetown-webpack
data/.rubocop.yml ADDED
@@ -0,0 +1,22 @@
1
+ require: rubocop-bridgetown
2
+
3
+ inherit_gem:
4
+ rubocop-bridgetown: .rubocop.yml
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 2.5
8
+ Include:
9
+ - lib/**/*.rb
10
+ - test/**/*.rb
11
+
12
+ Exclude:
13
+ - .gitignore
14
+ - .rubocop.yml
15
+
16
+ - Gemfile.lock
17
+ - CHANGELOG.md
18
+ - LICENSE.txt
19
+ - README.md
20
+
21
+ - script/**/*
22
+ - vendor/**/*
data/CHANGELOG.md ADDED
@@ -0,0 +1,14 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## Unreleased
9
+
10
+ - ...
11
+
12
+ ## 1.0.0 - 2021-09-25
13
+
14
+ - First release (currently experimental stage).
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+ gemspec
5
+
6
+ gem "bridgetown", ENV["BRIDGETOWN_VERSION"] if ENV["BRIDGETOWN_VERSION"]
7
+
8
+ group :test do
9
+ gem "minitest"
10
+ gem "minitest-reporters"
11
+ gem "shoulda"
12
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021-present Jared White and Bridgetown contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # Bridgetown Lit Renderer
2
+
3
+ Simple pipeline for SSR + hydration of Lit components in your Bridgetown site.
4
+
5
+ ## Installation
6
+
7
+ Run this command to add this plugin to your site's Gemfile, along with Lit 2 and Lit's SSR support:
8
+
9
+ ```shell
10
+ $ bundle add bridgetown-lit-renderer -g bridgetown_plugins
11
+
12
+ $ yarn add lit @lit-labs/ssr
13
+ ```
14
+
15
+ Create a file in `config/lit-ssr.config.js` with the following:
16
+
17
+ ```js
18
+ const build = require("bridgetown-lit-renderer/build")
19
+
20
+ // You can customize this as you wish, perhaps add new esbuild plugins
21
+ const esbuildOptions = {}
22
+
23
+ build(esbuildOptions)
24
+ ```
25
+
26
+ Add the following to the top of your `frontend/javascript/index.js` file:
27
+
28
+ ```js
29
+ import "bridgetown-lit-renderer"
30
+ import "./lit-components"
31
+ ```
32
+
33
+ For the purposes of testing your install, add the following to `frontend/javascript/lit-components.js`:
34
+
35
+ ```js
36
+ import "lit/experimental-hydrate-support.js"
37
+ import { LitElement, html, css } from "lit"
38
+
39
+ export class HappyDaysElement extends LitElement {
40
+ static styles = css`
41
+ :host {
42
+ display: block;
43
+ border: 2px dashed gray;
44
+ margin: 4px;
45
+ padding: 4px;
46
+ width: 20vw;
47
+ }
48
+ `
49
+
50
+ static properties = {
51
+ hello: { type: String }
52
+ }
53
+
54
+ render() {
55
+ return html`
56
+ <p>Hello ${this.hello}! ${Date.now()}</p>
57
+ `;
58
+ }
59
+ }
60
+
61
+ customElements.define('happy-days', HappyDaysElement)
62
+ ```
63
+
64
+ Finally, create a new `.erb` page somewhere in `src`, and add this somewhere in your template:
65
+
66
+ ```erb
67
+ <%= lit data: {hello: "there"} do %>
68
+ <happy-days hello="${data.hello}"></happy-days>
69
+ <% end %>
70
+ ```
71
+
72
+ Now start up your Bridgetown site, visit the page, and if all goes well, you should see a box containing "Hello there!" and a timestamp when the page was first rendered.
73
+
74
+ You can reload the page several times and see that the timestamp doesn't change, because Lit's SSR + Hydration support knows not to re-render the component. However, if you change the `hello` attribute in the HTML, you'll get a re-render and thus see a new timestamp. _How cool is that?!_
75
+
76
+ ### Configuration options
77
+
78
+
79
+
80
+ ## Testing
81
+
82
+ * Run `bundle exec rake test` to run the test suite
83
+ * Or run `script/cibuild` to validate with Rubocop and Minitest together.
84
+
85
+ ## Contributing
86
+
87
+ 1. Fork it (https://github.com/bridgetownrb/bridgetown-lit-renderer/fork)
88
+ 2. Clone the fork using `git clone` to your local development machine.
89
+ 3. Create your feature branch (`git checkout -b my-new-feature`)
90
+ 4. Commit your changes (`git commit -am 'Add some feature'`)
91
+ 5. Push to the branch (`git push origin my-new-feature`)
92
+ 6. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/test_*.rb"]
8
+ t.warning = false
9
+ end
10
+
11
+ task :default => :test
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/bridgetown-lit-renderer/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "bridgetown-lit-renderer"
7
+ spec.version = BridgetownLitRenderer::VERSION
8
+ spec.author = "Bridgetown Team"
9
+ spec.email = "maintainers@bridgetownrb.com"
10
+ spec.summary = "Simple pipeline for SSR + hydration of Lit components"
11
+ spec.homepage = "https://github.com/bridgetownrb/bridgetown-lit-renderer"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r!^(test|script|spec|features|frontend)/!) }
15
+ spec.test_files = spec.files.grep(%r!^test/!)
16
+ spec.require_paths = ["lib"]
17
+ spec.metadata = { "yarn-add" => "bridgetown-lit-renderer@#{BridgetownLitRenderer::VERSION}" }
18
+
19
+ spec.required_ruby_version = ">= 2.5.0"
20
+
21
+ spec.add_dependency "bridgetown", ">= 0.15", "< 2.0"
22
+
23
+ spec.add_development_dependency "bundler"
24
+ spec.add_development_dependency "rake", "~> 12.0"
25
+ spec.add_development_dependency "rubocop-bridgetown", "~> 0.2"
26
+ end
@@ -0,0 +1,93 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BridgetownLitRenderer
4
+ module Renderer
5
+ def self.reset_cache
6
+ @code_cache = {}
7
+ @lit_notice_printed = false
8
+ end
9
+
10
+ def self.esbuild(code, site)
11
+ unless @lit_notice_printed
12
+ Bridgetown.logger.info "Lit SSR:", "Bundling with esbuild..."
13
+ @lit_notice_printed = true
14
+ end
15
+
16
+ IO.popen(["node", site.in_root_dir("./config/lit-ssr.config.js")], "r+") do |pipe|
17
+ pipe.puts({ code: code }.to_json)
18
+ pipe.close_write
19
+ pipe.read
20
+ end
21
+ end
22
+
23
+ def self.render(code, data:, entry:, site:) # rubocop:disable Metrics/MethodLength
24
+ @code_cache ||= {}
25
+ cache_key = "#{code}#{entry}"
26
+
27
+ unless @code_cache[cache_key]
28
+ build_code = <<~JS
29
+ import { Readable } from "stream"
30
+ import { render } from "@lit-labs/ssr/lib/render-with-global-dom-shim.js"
31
+ import { html } from "lit"
32
+ import #{entry.to_json}
33
+
34
+ const ssrResult = render(html`
35
+ #{code}
36
+ `);
37
+
38
+ const _tmplStream = Readable.from(ssrResult)
39
+
40
+ let _tmplOutput = ""
41
+ _tmplStream.on('data', function(chunk) {
42
+ _tmplOutput += chunk;
43
+ });
44
+
45
+ _tmplStream.on('end',function() {
46
+ process.stdout.write("====== SSR ======") // marker to ensure stray console outputs don't end up in HTML
47
+ process.stdout.write(_tmplOutput)
48
+ });
49
+ JS
50
+
51
+ @code_cache[cache_key] = esbuild(build_code, site)
52
+ end
53
+
54
+ IO.popen(["node"], "r+") do |pipe|
55
+ pipe.puts "const data = #{data.to_json}; #{@code_cache[cache_key]}"
56
+ pipe.close_write
57
+ pipe.read
58
+ end.partition("====== SSR ======").last.html_safe
59
+ end
60
+ end
61
+
62
+ class Builder < Bridgetown::Builder
63
+ def self.cache
64
+ @cache ||= Bridgetown::Cache.new("LitSSR")
65
+ end
66
+
67
+ def build
68
+ BridgetownLitRenderer::Renderer.reset_cache
69
+ helper "lit", helpers_scope: true do |
70
+ data: {},
71
+ hydrate_root: true,
72
+ entry: "./frontend/javascript/lit-components.js",
73
+ &block
74
+ |
75
+ code = view.capture(&block)
76
+ if hydrate_root
77
+ code = "<hydrate-root>#{code.sub(%r{\<([a-zA-Z]+-[a-zA-Z-]*)}, "<\\1 defer-hydration")}</hydrate-root>" # rubocop:disable Layout/LineLength
78
+ end
79
+
80
+ if site.config.disable_lit_caching
81
+ next BridgetownLitRenderer::Renderer.render(code, data: data, entry: entry, site: site)
82
+ end
83
+
84
+ entry_key = entry.start_with?("./") ? File.stat(site.in_root_dir(entry)).mtime : entry
85
+ BridgetownLitRenderer::Builder.cache.getset("#{code}#{data}#{entry_key}") do
86
+ BridgetownLitRenderer::Renderer.render(code, data: data, entry: entry, site: site)
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ BridgetownLitRenderer::Builder.register
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BridgetownLitRenderer
4
+ VERSION = "1.0.0"
5
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bridgetown"
4
+ require "bridgetown-lit-renderer/builder"
data/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "bridgetown-lit-renderer",
3
+ "version": "1.0.0",
4
+ "main": "frontend/javascript/index.js",
5
+ "exports": {
6
+ ".": {
7
+ "browser": {
8
+ "default": "./frontend/javascript/index.js"
9
+ }
10
+ },
11
+ "./build": {
12
+ "require": "./src/build.js"
13
+ }
14
+ },
15
+ "repository": {
16
+ "type": "git",
17
+ "url": "https://github.com/bridgetownrb/bridgetown-lit-renderer.git"
18
+ },
19
+ "author": "Bridgetown Maintainers <maintainers@bridgetownrb.com>",
20
+ "homepage": "https://www.bridgetownrb.com",
21
+ "license": "MIT",
22
+ "private": false,
23
+ "files": [
24
+ "frontend",
25
+ "src"
26
+ ],
27
+ "dependencies": {
28
+ "@webcomponents/template-shadowroot": "^0.1.0",
29
+ "esbuild": "^0.12"
30
+ }
31
+ }
data/src/build.js ADDED
@@ -0,0 +1,32 @@
1
+ const path = require("path")
2
+
3
+ module.exports = (esbuildOptions) => {
4
+ let inputData = [];
5
+
6
+ process.stdin.resume();
7
+ process.stdin.setEncoding('utf8');
8
+
9
+ process.stdin.on('data', data => {
10
+ inputData.push(data)
11
+ })
12
+
13
+ process.stdin.on('end', () => {
14
+ const inputValues = JSON.parse(inputData.join())
15
+
16
+ require('esbuild').build({
17
+ ...esbuildOptions,
18
+ stdin: {
19
+ contents: inputValues.code,
20
+ resolveDir: process.cwd(),
21
+ sourcefile: 'lit-ssr-output.js' // imaginary file
22
+ },
23
+ platform: "node",
24
+ inject: [path.join(__dirname, "import-meta-url-shim.js")],
25
+ define: { "import.meta.url": "import_meta_url" },
26
+ bundle: true,
27
+ write: false,
28
+ }).then(result => {
29
+ process.stdout.write(result.outputFiles[0].text)
30
+ }).catch(_e => { })
31
+ })
32
+ }
@@ -0,0 +1 @@
1
+ export var import_meta_url = require('url').pathToFileURL(__filename);
data/yarn.lock ADDED
@@ -0,0 +1,13 @@
1
+ # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2
+ # yarn lockfile v1
3
+
4
+
5
+ "@webcomponents/template-shadowroot@^0.1.0":
6
+ version "0.1.0"
7
+ resolved "https://registry.yarnpkg.com/@webcomponents/template-shadowroot/-/template-shadowroot-0.1.0.tgz#adb3438d0d9a18e8fced08abc253f56b7eadab00"
8
+ integrity sha512-ry84Vft6xtRBbd4M/ptRodbOLodV5AD15TYhyRghCRgIcJJKmYmJ2v2BaaWxygENwh6Uq3zTfGPmlckKT/GXsQ==
9
+
10
+ esbuild@^0.12:
11
+ version "0.12.29"
12
+ resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.12.29.tgz#be602db7c4dc78944a9dbde0d1ea19d36c1f882d"
13
+ integrity sha512-w/XuoBCSwepyiZtIRsKsetiLDUVGPVw1E/R3VTFSecIy8UR7Cq3SOtwKHJMFoVqqVG36aGkzh4e8BvpO1Fdc7g==
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bridgetown-lit-renderer
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Bridgetown Team
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-09-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bridgetown
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0.15'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '0.15'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: bundler
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '12.0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '12.0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rubocop-bridgetown
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '0.2'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '0.2'
75
+ description:
76
+ email: maintainers@bridgetownrb.com
77
+ executables: []
78
+ extensions: []
79
+ extra_rdoc_files: []
80
+ files:
81
+ - ".gitignore"
82
+ - ".rubocop.yml"
83
+ - CHANGELOG.md
84
+ - Gemfile
85
+ - LICENSE.txt
86
+ - README.md
87
+ - Rakefile
88
+ - bridgetown-lit-renderer.gemspec
89
+ - lib/bridgetown-lit-renderer.rb
90
+ - lib/bridgetown-lit-renderer/builder.rb
91
+ - lib/bridgetown-lit-renderer/version.rb
92
+ - package.json
93
+ - src/build.js
94
+ - src/import-meta-url-shim.js
95
+ - yarn.lock
96
+ homepage: https://github.com/bridgetownrb/bridgetown-lit-renderer
97
+ licenses:
98
+ - MIT
99
+ metadata:
100
+ yarn-add: bridgetown-lit-renderer@1.0.0
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.5.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: Simple pipeline for SSR + hydration of Lit components
120
+ test_files: []