diff --git a/lib/client-assets.php b/lib/client-assets.php index 1bfe9c89da1545..f3ff9e23a4a113 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -895,7 +895,7 @@ function gutenberg_editor_scripts_and_styles( $hook ) { $script .= <<} handlers Object keyed by tag to match, + * with function value returning + * replacement string. + * + * @return {void} + */ + constructor( handlers ) { + this.handlers = reduce( handlers, ( result, handler, key ) => { + const regexp = new RegExp( `\\[${ escapeRegExp( key ) }\\]`, 'gi' ); + return [ ...result, [ regexp, handler ] ]; + }, [] ); + } + + /** + * Webpack plugin application logic. + * + * @param {Object} compiler Webpack compiler + * + * @return {void} + */ + apply( compiler ) { + compiler.plugin( 'compilation', ( compilation ) => { + compilation.mainTemplate.plugin( 'asset-path', ( path, data ) => { + for ( let i = 0; i < this.handlers.length; i++ ) { + const [ regexp, handler ] = this.handlers[ i ]; + if ( regexp.test( path ) ) { + return path.replace( regexp, handler( path, data ) ); + } + } + + return path; + } ); + } ); + } +} + const config = { entry: Object.assign( - entryPointNames.reduce( ( memo, entryPointName ) => { - memo[ entryPointName ] = `./${ entryPointName }/index.js`; + entryPointNames.reduce( ( memo, entryPoint ) => { + // Normalized entry point as an array of [ name, path ]. If a path + // is not explicitly defined, use the name. + entryPoint = castArray( entryPoint ); + const [ name, path = name ] = entryPoint; + + memo[ name ] = `./${ path }`; + return memo; }, {} ), packageNames.reduce( ( memo, packageName ) => { @@ -87,7 +143,7 @@ const config = { }, {} ) ), output: { - filename: '[name]/build/index.js', + filename: '[basename]/build/index.js', path: __dirname, library: [ 'wp', '[name]' ], libraryTarget: 'this', @@ -149,6 +205,16 @@ const config = { minimize: process.env.NODE_ENV === 'production', debug: process.env.NODE_ENV !== 'production', } ), + new CustomTemplatedPathPlugin( { + basename( path, data ) { + const rawRequest = get( data, [ 'chunk', 'entryModule', 'rawRequest' ] ); + if ( rawRequest ) { + return basename( rawRequest ); + } + + return path; + }, + } ), ], stats: { children: false,