-
Notifications
You must be signed in to change notification settings - Fork 142
Add tree shaking support for woocommerce components #7034
Conversation
| async function buildPackageScss( packagePath ) { | ||
| const srcDir = path.resolve( packagePath, SRC_DIR ); | ||
| const scssFiles = glob.sync( `${ srcDir }/*.scss` ); | ||
| const scssFiles = glob.sync( `${ srcDir }/**/*.scss` ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way we generate the css for the individual package components as well.
| "sideEffects": [ | ||
| "build-style/**", | ||
| "src/**/*.scss" | ||
| ], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I stole this from @wordpress/components, but it seemed to match our use case pretty well.
| * @return {string} Camel-cased string. | ||
| */ | ||
| function camelCaseDash( string ) { | ||
| return string.replace( /-([a-z])/g, ( _, letter ) => letter.toUpperCase() ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes allowed me to more easily exclude a package if needed by doing this:
new WooCommerceDependencyExtractionWebpackPlugin( {
excludedExternals: [ 'components' ],
} ),
| { | ||
| test: /\.s?css$/, | ||
| exclude: /storybook\/wordpress/, | ||
| exclude: [ /storybook\/wordpress/, /build-style\/*\/*.css/ ], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this to not include all the individual component styles as well. Not sure how much of a difference it makes (or if I did it correctly).
87ec791 to
1c16db6
Compare
| # Unreleased | ||
|
|
||
| - Add tree shaking support to this package. #7034 | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we go ahead with this we probably want to do a major release (7.0.0)
|
@samueljseay & @psealock I added both of you, given you guy's might be most interested in these changes, let me know what you think? |
| /** | ||
| * External dependencies | ||
| */ | ||
| import { useContext } from 'react'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should be importing this from @wordpress/element
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated here: e44d096
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I decided to test this with a brand new plugin using npm run create-wc-extension. All of the @woocommerce/components get bundled unfortunately, but I may not be doing it right. Here are my steps, but I excluded changes to babel-loader because I didn't quite understand what was needed there.
npm run create-wc-extensionto get a new extensionnpm linkthe dependency extraction webpack plugin so that the new extension can get the version in this branch- In my extension,
npm install npm install --save @woocommerce/componentsnpm link @woocommerce/dependency-extraction-webpack-plugin- Add the new option to exclude externals
excludedExternals: [ 'components' ], npm run buildBuild a production version
My extension's js looks like:
import { Count } from '@woocommerce/components';
console.log( Count );Then I check the build file, build/index.js and its pretty clear all the components got included. Can you advise what steps might be missing?
| } | ||
|
|
||
| const wooRequestToExternal = ( request ) => { | ||
| const wooRequestToExternal = ( request, excludedExternals ) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
excludedExternals is a new option value, so will need to be reflected in the README
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now that I think about it, this should really be upstream in Gutenberg as well https://github.com/WordPress/gutenberg/tree/trunk/packages/dependency-extraction-webpack-plugin if you want to make a PR there.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to clarify: We should keep the changes in this PR (with README updates), but also support the excludedExternals param in the original plugin within Gutenberg?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, thats right
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created a PR, I will just update this PR to follow the format I went with here: WordPress/gutenberg#32220
@psealock I just followed your steps, with the only additional step being |
50b649e to
221140c
Compare
psealock
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you forgot the npm link @woocommerce/components step?
Yup, that was the one. Thanks @louwie17 this is testing well for me and is a big improvement for @woocommerce/components
221140c to
23c2cd1
Compare
…ommerce-admin#7034) * Add woocommerce/components tree shaking support * Compile individual component styles for individual use * Update readme * Add changelog * Fix correct import * Update readme with dependency extraction updates * Update dependency extraction naming
Fixes #7024
This adds tree shaking support to the
@woocommerce/componentspackage, allowing you to only bundle in the used components.The biggest changes are the
sideEffectsaddition in package.json, and the update inextensions/build.jsto also build the style for each individual component if used outside of WooCommerce.For the rest none of the components really looked like they had
sideEffects, and I only updated a couple components that where in the same file.No changelog as it is in the package.
Detailed test instructions:
The easiest way of testing this is by making use of the extension examples, just have to update the webpack config for it a bit:
Update the
extensions/examples.config.jsby setting the mode toproduction(enabled tree shaking) adding components to theexcludedExternalslist for the extraction plugin:And I also had to set
looseto false for the babel-loader (I had some warnings show up), and disabled minification:npm run example -- --ext=add-taskadding awoocommerce/componentscomponent to it somewhere. (The Spinner for example).dist/index.jsfile to see if only the imported component and it's dependencies are added, I used a grep command:grep -r 'CONCATENATED MODULE: ./packages/components' docs/examples/extensions/add-task/distindex.scss(you might have to enqueue it as well, if it wasn't there before)Going with the spinner example:
@import '../../../../../packages/components/build-style/spinner/style.css';