Skip to content

Commit

Permalink
Update the way externals are handled
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Feb 13, 2019
1 parent 2675d83 commit ac86224
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions packages/webpack-config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,37 @@ const LiveReloadPlugin = require( 'webpack-livereload-plugin' );
const { camelCaseDash } = require( './utils' );

/**
* Converts @wordpress require into window reference.
* Converts @wordpress/* string request into request object.
*
* Note this isn't the same as camel case because of the
* way that numbers don't trigger the capitalized next letter.
*
* @example
* wordpressRequire( '@wordpress/api-fetch' ) = 'wp.apiFetch'
* wordpressRequire( '@wordpress/i18n' ) = 'wp.i18n'
* formatRequest( '@wordpress/api-fetch' );
* // { this: [ 'wp', 'apiFetch' ] }
* formatRequest( '@wordpress/i18n' );
* // { this: [ 'wp', 'i18n' ] }
*
* @param {string} request import name
* @return {string} global variable reference for import
* @param {string} request Request name from import statement.
* @return {Object} Request object formatted for further processing.
*/
const wordpressRequire = ( request ) => {
// @wordpress/components -> [ @wordpress, components ]
const formatRequest = ( request ) => {
// '@wordpress/api-fetch' -> [ '@wordpress', 'api-fetch' ]
const [ , name ] = request.split( '/' );

// components -> wp.components
return `wp.${ camelCaseDash( name ) }`;
// { this: [ 'wp', 'apiFetch' ] }
return {
this: [ 'wp', camelCaseDash( name ) ],
};
};

const wordpressExternals = ( context, request, callback ) =>
/^@wordpress\//.test( request ) ?
callback( null, `root ${ wordpressRequire( request ) }` ) :
const wordpressExternals = ( context, request, callback ) => {
if ( /^@wordpress\//.test( request ) ) {
callback( null, formatRequest( request ), 'this' );
} else {
callback();
}
};

const externals = [
{
Expand Down

0 comments on commit ac86224

Please sign in to comment.