svg.js + Angular 7.3: Build in production mode get 'not a constructor'

svg.js + Angular 7.3: Build in production mode get 'not a constructor'

开发模式下这段代码完美运行:

app.component.ts

import { Component, OnInit } from '@angular/core';
import SVG from "@svgdotjs/svg.js/src/svg"   //v 3.0.12

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'svgjs30';
  draw: any

  ngOnInit() {
    this.draw = SVG().addTo('#canvas').viewbox(0, 0, 300, 140)
    var text = this.draw.text('ABCDE')

  }
}

但是在生产模式(ng b --prod 之后)我得到

main.b6499f06315e700352a1.js:1 ERROR TypeError: du[e] is not a constructor
    at mu (main.b6499f06315e700352a1.js:1)
    at yu (main.b6499f06315e700352a1.js:1)
    at Ul.addTo (main.b6499f06315e700352a1.js:1)
    at t.ngOnInit (main.b6499f06315e700352a1.js:1)
    at main.b6499f06315e700352a1.js:1
    at main.b6499f06315e700352a1.js:1
    at Yo (main.b6499f06315e700352a1.js:1)
    at _s (main.b6499f06315e700352a1.js:1)
    at Object.updateDirectives (main.b6499f06315e700352a1.js:1)
    at Object.updateDirectives (main.b6499f06315e700352a1.js:1)

[trata tata ta]

这是 svg.js GitHub 页面上发布的问题摘要:

在撰写本文时,npm 包 @svgdotjs/svg.js v0.3.12 具有依赖调用 Function.name 的代码。有个fix for this issue on the current master branch, but it has not been published to NPM yet (more info here: https://github.com/svgdotjs/svg.js/issues/1005).

解法:

作为 work-around(直到发布下一个版本),您可以在构建过程中使用类似的规则 the rules used by svg.js when it is built. Specifically, you will need to tell your minifier to not mangle a specific list of function names. I am not sure how it would be done in Angular 7, but 可能是有用的起点 (Angular 5) .

或者(我不是特别推荐这个)你可以下载最新的master分支,运行构建命令,然后将构建的代码复制到你的源目录中,直接使用它而不是使用NPM 包。