Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a public bundledPackages/excludedExternals option to DEWP #45948

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions packages/dependency-extraction-webpack-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ For example:
// Source file entrypoint.js
import { Component } from '@wordpress/element';

// Webpack will produce the output output/entrypoint.js
// Webpack will produce the output `output/path/name.ext`
/* bundled JavaScript output */

// Webpack will also produce output/entrypoint.asset.php declaring script dependencies
// Webpack will also produce `output/path/name.asset.php` declaring script dependencies
<?php return array('dependencies' => array('wp-element'), 'version' => 'dd4c2dc50d046ed9d4c063a7ca95702f');
```

Expand Down Expand Up @@ -135,6 +135,22 @@ This option is useful only when the `combineAssets` option is enabled. It allows

Pass `useDefaults: false` to disable the default request handling.

##### `excludedExternals`

- Type: `array<string>`
- Default: `[ '@wordpress/icons', '@wordpress/interface' ]`

The list of dependencies to exclude from externalizing. The packages listed here will not reach `requestToExternal` callback.
You can provide a new or extend that list with
```javascript
new DependencyExtractionWebpackPlugin( {
excludedExternals: [
...DependencyExtractionWebpackPlugin.excludedExternals,
'@wordpress/components'
]
} )
```

##### `injectPolyfill`

- Type: boolean
Expand Down
11 changes: 11 additions & 0 deletions packages/dependency-extraction-webpack-plugin/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const { createHash } = webpack.util;
* Internal dependencies
*/
const {
EXCLUDED_EXTERNALS,
defaultRequestToExternal,
defaultRequestToHandle,
} = require( './util' );
Expand All @@ -23,6 +24,7 @@ class DependencyExtractionWebpackPlugin {
constructor( options ) {
this.options = Object.assign(
{
excludedExternals: this.constructor.excludedExternals,
combineAssets: false,
combinedOutputFile: null,
externalizedReport: false,
Expand Down Expand Up @@ -53,6 +55,9 @@ class DependencyExtractionWebpackPlugin {
}

externalizeWpDeps( _context, request, callback ) {
if ( this.options.excludedExternals.includes( request ) ) {
return callback();
}
let externalRequest;

// Handle via options.requestToExternal first.
Expand Down Expand Up @@ -277,4 +282,10 @@ class DependencyExtractionWebpackPlugin {
}
}

// Define a static member in the eslint's ES supported way.
/**
* Set of packages to be bundled regardless of `requestToExternal` result.
*/
DependencyExtractionWebpackPlugin.excludedExternals = EXCLUDED_EXTERNALS;

module.exports = DependencyExtractionWebpackPlugin;
7 changes: 2 additions & 5 deletions packages/dependency-extraction-webpack-plugin/lib/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const WORDPRESS_NAMESPACE = '@wordpress/';
const BUNDLED_PACKAGES = [ '@wordpress/icons', '@wordpress/interface' ];
const EXCLUDED_EXTERNALS = [ '@wordpress/icons', '@wordpress/interface' ];

/**
* Default request to global transformation
Expand Down Expand Up @@ -38,10 +38,6 @@ function defaultRequestToExternal( request ) {
return 'ReactRefreshRuntime';
}

if ( BUNDLED_PACKAGES.includes( request ) ) {
return undefined;
}

if ( request.startsWith( WORDPRESS_NAMESPACE ) ) {
return [
'wp',
Expand Down Expand Up @@ -93,6 +89,7 @@ function camelCaseDash( string ) {
}

module.exports = {
EXCLUDED_EXTERNALS,
camelCaseDash,
defaultRequestToExternal,
defaultRequestToHandle,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`DependencyExtractionWebpackPlugin Webpack \`bundled-packages\` should produce expected output: Asset file 'index.min.asset.php' should match snapshot 1`] = `
"<?php return array('dependencies' => array('react', 'wp-blob'), 'version' => '8df4ae5a12b83b01dbc3');
"
`;

exports[`DependencyExtractionWebpackPlugin Webpack \`bundled-packages\` should produce expected output: External modules should match snapshot 1`] = `
[
{
"externalType": "window",
"request": "React",
"userRequest": "react",
},
{
"externalType": "window",
"request": [
"wp",
"blob",
],
"userRequest": "@wordpress/blob",
},
]
`;

exports[`DependencyExtractionWebpackPlugin Webpack \`combine-assets\` should produce expected output: Asset file 'assets.php' should match snapshot 1`] = `
"<?php return array('fileA.js' => array('dependencies' => array('lodash', 'wp-blob'), 'version' => 'cbe985cf6e1a25d848e5'), 'fileB.js' => array('dependencies' => array('wp-token-list'), 'version' => '7f3970305cf0aecb54ab'));
"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* External dependencies
*/
import { ReactDOM } from 'react-dom';

/**
* WordPress dependencies
*/
import { isBlobURL } from '@wordpress/blob';

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

module.exports = {
output: {
filename: 'index.min.js',
},
plugins: [
new DependencyExtractionWebpackPlugin( {
excludedExternals: [ 'react-dom' ],
} ),
],
};
13 changes: 13 additions & 0 deletions packages/dependency-extraction-webpack-plugin/test/statics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Internal dependencies
*/
const DependencyExtractionWebpackPlugin = require( '../lib/index' );

describe( 'DependencyExtractionWebpackPlugin', () => {
test( 'should have .excludedExternals static property', () => {
expect( DependencyExtractionWebpackPlugin ).toHaveProperty(
'excludedExternals',
[ '@wordpress/icons', '@wordpress/interface' ]
);
} );
} );