代码之家  ›  专栏  ›  技术社区  ›  Royalsmed

Angular cli:引用散列脚本

  •  0
  • Royalsmed  · 技术社区  · 6 年前

    angular/cli6支持自定义webpack配置,可以指定自定义应用程序引导。

    "projects": {
      "custom-name": {
        // ...
        "architect": {
          // ...
          "build": {
            "builder": "@angular-devkit/build-webpack:webpack",
            "options": {
              "webpackConfig": "webpack.config.js"
            }
          }
        }
    

     const path = require('path');
     const config = {
       entry: './src/custom-app.js',
       mode: 'production',
       output: {
         path: path.resolve(__dirname, 'dist'),
         filename: 'custom-app.js'
       }
     };
     module.exports = config;
    

    然后可以在中引用输出包 angular.json

    "scripts": [
      { "input": "dist/custom-app.js", "lazy": true }
    ]
    

    export class AppComponent {
      ngOnInit(): void {
        const customApp = new Worker('custom-app.js');
      }
    }
    

    问题

    缓存可能会成为上述设置的问题! custom-app.js hashcontent ?

     const path = require('path');
     const config = {
       entry: './src/custom-app.js',
       mode: 'production',
       output: {
         path: path.resolve(__dirname, 'dist'),
         filename: '[name][contenthash].js'
       }
     };
     module.exports = config;
    

    这将不再导致 自定义app.js custom-app1247989898989.js .

    有没有一种方法可以加载散列文件而不必在构建时存储散列?

    干杯

    2 回复  |  直到 6 年前
        1
  •  0
  •   T. Shashwat    6 年前

    您可以尝试使用以下标志来避免散列

    有关详细信息,请访问 github

        2
  •  0
  •   Royalsmed    6 年前

    这是一个从SO和Github尝试了不少建议的结果。。。

    首先我得改变主意 webpack.config.js contenthash 支持

    const path = require('path');
    const config = {
      entry: './src/custom-app.js',
      mode: 'production',
      output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'custom-app.[contenthash].js'
      }
    };
    module.exports = config;
    

    在输出对象中, custom-app [name] 如果有多个条目。

    然后,我可以使用下面的代码片段包含我的散列脚本

    export class AppComponent {
      ngOnInit(): void {
        const customApp = new Worker('custom-app.XXXXXXXXXXXXXXXXXXXX.js);
      }
    }
    

    在哪里? XXXXXXXXXXXXXXXXXXXX

    这有一些缺点,例如如果我改变 自定义应用程序 文件,散列将更改,我需要引用新的散列。。。