Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML webpack plugin not parsing EJS variables

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

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

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 just gets rendered as this:

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

I've tried a few things (explicitly using ejs-loader which I couldn't get to work at all, using dotenv-webpack which turned out to be unnecessary). Any guidance on moving forward?

like image 327
Joshua Carlo Avatar asked Mar 08 '23 14:03

Joshua Carlo


1 Answers

You are not using the correct syntax to interpolate.

From EJS - Tags:

  • <% 'Scriptlet' tag, for control-flow, no output
  • <%_ 'Whitespace Slurping' Scriptlet tag, strips all whitespace before it
  • <%= Outputs the value into the template (escaped)
  • <%- Outputs the unescaped value into the template

By using <% you only evaluate the expression, but it has no output. You need to use <%= instead.

<%= htmlWebpackPlugin.options.placesApiKey %>
like image 170
Michael Jungo Avatar answered Mar 16 '23 00:03

Michael Jungo