编程语言
首页 > 编程语言> > javascript – HTML webpack插件不解析EJS变量

javascript – HTML webpack插件不解析EJS变量

作者:互联网

我正在尝试将.Google API密钥从.env文件加载到我的主索引中.我知道process.env.GOOGLE_PLACES_API_KEY正确加载,因为我可以控制日志并且它会吐出我的密钥.但它不会将变量渲染到DOM中.

我几乎从不使用EJS,而Webpack一直是我推动这个项目向前发展的最大绊脚石.似乎有千种不同的选择来做一些应该非常简单和直接的事情.我只需要将一个JS变量插入到我输出的HTML中.

这是我的webpack配置:

// 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(),
  ],
};

这是我的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>

Google商家信息的脚本代码只会呈现为:

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

我已经尝试了一些东西(明确使用ejs-loader,我根本无法工作,使用dotenv-webpack,结果证明是不必要的).前进的任何指导?

解决方法:

您没有使用正确的语法进行插值.

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

通过使用<%,您只评估表达式,但它没有输出.您需要使用<%=代替.

<%= htmlWebpackPlugin.options.placesApiKey %>

标签:javascript,node-js,webpack,ejs,html-webpack-plugin
来源: https://codeday.me/bug/20190608/1196529.html