Angular and Webpack integration in ASP.NET MVC application

I am looking for a record using steps that I can follow to add Angular to an existing MVC application that lives in my solution area. I found a record that shows me how to add Angular using the gulp.js file, which pulled out the necessary node_modules and translates my ts files into js files. I would like to do the same with webpack.

enter image description here

Currently my .json package looks like this.

{
    "version": "1.0.0",
    "name": "aspnet",
    "private": true,
    "scripts": {},
    "dependencies": {
        "@angular/animations": "4.3.5",
        "@angular/common": "4.3.5",
        "@angular/compiler": "4.3.5",
        "@angular/compiler-cli": "4.3.5",
        "@angular/core": "4.3.5",
        "@angular/forms": "4.3.5",
        "@angular/http": "4.3.5",
        "@angular/platform-browser": "4.3.5",
        "@angular/platform-browser-dynamic": "4.3.5",
        "@angular/platform-server": "4.3.5",
        "@angular/router": "4.3.5",
        "@angular/upgrade": "4.3.5",
        "angular-in-memory-web-api": "0.3.2",
        "bootstrap": "3.3.7",
        "core-js": "2.5.0",
        "ie-shim": "0.1.0",
        "rxjs": "5.4.3",
        "zone.js": "0.8.16",
        "systemjs": "^0.20.18"
    },
    "devDependencies": {
        "gulp": "^3.9.1",
        "gulp-clean": "^0.3.2",
        "gulp-concat": "^2.6.1",
        "gulp-tsc": "~1.3.2",
        "gulp-typescript": "^3.2.2",
        "path": "^0.12.7",
        "typescript": "^2.4.2"
    }
}

This is the entry I followed that uses gulp and it works.

But I would prefer to use webpack instead of the gulp task.

+4
source share
1 answer

, , webpack ASP.NET MVC. , , . .

Github

module 1

1.

: . mvc5- angular -webpack,

mvc5-angular-webpack/
โ”œโ”€โ”€ Server/
โ”‚   โ”œโ”€โ”€ WebApplication/
โ”‚   โ”‚     โ”œโ”€โ”€ Controllers/
โ”‚   โ”‚     โ”œโ”€โ”€ Scripts/
โ”‚   โ”‚     โ”œโ”€โ”€ Web.config
โ”‚   โ”‚     โ”œโ”€โ”€ Many more folder and file...
โ”‚   โ”‚        
โ”‚   โ””โ”€โ”€ Web-Core.sln
โ”‚   
โ”œโ”€โ”€ Client/
    โ”œโ”€โ”€ modules
    โ”‚     โ”œโ”€โ”€ angularModule-1/
    โ”‚     โ”‚      โ”œโ”€โ”€ main.ts
    โ”‚     โ”‚      โ”œโ”€โ”€ app.modules.ts
    โ”‚     โ”‚      โ”œโ”€โ”€ app.component.ts
    โ”‚     โ”‚      โ”œโ”€โ”€ Many more file...
    โ”‚     โ”‚
    โ”‚     โ”œโ”€โ”€ angularModule-2/
    โ”‚     โ”‚      โ”œโ”€โ”€ main.ts
    โ”‚     โ”‚      โ”œโ”€โ”€ app.modules.ts
    โ”‚     โ”‚      โ”œโ”€โ”€ app.component.ts
    โ”‚     โ”‚      โ”œโ”€โ”€ Many more file...
    โ”‚     โ”œโ”€โ”€ polyfill.ts
    โ”‚     โ”œโ”€โ”€ vendor.ts
    โ”‚
    โ””โ”€โ”€ build.bat
    โ””โ”€โ”€ npm-shrinkwrap.json
    โ””โ”€โ”€ package.json
    โ””โ”€โ”€ tsconfig.json
    โ””โ”€โ”€ tslint.json
    โ””โ”€โ”€ webpack.config.js
  • MVC Web-Core.sln , #.
  • . , build.bat. . , .

- , Razor. , AngularJS Angular.

2. webpack

, typescript npm. , webpack.config.js

const webpack = require('webpack')
const path = require('path')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const entryPath = path.resolve(__dirname, 'modules')
const corePath = path.resolve(__dirname, '../Server/WebApplication/Scripts/ng2')
const module1 = `${entryPath}/angularModule-1`
const module2 = `${entryPath}/angularModule-2`

module.exports = (envOptions) => {
    envOptions = envOptions || {};

    const config = {
        entry: {
            'polyfills': `${entryPath}/polyfill.ts`,
            'vendors': `${entryPath}/vendor.ts`,
            'module1': `${module1}/main.ts`,
            'module2': `${module2}/main.ts`            
        },
        output: {
            path: corePath,
            filename: '[name].js',
            sourceMapFilename: "[name].js.map"            
        },
        resolve: {
            extensions: ['.ts', '.js', '.html']
        },
        module: {
            rules: [
                {
                    test: /\.ts$/,
                    loaders: ['awesome-typescript-loader', 'angular2-template-loader']
                },
                {
                    test: /\.html$/,
                    loader: 'raw-loader'
                },
                {
                    test: /\.css$/,
                    loader: 'raw-loader'
                }
            ]
        },
        devtool: 'source-map',
        plugins: [
            new webpack.NoEmitOnErrorsPlugin(),
            new webpack.optimize.CommonsChunkPlugin({
                name: ['vendors', 'polyfills']
            })
        ]
    }

    if (envOptions.MODE === 'prod') {
        config.plugins.push(
            new UglifyJsPlugin()
        )
    }

    return config;
}

