How to use npm with ASP.NET Core

前端 未结 10 2002
孤街浪徒
孤街浪徒 2020-11-29 15:05

I\'m using npm to manage the jQuery, Bootstrap, Font Awesome and similar client libraries I need for my ASP.NET Core application.

The approach that worked for me sta

相关标签:
10条回答
  • 2020-11-29 15:48

    Instead of trying to serve the node modules folder, you can also use Gulp to copy what you need to wwwroot.

    https://docs.asp.net/en/latest/client-side/using-gulp.html

    This might help too

    Visual Studio 2015 ASP.NET 5, Gulp task not copying files from node_modules

    0 讨论(0)
  • 2020-11-29 15:49

    Much simpler approach is to use OdeToCode.UseNodeModules Nuget package. I just tested it with .Net Core 3.0. All you need to do is add the package to the solution and reference it in the Configure method of the Startup class:

    app.UseNodeModules();
    

    I learned about it from the excellent Building a Web App with ASP.NET Core, MVC, Entity Framework Core, Bootstrap, and Angular Pluralsight course by Shawn Wildermuth.

    0 讨论(0)
  • 2020-11-29 15:52

    Shawn Wildermuth has a nice guide here: https://wildermuth.com/2017/11/19/ASP-NET-Core-2-0-and-the-End-of-Bower

    The article links to the gulpfile on GitHub where he's implemented the strategy in the article. You could just copy and paste most of the gulpfile contents into yours, but be sure to add the appropriate packages in package.json under devDependencies: gulp gulp-uglify gulp-concat rimraf merge-stream

    0 讨论(0)
  • 2020-11-29 15:54

    I've found a better way how to manage JS packages in my project with NPM Gulp/Grunt task runners. I don't like the idea to have a NPM with another layer of javascript library to handle the "automation", and my number one requirement is to simple run the npm update without any other worries about to if I need to run gulp stuff, if it successfully copied everything and vice versa.

    The NPM way:

    • The JS minifier is already bundled in the ASP.net core, look for bundleconfig.json so this is not an issue for me (not compiling something custom)
    • The good thing about NPM is that is have a good file structure so I can always find the pre-compiled/minified versions of the dependencies under the node_modules/module/dist
    • I'm using an NPM node_modules/.hooks/{eventname} script which is handling the copy/update/delete of the Project/wwwroot/lib/module/dist/.js files, you can find the documentation here https://docs.npmjs.com/misc/scripts (I'll update the script that I'm using to git once it'll be more polished) I don't need additional task runners (.js tools which I don't like) what keeps my project clean and simple.

    The python way:

    https://pypi.python.org/pyp... but in this case you need to maintain the sources manually

    0 讨论(0)
  • 2020-11-29 15:56

    I give you two answers. npm combined with other tools is powerful but requires some work to setup. If you just want to download some libraries, you might want to use Library Manager instead (released in Visual Studio 15.8).

    NPM (Advanced)

    First add package.json in the root of you project. Add the following content:

    {
      "version": "1.0.0",
      "name": "asp.net",
      "private": true,
      "devDependencies": {
        "gulp": "3.9.1",
        "del": "3.0.0"
      },
      "dependencies": {
        "jquery": "3.3.1",
        "jquery-validation": "1.17.0",
        "jquery-validation-unobtrusive": "3.2.10",
        "bootstrap": "3.3.7"
      }
    }
    

    This will make NPM download Bootstrap, JQuery and other libraries that is used in a new asp.net core project to a folder named node_modules. Next step is to copy the files to an appropriate place. To do this we will use gulp, which also was downloaded by NPM. Then add a new file in the root of you project named gulpfile.js. Add the following content:

    /// <binding AfterBuild='default' Clean='clean' />
    /*
    This file is the main entry point for defining Gulp tasks and using Gulp plugins.
    Click here to learn more. http://go.microsoft.com/fwlink/?LinkId=518007
    */
    
    var gulp = require('gulp');
    var del = require('del');
    
    var nodeRoot = './node_modules/';
    var targetPath = './wwwroot/lib/';
    
    gulp.task('clean', function () {
        return del([targetPath + '/**/*']);
    });
    
    gulp.task('default', function () {
        gulp.src(nodeRoot + "bootstrap/dist/js/*").pipe(gulp.dest(targetPath + "/bootstrap/dist/js"));
        gulp.src(nodeRoot + "bootstrap/dist/css/*").pipe(gulp.dest(targetPath + "/bootstrap/dist/css"));
        gulp.src(nodeRoot + "bootstrap/dist/fonts/*").pipe(gulp.dest(targetPath + "/bootstrap/dist/fonts"));
    
        gulp.src(nodeRoot + "jquery/dist/jquery.js").pipe(gulp.dest(targetPath + "/jquery/dist"));
        gulp.src(nodeRoot + "jquery/dist/jquery.min.js").pipe(gulp.dest(targetPath + "/jquery/dist"));
        gulp.src(nodeRoot + "jquery/dist/jquery.min.map").pipe(gulp.dest(targetPath + "/jquery/dist"));
    
        gulp.src(nodeRoot + "jquery-validation/dist/*.js").pipe(gulp.dest(targetPath + "/jquery-validation/dist"));
    
        gulp.src(nodeRoot + "jquery-validation-unobtrusive/dist/*.js").pipe(gulp.dest(targetPath + "/jquery-validation-unobtrusive"));
    });
    

    This file contains a JavaScript code that is executed when the project is build and cleaned. It’s will copy all necessary files to lib2 (not lib – you can easily change this). I have used the same structure as in a new project, but it’s easy to change files to a different location. If you move the files, make sure you also update _Layout.cshtml. Note that all files in the lib2-directory will be removed when the project is cleaned.

    If you right click on gulpfile.js, you can select Task Runner Explorer. From here you can run gulp manually to copy or clean files.

    Gulp could also be useful for other tasks like minify JavaScript and CSS-files:

    https://docs.microsoft.com/en-us/aspnet/core/client-side/using-gulp?view=aspnetcore-2.1

    Library Manager (Simple)

    Right click on you project and select Manage client side-libraries. The file libman.json is now open. In this file you specify which library and files to use and where they should be stored locally. Really simple! The following file copies the default libraries that is used when creating a new ASP.NET Core 2.1 project:

    {
      "version": "1.0",
      "defaultProvider": "cdnjs",
      "libraries": [
        {
          "library": "jquery@3.3.1",
          "files": [ "jquery.js", "jquery.min.map", "jquery.min.js" ],
          "destination": "wwwroot/lib/jquery/dist/"
        },
        {
          "library": "jquery-validate@1.17.0",
          "files": [ "additional-methods.js", "additional-methods.min.js", "jquery.validate.js", "jquery.validate.min.js" ],
          "destination": "wwwroot/lib/jquery-validation/dist/"
        },
        {
          "library": "jquery-validation-unobtrusive@3.2.10",
          "files": [ "jquery.validate.unobtrusive.js", "jquery.validate.unobtrusive.min.js" ],
          "destination": "wwwroot/lib/jquery-validation-unobtrusive/"
        },
        {
          "library": "twitter-bootstrap@3.3.7",
          "files": [
            "css/bootstrap.css",
            "css/bootstrap.css.map",
            "css/bootstrap.min.css",
            "css/bootstrap.min.css.map",
            "css/bootstrap-theme.css",
            "css/bootstrap-theme.css.map",
            "css/bootstrap-theme.min.css",
            "css/bootstrap-theme.min.css.map",
            "fonts/glyphicons-halflings-regular.eot",
            "fonts/glyphicons-halflings-regular.svg",
            "fonts/glyphicons-halflings-regular.ttf",
            "fonts/glyphicons-halflings-regular.woff",
            "fonts/glyphicons-halflings-regular.woff2",
            "js/bootstrap.js",
            "js/bootstrap.min.js",
            "js/npm.js"
          ],
          "destination": "wwwroot/lib/bootstrap/dist"
        },
        {
          "library": "list.js@1.5.0",
          "files": [ "list.js", "list.min.js" ],
          "destination": "wwwroot/lib/listjs"
        }
      ]
    }
    

    If you move the files, make sure you also update _Layout.cshtml.

    0 讨论(0)
  • 2020-11-29 15:57

    Install the Bundler and Minifier into Visual Studio Extensions

    Then you create a bundleconfig.json and enter the following like :

    // Configure bundling and minification for the project.
    // More info at https://go.microsoft.com/fwlink/?LinkId=808241
    [
     {
        "outputFileName": "wwwroot/js/jquery.min.js",
        "inputFiles": [
          "node_modules/jquery/dist/jquery.js"
        ],
        // Optionally specify minification options
        "minify": {
          "enabled": true,
          "renameLocals": false
        },
        // Optionally generate .map file
        "sourceMap": false
      }
    ]
    

    So the bundler and minifier (gulp based) has access to the source files (which should be excluded from Visual Studio and also excluded from GIT) and puts them into the wwwroot as specified

    only side effect every time you save it will run this (but you can set it to run it manually)

    0 讨论(0)
提交回复
热议问题