qinqing3761 / webpack-build-demo

Angular 10 Webpack packaging configuration
2 stars 0 forks source link

After angular CLI custom webpack config, js file not loaded #1

Open jasmine-song opened 2 years ago

jasmine-song commented 2 years ago

webpack config splitchunk, but after compilation and deployment, static resources not send request, I found JS introduced by HTML not to load content, request js file has respones, but js file not loaded. What's wrong with my custom webpack config? this is my webpack.partial.js config "@angular/core": "9.1.13", "@types/webpack" : "4.41.32", "webpack":"4.42.0",

const webpack = require('webpack');
const pkg = require('./package.json');
const path = require('path');
// const CopyPlugin = require('copy-webpack-plugin');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
module.exports = {
   optimization: {
    splitChunks: {
      chunks: "async",
      minSize: 20000,
      minChunks: 1,
      maxAsyncRequests: 5,
      maxInitialRequests: 3,
      automaticNameDelimiter: '~',
      name: true,
      cacheGroups: {
        angular: {
          name: 'angular',
          test: /[\\/]node_modules[\\/]@angular[\\/]/,
          priority: -9
        },
        vendors: {
          name: 'vendors',
          test: /[\\/]node_modules[\\/]/,
          priority: -10
        },
        default: {
          name: 'default',
          minChunks: 2,
          priority: -20,
          reuseExistingChunk: true
        }
      }
    }
  },
  plugins: [
    new webpack.DefinePlugin({
      APP_VERSION: JSON.stringify(pkg.version)
    }),
  ]

this is my angular.json config

        "build": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "outputPath": "dist/onenote",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets",
              {
                "glob": "**/*",
                "input": "./node_modules/mathjax",
                "output": "/"
              },
              {
                "glob": "**/*",
                "input": "./node_modules/@ant-design/icons-angular/src/inline-svg/",
                "output": "/assets/"
              },
              {
                "glob": "**/*",
                "input": "./WEB-INF",
                "output": "/WEB-INF/"
              }
            ],
            "styles": [
              "src/styles/theme/light/antd-light.less",
              "src/styles.less",
              "./node_modules/highlight.js/styles/github.css",
              "./node_modules/monaco-editor/min/vs/editor/editor.main.css",
              "./node_modules/github-markdown-css/github-markdown.css"
            ],
            "stylePreprocessorOptions": {
              "includePaths": [
                "src/styles/theme",
                "src/styles/theme/dark",
                "src/styles/theme/light"
              ]
            },
            "scripts": [
              "node_modules/mathjax/MathJax.js",
              "node_modules/systemjs/dist/s.js",
              "node_modules/systemjs/dist/extras/amd.js",
              "node_modules/systemjs/dist/extras/named-register.js",
              "node_modules/systemjs/dist/extras/use-default.js"
            ],
            "customWebpackConfig": {
              "path": "./webpack.config.js"
            }
          },
          "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": false,
              "buildOptimizer": true
            },
            "analyzer": {
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": true,
              "namedChunks": true,
              "extractCss": true,
              "extractLicenses": true,
              "vendorChunk": true,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "10kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-builders/custom-webpack:dev-server",
          "options": {
            "browserTarget": "onenote:build",
            "customWebpackConfig": {
              "path": "./webpack.deployment.js"
            }
          },
          "configurations": {
            "production": {
              "browserTarget": "onenote:build:production"
            }
          }
        },
qinqing3761 commented 2 years ago

    您好,您的邮件我已收到,我会尽快给您回复。

jasmine-song commented 2 years ago

您好,您的邮件我已收到,我会尽快给您回复。

您好,找到原因了,是提出来的vendors没有被index.html引入。webpack config.js中的HtmlWebpackPlugin未和angular.json配置融合。是不能"index": { "input": "src/index.html", "output": "index-old.html" }, 这样配置的,会提示

image

把builder改为custom-webpack好了。

 "builder": "@angular-builders/custom-webpack:browser",

建议我为您的demo提个pull request修改吗。 还有个疑问。您为什么入口文件是main.ts,chunks中配置就要把main放到最后呢。