Closure 编译器重命名属性,尽管存在 externs

问题描述 投票:0回答:0

我正在创建一个网络应用程序,我使用闭包编译器 (v.20230411.0.0) 来优化性能。在这个应用程序中,我创建了一个弹出窗口,并使用

postMessage()
在主窗口和弹出窗口之间进行通信。为了交流,我创建了一个
Message
类。此类的实例将以这种方式发送和接收。我用不同入口点的
--dependency_mode PRUNE
编译代码两次。
Message
类由网络应用程序的两个部分使用。这就是为什么不应该重命名它的属性的原因。

这是

Message
类以及枚举
MessageTopic
MessageCommand
。在这个例子中我只包含文件,其属性和变量名不应该被重命名或删除,所以这里不需要依赖模式。

// ###########################   message.js  ##################################

import MessageTopic from "./messagetopic.js";
import MessageCommand from "./messagecommand.js";

/**
 * A message for the communication between the main and the mod screen.
 * @template T
 */
class Message {
  /**
   * Constructor of {@link Message}.
   *
   * @param {!T} messageData - The transmitted data.
   * @param {!MessageTopic} messageTopic - The part of the app the message is about.
   * @param {!MessageCommand} messageCommand - The operation that should be executed with the data.
   */
  constructor (messageData, messageTopic, messageCommand) {
    /**
     * The data of this message.
     * @const {T}
     */
    this.data = messageData;

    /**
     * The part of the app the message is about.
     * @const {MessageTopic}
     */
    this.topic = messageTopic;

    /**
     * The operation that should be executed with the data.
     * @const {MessageCommand}
     */
    this.command = messageCommand;
  }
}

export default Message;

// ###########################   messagetopic.js  ##################################

/**
 * Enum for the topic this message is about.
 * @enum {number}
 *
 * @readonly
 */
const MessageTopic = {
  /** The topic of this message is the view system. */
  VIEW: 0,
  /** The topic of this message is the team system. */
  TEAM: 1
};

Object.freeze(MessageTopic);

export default MessageTopic;

// ###########################   messagecommand.js  ##################################

/**
 * Enum for the command that should be executed on the message target.
 * @enum {number}
 *
 * @readonly
 */
const MessageCommand = {
  /** A new object should be added. */
  ADD: 0,
  /** An object should be removed. */
  REMOVE: 1,
  /** An object should be shown. */
  SHOW: 2
};

Object.freeze(MessageCommand);

export default MessageCommand;

这里是外部文件:

// ###########################   message.extern.js  ##################################

/**
 * @fileoverview
 * @externs
 */

/**
 * A message for the communication between the main and the mod screen.
 * @template T
 */
class Message {
  /**
   * Constructor of {@link Message}.
   *
   * @param {!T} messageData - The transmitted data.
   * @param {!MessageTopic} messageTopic - The part of the app the message is about.
   * @param {!MessageCommand} messageCommand - The operation that should be executed with the data.
   */
  constructor (messageData, messageTopic, messageCommand) {
    /**
     * The data of this message.
     * @const {T}
     * @nocollapse
     */
    this.data = messageData;

    /**
     * The part of the app the message is about.
     * @const {MessageTopic}
     * @nocollapse
     */
    this.topic = messageTopic;

    /**
     * The operation that should be executed with the data.
     * @const {MessageCommand}
     * @nocollapse
     */
    this.command = messageCommand;
  }
}

// ###########################   messagetopic.extern.js  ##################################

/**
 * @fileoverview
 * @externs
 */

/**
 * Enum for the topic this message is about.
 * @enum {number}
 *
 * @readonly
 */
const MessageTopic = {
  /** The topic of this message is the view system. */
  VIEW: 0,
  /** The topic of this message is the team system. */
  TEAM: 1
};

// ###########################   messagecommand.extern.js  ##################################

/**
 * @fileoverview
 * @externs
 */

/**
 * Enum for the command that should be executed on the message target.
 * @enum {number}
 *
 * @readonly
 */
const MessageCommand = {
  /** A new object should be added. */
  ADD: 0,
  /** An object should be removed. */
  REMOVE: 1,
  /** An object should be shown. */
  SHOW: 2
};

当我这样调用闭包编译器时:

google-closure-compiler --warning_level VERBOSE --js message/* --compilation_level ADVANCED_OPTIMIZATIONS --js_output_file build/message.js

它生成这个

build/message.js

Object.freeze({VIEW:0,TEAM:1});Object.freeze({ADD:0,REMOVE:1,SHOW:2});


它没有给我任何错误或警告。但结果这些属性仍然被重命名或删除。在我的项目中,实际使用了

Message
类,它的属性正在重命名。只有
data
属性保持不变。

在外部文件中添加

--extern
标志,得到相同的结果。我可以看到闭包编译器将文件识别为外部文件,因为:

  1. 使用
    --output_manifest
    标志我可以看到,外部文件不被视为源文件。然而它们也在
    message/
    目录中。
  2. 故意在外部文件中添加语法错误会导致编译器出现错误消息。

可能是什么问题?有没有办法在编译过程中获取有关处理的外部文件的任何信息?我是否以错误的方式编写外部文件?

javascript google-closure-compiler
© www.soinside.com 2019 - 2024. All rights reserved.