PHPプロジェクトをejs(ejs-html-loader)で管理していて、共通ヘッダーのincludeなど、?>
で終わらないファイルが存在する場合、productionビルドでエラーが出る。
Parse Error: <?php
これは html-loader(正確にはそこから利用される html-minifier-terser)のminimizeオプションでパースエラーとなるため。
これを回避するためには html-loader に continueOnParseError: true
を指定してやれば良い。
webpack.config.js
constExtractTextPlugin=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:+newDate()}}}]})}]},plugins:[newExtractTextPlugin('[name].php')]}];