简体   繁体   English

Rails 7 和 importmaps:我的 javascript 被分成许多文件,在生产环境中会发生什么情况?

[英]Rails 7 and importmaps: What will happen on production with my javascript that is split into many files?

I'm in the process of upgrading a heroku hosted website from Rails 5 to Rails 7. I was using webpacker, but since it's deprecated, I decided to switch to using importmaps.我正在将 heroku 托管网站从 Rails 5 升级到 Rails 7。我使用的是 webpacker,但由于它已被弃用,我决定改用 importmaps。 My site has a lot of javascript, mainly for a bunch of games/toys/experiments.我的站点有很多javascript,主要是一堆游戏/玩具/实验。 The javascript is very object oriented. javascript 非常面向 object。 Each game/toy/experiment uses a lot of classes, many of them quite small, and every class is in its own file, using es6 style imports: "import Something from './something'".每个游戏/玩具/实验都使用很多类,其中很多都非常小,每个 class 都在自己的文件中,使用 es6 样式导入:“从'./something'导入一些东西”。

Before the upgrade, some combination of shims, transpiling, and asset precompiling would turn these many files into just a few files, on both production and development servers.在升级之前,在生产和开发服务器上,垫片、转译和资产预编译的某种组合会将这么多文件变成只有几个文件。 I definitely don't remember which tools were doing what, and hopefully I won't have to, but here is my Gemfile before the upgrade:我绝对不记得哪些工具在做什么,希望我不必记住,但这是升级的 Gemfile:

source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.6'

gem 'rails', '~> 5.2.2'
# Use Puma as the app server
gem 'puma', '~> 4.3' #'~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false

group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end

group :development do
  # Access an interactive console on exception pages or by calling 'console' anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '>= 3.0.5', '< 3.2'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
  # Adds support for Capybara system testing and selenium driver
  gem 'capybara', '>= 2.15'
  gem 'selenium-webdriver'
  # Easy installation and use of chromedriver to run system tests with Chrome
  gem 'chromedriver-helper'
end



### Added manually


## Interface

# Style Defaults - Makes everything look good by default.
# Structure - Makes it easier to do almost everything you're going to do with the interface.
gem 'bootstrap'

# Templates - Makes templates easier to read and write.
gem 'haml'

# Forms - Makes forms not almost completely infuriating.
gem 'simple_form'

# Email
gem 'mailgun-ruby', '~>1.1.6'


## Database

# Postgres, which is good for text searching
gem 'pg'

# Bulk database insert
gem 'bulk_insert'

# Search (postgres specific)
gem 'textacular', '~> 5.0'


## Storage

# AWS S3 - For storage and retrieval of a lot of data cheaply.
gem 'aws-sdk-rails'
gem 'aws-sdk-s3'


## Javascript

# ES6 - Turns ES6 it into something that will work with all browsers.
gem "babel-transpiler"

# DOM - Makes the DOM much easier to deal with.
gem "jquery-rails"

# Modules - Allows use of separate files as modules and other things I can't remember
gem 'webpacker'


## Payments
# Stripe: Makes payments possible.
gem 'stripe', :git => 'https://github.com/stripe/stripe-ruby'


## Accounts
# Third Party Sign-In
gem 'omniauth-google-oauth2'
gem 'omniauth-facebook'

And here are the few changes to it after upgrading:以下是升级后的一些变化:

# CHANGED: Upgraded from 2.6.6 to 3.1.2 to upgrade from heroku-18 to heroku-22.
ruby '3.1.2'

# CHANGED: Upgraded from 5.2.2 to 7.0.4 to upgrade from heroku-18 to heroku-22.
gem 'rails', '~> 7.0.4'

# removed these:
# gem 'mini_racer', platforms: :ruby
# gem 'bcrypt', '~> 3.1.7'
# gem 'mini_magick', '~> 4.8'
# gem 'capistrano-rails', group: development

...

gem 'bootstrap', '~> 5.2.1' # added version

