Search code examples
javascriptwebpackwebpack-2

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


In Webpack 2, there is a useful banner plugin allowing for chunks of text to be inserted into your entries. However, how does one enter dynamic variables into the banner?

I have tried

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,
    }),

However the output looks like:

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

Solution

  • You didn't invoke toLocaleDateString so that expression evaluated to the function as opposed to the result obtained by invoking it.

    Invoking the method will provide you with the correct output:

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