Skip to content

Commit

Permalink
Add outputFilename option
Browse files Browse the repository at this point in the history
Allows specifying the asset filename format separately from the output
filename format. This is useful when the output filenames include a
hash.
  • Loading branch information
stefanfisk committed Nov 24, 2021
1 parent 03149f9 commit a6f01f2
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 4 deletions.
7 changes: 7 additions & 0 deletions packages/dependency-extraction-webpack-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ module.exports = {

The output format for the generated asset file. There are two options available: 'php' or 'json'.

##### `outputFilename`

- Type: string | function
- Default: null

The filename for the generated asset file. Accepts the same values as the Webpack `output.filename` option.

##### `combineAssets`

- Type: boolean
Expand Down
24 changes: 20 additions & 4 deletions packages/dependency-extraction-webpack-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class DependencyExtractionWebpackPlugin {
externalizedReport: false,
injectPolyfill: false,
outputFormat: 'php',
outputFilename: null,
useDefaults: true,
},
options
Expand Down Expand Up @@ -143,6 +144,7 @@ class DependencyExtractionWebpackPlugin {
externalizedReport,
injectPolyfill,
outputFormat,
outputFilename,
} = this.options;

// Dump actually externalized dependencies to a report file.
Expand Down Expand Up @@ -228,10 +230,24 @@ class DependencyExtractionWebpackPlugin {
continue;
}

const assetFilename = buildFilename.replace(
/\.js$/i,
'.asset.' + ( outputFormat === 'php' ? 'php' : 'json' )
);
let assetFilename;

if ( outputFilename ) {
assetFilename = compilation.getPath( outputFilename, {
chunk: entrypointChunk,
filename,
query,
basename: basename( filename ),
contentHash: createHash( 'md4' )
.update( assetString )
.digest( 'hex' ),
} );
} else {
assetFilename = buildFilename.replace(
/\.js$/i,
'.asset.' + ( outputFormat === 'php' ? 'php' : 'json' )
);
}

// Add source and file into compilation for webpack to output.
compilation.assets[ assetFilename ] = new RawSource( assetString );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ declare interface DependencyExtractionWebpackPluginOptions {
injectPolyfill?: boolean;
useDefaults?: boolean;
outputFormat?: 'php' | 'json';
outputFilename?: string | Function,
requestToExternal?: ( request: string ) => string | string[] | undefined;
requestToHandle?: ( request: string ) => string | undefined;
combinedOutputFile?: string | null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,46 @@ exports[`Webpack \`no-deps\` should produce expected output: Asset file 'main.as

exports[`Webpack \`no-deps\` should produce expected output: External modules should match snapshot 1`] = `Array []`;

exports[`Webpack \`option-function-output-filename\` should produce expected output: Asset file 'chunk--main--main.asset.php' should match snapshot 1`] = `"<?php return array('dependencies' => array('lodash', 'wp-blob'), 'version' => '844c4b1f2a6db3a13416e9ea339e6334');"`;

exports[`Webpack \`option-function-output-filename\` should produce expected output: External modules should match snapshot 1`] = `
Array [
Object {
"externalType": "window",
"request": "lodash",
"userRequest": "lodash",
},
Object {
"externalType": "window",
"request": Array [
"wp",
"blob",
],
"userRequest": "@wordpress/blob",
},
]
`;

exports[`Webpack \`option-output-filename\` should produce expected output: Asset file 'main-foo.asset.php' should match snapshot 1`] = `"<?php return array('dependencies' => array('lodash', 'wp-blob'), 'version' => '844c4b1f2a6db3a13416e9ea339e6334');"`;

exports[`Webpack \`option-output-filename\` should produce expected output: External modules should match snapshot 1`] = `
Array [
Object {
"externalType": "window",
"request": "lodash",
"userRequest": "lodash",
},
Object {
"externalType": "window",
"request": Array [
"wp",
"blob",
],
"userRequest": "@wordpress/blob",
},
]
`;

exports[`Webpack \`output-format-json\` should produce expected output: Asset file 'main.asset.json' should match snapshot 1`] = `"{\\"dependencies\\":[\\"lodash\\"],\\"version\\":\\"c0593f49cf023b00b43d085327cd4e3b\\"}"`;

exports[`Webpack \`output-format-json\` should produce expected output: External modules should match snapshot 1`] = `
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* WordPress dependencies
*/
import { isBlobURL } from '@wordpress/blob';

/**
* External dependencies
*/
import _ from 'lodash';

_.isEmpty( isBlobURL( '' ) );
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Internal dependencies
*/
const DependencyExtractionWebpackPlugin = require( '../../..' );

module.exports = {
plugins: [
new DependencyExtractionWebpackPlugin( {
outputFilename( chunkData ) {
return `chunk--${ chunkData.chunk.name }--[name].asset.php`;
},
} ),
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* WordPress dependencies
*/
import { isBlobURL } from '@wordpress/blob';

/**
* External dependencies
*/
import _ from 'lodash';

_.isEmpty( isBlobURL( '' ) );
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Internal dependencies
*/
const DependencyExtractionWebpackPlugin = require( '../../..' );

module.exports = {
plugins: [
new DependencyExtractionWebpackPlugin( {
outputFilename: '[name]-foo.asset.php',
} ),
],
};

0 comments on commit a6f01f2

Please sign in to comment.