简体   繁体   English

Visual Studio的JavaScript项目类型?

[英]JavaScript project type for Visual Studio?

I have a tasks that involves creating a JavaScript library that will then be used by multiple projects in a Visual Studio solution. 我有一个任务涉及创建一个JavaScript库,然后由Visual Studio解决方案中的多个项目使用。 Ideally I would like to find a project type that would, for my JavaScript code, behave as if it was a C# class library, ie: 理想情况下,我想找到一个项目类型,对于我的JavaScript代码,它的行为就好像它是一个C#类库,即:

  • It would "compile" (minify, check by Closure, ...) the JavaScript code into some output **.js* file 它将“编译”(缩小,通过Closure检查,......)将JavaScript代码转换为某些输出** .js *文件
  • This output could be "referenced" by other projects, eg by an ASP.NET MVC project 此输出可以由其他项目“引用”,例如,通过ASP.NET MVC项目
  • I could specify a "build order" of my projects (standard VS feature) 我可以指定我的项目的“构建顺序”(标准VS功能)

Is this possible with VS 2010 / 11 or do I need to write some BAT / PowerShell files and script it myself? 这可能与VS 2010/11一样,还是我需要编写一些BAT / PowerShell文件并自行编写脚本?

Similar but slightly different question: Visual Studio Project Template for JavaScript/VBScript? 类似但略有不同的问题: 用于JavaScript / VBScript的Visual Studio项目模板?

Based on my experience developing Asp.Net MVC, Visual Studio has limited support for JavaScript. 根据我开发Asp.Net MVC的经验,Visual Studio对JavaScript的支持有限。 I suppose there is something you can do to mimic the behavior you want: 我想你可以做些什么来模仿你想要的行为:

  1. Create a project to store your JavaScript files, perhaps a Class Library project it doesn't really matter, as long as it supports Build Events. 创建一个项目来存储你的JavaScript文件,也许是一个它并不重要的类库项目,只要它支持构建事件。 Put your JavaScript files inside new project. 将您的JavaScript文件放在新项目中。

  2. Create a post build step on this project to minimize your JavaScript using an external tool. 在此项目上创建一个post build步骤,以使用外部工具最小化JavaScript。 I use YUI Compressor . 我使用YUI Compressor Your post build step should contain lines similar to the following: 您的帖子构建步骤应包含类似于以下内容的行:

    java -jar $(ProjectDir)Scripts\\yuicompressor-2.4.7.jar $(SolutionDir)Scripts\\yourJsFile.js -o $(SolutionDir)Scripts\\yourJsFile.min.js --charset utf-8 java -jar $(ProjectDir)Scripts \\ yuicompressor-2.4.7.jar $(SolutionDir)Scripts \\ yourJsFile.js -o $(SolutionDir)Scripts \\ yourJsFile.min.js --charset utf-8

  3. Include this new project in your solution. 在您的解决方案中包含此新项目。 Then for your Asp.Net projects, set up your Active Server Pages such that they reference the JavaScript files, I am using Razor syntax as an example. 然后,对于您的Asp.Net项目,设置您的Active Server Pages以便它们引用JavaScript文件,我使用Razor语法作为示例。 It might be tricky to specific the correct path though: 具体而言,确定正确的路径可能很棘手:

 @if (@Model.IsDebug) { <script src="@Url.Content("~/Scripts/yourJsFile.js")" type="text/javascript"> </script> } else { <script src="@Url.Content("~/Scripts/yourJsFile.min.js")" type="text/javascript"></script> } 

Again it might be tricky to ensure that you can accurately reference the JavaScript files from your Asp.Net project. 再次确保您可以准确地引用Asp.Net项目中的JavaScript文件可能很棘手。 But I'm sure there is a way to do it. 但我确信有办法做到这一点。 Perhaps you can have your post build step copy the JavaScript files to some common location. 也许您可以让您的帖子构建步骤将JavaScript文件复制到某个公共位置。 If you do this you will also want to mark the post build event on your JavaScript project as "Always Run", so the JavaScript files are always copied. 如果这样做,您还需要将JavaScript项目上的post build事件标记为“Always Run”,因此始终会复制JavaScript文件。

Good question (+1). 好问题(+1)。 Me decision was to put all javascript general files to separate javascript project and use linked files to needed js files in web/mvc project. 我决定将所有javascript通用文件放在单独的javascript项目中,并使用链接文件到web / mvc项目中需要的js文件。

