【发布时间】:2020-10-11 18:11:22
【问题描述】:
我正在尝试使用节点 js 在我的网站上配置 webpack,我也使用 ejs 作为视图。我尝试了很多方法来处理我的 webpack 中的 ejs,但直到现在我都没有成功。
const path = require('path')
const nodeExternals = require('webpack-node-externals')
module.exports = (env, argv) => {
return ({
entry: {
server: './src/app.js',
},
output: {
path: path.join(__dirname, 'dist'),
publicPath: '/',
filename: 'main.js'
},
mode: argv.mode,
target: 'node',
node: {
// Need this when working with express, otherwise the build fails
__dirname: true,
__filename: true,
},
externals: [nodeExternals()], // Need this to avoid error when working with Express
module: {
rules: [
{
// Transpiles ES6-8 into ES5
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.ejs$/,
loader: 'ejs-loader',
options: {
esModule: false
}
}
]
}
})
}
当我使用HtmlWebPackPlugin 时,由于<%- %> 中的数据而出现一些错误,就好像他不知道这些数据来自哪里。例如,<%- include('partials/head.ejs') %>。
有没有办法使用 webpack 将我的视图作为 ejs 处理?
【问题讨论】:
-
非常好的问题。 Ejs 是常用的服务端渲染框架。你是在使用 react、angular、vue 等客户端渲染吗?
-
@JRichardsz 我只使用 node js,没有任何其他前端客户端。