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

Fixed PnP compatibility for bundled components package #18015

Merged
merged 1 commit into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/components/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@
"@storybook/theming": "6.5.0-beta.7",
"@types/react-syntax-highlighter": "11.0.5",
"core-js": "^3.8.2",
"qs": "^6.10.0",
"react-syntax-highlighter": "^15.4.5",
"regenerator-runtime": "^0.13.7"
"regenerator-runtime": "^0.13.7",
"util-deprecate": "^1.0.2"
},
"devDependencies": {
"@popperjs/core": "^2.6.0",
Expand All @@ -64,13 +66,11 @@
"polished": "^4.2.2",
"prettier": ">=2.2.1 <=2.3.0",
"prop-types": "^15.7.2",
"qs": "^6.10.0",
"react-colorful": "^5.1.2",
"react-popper-tooltip": "^3.1.1",
"react-textarea-autosize": "^8.3.0",
"ts-dedent": "^2.0.0",
"ts-node": "^10.4.0",
"util-deprecate": "^1.0.2"
"ts-node": "^10.4.0"
},
"peerDependencies": {
"react": "^16.8.0 || ^17.0.0 || ^18.0.0",
Expand Down
10 changes: 9 additions & 1 deletion scripts/bundle-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@ interface Options {
watch?: boolean;
}

const makeExternalPredicate = (externals: string[]) => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might matter for some packages so you don't end up bundling stuff like lodash/pickBy, because without a solution like this 'lodash' is treated as an external but all internal submodules are just bundled

if (externals.length === 0) {
return () => false;
}
const pattern = new RegExp(`^(${externals.join('|')})($|/)`);
return (id: string) => pattern.test(id);
};
Comment on lines +24 to +30
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this returns a fn?

I guess rollup supports this?
Why is this better?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this returns a fn?

yes, this returns a predicate function testing if a particular import source should be treated as external or not

I guess rollup supports this?

yes

Why is this better?

#18015 (comment) , basically without this you have such a situation:

import _ from 'lodash' // treated as external
import pickBy from 'lodash/pickBy' // bundled in your script

The external match is exact so anything that you import using a "deep import" will be bundled (by default). This kind of predicate fixes the problem.


async function build(options: Options) {
const { input, externals, cwd, optimized } = options;
const setting: RollupOptions = {
input,
external: externals,
external: makeExternalPredicate(externals),
plugins: [
nodeResolve({
preferBuiltins: true,
Expand Down