Skip to content

Commit

Permalink
Add ManifestPlugin to the webpack config.
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-manuilov committed Jul 29, 2020
1 parent 213f896 commit 7da74fe
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ vendor
!/php-scoper/composer.*
!/php-scoper/README.md
/third-party
/includes/Core/Assets/Manifest.php
release
phpunit.xml
.idea
Expand Down
41 changes: 40 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,38 @@ const TerserPlugin = require( 'terser-webpack-plugin' );
const WebpackBar = require( 'webpackbar' );
const { ProvidePlugin } = require( 'webpack' );
const FeatureFlagsPlugin = require( 'webpack-feature-flags-plugin' );
const ManifestPlugin = require( 'webpack-manifest-plugin' );

/**
* Internal dependencies
*/
const flagsConfig = require( './webpack.feature-flags.config' );

const projectPath = ( relativePath ) => {
return path.resolve( fs.realpathSync( process.cwd() ), relativePath );
};

const manifestTemplate = `<?php
/**
* Class Google\\Site_Kit\\Core\\Assets\\Manifest
*
* @package Google\Site_Kit
* @copyright ${ ( new Date() ).getFullYear() } Google LLC
* @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
* @link https://sitekit.withgoogle.com
*/
namespace Google\\Site_Kit\\Core\\Assets;
class Manifest {
public static $assets = array(
{{assets}}
);
}
`;

const noAMDParserRule = { parser: { amd: false } };

const siteKitExternals = {
Expand Down Expand Up @@ -124,7 +150,7 @@ const webpackConfig = ( mode ) => {
},
externals,
output: {
filename: '[name].js',
filename: '[name].[hash].js',
path: __dirname + '/dist/assets/js',
chunkFilename: '[name]-[chunkhash].js',
publicPath: '',
Expand Down Expand Up @@ -163,6 +189,19 @@ const webpackConfig = ( mode ) => {
mode,
},
),
new ManifestPlugin( {
fileName: '../../../includes/Core/Assets/Manifest.php',
serialize( manifest ) {
const files = [];
Object.keys( manifest ).forEach( ( key ) => {
if ( key.match( /.js$/ ) ) {
files.push( `"${ key.replace( '.js', '' ) }" => "${ manifest[ key ] }",` );
}
} );

return manifestTemplate.replace( '{{assets}}', files.join( '\n\t\t' ) );
},
} ),
],
optimization: {
minimizer: [
Expand Down

0 comments on commit 7da74fe

Please sign in to comment.