vite_ruby-roda 3.9.2.1

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.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Maximo Mussini
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,84 @@
1
+ <h1 align="center">
2
+ <a href="https://vite-ruby.netlify.app/">
3
+ <img src="https://raw.githubusercontent.com/ElMassimo/vite_ruby/main/logo.svg" width="120px"/>
4
+ </a>
5
+
6
+ <br>
7
+
8
+ <a href="https://vite-ruby.netlify.app/">
9
+ Vite Ruby
10
+ </a>
11
+
12
+ <br>
13
+
14
+ <p align="center">
15
+ <a href="https://github.com/ElMassimo/vite_ruby/actions">
16
+ <img alt="Build Status" src="https://github.com/ElMassimo/vite_ruby/workflows/build/badge.svg"/>
17
+ </a>
18
+ <a href="https://codeclimate.com/github/ElMassimo/vite_ruby">
19
+ <img alt="Maintainability" src="https://codeclimate.com/github/ElMassimo/vite_ruby/badges/gpa.svg"/>
20
+ </a>
21
+ <a href="https://codeclimate.com/github/ElMassimo/vite_ruby">
22
+ <img alt="Test Coverage" src="https://codeclimate.com/github/ElMassimo/vite_ruby/badges/coverage.svg"/>
23
+ </a>
24
+ <a href="https://rubygems.org/gems/vite_ruby">
25
+ <img alt="Gem Version" src="https://img.shields.io/gem/v/vite_ruby.svg?colorB=e9573f"/>
26
+ </a>
27
+ <a href="https://github.com/ElMassimo/vite_ruby/blob/master/LICENSE.txt">
28
+ <img alt="License" src="https://img.shields.io/badge/license-MIT-428F7E.svg"/>
29
+ </a>
30
+ </p>
31
+ </h1>
32
+
33
+ [website]: https://vite-ruby.netlify.app/
34
+ [configuration reference]: https://vite-ruby.netlify.app/config/
35
+ [features]: https://vite-ruby.netlify.app/guide/introduction.html
36
+ [guides]: https://vite-ruby.netlify.app/guide/
37
+ [config]: https://vite-ruby.netlify.app/config/
38
+ [vite_rails]: https://github.com/ElMassimo/vite_ruby
39
+ [webpacker]: https://github.com/rails/webpacker
40
+ [vite]: http://vitejs.dev/
41
+ [config file]: https://github.com/ElMassimo/vite_ruby/blob/main/vite-plugin-ruby/default.vite.json
42
+ [example app]: https://github.com/ElMassimo/pingcrm-vite
43
+ [heroku]: https://pingcrm-vite.herokuapp.com/
44
+ [Issues]: https://github.com/ElMassimo/vite_ruby/issues?q=is%3Aissue+is%3Aopen+sort%3Aupdated-desc
45
+ [Discussions]: https://github.com/ElMassimo/vite_ruby/discussions
46
+ [vite_rails]: https://github.com/ElMassimo/vite_ruby/tree/main/vite_rails
47
+ [vite_ruby]: https://github.com/ElMassimo/vite_ruby/tree/main/vite_ruby
48
+ [vite_hanami]: https://github.com/ElMassimo/vite_ruby/tree/main/vite_hanami
49
+
50
+
51
+ [Vite] is to frontend tooling as Ruby to programming, pure joy! 😍
52
+
53
+ This is the core library for Rack apps, used by [<kbd>vite_rails</kbd>][vite_rails] and [<kbd>vite_hanami</kbd>][vite_hanami].
54
+
55
+ ## Features ⚡️
56
+
57
+ - 💡 Instant server start
58
+ - ⚡️ Blazing fast hot reload
59
+ - 🚀 Zero-config deployments
60
+ - 🤝 Integrated with <kbd>assets:precompile</kbd>
61
+ - [And more!][features]
62
+
63
+ ## Documentation 📖
64
+
65
+ Visit the [documentation website][website] to check out the [guides] and searchable [configuration reference].
66
+
67
+ ## Contact ✉️
68
+
69
+ Please use [Issues] to report bugs you find, and [Discussions] to make feature requests or get help.
70
+
71
+ Don't hesitate to _⭐️ star the project_ if you find it useful!
72
+
73
+ Using it in production? Always love to hear about it! 😃
74
+
75
+
76
+ ## Special Thanks 🙏
77
+
78
+ - [webpacker]
79
+ - [vite]
80
+
81
+
82
+ ## License
83
+
84
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/default.vite.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "additionalEntrypoints": ["~/{assets,fonts,icons,images}/**/*"],
3
+ "assetHost": null,
4
+ "assetsDir": "assets",
5
+ "autoBuild": false,
6
+ "buildCacheDir": "tmp/cache/vite",
7
+ "publicOutputDir": "vite",
8
+ "configPath": "config/vite.json",
9
+ "devServerConnectTimeout": 0.01,
10
+ "packageManager": null,
11
+ "publicDir": "public",
12
+ "entrypointsDir": "entrypoints",
13
+ "sourceCodeDir": "app/frontend",
14
+ "skipCompatibilityCheck": false,
15
+ "skipProxy": false,
16
+ "host": "localhost",
17
+ "https": null,
18
+ "port": 3036,
19
+ "hideBuildConsoleOutput": false,
20
+ "viteBinPath": null,
21
+ "watchAdditionalPaths": [],
22
+ "base": "",
23
+ "ssrBuildEnabled": false,
24
+ "ssrEntrypoint": "~/ssr/ssr.{js,ts,jsx,tsx}",
25
+ "ssrOutputDir": "public/vite-ssr"
26
+ }
data/exe/vite ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ ENV["RAILS_ENV"] ||= ENV["RACK_ENV"]
5
+
6
+ require "bundler/setup"
7
+ require "vite_ruby"
8
+
9
+ cli = ViteRuby::CLI
10
+ cli.require_framework_libraries
11
+
12
+ Dry::CLI.new(cli).call
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ $stdout.sync = true
4
+
5
+ require "rake"
6
+
7
+ namespace :vite do
8
+ task :binstubs do
9
+ ViteRuby.commands.install_binstubs
10
+ end
11
+
12
+ desc "Bundle frontend entrypoints using ViteRuby"
13
+ task build: :"vite:verify_install" do
14
+ ViteRuby.commands.build_from_task
15
+ end
16
+
17
+ desc "Bundle a Node.js app from the SSR entrypoint using ViteRuby"
18
+ task build_ssr: :"vite:verify_install" do
19
+ ViteRuby.commands.build_from_task("--ssr")
20
+ end
21
+
22
+ desc "Bundle entrypoints using Vite Ruby (SSR only if enabled)"
23
+ task build_all: :"vite:verify_install" do
24
+ ViteRuby.commands.build_from_task
25
+ ViteRuby.commands.build_from_task("--ssr") if ViteRuby.config.ssr_build_enabled
26
+ end
27
+
28
+ desc "Remove the build output directory for ViteRuby"
29
+ task clobber: :"vite:verify_install" do
30
+ ViteRuby.commands.clobber
31
+ end
32
+
33
+ desc "Verify if ViteRuby is properly installed in the app"
34
+ task :verify_install do
35
+ ViteRuby.commands.verify_install
36
+ end
37
+
38
+ desc "Ensure build dependencies like Vite are installed before bundling"
39
+ task :install_dependencies do
40
+ install_env_args = (ENV["VITE_RUBY_SKIP_INSTALL_DEV_DEPENDENCIES"] == "true") ? {} : {"NODE_ENV" => "development"}
41
+
42
+ install_cmd = case (pkg = ViteRuby.config.package_manager)
43
+ when "npm" then "npm ci"
44
+ else "#{pkg} install --frozen-lockfile"
45
+ end
46
+
47
+ system(install_env_args, install_cmd)
48
+ end
49
+
50
+ desc "Provide information on ViteRuby's environment"
51
+ task :info do
52
+ ViteRuby.commands.print_info
53
+ end
54
+ end
55
+
56
+ unless ENV["VITE_RUBY_SKIP_ASSETS_PRECOMPILE_EXTENSION"] == "true"
57
+ if Rake::Task.task_defined?("assets:precompile")
58
+ Rake::Task["assets:precompile"].enhance do |task|
59
+ prefix = task.name.split(/#|assets:precompile/).first
60
+ unless ENV["VITE_RUBY_SKIP_ASSETS_PRECOMPILE_INSTALL"] == "true"
61
+ Rake::Task["#{prefix}vite:install_dependencies"].invoke
62
+ end
63
+ Rake::Task["#{prefix}vite:build_all"].invoke
64
+ end
65
+ else
66
+ desc "Bundle Vite assets"
67
+ if ENV["VITE_RUBY_SKIP_ASSETS_PRECOMPILE_INSTALL"] == "true"
68
+ Rake::Task.define_task("assets:precompile" => "vite:build_all")
69
+ else
70
+ Rake::Task.define_task("assets:precompile" => ["vite:install_dependencies", "vite:build_all"])
71
+ end
72
+ end
73
+
74
+ if Rake::Task.task_defined?("assets:clobber")
75
+ Rake::Task["assets:clobber"].enhance do
76
+ Rake::Task["vite:clobber"].invoke
77
+ end
78
+ else
79
+ desc "Remove compiled assets"
80
+ Rake::Task.define_task("assets:clobber" => "vite:clobber")
81
+ end
82
+ end
83
+
84
+ # Any prerequisite task that installs packages should also install build dependencies.
85
+ if ARGV.include?("assets:precompile")
86
+ if ViteRuby.commands.legacy_npm_version?
87
+ ENV["NPM_CONFIG_PRODUCTION"] = "false"
88
+ else
89
+ ENV["NPM_CONFIG_INCLUDE"] = "dev"
90
+ end
91
+
92
+ if ViteRuby.commands.legacy_yarn_version?
93
+ ENV["YARN_PRODUCTION"] = "false"
94
+ end
95
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "time"
5
+
6
+ # Internal: Value object with information about the last build.
7
+ ViteRuby::Build = Struct.new(:success, :timestamp, :vite_ruby, :digest, :current_digest, :last_build_path, :errors, keyword_init: true) do
8
+ class << self
9
+ # Internal: Combines information from a previous build with the current digest.
10
+ def from_previous(last_build_path, current_digest)
11
+ new(
12
+ **parse_metadata(last_build_path),
13
+ current_digest: current_digest,
14
+ last_build_path: last_build_path,
15
+ )
16
+ end
17
+
18
+ private
19
+
20
+ # Internal: Reads metadata recorded on the last build, if it exists.
21
+ def parse_metadata(pathname)
22
+ return default_metadata unless pathname.exist?
23
+
24
+ JSON.parse(pathname.read.to_s).transform_keys(&:to_sym).slice(*members)
25
+ rescue JSON::JSONError, Errno::ENOENT, Errno::ENOTDIR
26
+ default_metadata
27
+ end
28
+
29
+ # Internal: To make it evident that there's no last build in error messages.
30
+ def default_metadata
31
+ {timestamp: "never", digest: "none"}
32
+ end
33
+ end
34
+
35
+ # Internal: A build is considered stale when watched files have changed since
36
+ # the last build, or when a certain time has ellapsed in case of failure.
37
+ def stale?
38
+ digest != current_digest || retry_failed? || vite_ruby != ViteRuby::VERSION
39
+ end
40
+
41
+ # Internal: A build is considered fresh if watched files have not changed, or
42
+ # the last failed build happened recently.
43
+ def fresh?
44
+ !stale?
45
+ end
46
+
47
+ # Internal: To avoid cascading build failures, if the last build failed and it
48
+ # happened within a short time window, a new build should not be triggered.
49
+ def retry_failed?
50
+ !success && Time.parse(timestamp) + 3 < Time.now # 3 seconds
51
+ rescue ArgumentError
52
+ true
53
+ end
54
+
55
+ # Internal: Returns a new build with the specified result.
56
+ def with_result(**attrs)
57
+ self.class.new(
58
+ **attrs,
59
+ timestamp: Time.now.strftime("%F %T"),
60
+ vite_ruby: ViteRuby::VERSION,
61
+ digest: current_digest,
62
+ current_digest: current_digest,
63
+ last_build_path: last_build_path,
64
+ )
65
+ end
66
+
67
+ # Internal: Writes the result of the new build to a local file.
68
+ def write_to_cache
69
+ last_build_path.write to_json
70
+ end
71
+
72
+ # Internal: Returns a JSON string with the metadata of the build.
73
+ def to_json(*_args)
74
+ JSON.pretty_generate(
75
+ success: success,
76
+ errors: errors,
77
+ timestamp: timestamp,
78
+ vite_ruby: vite_ruby,
79
+ digest: digest,
80
+ )
81
+ end
82
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "digest/sha1"
4
+
5
+ # Public: Keeps track of watched files and triggers builds as needed.
6
+ class ViteRuby::Builder
7
+ def initialize(vite_ruby)
8
+ @vite_ruby = vite_ruby
9
+ end
10
+
11
+ # Public: Checks if the watched files have changed since the last compilation,
12
+ # and triggers a Vite build if any files have changed.
13
+ def build(*args)
14
+ last_build = last_build_metadata(ssr: args.include?("--ssr"))
15
+
16
+ if args.delete("--force") || last_build.stale? || config.manifest_paths.empty?
17
+ stdout, stderr, status = build_with_vite(*args)
18
+ log_build_result(stdout, stderr, status)
19
+ record_build_metadata(last_build, errors: stderr, success: status.success?)
20
+ status.success?
21
+ elsif last_build.success
22
+ logger.debug "Skipping vite build. Watched files have not changed since the last build at #{last_build.timestamp}"
23
+ true
24
+ else
25
+ logger.error "Skipping vite build. Watched files have not changed since the build failed at #{last_build.timestamp} ❌"
26
+ false
27
+ end
28
+ end
29
+
30
+ # Internal: Reads the result of the last compilation from disk.
31
+ def last_build_metadata(ssr: false)
32
+ ViteRuby::Build.from_previous(last_build_path(ssr: ssr), watched_files_digest)
33
+ end
34
+
35
+ private
36
+
37
+ extend Forwardable
38
+
39
+ def_delegators :@vite_ruby, :config, :logger, :run
40
+
41
+ # Internal: Writes a digest of the watched files to disk for future checks.
42
+ def record_build_metadata(build, **attrs)
43
+ config.build_cache_dir.mkpath
44
+ build.with_result(**attrs).write_to_cache
45
+ end
46
+
47
+ # Internal: The file path where metadata of the last build is stored.
48
+ def last_build_path(ssr:)
49
+ config.build_cache_dir.join("last#{"-ssr" if ssr}-build-#{config.mode}.json")
50
+ end
51
+
52
+ # Internal: Returns a digest of all the watched files, allowing to detect
53
+ # changes, and skip Vite builds if no files have changed.
54
+ def watched_files_digest
55
+ return @last_digest if @last_digest_at && Time.now - @last_digest_at < 1
56
+
57
+ config.within_root do
58
+ files = Dir[*config.watched_paths].reject { |f| File.directory?(f) }
59
+ file_ids = files.sort.map { |f| "#{File.basename(f)}/#{Digest::SHA1.file(f).hexdigest}" }
60
+ @last_digest_at = Time.now
61
+ @last_digest = Digest::SHA1.hexdigest(file_ids.join("/"))
62
+ end
63
+ end
64
+
65
+ # Public: Initiates a Vite build command to generate assets.
66
+ def build_with_vite(*args)
67
+ logger.info "Building with Vite ⚡️"
68
+
69
+ run(["build", *args])
70
+ end
71
+
72
+ # Internal: Outputs the build results.
73
+ #
74
+ # NOTE: By default it also outputs the manifest entries.
75
+ def log_build_result(_stdout, stderr, status)
76
+ if status.success?
77
+ logger.info "Build with Vite complete: #{config.build_output_dir}"
78
+ logger.error stderr unless stderr.empty?
79
+ else
80
+ logger.error stderr
81
+ logger.error status
82
+ logger.error "Build with Vite failed! ❌"
83
+ logger.error "❌ Check that vite and vite-plugin-ruby are in devDependencies and have been installed. " if stderr.include?("ERR! canceled")
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ViteRuby::CLI::Build < ViteRuby::CLI::Vite
4
+ DEFAULT_ENV = CURRENT_ENV || "production"
5
+
6
+ desc "Bundle all entrypoints using Vite."
7
+ shared_options
8
+ option(:ssr, desc: "Build the SSR entrypoint instead", type: :boolean)
9
+ option(:force, desc: "Force the build even if assets have not changed", type: :boolean)
10
+ option(:watch, desc: "Start the Rollup watcher and rebuild on files changes", type: :boolean)
11
+ option(:profile, desc: "Gather performance metrics from the build ", type: :boolean)
12
+
13
+ def call(**options)
14
+ super { |args| ViteRuby.commands.build_from_task(*args) }
15
+ end
16
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ViteRuby::CLI::Clobber < Dry::CLI::Command
4
+ desc "Clear the Vite cache, temp files, and builds"
5
+
6
+ current_env = ENV["RACK_ENV"] || ENV["RAILS_ENV"] || "development"
7
+
8
+ option(:mode, default: current_env, values: %w[development production test], aliases: ["m"], desc: "The mode to use")
9
+
10
+ def call(mode:, **)
11
+ ViteRuby.env["VITE_RUBY_MODE"] = mode
12
+ ViteRuby.commands.clobber
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ViteRuby::CLI::Dev < ViteRuby::CLI::Vite
4
+ DEFAULT_ENV = CURRENT_ENV || "development"
5
+
6
+ desc "Start the Vite development server."
7
+ shared_options
8
+ option(:force, desc: "Force Vite to re-bundle dependencies", type: :boolean)
9
+
10
+ def call(**options)
11
+ super { |args| ViteRuby.run(args, exec: true) }
12
+ end
13
+ end
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+ require "fileutils"
5
+
6
+ # NOTE: Extracted from dry-cli version 0.6.0, which later removed this file as
7
+ # it was refactored and extracted into the more complete (and complex) dry-files.
8
+ module ViteRuby::CLI::FileUtils
9
+ class << self
10
+ # Creates a new file or rewrites the contents of an existing file.
11
+ #
12
+ # @since 1.2.11
13
+ # @api private
14
+ def write(path, *content)
15
+ mkdir_p(path)
16
+ File.open(path, File::CREAT | File::WRONLY | File::TRUNC) do |file|
17
+ file.write(Array(content).flatten.join)
18
+ end
19
+ end
20
+
21
+ # Copies source into destination.
22
+ #
23
+ # @since 1.2.11
24
+ # @api private
25
+ def cp(source, destination)
26
+ mkdir_p(destination)
27
+ FileUtils.cp(source, destination) unless File.exist?(destination)
28
+ end
29
+
30
+ # Adds a new line at the bottom of the file.
31
+ #
32
+ # @since 1.2.11
33
+ # @api private
34
+ def append(path, contents)
35
+ content = read_lines(path)
36
+ return if content.join.include?(contents)
37
+
38
+ content << "\n" unless content.last&.end_with?("\n")
39
+ content << "#{contents}\n"
40
+
41
+ write(path, content)
42
+ end
43
+
44
+ # Replace first line in `path` that contains `target` with `replacement`.
45
+ #
46
+ # @since 1.2.11
47
+ # @api private
48
+ def replace_first_line(path, target, replacement)
49
+ content = read_lines(path)
50
+ content[index(content, path, target)] = "#{replacement}\n"
51
+
52
+ write(path, content)
53
+ end
54
+
55
+ # Inject `contents` in `path` before `target`.
56
+ #
57
+ # @since 1.2.11
58
+ # @api private
59
+ def inject_line_before(path, target, contents)
60
+ _inject_line_before(path, target, contents, method(:index))
61
+ end
62
+
63
+ # Inject `contents` in `path` after `target`.
64
+ #
65
+ # @since 1.2.11
66
+ # @api private
67
+ def inject_line_after(path, target, contents)
68
+ _inject_line_after(path, target, contents, method(:index))
69
+ end
70
+
71
+ # Inject `contents` in `path` after last `target`.
72
+ #
73
+ # @since 1.2.11
74
+ # @api private
75
+ def inject_line_after_last(path, target, contents)
76
+ _inject_line_after(path, target, contents, method(:rindex))
77
+ end
78
+
79
+ private
80
+
81
+ # Creates all parent directories for the given file path.
82
+ #
83
+ # @since 1.2.11
84
+ # @api private
85
+ def mkdir_p(path)
86
+ Pathname.new(path).dirname.mkpath
87
+ end
88
+
89
+ # Returns an array with lines in the specified file, empty if it doesn't exist.
90
+ def read_lines(path)
91
+ File.exist?(path) ? File.readlines(path) : []
92
+ end
93
+
94
+ # @since 1.2.11
95
+ # @api private
96
+ def index(content, path, target)
97
+ content.index { |line| line.include?(target) } ||
98
+ raise(ArgumentError, "Cannot find `#{target}' inside `#{path}'.")
99
+ end
100
+
101
+ # @since 1.2.11
102
+ # @api private
103
+ def rindex(content, path, target)
104
+ content.rindex { |line| line.include?(target) } ||
105
+ raise(ArgumentError, "Cannot find `#{target}' inside `#{path}'.")
106
+ end
107
+
108
+ # @since 1.2.11
109
+ # @api private
110
+ def _inject_line_before(path, target, contents, finder)
111
+ content = read_lines(path)
112
+ return if content.join.include?(contents)
113
+
114
+ i = finder.call(content, path, target)
115
+
116
+ content.insert(i, "#{contents}\n")
117
+ write(path, content)
118
+ end
119
+
120
+ # @since 1.2.11
121
+ # @api private
122
+ def _inject_line_after(path, target, contents, finder)
123
+ content = read_lines(path)
124
+ return if content.join.include?(contents)
125
+
126
+ i = finder.call(content, path, target)
127
+
128
+ content.insert(i + 1, "#{contents}\n")
129
+ write(path, content)
130
+ end
131
+ end
132
+ end