Now I understand the following:现在我明白了以下几点:

  • importmaps will load up all the libraries that aren't mine from CDN's, and it will load them separately, and it seems like it will do this fine. importmaps 将从 CDN 加载所有不是我的库,它会单独加载它们,看起来它会做得很好。 I know this because I actually upgraded another website before this (but it had less of it's own javascript).我知道这一点是因为在此之前我实际上升级了另一个网站(但它自己的 javascript 较少)。

  • It will load my javascript somehow.它会以某种方式加载我的 javascript。 Most of it will probably work if I deploy it right now, but...如果我现在部署它,其中大部分可能会起作用,但是......

In development, something unexpected is happening, which is that every single little javascript file that I have for each of these classes is getting loaded separately, and it's annoyingly slow.在开发过程中,发生了一些意想不到的事情,即我为这些类中的每一个创建的每个小文件 javascript 都被单独加载,而且速度慢得令人讨厌。 I knew that it would load the "top-level" sort of entry point modules separately, but I thought that the modules imported by modules would end up wrapped up somehow.我知道它会单独加载“顶级”类型的入口点模块,但我认为模块导入的模块最终会以某种方式结束。 So now I have two questions:所以现在我有两个问题:

  1. Will all these files load separately on production?所有这些文件是否会在生产环境中单独加载? (Or will they somehow load separately but in a way that is fast?) On the page that I'm currently looking at there are over 30 of my javascript files loading. (或者它们会以某种方式单独加载但速度很快吗?)在我当前查看的页面上,我的 javascript 文件中有超过 30 个正在加载。

  2. (bonus-ish) Can I make the loading process better on development? (奖励)我可以在开发中改进加载过程吗? It's tolerable for now, and I really need to get the upgrade done for production more than I need to make development more pleasant to deal with, but it would be nice to make development load faster as well.现在是可以容忍的,我真的需要为生产完成升级,而不是让开发更容易处理,但如果能让开发加载速度更快也更好。 One nice thing about it is that if it loads fast on development I can at least be a little more confident that it will be ok on production.关于它的一个好处是,如果它在开发中加载速度很快,我至少可以更有信心一点,它在生产中是可以的。

  1. Will all these files load separately on production?所有这些文件是否会在生产环境中单独加载?

Yes, the idea is to use importmaps together with HTTP2 / HTTP3 / QUIC that have pipelining which removes penalty of creating connections for every file – multiple files is not an issue anymore for HTTP2+ – vice-versa – compiled big files and image sprites are bad now.是的,这个想法是将 importmaps 与具有流水线的HTTP2/HTTP3/QUIC一起使用,这消除了为每个文件创建连接的惩罚——多个文件不再是 HTTP2+ 的问题——反之亦然——编译的大文件和图像精灵是不好的现在。

  1. Can I make the loading process better on development?我可以在开发中改进加载过程吗?

In development files are loading through HTTP1.1, unfortunately, I don't know solutions for this for now.在开发中文件是通过 HTTP1.1 加载的,不幸的是,我现在不知道解决方案。 Although there is one cool thing if Stimulus is used – lazy controllers loading , you can make so that if you are viewing page of one of your game/toy/experiment then javascript files for that particular experiment are loaded but not for others.虽然如果使用 Stimulus 有一件很酷的事情——延迟控制器加载,你可以这样做,如果你正在查看你的游戏/玩具/实验之一的页面,那么加载该特定实验的 javascript 个文件,但不加载其他文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 Rails 7 中使用导入映射导入自定义 JavaScript 文件 - Can't import custom JavaScript files with importmaps in Rails 7 生产中的Rails:JavaScript文件未运行 - Rails in production: JavaScript files not run 我的JavaScript会怎样? 它无法正常工作 - What happen on my javascript? it's not working properly Rails应用程序未加载所有javascript文件生产 - Rails Application Not Loading All javascript files production Webpack 不会在生产中最小化我的 JavaScript 文件 - Webpack is not minimiziing my JavaScript files in production 在 Rails 开发中拆分 css 和 javascript 文件 - Split css and javascript files while in rails development 将 Rails 项目设置为使用 importmaps 时在 javascript/application.js 中获取 SyntaxError - Getting SyntaxError in javascript/application.js when setting rails project to use importmaps 在Rails应用程序中拆分Javascript或Coffeescript的最佳方法是什么? - What's the best way to split up my Javascript or Coffeescript in a Rails app? 我应该将我的 javascript 分成多个文件吗? - Should I split my javascript into multiple files? Rails 4 - javascript文件未在生产中加载但在本地运行良好。 (Heroku的) - Rails 4 - javascript files not loading in production but working well locally. (Heroku)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM