A webpack loader for blade template engine using Laravel's Illuminate/View component itself.
This loader passes on the contents of your blade views to a PHP script. The PHP script sets up a ViewFactory to render the blade code to HTML. This means that your views are rendered entirely using PHP. The resulting HTML is passed back into the Webpack flow.
As we're using PHP behind the scenes, you should have PHP available on your system. Please see the Laravel Docs for the minimal supported version.
Upon installing the loader, Illuminate/View is being pulled in using composer. To install composer on your system, please see getcomposer.org.
To begin, you'll need to install webpack-blade-native-loader
:
$ npm install webpack-blade-native-loader --save-dev
webpack.config.js
module.exports = {
module: {
rules: [
{
test: /\.blade\.php$/,
use: [
{
loader: 'webpack-blade-native-loader',
options: {
viewDir: 'resources/views'
}
}
],
},
],
},
};
This loader works great combined with HtmlWebpackPlugin. Here's an example how to set that up.
webpack.config.js
const pages = glob.sync('**/*.blade.php', {
cwd: path.join(process.cwd(), `resources/views/`),
root: '/',
})
.map((page) => new HtmlWebpackPlugin({
filename: page.replace('.blade.php', '.html'),
template: `resources/views/${page}`,
}));
module.exports.plugins.push(...pages);
This will render all of the blade files in the resources/views directory to HTML. If you're using partials, this might not be what you want. The updated example below filters out any views that contain an underscore in their paths. You can specify your own rules by modifying the filter statement or add more to suit your needs.
webpack.config.js
const pages = glob.sync('**/*.blade.php', {
cwd: path.join(process.cwd(), `resources/views/`),
root: '/',
})
// Filter out views that contain an underscore in their paths
.filter(filename => filename.indexOf('_') === -1)
.map((page) => new HtmlWebpackPlugin({
filename: page.replace('.blade.php', '.html'),
template: `resources/views/${page}`,
}));
module.exports.plugins.push(...pages);
Type: String
Default: ''
The path of the directory where template files are located.
The MIT License (MIT). Please see License File for more information.
This package is being maintained by Esign. We're a creative digital agency located in Ghent, Belgium.