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

时间:2017-08-05 01:29:36

标签: javascript webpack webpack-2

在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] }
 */

1 个答案:

答案 0 :(得分:3)

您没有调用toLocaleDateString,因此表达式会对函数进行求值,而不是通过调用它获得的结果。

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

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