HTMLWebpackPlugin을 사용하여 index.ejs
템플릿 파일을 가져 와서 내 번들 애셋을 삽입하고 최종 파일 index.ejs
을 출력하고 싶습니다.HTMLWebpackPlugin을 EJS 파일과 함께 사용
이 예제에는 EJS 변수 <%= API_URL %>
이 있지만 webpack에서는이를 해석합니다.
어떻게 webpack이 변수를 대체하지 못하게 할 수 있습니까?
시작 "템플릿"
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Monitor</title>
<script>
window.config = {
API_URL: "<%= API_URL %>"
}
</script>
</head>
<body>
<div class="container"></div>
</body>
</html>
당신이 웹팩 실행하려고 :
ERROR in Template execution failed: ReferenceError: API_URL is not defined
원하는 결과 index.ejs : (번들로 제공 한 자산과 EJS의 VAR)
모니터 window.config = { API_URL "<% = API_URL의 %"> }
webpack.config.js
var webpack = require('webpack');
var path = require('path');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = {
entry: {
bundle: './src/index.js'
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].[chunkhash].js'
},
module: {
rules: [
{
test: /\.js$/,
use: 'babel-loader',
exclude: /node_modules/
},
{
// CSS is imported in app.js.
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallbackLoader: 'style-loader',
loader: ["css-loader", "sass-loader"]
})
}]
},
plugins: [
new webpack.DefinePlugin({
'process.env': {
'NODE_ENV': JSON.stringify(process.env.NODE_ENV),
'API_URL': JSON.stringify(process.env.API_URL)
}
}),
new HtmlWebpackPlugin({
template: 'src/index.ejs',
inject: true
}),
new ExtractTextPlugin("styles.css")
],
};