HTML webpack plugin does not parse EJS variables

I am trying to load a Google Places API key from a .env file into my main index. I know that process.env.GOOGLE_PLACES_API_KEY is loading correctly, as I can customize it and it spits out my key. But that doesn't turn the variable into DOM.

I almost never use EJS and Webpack has been the biggest stumbling block for me in promoting this project. There seem to be a thousand different options for what should be pretty simple and straightforward. I just need to get a JS variable interpolated into my HTML output.

Here's my webpack config:

// webpack.dev.config.js
const webpack = require('webpack');
const path = require('path');
const SplitByPathPlugin = require('webpack-split-by-path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: [
    path.join(__dirname, 'client', 'src', 'main.js'),
    'webpack-hot-middleware/client',
    'webpack/hot/dev-server',
  ],
  devtool: 'source-map',
  target: 'web',
  output: {
    path: '/',
    publicPath: 'http://localhost:3000/',
    filename: '[name].js',
  },
  module: {
    loaders: [
      {
        test: path.join(__dirname, 'client', 'src'),
        loaders: [
          'react-hot-loader',
          'babel-loader?presets[]=react,presets[]=es2015,presets[]=stage-0,plugins[]=transform-decorators-legacy,cacheDirectory=babel_cache',
        ],
        exclude: /node_modules/,
      },
      { test: /\.css$/, loader: 'style-loader!css-loader' },
      { test: /\.(png|jpg)$/, loader: 'url-loader?limit=8192' },
    ],
  },
  resolve: {
    extensions: ['.js', 'map'],
  },
  plugins: [
    new SplitByPathPlugin([
      {
        name: 'vendor',
        path: path.join(__dirname, '..', 'node_modules'),
      },
    ]),
    new HtmlWebpackPlugin({
      title: 'Better Beehive Project',
      template: 'client/index.dev.ejs',
      inject: false,
      appMountId: 'app',
      filename: '../index.html',
      placesApiKey: process.env.GOOGLE_PLACES_API_KEY,
    }),
    new webpack.DefinePlugin({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
    }),
    new webpack.HotModuleReplacementPlugin(),
  ],
};

      

Here's my index.ejs

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
    <title>Better Beehive Project</title>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=<% htmlWebpackPlugin.options.placesApiKey %>&libraries=places"></script>
  </head>
  <body>
    <div id='app'></div>
    <script type="text/javascript" src="manifest.js"></script>
    <script type="text/javascript" src="main.js"></script>
  </body>
</html>

      

The script tag for Google Places is simply rendered like this:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=&libraries=places"></script>

      

I tried a few things (obviously using ejs-loader, which I couldn't work with at all, using dotenv-webpack which turned out to be unnecessary). Any directions for moving forward?

+3


source to share


1 answer


You are not using the correct syntax for interpolation.

From EJS - Tags :

  • <%

    Scriptlet tag, for control flow, no output
  • <%_

    "Scroll space" Script-label, runs through all spaces before it
  • <%=

    Outputs the value to the template (escaped)
  • <%-

    Outputs undefined value to template


By using <%

, you only evaluate the expression, but it has no output. Instead, you need to use <%=

.

<%= htmlWebpackPlugin.options.placesApiKey %>

      

+3


source







All Articles