LoginSignup
2
0

More than 3 years have passed since last update.

PHPファイルをhtml-loaderにかけたときにエラーが出るときの対策

Last updated at Posted at 2020-04-03

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
                                }

参考: 【備忘録】HTMLMinifierの全オプションについて調査した

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0