2017-04-11 13 views
1

EJSテンプレートを実行せずにbundle.jsを動的に挿入しようとしていますが、以下のエラーが表示されます。 EJSテンプレートを実行せずにJSだけを挿入する方法はありますか?html-webpack-plugin - EJSテンプレートを実行しないでwebpack bundle.jsを挿入する方法

ERROR in Template execution failed: ReferenceError: description is not defined 

ERROR in ReferenceError: description is not defined 

私は実際にノードを使用してテンプレートをレンダリングしていると私はちょうどバンドルファイルを動的にtemplate.ejs

res.status(200).render('template', 
{          
description: description, 
title:title 
}); 

のWebPACKの設定に挿入されるようにしたい:

output: { 
     path: path.join(__dirname, 'dist'), 
     filename: "output.[hash].bundle.js", 
     publicPath: '/' 
    }, 
    new HtmlWebpackPlugin({ 
       inject: 'body', 
       template: 'views/template.ejs' 
      }), 

テンプレート。 ejs

<!DOCTYPE html> 
<html lang="en" class="ddhub-site"> 
<head> 
    <meta charset="utf-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <meta name="format-detection" content="telephone=no"> 
    <meta description=<%=description%>/> 
    <title> <%= title %> </title> 
</head> 
<body></body> 
</html> 

答えて

0

私はgithub問題の1つに投稿された情報を使って簡単なカスタムプラグインを使用してしまいました。

var fs = require("fs"); 
const path = require('path') 

function UpdateBundleJsPlugin(options) { 
    // Setup the plugin instance with options... 
} 

UpdateBundleJsPlugin.prototype.apply = function(compiler) { 
    compiler.plugin("done", function(statsData) { 
     const stats = statsData.toJson(); 

     if (!stats.errors.length) { 
      const htmlFileName = "search.ejs"; 
      const html = fs.readFileSync(path.join('./views',htmlFileName), "utf8"); 

      // need to read the final js bundle file to replace in the distributed index.html file 
      const asset = stats.assets[0]; 
      let htmlOutput = html.replace(/static\/.*bundle\.js/, 'static/'+asset.name); 

      fs.writeFileSync(
       path.join('./views', htmlFileName), 
       htmlOutput); 
     } 
    }); 
}; 

module.exports = UpdateBundleJsPlugin; 
関連する問題