在 Webpack 2 中,如何将日期(或任何其他变量)添加到横幅输出中?

In Webpack 2, how do you add the date (or any other variable) to the banner output?

在 Webpack 2 中,有一个有用的横幅插件允许将文本块插入到您的条目中。但是,如何在横幅中输入动态变量?

我试过了

new webpack.BannerPlugin({
      banner: `
/* 
 * Copyright © ACME Software, Inc - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 * Written by Chris <cjke@acme.com.au>, ${new Date(Date.now()).toLocaleDateString}
 */
      `,
      entryOnly: true,
      raw: true,
    }),

但是输出看起来像:

/* 
 * Copyright © ACME Software, Inc - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 * Written by Chris <cjke@acme.com.au>, function toLocaleDateString() { [native code] }
 */

您没有调用 toLocaleDateString,因此表达式计算为函数而不是通过调用它获得的结果。

调用该方法将为您提供正确的输出:

${ new Date(Date.now()).toLocaleDateString() }
//Invoke it by adding parenthesis here ---^