-
-
Notifications
You must be signed in to change notification settings - Fork 296
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(rollup-plugin-import-meta-assets): init plugin #553
feat(rollup-plugin-import-meta-assets): init plugin #553
Conversation
💥 No ChangesetLatest commit: c997b17 Merging this PR will not cause any packages to be released. If these changes should not cause updates to packages in this repo, this is fine 🙂 If these changes should be published to npm, you need to add a changeset. This PR includes no changesetsWhen changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types Click here to learn what changesets are, and how to add one. Click here if you're a maintainer who wants to add a changeset to this PR |
packages/rollup-plugin-import-meta-assets/src/rollup-plugin-import-meta-assets.js
Outdated
Show resolved
Hide resolved
packages/rollup-plugin-import-meta-assets/src/rollup-plugin-import-meta-assets.js
Outdated
Show resolved
Hide resolved
packages/rollup-plugin-import-meta-assets/src/rollup-plugin-import-meta-assets.js
Outdated
Show resolved
Hide resolved
const { base: assetName } = path.parse(absoluteAssetPath); | ||
|
||
try { | ||
const assetContents = await fs.promises.readFile(absoluteAssetPath); |
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.
need to check if this works on node v10
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.
Will do. Official docs says v10. Local test with latest v10 works.
@LarsDenBakker I fixed code and docs according to your review in an auto fixup commit so it's easier for you to see what I changed and we'll be able to rebase before merging. |
I think doing it outside of this plugin would be more powerful - especially as there might be other plugins that add assets as well. For example, we surely want to also support optimizations for files references in html like imho there should be an official rollup hook to transform assets (iirc currently there is none 🙈) ideally, you would at some point install |
@daKmoR Hey 😉
|
I filed a feature request on rollup for this: rollup/rollup#3778 In the meantime we have to do something. If we don't include a transform option, we still need to make this possible for users in some way. We either need to make a separate plugin for this, or we need to document for people how to do this manually: // untested code
export default {
plugins: [
{
generateBundle(options, bundle) {
for (const name of Object.keys(bundle)) {
if (name.endsWIth('.json') {
bundle[key] = JSON.stringify(JSON.parse(bundle[key].content));
}
}
}
}
]
} @daKmoR what do you think? |
from you 😬
jup
the rollup |
@daKmoR 😆 you're right, this explainer should not be part of this PR. It will be integrated in a reworked form in the guides. Sorry. About the |
@LarsDenBakker I tried your example with
It works for the content but the file hash is not updated. If rollup supported such a |
Good catch... we might need to use another hook like buildEnd. Not sure if they expose the information there. |
I don't see how we can make this work. I mean in |
@LarsDenBakker On another note, I'm using "snapshots" files to compare with the output of some of my tests. Seems like I need a |
Yes, go ahead. |
so hashing the filename and renaming it... should imho be part of a plugin and not part of rollup as it's probably out of scope (and we wanna keep the core small and performant I would say) the hashing + rename we already have in this plugin 👍 So overall I think this structure would cover multiple "use cases"
Combine all of thoseWant copy + svg optimization? Want hash + svg optimization? NameWriting it out like this I think a better name could be
Follow-upsHaving plugins to optimize svg, json, css, images? would be awesome |
|
I see the big picture you're drawing; I mostly agree with the set of decoupled specialized plugins you're proposing; but I don't see how this can be done properly without non trivial updates to Rollup. I don't have an advanced understanding of Rollup's inner behaviour so please correct me if I'm wrong. From reading the docs and the code, it seems like (right now) Rollup is tailored for ES modules but not for really for assets. Almost all asset plugins get away with it by importing assets as ES modules and producing ES modules exporting the asset as a string. I don't see how the features required to achieve this multiple plugins setup you're describing could land before a few months (or more). Therefore, I think we should ship this plugin as is, with the Now let me try to comment on your messages 😉
Well, Rollup already has a transform hook but as I said, it's only for modules. I think we need Rollup already knows what an asset is and has a dedicated config for how to rename and maybe hash them : output.assetFileNames. If you use
I don't think so. Rollup's core already handles this. I think Rollup should continue to handle this in core but maybe should open a bit more hooks and features related to assets.
This plugin has no logic related to hashing, it doesn't even know Rollup can do this.
I agree with this. If Rollup provides a
This is where I don't understand. It's about looking for
I agree with you that once Rollup ships a
I don't know much about
If Rollup provides a Personal taste: for the very case of optimizing SVGs, there are so much options that I'd rather use the hook directly with svgo and my set of options (one less "pass-through dependency). For other more plug-and-play situations with less options, I could use such plugins. That's my opinion on this but maybe it's biased because I spent some time working on it and it solves exactly the problem I have in its current form :-D |
You're right, rollup core does the hashing. Looking through the docs, I don't see any other way to get the emitted assets :( At the same time, you don't necessarily need to rely on the hashing of the file content after minifying. It might actually less ideal, because a change in minifier will bust the cache. @hsablonniere what do you think about documenting the |
@hsablonniere really good feedback and explanation - I think I understand it better now - thx 🤗 I agree that the hash of the original content should be good enough for now... so the is the name still open to discussion? 😬 as most of the things will be most likely called with file... e.g. |
First point, thanks for your feedbacks, this discussion and those details are important. It feels good to have challenging opinions on this 👍 Hashing and cache busting
I have a strong opinion on the fact that a change in the minifying config MUST bust the cache. I got bitten by this twice in the last 6 months. Once with terser and Leaftlet heatmap global behaviour; and once with the HTML minifier of LitElement templates. In both situation, I had to update the minifying config to fix the bug. You could say it's rare and it's not a really a problem for assets like CSS or SVG and I would agree. The other reason is the work I'm doing on our CDN stuffs. I intend to store all generated files with a hashed name on an S3 compatible object storage, without the version in the URL. This has many benefits but I need a strong "content is different => name is different". Just like you, I like aiming for "good enough" but I feel like my PR contains a solution that gives the best result with a good enough API (some plugin config duplication if you're using the HTML plugin which I'm not). It seems like you're proposing that we aim for the best API (no Waiting for Rollup
Looking at many Rollup issue threads yesterday, I don't have the feeling such a feature would be added quickly. Even if it did, users would have to update their Rollup to benefit from this and we're not sure it would land in semver minor. NOTE: My all "this will not happen" is a secret strategy to dare Luke and his team to ship this quickly 😉 Sorry if I sound like a broken record but I think we should ship this as is. We can start lobbying and working on a If such a hook lands tomorrow in core, users would have to update Rollup. That's a good opportunity for them to update their plugin configs and move away from the Documenting the generateBundle approachAs I explained in the previous sections, I don't believe in this approach so I don't feel like writing it myself. Feel free to add it but you've been warned that I think it's not a good suggestion to advocate. Naming
Of course, it's always open to discussion 😉 At this point I just want this to be shipped so I can use it in my projects so I don't care so much about the name but if you want my advice: This very plugin deals with assets. It calls See https://rollupjs.org/guide/en/#thisemitfileemittedfile-emittedchunk--emittedasset--string Rollup already has a I know some naming recently changed to group assets and chunks but we still have a dedicated
So I think |
); | ||
} | ||
|
||
function importMetaAssets({ include, exclude, warnOnError, transform } = {}) { |
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.
@LarsDenBakker there are no types right? why does the linting of types does not fail?
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 no idea. I'm new here 😆
Is there another plugin source I can get inspiration from?
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 to emit files without source, then I tried in a different plugin several hooks to see if I could set the source of an asset => failed. |
I think your points are fair enough so we can merge and iterate... still types & windows support is probably a blocker 😅 |
Well, I may have a proposition 😆
Yeah sure, I asked helped for windows support as I'm not sure what's happening and regarding types, are we just talking about some JSDocs? Do you have examples in the modern web repo? |
What I just achieved:
I tried all the ideas I had to make it work right now with what we have but I cannot do better than this. |
Windows seems to fail because of string comparisons and end of line differences. I'm not sure how to fix those. |
I think I found out the root of the pb for Windows 😌 Doing some grep in Rollup's code, it does not seem to honor Node.js Right now, the GitHub actions setup triggers the checkout with https://github.com/actions/checkout. The effect is, when I use We're not alone in this: actions/checkout#135 Comments suggest two ways of doing it. Adding steps in the action config like this seems better than adding a |
|
Let's try to discuss about this plugin 😉
Let me know what you think about this...