From 3d851b92f70707043a6b01922425acfdd2d9ed45 Mon Sep 17 00:00:00 2001 From: Andrew Duthie Date: Wed, 8 May 2019 17:53:52 -0400 Subject: [PATCH] Build Tooling: Update Webpack config to operate on source files --- webpack.config.js | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/webpack.config.js b/webpack.config.js index a8b11fd9aac6f9..c1725394d79802 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -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 @@ -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', @@ -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' ], {} ); @@ -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;