-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Build: Assign edit-post global as wp.editPost
#4966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
6239d53
3e9cb17
81a366c
d3a43bf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -53,7 +55,7 @@ const entryPointNames = [ | |
| 'i18n', | ||
| 'utils', | ||
| 'data', | ||
| 'edit-post', | ||
| [ 'editPost', 'edit-post' ], | ||
| ]; | ||
|
|
||
| const packageNames = [ | ||
|
|
@@ -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} | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need to explicitly document that function doesn't return anything? Wouldn't it enough to assume that a function by default doesn't return value?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There's some precedent of this in existing core documentation. In general I'd be inclined to agree with you that it could be omitted for
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Docs again? Count me in ❤️ |
||
| */ | ||
| 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, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we plan to extract it and move to
WordPress/packagesat some point? It might be useful for other repositories.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, seems like a generally useful plugin.