【问题标题】:webpack html (ejs) include other templateswebpack html (ejs) 包含其他模板
【发布时间】:2016-12-28 01:20:03
【问题描述】:

这是我的 webpack 配置:

import path from 'path';

var HtmlWebpackPlugin = require('html-webpack-plugin');


module.exports = {
    entry: {
        index: './dev/index.js'
    },
    output: {
        path: path.join(__dirname, 'dist'),
        // publicPath: 'http://localhost:3000/',
        filename: 'bundle.js',
        chunkFilename: '[id].bundle.js'
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: path.resolve(__dirname, "node_modules"),
                loader: 'babel-loader'
            }
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            hash: true,
            template: 'ejs!./dev/index.ejs',
            inject: 'body'
        })
    ]

};

我的index.ejs 文件:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <%- include html/one/1.ejs %>

</body>

</html>

我的文件夹结构:

dev/
  /assets
  /html
    /one
      1.ejs
      1.scss
      1.js
    /two
    /three
  index.js
  index.ejs

我想模块化我的 html 文件,所以我想包含它们...

我尝试了很多方法,包括另一个模板,但都没有奏效...

有人可以告诉我如何进行这项工作吗?

【问题讨论】:

    标签: webpack template-engine ejs webpack-dev-server html-webpack-plugin


    【解决方案1】:

    使用ejs-render-loader 包。 see package

    plugins: [
        new HtmlWebpackPlugin({
            hash: true,
            template: 'ejs-render!./dev/index.ejs',
            inject: 'body'
        })
    ]
    

    我认为它会解决你的问题。

    更新:使用 webpack3,加载器的语法发生了变化。 'ejs-render!./dev/index.ejs' 现在应该是:'ejs-render-loader!./dev/index.ejs'

    【讨论】:

    • 404 页面未找到
    【解决方案2】:

    ejs-webpack-loader 为我工作 ("ejs-webpack-loader": "^2.2.2", "webpack": "^4.41.5")。它的网页上有一个包含示例 ejs。

    【讨论】:

      【解决方案3】:

      您正在使用 html 插件,请确保它是否支持 ejs,并查看您是否遇到任何错误,例如您是否需要为 ejs 使用任何 öoder。

      【讨论】:

        猜你喜欢
        • 2016-05-21
        • 1970-01-01
        • 2013-12-16
        • 1970-01-01
        • 1970-01-01
        • 2017-02-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多