Skip to content

Commit

Permalink
Build Tooling: Update Webpack config to operate on source files
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed May 8, 2019
1 parent 4e1ca13 commit 3d851b9
Showing 1 changed file with 20 additions and 5 deletions.
25 changes: 20 additions & 5 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const WebpackRTLPlugin = require( 'webpack-rtl-plugin' );
const CopyWebpackPlugin = require( 'copy-webpack-plugin' );
const postcss = require( 'postcss' );
const { get, escapeRegExp } = require( 'lodash' );
const { basename, sep } = require( 'path' );
const { basename, sep, join } = require( 'path' );
const { statSync } = require( 'fs' );

/**
* WordPress dependencies
Expand All @@ -31,11 +32,20 @@ module.exports = {
...defaultConfig,
entry: gutenbergPackages.reduce( ( memo, packageName ) => {
const name = camelCaseDash( packageName );
memo[ name ] = `./packages/${ packageName }`;

let path = `./packages/${ packageName }`;
try {
// Operate on source files, if exists. Since statSync will throw if
// the path doesn't exist, append will only occur if it does exist.
statSync( `${ path }/src` );
path += '/src';
} catch ( error ) {}

memo[ name ] = path;
return memo;
}, {} ),
output: {
filename: './build/[basename]/index.js',
filename: './build/[package]/index.js',
path: __dirname,
library: [ 'wp', '[name]' ],
libraryTarget: 'this',
Expand All @@ -53,7 +63,7 @@ module.exports = {
minify: defaultConfig.mode === 'production' ? { safe: true } : false,
} ),
new CustomTemplatedPathPlugin( {
basename( path, data ) {
package( path, data ) {
let rawRequest;

const entryModule = get( data, [ 'chunk', 'entryModule' ], {} );
Expand All @@ -68,7 +78,12 @@ module.exports = {
}

if ( rawRequest ) {
return basename( rawRequest );
let packageName = basename( rawRequest );
if ( packageName === 'src' ) {
packageName = basename( join( rawRequest, '..' ) );
}

return packageName;
}

return path;
Expand Down

0 comments on commit 3d851b9

Please sign in to comment.