Skip to content

Commit

Permalink
Build: Assign edit-post global as wp.editPost (#4966)
Browse files Browse the repository at this point in the history
* Build: Add Webpack plugin dirname tag transform

* Build: Assign edit-post library target as editPost

* Use basename on rawRequest for generating output directory

directory of resolved request is not sufficient because the resolved file of WordPress dependencies is within the `build-module` directory and would output as such. Instead format in such a way that we can call basename on the _raw_ requested path (e.g. `./editor` -> "editor", `./node_modules/@wordpress/hooks" -> "hooks")

* Build: Clarify intent of entry definition destructuring
  • Loading branch information
aduth authored Feb 12, 2018
1 parent b4d3132 commit 9f160d3
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ function gutenberg_editor_scripts_and_styles( $hook ) {
$script .= <<<JS
window._wpLoadGutenbergEditor = wp.api.init().then( function() {
wp.blocks.registerCoreBlocks();
return wp[ 'edit-post' ].initializeEditor( 'editor', window._wpGutenbergPost, editorSettings );
return wp.editPost.initializeEditor( 'editor', window._wpGutenbergPost, editorSettings );
} );
JS;
$script .= '} )();';
Expand Down
76 changes: 71 additions & 5 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
const webpack = require( 'webpack' );
const ExtractTextPlugin = require( 'extract-text-webpack-plugin' );
const WebpackRTLPlugin = require( 'webpack-rtl-plugin' );
const { reduce, escapeRegExp, castArray, get } = require( 'lodash' );
const { basename } = require( 'path' );

// Main CSS loader for everything but blocks..
const mainCSSExtractTextPlugin = new ExtractTextPlugin( {
filename: './[name]/build/style.css',
filename: './[basename]/build/style.css',
} );

// CSS loader for styles specific to block editing.
Expand Down Expand Up @@ -53,7 +55,7 @@ const entryPointNames = [
'i18n',
'utils',
'data',
'edit-post',
[ 'editPost', 'edit-post' ],
];

const packageNames = [
Expand All @@ -75,10 +77,64 @@ const externals = {
};
} );

/**
* Webpack plugin for handling specific template tags in Webpack configuration
* values like those supported in the base Webpack functionality (e.g. `name`).
*
* @see webpack.TemplatedPathPlugin
*/
class CustomTemplatedPathPlugin {
/**
* CustomTemplatedPathPlugin constructor. Initializes handlers as a tuple
* set of RegExp, handler, where the regular expression is used in matching
* a Webpack asset path.
*
* @param {Object.<string,Function>} 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 ) => {
Expand All @@ -87,7 +143,7 @@ const config = {
}, {} )
),
output: {
filename: '[name]/build/index.js',
filename: '[basename]/build/index.js',
path: __dirname,
library: [ 'wp', '[name]' ],
libraryTarget: 'this',
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 9f160d3

Please sign in to comment.