@iamota/iamota-webpack
Version:
Helper tools for Webpack for iamota Framework (WordPress and Shopify)
140 lines (124 loc) • 4.54 kB
JavaScript
// ----------------------------------------------------------------------------
// Webpack WordPress Config
// ----------------------------------------------------------------------------
//
// Webpack configuration for pre-processing WordPress-related files
//
// ----------------------------------------------------------------------------
/**
* Load Dependencies
*/
const { merge } = require('webpack-merge');
const path = require('path');
const ConcatPlugin = require('webpack-concat-files-plugin');
const CopyPlugin = require('copy-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
/**
* Initialize Config
*/
let config = {};
exports.init = function(config) {
this.config = config;
return this;
};
/**
* Process WordPress Tasks
*/
exports.process = function() {
return merge([
this.concatI18nConfigs(),
this.concatPhpConfigs(),
this.copyAcfJSON(),
]);
};
/**
* Concatenate PHP Config Files
*/
exports.concatPhpConfigs = () => ({
plugins: [
new ConcatPlugin({
bundles: [
{
source: 'resources/vendor/component-library/**/*.config.php',
destination: 'resources/vendor/component-library/Config/iamotaComponentLibraryComponentConfig.php',
transforms: {
// Create a reference to each config file
before: (src, filepath) => {
return "require_once($app_config['base_path'].'/" + filepath + "');";
},
// Prefix a header to the concatentated files
after: (src) => {
return '<?php // Generated by Webpack:concatPhpConfigs\n\n' + src;
},
},
}
]
}),
],
});
/**
* Concatenate PHP i18n Files
*/
exports.concatI18nConfigs = () => ({
plugins: [
new ConcatPlugin({
bundles: [
{
source: 'resources/vendor/component-library/**/*.i18n.php',
destination: 'resources/vendor/component-library/Config/iamotaComponentLibraryI18nConfig.php',
transforms: {
// Transform the contents of each PHP file
before: (src, filepath) => {
return '\n// ==================================================================' +
'\n// Source: ' + filepath +
'\n// ==================================================================' +
'\n\n' +
'\nadd_action(\'pll_pre_init\', function() {' +
'\n' + src.replace(/(^|\n)[ \t]*(<\?php)?\s*/g, '$1') +
'\n});' +
'\n\n';
},
// Prefix a header to the concatentated files
after: (src) => {
return '<?php // Generated by Webpack:concatI18nConfigs\n\n' + src;
},
},
}
]
}),
],
});
/**
* Copy ACF JSON Files from the Resources folder to a Single Folder
* This improves PHP performance vs. crawling to find all of the JSON files at compile time
*/
exports.copyAcfJSON = ({
destinationPath = path.resolve(this.config.projectPath, 'app/Acf/Components')
} = {}) => ({
plugins: [
// Copy JSON Files
new CopyPlugin({
patterns: [
{
from: 'resources/**/group_*.json',
to: destinationPath,
flatten: true,
}
]
}),
// Clean the ACF JSON files between builds
new CleanWebpackPlugin({
verbose: false,
dry: false, // Dry run (report what would be deleted without following through)
cleanOnceBeforeBuildPatterns: [
destinationPath
]
}),
],
// Exclude ACF JSON files from the "emitted" output (overly verbose since there are so many)
devServer: {
stats : {
excludeAssets: [/.*\/group_.*\.json/]
}
}
});