5

所以这是我的 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 文件,所以我想包含它们......

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

有人可以给我任何关于如何使这项工作的想法吗?

4

3 回答 3

5

使用ejs-render-loader包。见包装

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'

于 2016-10-13T02:39:34.720 回答
0

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

于 2020-02-10T15:01:16.250 回答
-1

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

于 2016-08-21T19:08:26.947 回答