简体   繁体   English

最小化Angular 4代码

[英]Uglify and minify Angular 4 code

I'm using Asp Core +Angular 4 template + webpack in VS 2017. I've published my app ..and looking to ClientApp/dist/main-server.js I see the content is not uglify and minify..it is something like this 我在VS 2017中使用Asp Core + Angular 4模板+ webpack。我已经发布了我的应用程序..并查看ClientApp / dist / main-server.js,我发现内容没有被丑化和缩小。像这样

    ...
ConfirmComponent.prototype.ngAfterViewInit = function () {
            try {
                $('.modal').removeClass('fade'); 
                setTimeout(function () {
                    $('.modal').addClass('fade');
                }, 500);
            }
            catch (exception) { }
        };
...

in webpack.config.vendor.js I can see a plugin call: 在webpack.config.vendor.js中,我可以看到一个插件调用:

.... 
  plugins: [
            extractCSS,
            new webpack.DllPlugin({
                path: path.join(__dirname, 'wwwroot', 'dist', '[name]-manifest.json'),
                name: '[name]_[hash]'
            })
        ].concat(isDevBuild ? [] : [
            new webpack.optimize.UglifyJsPlugin()   //HERE
        ])
    });
....

in package.json I've added: "uglifyjs-webpack-plugin": "1.0.1", how to uglify and minify the code? 在package.json中,我添加了: "uglifyjs-webpack-plugin": "1.0.1",如何将代码丑化并缩小? thanks 谢谢

I recommend refactoring your code to work with the Angular CLI, and then minifying with: 我建议将代码重构为可与Angular CLI一起使用,然后使用以下代码进行精简:

ng build --prod

With a Web API project on .net core, once you create a new Angular App with (run this from the same directory as your solution (sln) file: 对于.net核心上的Web API项目,一旦您使用(在与解决方案(sln)文件相同的目录中运行此应用程序)创建新的Angular应用,即可:

ng new my-app

Then: 然后:

  • In angular-cli.json , change outDir to wwwroot angular-cli.json中 ,将outDir更改为wwwroot
  • In tsconfig.json , change outDir to ./wwwroot/out-tsc tsconfig.json中 ,将outDir更改为./wwwroot/out-tsc

At this point, ng build will publish everything to wwwroot , so as long as you include: 此时, ng build会将所有内容发布到wwwroot ,只要您包括:

app.UseDefaultFiles();

In the Configure method in Startup.cs , browsing to the root of your API will display your Angular app. Startup.csConfigure方法中,浏览到API的根目录将显示Angular应用程序。

Additionally, I like to include a proxy file, called proxy.config.json , which looks like this (replace the port with your API port): 此外,我喜欢包含一个名为proxy.config.json的代理文件,该文件如下所示(将端口替换为您的API端口):

{
  "/api/*": {
    "target": "http://localhost:12345",
    "secure": false,
    "logLevel": "debug"
  }
}

Then, I amend the start script in package.json , like so: 然后,我修改package.json中启动脚本,如下所示:

"start": "ng serve --proxy-config proxy.config.json"

At this point, you can debug your API, and just run npm start from the command line, making full use of the Angular CLI. 此时,您可以调试您的API,只需从命令行运行npm start即可充分利用Angular CLI。 Then, when you want to release, run: 然后,当您要释放时,运行:

ng build --prod

Also, you can amend your .csproj file to build and minify your angular app when you build your Web API project like so: 此外,在构建Web API项目时,您可以修改.csproj文件以构建和最小化angular应用程序,如下所示:

<Target Name="Build Angular" BeforeTargets="Build">
  <Message Text="* * * * * * Building Angular App * * * * * *" Importance="high" />
  <Exec Command="ng build --prod" />
</Target>

I personally view Angular as an all-in framework. 我个人认为Angular是一个全能框架。 I don't see the value of using it with Razor. 我看不到将其与Razor一起使用的价值。 If you want progressive enhancement, use React! 如果要逐步增强,请使用React!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM