All I would like to do is to be able to modularise my javascript/jquery/ajax client-side code, to use some existing library code which I have written and now want to use in the scripts section of my pages - using something simple like in Node "require" or ES6 "import" syntax.
[ASP.Net MVC 5, Visual Studio 2015]
In webpage:
@section scripts
{
@Scripts.Render("~/Scripts/Views/Items/Index.js")
}
In Index.js:
$(function ()
{
var myLib = require("../Shared/my-lib");
OR
$(function ()
{
import * from "../Shared/my-lib";
In my-lib.js:
export var myFunction1 = myFunction1;
export var myFunction2 = myFunction2;
I am an experienced .NETter but a bit of a js noob, so if I am missing anything utterly obvious, please tell me kindly! I just want to keep it DRY, you know?
I tried just doing it as above but it throws syntax errors about either "import" or "require", so I figure it doesn't just work out of the box.
I've looked up Browserify and RequireJS - but that seems like a TON of fiddly setup (yeah i am fine with npm but it is all the settings files, build files, specific way of writing the library code ... etc. etc.) - do I really have to do all that just to "include"/ import some code?
Should I write my library as OO javascript? Would that help?
At the moment I am just doing:
<script src="@Url.Content("~/Scripts/Views/Shared/my-lib.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/Views/Item/Index.js")" type="text/javascript"></script>
which doesn't strike me as very elegant...