如何在开发和上线过程中使用 JavaScript
我从事前端开发工作,希望找到一种在(非压缩和多个文件)开发环境和(压缩和组合文件)实时环境之间使用 javaScript 的解决方案。
我找到了一种 CSS 解决方案,这意味着我只需要在导入中包含一个全局 CSS 文件,然后在部署到实时环境时组合并压缩这些导入。这意味着我们不必切换将引用添加到开发和实时的头部。
关于 JavaScipt 的类似解决方案有什么想法吗?
谢谢
戴夫
I work on front end development and am looking to find a solution for working with javaScript between (non compressed and multiple files) development environment and (compressed and combined files) live environment.
I have found a solution with CSS which means that I only need to include one global CSS file with imports, then we combine and compress those imports when deploying to a live environment. This means that we don't have to toggle adding references in to the head for dev and live.
Any ideas on a similar solution for JavaScipt?
Thanks
Dave
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您使用 jQuery ,那么从 Javascript 中包含外部 Javascript 文件非常容易,这基本上就是您所描述的那样与CSS。
阅读 jQuery getScript()
If you are using jQuery it's really easy to include external Javascript files from within Javascript which is basically what you described you did with CSS.
Read up on jQuery getScript()
您可以使用 Charles Web 调试代理。或者类似的东西。
Charles 允许提供任何本地文件而不是任何 url。因此,您可以为浏览器提供本地 JS 文件,而不是实时 JS。因此,您将能够测试 JS 或 CSS 更改,而无需向用户显示它们。
You can use Charles Web debugging proxy. Or smth similar.
Charles allows to give any local file instead of any url. So you can give your browser your local JS file instead of live JS. Thus you will be able to test JS or CSS changes without showing them to your users.
我使用 ESC 将所有独立的 JavaScript 合并并压缩到一个中央 JavaScript,并拥有它作为“构建后”任务运行。
I use ESC to merge and compress all the independant JavaScripts to a central one, and have it run as a 'post build' task.
对于 Visual Studio,我编写了一个小型控制台应用程序(如某人提到的 ESC),用作构建后事件。它很简单,但可以通过以下方式自动执行您所描述的工作:
.js
文件中然后在站点项目中,从资源加载该文件,并在类中执行切换
您还可以启用对 JS 文件进行 GZIP 压缩,加载时间更快。如果您不使用 Microsoft 开发环境,那么我将删除它。
For Visual Studio I wrote a small console application I wrote (like ESC as someone mentioned) that is used as a post-build event. It's simple but automates the job you're describing by:
.js
fileThen in the site project, the file is loaded from a resource, and a toggle is performed in a class
You could also enable GZIP compression on the JS files for even faster load times. If you're not using the Microsoft dev environment then I'll delete this.
感谢您的所有回复。我提出了一个使用您的一些想法的解决方案。
我有一个全局 js 文件,其中包含要包含的文件列表,在开发期间运行时仅将脚本链接写入页面。
然后,部署过程中包含一个脚本,该脚本解析全局 js 文件,查找它链接在一起的文件,将它们组合并压缩为一个全局 js 文件。
这意味着我在此过程中不需要任何服务器端代码,这使得自由前端开发团队的维护变得更容易。
当最后一堆代码准备好后,我会将其发布在我的博客上。
Thanks for all your responses. I have come up with a solution which uses some of your ideas.
i have a global js file which has a list of files to include and when run during dev just writes the script links to the page.
Then included in the deployment process is a script which parses the global js file, looks up which files it is linking together, combines and compresses them in to one global js file.
This means that I don't need any server side code during the process which makes things easier to maintain across a team of freelance front end devs.
i'll post the final bunch of code when it's ready on my blog.
我不知道您的开发环境是什么样子,但您可以将所有脚本标签放入一个文件中进行开发,并使用另一个文件进行生产,其中包含单个文件的脚本标签。例如:
development_js.extension
和product_js.extension
。然后只需使用服务器端包含或某些构建工具将正确的文件合并到 HTML 文件中即可。
I don't know how your dev environment looks like but you could put all the script tags into one file for development and have another for production that has the script tag for your one single file. For example:
development_js.extension
andproduction_js.extension
.Then it's just a matter of either using server-side include or some build tool to merge the correct file into your HTML file.