PHPプロジェクトをejs(ejs-html-loader)で管理していて、共通ヘッダーのincludeなど、?>
で終わらないファイルが存在する場合、productionビルドでエラーが出る。
Parse Error: <?php
これは html-loader(正確にはそこから利用される html-minifier-terser )のminimizeオプションでパースエラーとなるため。
これを回避するためには html-loader に continueOnParseError: true
を指定してやれば良い。
webpack.config.js
const ExtractTextPlugin = require('extract-text-webpack-plugin');
module.exports = [
{
context: path.resolve(__dirname, 'dev'),
entry: {
// ?> で終わらないファイルを含む
index: './index.php'.
header: './header.php'
},
output: {
path: path.resolve(__dirname, 'public'),
filename: '[name].php'
},
module: {
rules: [
{
test: /\.php/,
exclude: /node_modules/,
use: ExtractTextPlugin.extract({
fallback: "raw-loader",
use: [
{
loader: 'html-loader',
options: {
minimize: {
continueOnParseError: true
}
}
},
{
loader: 'ejs-html-loader',
options: {
context: {
// タイムスタンプをPHPファイルに渡すとか
timestamp: +new Date()
}
}
}
]
})
}
]
},
plugins: [
new ExtractTextPlugin('[name].php')
]
}
];
2020/06/03追記
こちらでうまく行かない場合、minimizeをfalse(ソースの圧縮を行わない)にする。
最新のhtml-loaderだとこちらでないとうまく行かないかもしれない。
webpack.config.js
loader: 'html-loader',
options: {
minimize: false
}