This way you get possibility to use subversioning control from javascript project side and compacting and merging js files from separate general projects side using all available tools. 通过这种方式,您可以使用所有可用工具从javascript项目端使用subversioning控件,并从单独的常规项目侧压缩和合并js文件。

I am really curious about this! 我真的好奇这个! Please @ comment to me if anyone comes up with other/better ideas. 如果有人提出其他/更好的想法,请@评论给我。

Option 1: ASP.NET Web project with Web Essentials 选项1:使用Web Essentials的ASP.NET Web项目

If you just want a front end javascript project, this is an easy option. 如果您只想要一个前端javascript项目,这是一个简单的选择。 Web Essentials is easy to use and install in Visual Studio. Web Essentials易于使用并在Visual Studio中安装。 You can use this for easy minification. 您可以使用它来轻松缩小。 Then you can use Qunit for testing. 然后你可以使用Qunit进行测试。 This is a pretty easy and light weight entry point into javascript client-side development. 这是javascript客户端开发的一个非常简单和轻量级的切入点。

Option 2: Node.js Tools for Visual Studio 选项2:Visual Studio的Node.js工具

Use Node.js Tools for Visual Studio . 使用Visual Studio的Node.js工具 It will give you project templates for a lot of these good things. 它将为您提供许多这些好东西的项目模板。 You may not end up using node.js especially if you are just creating a client side js library, but having node.js is helpful for testing and you will want/need npm to install all the other good things mentioned in my original answer (Option 3). 你最终可能不会使用node.js,特别是如果你只是创建一个客户端js库,但是让node.js有助于测试,你需要/需要npm来安装我原来答案中提到的所有其他好东西(选项3)。

There is a lot of setup involved with this one. 这个涉及很多设置。 It may be a barrier to entry for some .NET developers. 对于某些.NET开发人员来说,这可能是一个障碍。

Option 3: Web Site Project 选项3:网站项目

Here is what I have done in the past: 这是我过去所做的:

在此输入图像描述

I actually created this project in Eclipse! 我实际上是在Eclipse中创建了这个项目 (Don't hate me). (不要恨我)。 Later I created a Visual Studio solution with a Web Site Project . 后来我用Web站点项目创建了一个Visual Studio解决方案。 I installed node.js which is certainly not required, but I was using it as a lightweight web server. 我安装了node.js,这当然不是必需的,但我使用它作为轻量级的Web服务器。

Then you can use Nuget or Bower to install other things like: 然后你可以使用Nuget或Bower来安装其他东西,比如:

  1. require.js for module management require.js用于模块管理
  2. jasmine for unit testing 用于单元测试的茉莉花
  3. grunt or gulp for builds (minification) 构建的咕噜声或咕噜声 (缩小)
  4. You could install jslint or jshint for code correction. 您可以安装jslint或jshint进行代码更正。

Not all of these things are required. 并非所有这些都是必需的。 I think Bower is integrated into Visual Studio 2015. Some of these will require command line builds, but there are very few commands to run once you set it up. 我认为Bower已集成到Visual Studio 2015中。其中一些需要命令行构建,但是一旦设置它就会运行很少的命令。

If you want access to pre-build/pos-build (not really needed with task runner explorer), create an empty ASP.net Core project (it will hide the node_modules folder and files will automatically be put into project (just copying them, it is not required to add them manually). BUT, this generates an obj and bin folder, which can be annoying. One good thing here is that if you build your project in the wwwfolder, you can use the publish command from Visual Studio. 如果你想访问预构建/ pos-build(不需要使用任务运行器资源管理器),创建一个空的ASP.net核心项目(它将隐藏node_modules文件夹,文件将自动放入项目中(只需复制它们,它不需要手动添加它们。但是,这会生成一个obj和bin文件夹,这可能很烦人。这里有一件好事,如果你在www文件夹中构建你的项目,你可以使用Visual Studio中的publish命令。

If you don't care about none of the above, a shared C# project don't generate output, so it is a good template to make a javascript project (I use it for all my vue spa projects). 如果您不关心上述任何一项,则共享C#项目不会生成输出,因此它是制作javascript项目的好模板(我将其用于所有我的vue spa项目)。 The downside here is that file nesting won't work =\\ 这里的缺点是文件嵌套不起作用= \\

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

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