Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I Need to integrate Vue.js to some ASP.NET Core MVC views. I picked Vue.js over other alternatives because it seemed to be simpler: -"just add it via <script> tag" they said. No need to learn gulp/grunt/webpack/browserify/etc.

That turned out to be false. At my first attempt to handle dates I tried some extensions like vue-moment or vue-datetime-picker, taken from this official curated list of awesome things related to Vue.js but I hit a wall here. While the first is not mandatory using the require() js syntax (¿CommonJS?), the second one doesn't work without it. Other extensions happen to 'use babel' and imports/exports which is ECMAScript 6 that needs to be compiled. So, most vue.js libraries and toolings indeed needs a compiler, plus the require() syntax, and all that stuff from the node world?

How should I setup my project to work with ASP.NET Core MVC + Vue.js, in a way that I can develop many small vue-apps using vue plugins (that can require(stuff))?

share|improve this question
    
Turns out that vue-moment failing on non-compliled environnements is actually a bug. - stackoverflow.com/q/39601389/97471 – Gerardo Grignoli Sep 21 at 0:05
    
I tried ASP.NET Core Template Pack. But I don't get it. Is it just a vue-webpack-boilerplate hosted on kestrel ? – Gerardo Grignoli Sep 22 at 6:40
up vote 8 down vote accepted

I was totally lost when I asked the above question. I’ve spent a few days and I still don’t have a complete picture. What I am pretty sure is that 2016 is a hard year to learn JavaScript

I wanted to use Vue.JS because it’s simpler than the alternatives. Less ceremony, less boilerplates, less code. It's branded as the Progressive Framework... Right! but only to a point. Vue.Js does not solve the Javascript ecosystem fragmentation problem with build systems.

So, you will have to pick a side: Do you need a javascript Modules and a build system?

Option 1: Keep it simple: Avoid Js modules and build systems.

Reasons to follow this path:

  • You don’t have many days to learn A LOT of stuff. (configuring bundler, npm+package dependencies hell, ES6 stuff
  • You do not want to make a leading-edge Single-Page-Application. Embedding Vue.js inside a few html pages seems enough.
  • HTTP/2 is becoming mainstream, so bundlers like Webpack or Browserify will provide less benefits, at least on perfomance.
  • Eventually ES6 Modules will be supported directly in the browser, so we won’t need to build whatever latest-javascript into browser-compatible-javascript.

You will save many days by not spending time learning stuff that will probably be obsolete in a few years.

If you follow this path, a few recommendations:

  • Just add js libraries using the <script> tag.
  • Only use browser-ready Javascript libraries. Whatever code that uses require() or the UMD prefix (function (root, factory) { requires you setup modules (therefore they are not browser ready unless you setup CommonJS). Js files with import/export statements are written in ES6 so avoid them too.
  • Use Bower to download browser-ready libs. Avoid NPM (which implies having a module system in place).

Caveat: You will not be able to use advanced Vue.js features like Single File Components or vue-router, but that is ok. You will have to do a few things manually.

Option 2: Learn Javascript Modules + build systems.

Prepare a few days to learn and not code. I will only explain briefly how Webpack worked for me. Browserify also works, but I haven't tried it.

I recoomend you spend some time learning what JavaScript Modules are. Then learn to build them and pack them: I used WebPack. Its documentation is not great, so what worked for me was to follow its tutorial step by step.

At this point you may have heard that webpack ALSO has a builtin web-server with Hot-Module-Reloading. This is a web server for static files to be used only for development. It's benefit is that whenever you edit a JS module, the browser will auto-magically apply the change without refreshing. This is a very nice, but optional, feature. The problem: this builtin web-server competes with our web server (Kestrel). So, if you want to try this feature during development use the Webpack Asp.Net core Middleware provided at Microsoft’s JavaScriptServices repo . There you will find the WebApplicationBasic template that I am currently using. I dissected it, removed most of its parts and by trying to use it I slowly understood what each part was originally for.

When using webpack you will mostly use 3 workflows:

  • Build in development mode: Creates huge files, easy for debugging. Use it together with ‘watch-mode’ so whenever you modify a file, a new Webpack build is triggered.
  • Build in production mode: Creates small minified files. Usefull for ‘dotnet publish’.
  • Using Webpack-web-server+Hot-Module-Reload with Webpack Asp.Net core Middleware then your app will run Webpack in the background, build, and watch the source files for changes. The compilation output is not written to disk and only kept in memory and served via http. The JavaScriptServices middleware forwards requests from Kestrel to Webpack-web-server to make this work.

Whatever webpack config you go with, you have to include ‘vue-loader’ into your webpack config. You may inspire in Vue’s webpack-simple template.

I haven’t covered everything that I wanted to, but this topic is too extense and I need to go back to code. Please leave some feedback.

share|improve this answer
    
Great answer ! Do you know any simple template link with Vue js + Webpack + Laravel or VueJs + Browserify + Laravel instead of Asp.net core ? – Rasel Nov 22 at 13:26
    
I really feel with you. I've been told to make Vue work under our Core app and it went well until I had to add routing. After that, I was totally lost. Would you have time to shed some more light on how to make the routing work for Vue under DotNet.Core? We don't run NodeJs but I might be able to execute gulpfile.json manually every now and then. – Konrad Viltersten Nov 27 at 8:01
    
Hi @Rasel . I haven't used Laravel. Maybe you can search on LaraCasts.com that have a lot of info. laracasts.com/index/webpack – Gerardo Grignoli Nov 28 at 16:46
    
@KonradViltersten There is a great guide here: liquidlight.co.uk/blog/article/… – Gerardo Grignoli Nov 28 at 17:01

First, a disclaimer: I couldn't find anything that really fit what I needed, so I put together a solution from scratch, see the end

You ask about including Vue via the <script> tag (in that case the CDN build). However, you also mention using Babel and the ES6 modules feature. In that case I would recommend using Webpack for the client side app, which will compile your ES6 with Babel, allow you to use modules and components, and work with a template! You also get hot module reload (edit your source and see changes in the client app in real time!) and Webpack will bundle your SPA into a static HTML5 app.

The official Vue.js docs point to their own Webpack template.

So you can run the Webpack dev server and your ASP.NET Core app independently, if that suits your needs, but there's a better solution that makes development even more streamlined:

Microsoft's open source JavaScriptServices lets you execute Node.js from ASP.NET Core, and they have some Webpack middleware that integrates the Webpack dev server into your app during debug builds.

They provide official templates for Angular 2, and even a template labeled Vue.js, but the Vue template is just the official webpack template without any integration with .NET; this is just like the standalone server.

I couldn't find any templates that did this for Vue.js, so I put together a sample ASP.NET Core application that loads the Webpack dev middleware with a Vue.js Webpack app. When the .NET Core server is running in dev mode, you can edit the Vue source, and the changes will be reflected with quick incremental patches without needing to rebuild the entire application. In release mode, .NET Core will use the prebuilt Webpack output. You can find it on GitHub:

https://github.com/0xFireball/YetAnotherShrinker The repository linked above has a full application demo that uses NancyFx, axios, Vue.js, and Vue Material, and is a simple URL shortener. If you want steps for a more minimal setup that can easily be added to an existing app, check out this blog post of mine.

Obligatory disclaimer: I wrote that blog post.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.