How to use npm with ASP.NET Core

前端 未结 10 2005
孤街浪徒
孤街浪徒 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 16:11

    Please excuse the length of this post.

    This is a working example using ASP.NET Core version 2.5.

    Something of note is that the project.json is obsolete (see here) in favor of .csproj. An issue with .csproj. file is the large amount of features and the fact there is no central location for its documentation (see here).

    One more thing, this example is running ASP.NET core in a Docker Linux (alpine 3.9) container; so the paths will reflect that. It also uses gulp ^4.0. However, with some modification, it should work with older versions of ASP.NET Core, Gulp, NodeJS, and also without Docker.

    But here's an answer:

    gulpfile.js see the real working exmple here

    // ROOT and OUT_DIR are defined in the file above. The OUT_DIR value comes from .NET Core when ASP.net us built.
    const paths = {
        styles: {
            src: `${ROOT}/scss/**/*.scss`,
            dest: `${OUT_DIR}/css`
        },
        bootstrap: {
            src: [
                `${ROOT}/node_modules/bootstrap/dist/css/bootstrap.min.css`,
                `${ROOT}/node_modules/startbootstrap-creative/css/creative.min.css`
            ],
            dest: `${OUT_DIR}/css`
        },
        fonts: {// enter correct paths for font-awsome here.
            src: [
                `${ROOT}/node_modules/fontawesome/...`,
            ],
            dest: `${OUT_DIR}/fonts`
        },
        js: {
            src: `${ROOT}/js/**/*.js`,
            dest: `${OUT_DIR}/js`
        },
        vendorJs: {
            src: [
                `${ROOT}/node_modules/jquery/dist/jquery.min.js`
                `${ROOT}/node_modules/bootstrap/dist/js/bootstrap.min.js`
            ],
            dest: `${OUT_DIR}/js`
        }
    };
    
    // Copy files from node_modules folder to the OUT_DIR.
    let fonts = () => {
        return gulp
            .src(paths.styles.src)
            .pipe(gulp.dest(paths.styles.dest));
    };
    
    // This compiles all the vendor JS files into one, jsut remove the concat to keep them seperate.
    let vendorJs = () => {
        return gulp
            .src(paths.vendorJs.src)
            .pipe(concat('vendor.js'))
            .pipe(gulp.dest(paths.vendorJs.dest));
    }
    
    // Build vendorJs before my other files, then build all other files in parallel to save time.
    let build = gulp.series(vendorJs, gulp.parallel(js, styles, bootstrap));
    
    module.exports = {// Only add what we intend to use externally.
        default: build,
        watch
    };
    
    

    Add a Target in .csproj file. Notice we also added a Watch to watch and exclude if we take advantage of dotnet run watch command.

    app.csprod

      
        
      
    
      
        
        
      
    

    Now when dotnet run build is run it will also install and build node modules.

提交回复
热议问题