简体   繁体   English

Require JS 优化/连接网站文件的部署策略

[英]Deployment Strategy for Require JS Optimized/Concatenated Website Files

My question is partly technical and partly about deployment strategies and workflow.我的问题部分是技术性的,部分是关于部署策略和工作流程的。 I built a project using Require JS.我使用 Require JS 构建了一个项目。 It includes a number of distinct js modules, and is built upon Kirby CMS.它包括许多不同的 js 模块,并建立在 Kirby CMS 之上。 The directory structure of the project is something like this:项目的目录结构是这样的:

project    
  assets    
    styles
      style.css
    js   
      scripts   
        script1.js
        script2.js
        script3.js  
      vendor   
      app.js
    images    
    fonts    
  content
  ... 
  kirby folders
  ....

The file app.js is called in the footer of my site's page like so:文件 app.js 在我网站页面的页脚中被调用,如下所示:

<script data-main="/assets/js/app" src="/assets/js/vendor/require.js"></script>

It configures RequireJS by calling the requirejs.config() function and then calls the main script file that loads everything else using RequireJS's requirejs() function.它通过调用requirejs.config()函数来配置 RequireJS,然后调用使用 RequireJS 的requirejs()函数加载其他所有内容的主脚本文件。

I've used RequireJS' s optimization tool to compile the project in such a way that the optimized files are all dumpted into a directory called dist (a name I just picked up from this tutorial ).我使用RequireJS 的优化工具来编译项目,将优化后的文件全部转储到一个名为 dist(我刚刚从本教程中获取的名称)的目录中。 So in the end dist contains a replication of every directory and file under assets, only optimized, and the file app.js is a concatenated and optimized version of all the js modules that I have in the project.所以最后dist包含了assets下每个目录和文件的副本,只是优化了,文件app.js是我项目中所有js模块的串联优化版本。 So far so good.到现在为止还挺好。

What I am unsure about, however, is how I'm the supposed to make use of this new secondary version of all the code.然而,我不确定的是我应该如何使用所有代码的这个新的二级版本。 What for instance if I want to deploy a version of the site to the production server without all the source js files?例如,如果我想在没有所有源 js 文件的情况下将站点的某个版本部署到生产服务器,该怎么办? Each time I deploy the site, I would need to go through my code and in every place that I referred to files under the assets directory, I would need to replace that with dist .每次我部署站点时,我都需要检查我的代码,并且在我引用assets目录下文件的每个地方,我都需要用dist替换它。 I deploy using git and beanstalk .我使用 git 和beanstalk 进行部署。 One way to do this would be to manage different branches for staging, production, and development, in which the production and perhaps staging branches have references to the files under dist, but this seems awkward.实现这一点的一种方法是管理不同的分支,用于暂存、生产和开发,其中生产和可能暂存分支引用 dist 下的文件,但这似乎很尴尬。

So my question is given this kind of optimization set up, which if you look at the tutorial linked above is one way to do this, how then do you manage the switch to the optmized version of everything seemlessly, without having to go back into your code and change everything up?所以我的问题是这种优化设置,如果你看上面链接的教程是一种方法,那么你如何无缝地管理所有东西的优化版本的切换,而不必回到你的编码并改变一切? Is there some key part of the process that I'm missing here?我在这里遗漏了该过程的某些关键部分吗?

Each time I deploy the site, I would need to go through my code and in every place that I referred to files under the assets directory, I would need to replace that with dist .每次我部署站点时,我都需要检查我的代码,并且在我引用assets目录下文件的每个地方,我都需要用dist替换它。

I've looked at the tutorial you've linked to and do not see how it is true for the tutorial.我已经查看了您链接到的教程,但不知道该教程是如何正确的。 The tutorial does not use absolute paths, so should be deployable from dist just as well as from the directory that contains the pre-optimization sources.本教程不使用绝对路径,因此应该可以从dist以及包含预优化源的目录中进行部署。 If you cannot do this for your application, that's because you've done something different from the tutorial.如果您不能为您的应用程序执行此操作,那是因为您做了与教程不同的操作。 Your script tag, for instance, shows absolute paths.例如,您的script标签显示绝对路径。

So the solution is to design your application to avoid absolute paths.所以解决方案是设计你的应用程序以避免绝对路径。 This way, you won't have to change paths when you deploy from dist .这样,从dist部署时就不必更改路径。 I'm using this very method to deploy optimized and non-optimized versions of one of my apps.我正在使用这种方法来部署我的一个应用程序的优化和非优化版本。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM