Skip to content

Instantly share code, notes, and snippets.

@mdalaminbey
Created January 20, 2024 17:42
Show Gist options
  • Select an option

  • Save mdalaminbey/5ce443c7d5a7bc40c85468decaf1c383 to your computer and use it in GitHub Desktop.

Select an option

Save mdalaminbey/5ce443c7d5a7bc40c85468decaf1c383 to your computer and use it in GitHub Desktop.
webpack.config.js
const path = require( 'path' );
const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const DependencyExtractionWebpackPlugin = require( '@wordpress/dependency-extraction-webpack-plugin' );
const HELPGENT_NAMESPACE = '@helpgent/';
/**
* Given a string, returns a new string with dash separators converted to
* camelCase equivalent. This is not as aggressive as `_.camelCase` in
* converting to uppercase, where Lodash will also capitalize letters
* following numbers.
*
* @param {string} string Input dash-delimited string.
* @return {string} Camel-cased string.
*/
function camelCaseDash( string ) {
return string.replace( /-([a-z])/g, ( _, letter ) => letter.toUpperCase() );
}
const chunkUniqueKey = Date.now().toString();
module.exports = {
...defaultConfig,
entry: {
'js/index': './resources/js/admin/index.js',
'js/frontend/ChatForm/index':
'./resources/js/frontend/pages/ChatForm/index.js',
'js/frontend/ChatBubble/index':
'./resources/js/frontend/pages/ChatBubble/index.js',
'js/frontend/UserDashboard/index':
'./resources/js/frontend/pages/UserDashboard/index.js',
'js/notification': './resources/js/components/Notification.js',
'css/notification': './resources/js/components/notification.scss',
'js/queryStore': './resources/js/queryStore/index.js',
'css/global': './resources/sass/global.scss',
modules: './resources/js/modules/index.js',
components: './resources/js/components/index.js',
// elements: './resources/js/elements.js',
'hooks/index': './resources/js/hooks/index.js',
},
output: {
path: path.resolve( __dirname, './assets/build/' ),
filename: '[name].js',
chunkFilename: '[name]' + '.js?ver=' + chunkUniqueKey,
clean: false,
},
plugins: [
...defaultConfig.plugins.filter(
( plugin ) =>
plugin.constructor.name !== 'DependencyExtractionWebpackPlugin'
),
new DependencyExtractionWebpackPlugin( {
requestToExternal( request ) {
if (
'@helpgent/modules' === request ||
'@helpgent/components' === request ||
'@helpgent/hooks' === request
// '@helpgent/elements' === request
) {
return [
'helpgent',
camelCaseDash(
request.substring( HELPGENT_NAMESPACE.length )
),
];
}
},
requestToHandle( request ) {
if (
'@helpgent/modules' === request ||
'@helpgent/components' === request ||
'@helpgent/hooks' === request
// '@helpgent/elements' === request
) {
return `helpgent/${ camelCaseDash(
request.substring( HELPGENT_NAMESPACE.length )
) }`;
}
},
} ),
],
resolve: {
alias: {
'@helpgent': path.resolve( __dirname, 'resources/js' ),
'@icon': path.resolve( __dirname, 'resources/svg/icon' ),
'@assets': path.resolve( __dirname, 'assets' ),
},
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment