0

我正在尝试在我的模板中导入静态 html 组件,

我有这个结构:

/src/home.html.ejs
/src/parts/header.html
/src/parts/breadcrumbs.html

/src/home.html.ejs 正文

<%= require('./header.html') %>

/src/parts/header.html

<h1>Title</h1>
<%= require('./breadcrumbs.html') %>

/src/parts/breadcrumbs.html

It's a breadcrumb!

当 webpack 正在构建时,我在 index.html 中得到了这个:

<h1>Title</h1>
<%= require('./breadcrumbs.html') %>

网络包配置:

const path = require('path');
const miniCss = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
    entry: './src/index.js',
    output: {
        publicPath: '',
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    module: {
        rules: [
            {
                test: /\.html$/,
                loader: 'html-loader',
            },
            {
                test: /\.(s*)css$/,
                use: [
                    miniCss.loader,
                    'css-loader',
                    'sass-loader',
                ]
            },
            {
                test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            name: '[name].[ext]',
                            outputPath: 'fonts/'
                        }
                    }
                ],
            }
        ]
    },
    plugins: [
        new miniCss({
            filename: 'style.css',
        }),
        new HtmlWebpackPlugin({
            title: 'Home',
            template: path.resolve(__dirname, './src/home.html.ejs'),
            filename: 'index.html',
        })
    ],
    devServer: {
        contentBase: path.join(__dirname, 'dist'),
        compress: true,
        port: 9000
    }
};

当开发人员可以在 html-loader 中使用选项 interpolate 时,我还找到了一些解决方案,但我使用的 html-loader 高于 v0.5.5,并且此选项已被删除

我做错了什么?

4

1 回答 1

1

我通过使用 ejs-webpack-loader 解决了这个问题:

npm i ejs-webpack-loader

webpack.config.js

const path = require('path');
const miniCss = require('mini-css-extract-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin')

module.exports = {
    entry: './src/index.js',
    output: {
        publicPath: '',
        filename: 'bundle.js',
        path: path.resolve(__dirname, 'dist')
    },
    module: {
        rules: [
            {
            test: /\.ejs$/,
            use: [
                {
                  loader: "ejs-webpack-loader",
                }
            ]
        },
            {
                test: /\.(s*)css$/,
                use: [
                    miniCss.loader,
                    'css-loader',
                    'sass-loader',
                ]
            },
            {
                test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
                use: [
                    {
                        loader: 'file-loader',
                        options: {
                            name: '[name].[ext]',
                            outputPath: 'fonts/'
                        }
                    }
                ],
            }
        ]
    },
    plugins: [
        new miniCss({
            filename: 'style.css',
        }),
        new HtmlWebpackPlugin({
            title: 'Home',
            template: path.resolve(__dirname, './src/home.html'),
            filename: 'index.html',
        })
    ],
    devServer: {
        contentBase: path.join(__dirname, 'dist'),
        compress: true,
        port: 9000
    }
};

结构

/src/home.html
/src/parts/header.ejs
/src/parts/breadcrumbs.ejs

在 home.html

<%= require('./parts/header.ejs')() %>

在 header.ejs

<%- require('./breadcrumbs.ejs')() %>

使用<%-而不是<%=导入子组件,并在导入结束时使用方括号 ()。您可以在这个括号内传递一些变量

在面包屑.ejs

It's a breadcrumb!
于 2021-01-19T19:13:27.513 回答