Scripts/ng2 .

3. npm script

webpack script . package.json

"scripts": {
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "dev": "webpack-dev-server --https --open",
    "watch": "webpack --config webpack.config.js --watch",
    "build": "webpack --config webpack.config.js",
    "build:html": "webpack --config webpack-html-plugin.config.js --env.MODE=prod",
    "build:prod": "webpack --config webpack.config.js --env.MODE=prod"    
}

4. build.bat

Angular 2 - front-end WebApplication. - , front-end. front-end , WebApplication.

build.bat , , . .

call npm install --production  --loglevel verbose
echo "Build Angular projects"
npm run build:prod

call , .

script . -, npm . build:prod, package.json. Webpack , Typescript JavaScript vendors.js, polyfills.js module1.js.

Jenkins , build.bat, . , , .

pre-build and post-build

5. JavaScript .

, . my-angular-app - , app.component.ts

Index.cshtml

<script src="~/Scripts/ng2/polyfills.js"></script>
<script src="~/Scripts/ng2/vendors.js"></script>
<script src="~/Scripts/ng2/module1.js"></script>

<my-angular-app>
    Loading...
</my-angular-app>

HomeController.cs

public ActionResult Module1()
{
    return View();
}

. . JavaScript , - - . . 3 .

. html-webpack-plugin

front-end, webpack html-webpack-plugin, , . , JavaScript .

webpack-html-webpack-plugin.config.js

...
const HtmlWebpackPlugin = require('html-webpack-plugin');
const viewPath = path.resolve(
    __dirname,
    "../Server/WebApplication/Views/Home"
  );
...
entry: {
    polyfills: `${entryPath}/polyfill.ts`,
    vendors: `${entryPath}/vendor.ts`,
    module1: `${module1}/main.ts`      
},
output: {
    path: corePath,
    filename: "[name].[hash].js",
    sourceMapFilename: "[name].[hash].js.map"
}
....
plugins: [,
    ...,
    new HtmlWebpackPlugin({
        template: viewPath + "/loader.cshtml",
        filename: viewPath + "/Module1.cshtml",
        inject: false
    })
]

cshtml loader.cshtml

loader.cshtml

<% for (var chunk in htmlWebpackPlugin.files.chunks) { %>  
    <script src="<%= htmlWebpackPlugin.files.chunks[chunk].entry %>"></script>  
<% } %> 

<my-angular-app>
    Loading...
</my-angular-app>

npm run build:html, package.json. , .

build html plugin

network html plugin

@{
    ViewBag.Title = "Module 1";
}
<script src="../../Scripts/ng2/vendors.1470de344a0f2260b700.js"></script>
<script src="../../Scripts/ng2/vendors.1470de344a0f2260b700.js"></script>
<script src="../../Scripts/ng2/module1.1470de344a0f2260b700.js"></script>

<my-angular-app>Loading....</my-angular-app>

II. JavaScript

ASP.NET MVC , Angular. JS. , , . - , . , .

<script src="@string.Format("{0}?v={1}", "~/Scripts/ng2/polyfills.js", VersionHelper.CurrentVersion())</script>
<script src="@string.Format("{0}?v={1}", "~/Scripts/ng2/vendors.js", VersionHelper.CurrentVersion())</script>
<script src="@string.Format("{0}?v={1}", "~/Scripts/ng2/module1.js", VersionHelper.CurrentVersion())</script>

<script src="~/Scripts/ng2/polyfills.js?v=1.1.0"></script>
<script src="~/Scripts/ng2/vendors.js?v=1.1.0"></script>
<script src="~/Scripts/ng2/module1.js?v=1.1.0"></script>

III. ASP.NET

Zyllem - , JavaScript .

App_Start\BundleConfig.cs . , module1

bundles.Add(new ScriptBundle("~/bundles/module1").Include(
                "~/Scripts/ng2/polyfills.js",
                "~/Scripts/ng2/vendors.js"
                "~/Scripts/ng2/module1.js"));

, .

Index.cshtml

@Scripts.Render("~/bundles/module1")

, , . - script.

<script src="/bundles/module1?v=2PbUw0z_gh_Ocp_Vz13rdmmBSG6utLJAXm2lThxYAow1"></script>

, .

module 1

module 2


Github https://github.com/trungk18/mvc5-angular-webpack

+8

Source: https://habr.com/ru/post/1690971/


All Articles