Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. Ideally I would like to find a project type that would, for my JavaScript code, behave as if it was a C# class library, i.e.:

  • It would "compile" (minify, check by Closure, ...) the JavaScript code into some output **.js* file
  • This output could be "referenced" by other projects, e.g. by an ASP.NET MVC project
  • I could specify a "build order" of my projects (standard VS feature)

Is this possible with VS 2010 / 11 or do I need to write some BAT / PowerShell files and script it myself?

Similar but slightly different question: Visual Studio Project Template for JavaScript/VBScript?

like image 930
Borek Bernard Avatar asked Mar 02 '12 21:03

Borek Bernard


People also ask

How do I create a JavaScript project in Visual Studio?

Add a new project file With your project open in Visual Studio, right-click on a folder or your project node in Solution Explorer (right pane), and choose Add > New Item. In the New File dialog box, under the General category, choose the file type that you want to add, such as JavaScript File, and then choose Open.

Can JavaScript be used in Visual Studio?

Visual Studio 2022 provides rich support for JavaScript development, both using JavaScript directly, and also using the TypeScript programming language, which was developed to provide a more productive and enjoyable JavaScript development experience, especially when developing projects at scale.

Can I use JavaScript in a TypeScript project?

It is possible to have mixed TypeScript and JavaScript projects. To enable JavaScript inside a TypeScript project, you can set the allowJs property to true in the tsconfig. json .


2 Answers

Based on my experience developing Asp.Net MVC, Visual Studio has limited support for 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. Put your JavaScript files inside new project.

  2. Create a post build step on this project to minimize your JavaScript using an external tool. I use 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

  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. 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. 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. 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.

like image 63
Bojin Li Avatar answered Sep 20 '22 17:09

Bojin Li


Good question (+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.

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.

like image 45
Saulius Avatar answered Sep 19 '22 17:09

Saulius