javascript 的捆绑器,或如何对外部 javascript 文件进行源代码控制

发布于 2024-12-20 02:42:22 字数 1046 浏览 4 评论 0原文

我正在将为客户端制作的现有 Rails 3.1 应用程序转换为 Backbone.js 应用程序,并将 Rails 应用程序仅作为后端服务器扩展。这只是我的个人项目,旨在了解有关 Backbone.js 的更多信息。

在设置 Backbone.js 时(使用 Backbone-on-Rails),我注意到我有一些依赖项(例如 backbone-forms),这些依赖项来自外部来源并且经常出现已更新。

我已经习惯使用 Bundler 来管理我的 Ruby gem,但我还没有找到任何类似的 JavaScript 文件。我想知道是否有任何方法可以对 Javascript(可能还有 css)文件执行相同的操作。

基本上我可以看到解决这个问题的三种可能性:

  • 简单地写下每个 JS 文件的所有源代码,并时不时地检查这些源代码以查看发生了什么变化。

  • 使用某种现有的“Bundler for Javascript”类型的工具,我一直在寻找类似的东西,但尚未找到任何东西(好的)。

  • 由于这些 JS 文件大部分都来自 Git,因此请使用 Git 直接获取文件,并时不时使用 checkout 获取最新版本。

我更喜欢最后一个选项,但希望从其他走这条路或更喜欢其他方式来解决这个问题的人那里得到更多的意见(或者这甚至是一个问题吗?)。

我认为 Git 方式似乎很简单,但我还不太确定如何使其与 Rails 3.1 和 Sprockets 完美配合。我想我会尝试使用Git签出单个文件并拥有它被克隆到链轮可以访问的目录中,但我还没有尝试过。

有什么想法吗?

I am in the process of converting an existing Rails 3.1 app I made for a client into a Backbone.js app with the Rails app only as a backend server extension. This is only a personal project of mine, to learn more about Backbone.js.

While setting up Backbone.js (using Backbone-on-Rails), I noticed I have some dependencies (like backbone-forms) that come from external sources and are frequently updated.

I've grown accustomed to using Bundler to manage my Ruby gems, but I haven't found anything similar for JavaScript files. I'm wondering if there is any way to do the same for Javascript (and possibly css) files.

Basically I can see three possibilities to solve this issue:

  • Simply write down all the sources for each JS file and check these sources from time to time to see what has changed.

  • Use some kind of existing "Bundler for Javascript" type of tool, I've been looking for something like this but have yet to find anything (good).

  • Since most of these JS files will be coming from Git anyway, use Git to get the files directly and use checkout to get the latest version from time to time.

I prefer the last option, but was hoping on some more input from other people who have gone this route or preferred some other way to tackle this issue (or is this even an issue?).

I figure the Git way seems easy, but I am not quite sure yet how I could make this work nicely with Rails 3.1 and Sprockets. I guess I'd try to checkout a single file using Git and have it be cloned in a directory that is accessible to Sprockets, but I haven't tried this yet.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蘸点软妹酱 2024-12-27 02:42:22

您不会在替代方案中提及它,但理想情况下您应该使用 Maven 之类的东西来管理您的依赖项。不幸的是,没有 JavaScript 文件的公共存储库。此讨论列出了一些可能对您有帮助的其他选项:Maven 存储库上的 JQuery 可用性< /a>

You don't mention it in your alternatives, but ideally you should use something like Maven to manage your dependencies. Unfortunately, there are no public repositories for javascript files. This discussion lists some other options which might be of help to you: JQuery Availability on Maven Repositories

叹倦 2024-12-27 02:42:22

目前,我决定使用 Git 解决方案与一些 guard-shell 魔法相结合。

我遵循的步骤:

  • 在本地驱动器上的某个位置创建一个 dependencies 目录
  • 使用您想要在应用程序中使用的 javascript(或 css)文件克隆存储库
  • 设置自定义 guard-shell 命令来执行以下操作:
group 'dependencies' do
  guard 'shell' do
    dependencies = '~/path/to/dependencies/'

    watch(%r{backbone-forms/src/(backbone\-forms\.js)}) {|m| `cp #{dependencies + m[0]} vendor/assets/javascripts/#{m[1]}` }
  end
end
  • Guardfile 放在应用程序目录的根目录中

这需要一些时间来设置,但之后,当您运行 Guard 时,并将更改拉入您的依赖项,所需的文件会自动复制到您的应用程序目录,然后它就成为存储库的一部分。

它似乎工作得很好,您需要为要包含在资产管道中的每个新文件做一些工作,但所需要做的就是将存储库克隆到您的 dependency 目录中并添加一行添加到您的 Guardfile,例如 backbone-form css

watch(%r{backbone-forms/src/(backbone\-forms\.css)}) {|m| `cp #{dependencies + m[0]} vendor/assets/stylesheets/#{m[1]}` }

此外,我将此 Guard 添加到 group 的原因是因为我将依赖项保留在外部 strong> 主应用目录,表示正常守护不检查我的依赖目录。为了完成这项工作,我使用 bundle execguard -g main 启动我的主 Guard 进程,并使用 bundle execguard -w ~/path/to/dependencies -g dependency在新的终端窗口/选项卡中指定 -w(atchdir)

For now I've settled on using the Git solution combined with some guard-shell magic.

The steps I follow:

  • Create a dependencies directory somewhere on your local drive
  • Clone the repositories with javascript (or css) files you want to use in the app
  • Set up a custom guard-shell command to do the following:
group 'dependencies' do
  guard 'shell' do
    dependencies = '~/path/to/dependencies/'

    watch(%r{backbone-forms/src/(backbone\-forms\.js)}) {|m| `cp #{dependencies + m[0]} vendor/assets/javascripts/#{m[1]}` }
  end
end
  • Place the Guardfile at the root of the app directory

It takes some time to set things up, but after that, when you have the Guard running, and you pull changes into your dependencies, the required files are automatically copied to your application directory, which are then part of your repository.

It seems to work great, you need to do some work for each new file you want to include in the asset pipeline, but all that is required is cloning the repository in your dependencies directory and adding a single line to your Guardfile, for example for the backbone-form css:

watch(%r{backbone-forms/src/(backbone\-forms\.css)}) {|m| `cp #{dependencies + m[0]} vendor/assets/stylesheets/#{m[1]}` }

Also, the reason I added this Guard to a group is because I keep my dependencies outside the main application directory, which means guard normally doesn't check my dependencies directory. To make this work, I start up my main Guard processes using bundle exec guard -g main and use bundle exec guard -w ~/path/to/dependencies -g dependencies in a new terminal window/tab to specify the -w(atchdir).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文