Skip to content

Commit

Permalink
Framework: Use CustomTemplatedPathPlugin updated for Webpack 4 (#5687)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Mar 22, 2018
1 parent eb7499f commit 9e13e1d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 51 deletions.
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
},
"devDependencies": {
"@wordpress/babel-preset-default": "1.1.1",
"@wordpress/custom-templated-path-webpack-plugin": "1.0.0",
"@wordpress/jest-preset-default": "1.0.3",
"@wordpress/scripts": "1.1.0",
"autoprefixer": "6.7.7",
Expand Down
71 changes: 20 additions & 51 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
*/
const ExtractTextPlugin = require( 'extract-text-webpack-plugin' );
const WebpackRTLPlugin = require( 'webpack-rtl-plugin' );
const { reduce, escapeRegExp, get } = require( 'lodash' );
const { get } = require( 'lodash' );
const { basename } = require( 'path' );

/**
* WordPress dependencies
*/
const CustomTemplatedPathPlugin = require( '@wordpress/custom-templated-path-webpack-plugin' );

// Main CSS loader for everything but blocks..
const mainCSSExtractTextPlugin = new ExtractTextPlugin( {
filename: './[basename]/build/style.css',
Expand Down Expand Up @@ -104,54 +109,6 @@ 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 = {
mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',

Expand All @@ -167,7 +124,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 @@ -224,7 +181,19 @@ const config = {
} ),
new CustomTemplatedPathPlugin( {
basename( path, data ) {
const rawRequest = get( data, [ 'chunk', 'entryModule', 'rawRequest' ] );
let rawRequest;

const entryModule = get( data, [ 'chunk', 'entryModule' ], {} );
switch ( entryModule.type ) {
case 'javascript/auto':
rawRequest = entryModule.rawRequest;
break;

case 'javascript/esm':
rawRequest = entryModule.rootModule.rawRequest;
break;
}

if ( rawRequest ) {
return basename( rawRequest );
}
Expand Down

0 comments on commit 9e13e1d

Please sign in to comment.