简体   繁体   English

Angular 7 使用 ng build --prod 缩小资产

[英]Angular 7 minify assets with ng build --prod

JavaScript and CSS files in /assets are not minified after ng build --prod . /assets中的 JavaScript 和 CSS 文件在ng build --prod之后没有被缩小。 Does someone know how I can minify those files?有人知道我可以如何缩小这些文件吗? I don't see anything in the angular doc.我在 angular 文档中看不到任何内容。

Everything you build in production are already minify if it not minify I think you havent turn on configuration in angular.json.如果没有缩小,您在生产中构建的所有内容都已经缩小了,我认为您还没有在 angular.json 中打开配置。 You can use these config in your angular.json您可以在 angular.json 中使用这些配置

"configurations": {
    "production": {
        "fileReplacements": [
            {
                "replace": "src/environments/environment.ts",
                "with": "src/environments/environment.prod.ts"
            }
        ],
        "optimization": true,
        "outputHashing": "all",
        "sourceMap": false,
        "extractCss": true,
        "namedChunks": false,
        "aot": true,
        "extractLicenses": true,
        "vendorChunk": true,
        "buildOptimizer": true,
        "serviceWorker": true,
        "deleteOutputPath": true,
        "budgets": [
            {
                "type": "initial",
                "maximumWarning": "2mb",
                "maximumError": "5mb"
            }
        ]
    }
}

Then run ng build --prod again然后再次运行ng build --prod

When you run ng build --prod angular-cli creates a new folder named dist in your project folder.当您运行ng build --prod angular-cli 时,会在您的项目文件夹中创建一个名为dist的新文件夹。 the dist is the folder that you upload to your server in order to deploy your project. dist是您上传到服务器以部署项目的文件夹。

the assets folder is not minimized because there is no code to minimize in that folder, angular-cli does not minimize images, svgs text files etc ... if you want to minify images, you can search for online tools such as TinyJPG资产文件夹未最小化,因为该文件夹中没有最小化代码,angular-cli 不会最小化图像、svgs 文本文件等...如果您想缩小图像,可以搜索TinyJPG等在线工具

edit:编辑:

Not necessarily mean that you want to minimize your images, but in your assets folder there shouldn't be any files which are .js .css .html .ts etc... only Jsons, images and other assets不一定意味着你想最小化你的图像,但在你的assets文件夹中不应该有任何 .js .css .html .ts 等文件......只有 Jsons、图像和其他资产

When you run ng build --prod angular will create a new folder in your project called dist.当您运行 ng build --prod angular 时,将在您的项目中创建一个名为 dist 的新文件夹。 the dist, This is the folder that you place on your server to run your application from. dist,这是您放置在服务器上以运行应用程序的文件夹。 You will see inside that folder, you will get a few files.你会看到在那个文件夹里面,你会得到一些文件。 All of your CSS and JS will be bundled and minified into one file each你所有的 CSS 和 JS 都将被打包并压缩到一个文件中

ng build creates a build in the dist folder using webpack . ng build使用webpack在 dist 文件夹中创建一个构建。 The dist folder is published to your production website and contains everything needed to run your website (the minified Angular framework, your code, and dependencies referenced in the application). dist 文件夹发布到您的生产网站,并包含运行您的网站所需的一切(缩小的 Angular 框架、您的代码和应用程​​序中引用的依赖项)。

CSS and JavaScript in the head of index.html will be referenced from your assets folder without minification. index.html头部的 CSS 和 JavaScript 将从您的资产文件夹中引用而不会缩小。 Consider adding these files to your configuration instead.考虑将这些文件添加到您的配置中 The working files in the source folder are used for development purposes and are not modified during the build process.源文件夹中的工作文件用于开发目的,在构建过程中不会被修改。

You can use webpack to minify the ts file:您可以使用 webpack 来缩小 ts 文件:

Install webpack, webpack-cli, @babel/preset-env and @babel/preset-typescript;安装 webpack、webpack-cli、@babel/preset-env 和 @babel/preset-typescript;

Add webpack.config.js to your project root folder:webpack.config.js添加到您的项目根文件夹:

const path = require('path');

module.exports = [
  {
    mode: 'production',
    resolve: {
      extensions: ['.ts', '.tsx', '.js', '.json'],
    },
    module: {
      rules: [
        {
          test: /\.ts$/,
          exclude: /node_modules/,
          use: {
            loader: "babel-loader",
            options: {
                presets: [
                    '@babel/preset-env',
                    '@babel/preset-typescript'
                ]
            }
          }
        }
      ],
    },
    entry: {
      main: './apps/js/some-file.ts'
    },
    output: {
      path: path.resolve(__dirname, './dist/js/'),
      filename: 'some-file.min.js'
    }
  }
];

In your build command, just add & webpack --watch在您的构建命令中,只需添加& webpack --watch

Hope that helps希望有帮助

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

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