Skip to content

Commit 7f1a247

Browse files
committed
📦 NEW: Externals for WP + DRY
1 parent 5738f69 commit 7f1a247

1 file changed

Lines changed: 46 additions & 0 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Utility methods for use when generating build configuration objects.
3+
*/
4+
const { join } = require( 'path' );
5+
6+
/**
7+
* Given a string, returns a new string with dash separators converted to
8+
* camel-case equivalent. This is not as aggressive as `_.camelCase`, which
9+
* which would also upper-case letters following numbers.
10+
*
11+
* @param {string} string Input dash-delimited string.
12+
*
13+
* @return {string} Camel-cased string.
14+
*/
15+
const camelCaseDash = string => string.replace( /-([a-z])/g, ( match, letter ) => letter.toUpperCase() );
16+
17+
/**
18+
* Define externals to load components through the wp global.
19+
*/
20+
const externals = [
21+
'components',
22+
'edit-post',
23+
'element',
24+
'plugins',
25+
'editor',
26+
'blocks',
27+
'utils',
28+
'date',
29+
'data',
30+
'i18n',
31+
].reduce(
32+
( externals, name ) => ( {
33+
...externals,
34+
[ `@wordpress/${ name }` ]: `wp.${ camelCaseDash( name ) }`,
35+
} ),
36+
{
37+
wp: 'wp',
38+
ga: 'ga', // Old Google Analytics.
39+
gtag: 'gtag', // New Google Analytics.
40+
react: 'React', // React itself is there in Gutenberg.
41+
jquery: 'jQuery', // import $ from 'jquery' // Use the WordPress version after enqueuing it.
42+
'react-dom': 'ReactDOM',
43+
}
44+
);
45+
46+
module.exports = externals;

0 commit comments

Comments
 (0)