-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
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
[codemod] Add codemods for optimal tree-shakeable imports #16192
[codemod] Add codemods for optimal tree-shakeable imports #16192
Conversation
jedwards1211
commented
Jun 12, 2019
- I have followed (at least) the PR section of the contributing guide.
We don't need this codemod for anything below 4.x. I think this is currently a bit overzealous. A good heuristic would not change any code documented in our docs. |
7d24e4b
to
2abc72e
Compare
@eps1lon I'm planning to make a separate PR for a codemod that preserves 2nd-level subpaths. Having multiple options seems to be leading to unnecessary debate about where to import from. If you don't want this one then I'll have to publish it in its own repository. |
Also, would this not be useful for 3.x? I assume that the tree-shaking problem applies to at least 3.x as well as 4.x. |
2abc72e
to
80bb909
Compare
Yeah now that I think about this might be better since 3.x only has tree-shaking for top level imports. Only 4.x supports tree shaking for second level imports. We should test this on the 3.x docs code and see if it still compiles though. It needs an exception for |
No bundle size changes comparing 2853239...a0ef850 |
@eps1lon theoretically 3.x should be handled properly and The separate 2nd-level-imports codemod I'm making (maybe I should call it |
555f43e
to
9cd40f6
Compare
Okay I rebased this onto I had to do the following hack to get the path to const whitelist = getTSExports(process.env.TEST
? require.resolve('../../../material-ui/src/index.d.ts')
: require.resolve(`${importModule}/index.d.ts`, {
paths: [dirname(fileInfo.source)],
})
); |
I don't think this should parse the TS files. For one it requires |
Do we all agree that this pull request should target the master branch? |
@oliviertassinari as discussed above this codemod is useful for 3.x...but maybe you have better ideas about how to release it. |
@eps1lon that's a good point, I'll do that |
I don't understand the logic. From what I understand:
|
It should target master. The codemod however should only be used for 3.x and below. I meant we should only test it manually on 3.x not target at 3.x |
@oliviertassinari I'm so confused, |
9cd40f6
to
5b2ac0c
Compare
okay rebased onto master |
|
@eps1lon gotcha. I just added a |
let me know if there are any other codemods I could help write...I love writing codemods |
not sure what's up with the argos difference |
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.
Test look good to me. Just some minor questions/suggestions about the implementation.
import addImports from 'jscodeshift-add-imports'; | ||
|
||
// istanbul ignore next | ||
if (process.env.NODE_ENV === 'test') { |
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.
We shouldn't publish env branches unless they compare with production.
You should be able to get the esModules index barrel by reading @material-ui/core/package.json
and using the module
entry. Then you can use main
as a fallback to account for test runs in the monorepo.
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 don't understand about branches, i'm checking if it's test mode, not if it's a branch
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.
Oh you mean a branch statement, not a code branch.
How do you want me to get the import path in testing? babel-plugin-module-resolver
doesn't work for for dynamic require.resolve statements. Mocking require.resolve outside of this file won't work AFAIK. Maybe using a define plugin to inject some code in test mode?
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.
Btw reading the package json and using it in require.resolve on subpaths still won't work in test mode. Unlike the v3 codemod, this one parses the 2nd-level files to build whitelists, rather than the top-level file
@@ -0,0 +1,12 @@ | |||
const memoize = (func, resolver = a => a) => { |
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.
yarn add -D -W memoize-one
It's already in node_modules from 4 other dependencies
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.
Unfortunately memoize-one
is not great for this, as it only remembers the most recent arguments. With this impl I assume cached info from parsing will stay in memory from one file being transformed to the next. (If passing a dir to jscodeshift instead of the strange usage of find/xargs, at least)
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.
Code size isn't a major concern for the codemods package is it?
@@ -0,0 +1,22 @@ | |||
import memoize from './memoize'; | |||
import { readFileSync } from 'fs'; | |||
import { parseSync } from '@babel/core'; |
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.
Can we use the jscodeshift API instead i.e. api.jscodeshift(muiCoreIndexSource).findExportSpecifier()
(no idea if that find method exists). I guess it's not guarenteed that the parser config of the user can also read node_modules source but that's also not guarenteed in this form since we don't publish our babel config anyway.
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 tried that, got parse errors on the export from
statements...perhaps I could build this list at compile time?
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.
packages/material-ui/package.json
doesn't have module
or main
entries, otherwise i would have done what you suggest. Should i add them? Or do they get added at build time?
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.
Since the babel config is not published, is it up to someone importing from es/
to set the right babel options, or do you transpile at least things like export from
statements in the build?
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.
@eps1lon I still want to finish up this PR, how do you want me to resolve this?
Rebased. |
d2dafaa
to
51a064a
Compare
55946a3
to
2237fe0
Compare
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 have rebased on the master branch, fix the markdown and move the two codemod under the v4 folder. From my end, it's good enough to ship and to put between our user's hand.
4a0d3f8
to
604fffe
Compare
604fffe
to
a0ef850
Compare
Guys, I'm a little bit confused with those two options. |
@Blinnikov To sum-up:
|
To clarify: This is slow for cold starts of development builds or when building for production. It has no impact on the consumer of your production bundles. This situation will improve the more advanced build caches get (webpack is currently working on it). |