The Wayback Machine - https://web.archive.org/web/20210313041412/https://github.com/vuejs/vue-devtools/issues/1382
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

feat: show unused components loaded on page #1382

Open
privatenumber opened this issue Mar 13, 2021 · 0 comments
Open

feat: show unused components loaded on page #1382

privatenumber opened this issue Mar 13, 2021 · 0 comments

Comments

@privatenumber
Copy link

@privatenumber privatenumber commented Mar 13, 2021

What problem does this feature solve?

The app I'm working with has a 1 MB+ entry-point and Chrome's coverage feature indicated 30%+ of it was unused for first paint.

To defer non-critical code with code-splits, I wanted to see all the components that are declared but not initialized within the first few seconds (or as I used the app).

This is pretty scrappy, but I was able to accomplish this by modifying my local copy of vue-loader and inserting the following after vue-loader/lib/index.js:L154:

if (!window._uninitializedComponents) {
    window._uninitializedComponents = {};
}

window._uninitializedComponents[${JSON.stringify(rawShortFilePath)}] = {
    importedBy: ${JSON.stringify(path.relative(context, loaderContext._module.issuer.resource.split('?')[0]))},
    script,
};

const { created } = script;
script.created = function () {
    delete window._uninitializedComponents[${JSON.stringify(rawShortFilePath)}];

    if (created) {
        created.apply(this, arguments);
    }
};

Log window._uninitializedComponents on page load, or as you use the app, to see declared components that haven't been used.

In my case, there were 134 components unused on first paint. Upon investigating, I found that the app just had a lot of v-if/v-elses that really added up. There were also cases where tree-shaking didn't work. (eg. index.js that aggregates and exports multiple components, but only one is imported.) There weren't any cases of "registered but unused" or "imported but unregistered" components thanks to eslint-plugin-vue, but this should catch them too.

I haven't finished going through all of them yet, but will update if I find more patterns.

I feel like this insight would be valuable in Vue Devtools (and easy to add) to help developers be more aware of delivering critical code and making better decisions about where to add split points.

What does the proposed API look like?

A tab with a list of components that are declared but unused. Clicking on one would show me details about the component, and where it's imported. It would also be nice if I can keep going up the dependency-tree to find the biggest unused subtree so I know the best place to add the code-split.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant