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

Improve plugin-vue output #1310

Merged
merged 2 commits into from
Oct 14, 2020
Merged

Improve plugin-vue output #1310

merged 2 commits into from
Oct 14, 2020

Conversation

drwpow
Copy link
Collaborator

@drwpow drwpow commented Oct 14, 2020

Changes

Before, we were sending .css files to Snowpack from every Vue file, even if the Vue file generated no styles. This PR changes that, and only sends styles to Snowpack when needed.

Here are screenshots of a Vue application that loads external styles:

Before (1 empty CSS file generated per-component)

Screen Shot 2020-10-14 at 12 00 33 PM

After (no empty CSS files generated)

Screen Shot 2020-10-14 at 12 15 08 PM

Basically, all the empty <style> tags were dozens of empty .css.proxy.js files being generated & loaded.

Testing

Tested manually, as seen from screenshots.

But you can also see the snapshot diff in the changes, where some empty CSS files were deleted (comment below)

Docs

@drwpow drwpow requested a review from a team as a code owner October 14, 2020 18:22
@vercel
Copy link

vercel bot commented Oct 14, 2020

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/pikapkg/snowpack/4m2tki5kc
✅ Preview: https://snowpack-git-drwpow-improve-plugin-vue.pikapkg.vercel.app

@@ -108,6 +108,10 @@ module.exports = function plugin(snowpackConfig) {
if (sourceMaps && js.map) output['.js'].map += JSON.stringify(js.map);
}

// clean up
if (!output['.js'].code) delete output['.js'];
if (!output['.css'].code) delete output['.css'];
Copy link
Collaborator Author

@drwpow drwpow Oct 14, 2020

Choose a reason for hiding this comment

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

This is a very non-controversial change here, that fixes Vue specifically.

I don’t believe other plugins had this problem—this was behavior caused by starting out with a blank code key to start, because multiple Vue things would then run in parallel to speed things up. But after everything ran we failed to check to make sure something was generated in every case (often it wasn’t)

@@ -84,6 +89,9 @@ async function runPipelineLoadStep(

// if source maps disabled, don’t return any
if (!sourceMaps) result[ext].map = undefined;

// clean up empty files
if (!result[ext].code) delete result[ext];
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@FredKSchott This, however, is a possibly-controversial change. This asks the question: should we rely on every plugin to clean up perfectly after itself? Or should Snowpack assume an empty file shouldn’t be generated?

If the latter, then maybe this is a good change to make? I don’t see this causing any issues, unless in some cases an empty file isn’t a mistake (though I can’t think of any of those scenarios off the top of my head)

Copy link
Owner

Choose a reason for hiding this comment

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

I thought that today we treated empty string as equivalent to a file that shouldn't be built. I think i saw this issue pop up in some other work that I was doing, so maybe we need to revisit this specifically. it's also different for a plugin that creates more than one file.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah so we did check that the plugin generated something, but we weren’t checking if it returned { '.js': { code: '' } }. This is the change here.

@@ -101,10 +109,14 @@ async function runPipelineLoadStep(
};
}

async function composeSourceMaps(id: string, base: string | RawSourceMap, derived: string | RawSourceMap) : Promise<string> {
async function composeSourceMaps(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ignore the other changes in this file; these are all just Prettier auto-formats.

".css": Object {
"code": "",
"map": "",
},
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hiding right under our noses! 🎉

Copy link
Owner

Choose a reason for hiding this comment

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

aha! Glad we have the individual test files now 👍

Copy link
Owner

@FredKSchott FredKSchott left a comment

Choose a reason for hiding this comment

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

LGTM!

@drwpow drwpow merged commit e5f73c5 into master Oct 14, 2020
@drwpow drwpow deleted the drwpow/improve-plugin-vue branch October 14, 2020 20:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants