-
So there were 2 reasons why we decided to use Laravel Mix and NPM in Backpack 4.0:
We maintainers took the burden of using NPM behind the scenes, so that end-developer, Backpack developers, don't have to. We pull all the dependencies, compile them and spit out the
So it's an inconvenience. A problem. There's been talk of symlinking the But. What if there was another way to achieve the same thing? One that didn't involve using NPM? One that was as easy as loading files from CDNs, like we used to do in Backpack v3, but as secure as loading them from a local directory? And it would solve both the maintainer problem and the developer problem? Here's what I've thought of: Premise:
Solution:
🤯 💥 🎉 Mind. Blown. Right? This seems such a simple solution, that I don't know why we haven't thought of this before. Why nobody has thought of this before - I looked around, and it doesn't seem like anybody has built anything like this. Which makes me wonder... is this brilliant... or stupid? Am I missing something huge? Something that makes this a no-go? Does anybody see a problem with this? Possible ways to do this: I like option B a lot better, if it's technically possible. I'm open to alternative implementations, of course. Either way this would mean:
Using a helper like this would also make it possible to have a config file, so that developer can choose:
-- Possible problems:
Does anybody see other problems with this? Am I missing something or would this be a really really cool way of never having to deal with NPM ever again? Feedback is very much appreciated - I'm excited about this right now, but it wouldn't be the first brilliant idea that turns out to be stupid 🙄 PS. I use the word "localize" here a lot - and it might be confusing, but it has NOTHING to do with translation - how it's sometimes used. It's about making a file LOCAL instead of EXTERNAL. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 4 replies
-
This should be a separate package. People should be able to use this even if they don't use Backpack - and I think many will. I'm thinking something like To sum it up, here are all its features: Easily localize files from public CDNs
Blade syntax for loading CSS and JS assets (external or internal)
Easily make sure the same assets are never loaded twice per page - PR Laravel-Backpack/CRUD#2652
Improve performance by loading small CSS/JS assets inline
Improve performance by deferring loading non-critical CSS files
|
Beta Was this translation helpful? Give feedback.
-
Sounds like a greate idea. I do however see one problem, the helper needs to be quite "smart". For example, how to deal with CSS files wich use additional files, like some fonts. |
Beta Was this translation helpful? Give feedback.
-
Ugh that's right! That's one thing I didn't think of, thank you @martijnb92 ! |
Beta Was this translation helpful? Give feedback.
-
Guess I'm kinda late into the story, but here's my view. I think what's being described here is very similar to proxy caching. I don't think it's a bad idea, but I think it comes with an added complexity that might not be obvious at first sight. E.g.
I'd also be a little hesitant about introducing more non-standard Blade directives. I think this would make contributions to the project a bit harder. My suggestionsBackpack should only come with assets necessary for its running -- their minimized versions at least.
Serve assets via PHP in dev. Use a symlink/copy in prod.When in dev, why not simply serve assets through a custom 'assets' controller? That way we don't have to export anything, not even publish defaults into Then in prod, I'd suggest doing something like Just for the record, I still think (#2246) that going with the NPM/Laravel Mix flow like laravel/ui is the way to go. I understand why you may want to keep developers "shielded" from npm, but Laravel currently doesn't offer anything better IMHO (not that what it offers is bad -- I actually think the Mix integration is really good.). Beyond minimization, webpack (Mix) offers things like tree shaking or lazy loading which, when used correctly (and with a bit of black magic, admittedly) can make asset serving very efficient. |
Beta Was this translation helpful? Give feedback.
-
@tabacitu - Your reply got me thinking, I really like the idea but it could probably be done even simpler. I made this example helper function which returns an asset by a "key", incase someone wants to use their own asset they can easily overwrite it via the base config file.
<?php
function backpack_asset($asset){
//Create the overwrite asset array from the config file.
$overwrite_asset = config("backpack.base.overwrite_asset", []);
// Partial example list with some assets, might need a better location. (datatables assets are already combined into one instead of multiple)
$asset_list = [
"momentjs" => "//cdnjs.cloudflare.com/ajax/libs/moment.js/2.27.0/moment.min.js",
"jquery" => "//code.jquery.com/jquery-3.5.1.min.js",
"datatables.net" => "//cdn.datatables.net/v/bs4/dt-1.10.21/fh-3.1.7/r-2.2.5/datatables.min.js",
"datatables.net-css"=> "//cdn.datatables.net/v/bs4/dt-1.10.21/fh-3.1.7/r-2.2.5/datatables.min.css",
];
//Merge the overwrite assets, in case someone depends on custom assets they probably have and advanced use case.
//This allows them to define a custom path.
$asset_list = array_replace($asset_list,$overwrite_asset);
//Check if the requested asset exists otherwise return the supplied variable (complete urls e.g.)
return (array_key_exists($asset,$asset_list)) ? $asset_list[$asset] : $asset;
}
?> This: And this:
Would become: More examples: This will be usefull for both JS and CSS files, we might need come up with a efficient naming scheme. This would:
This would not:
|
Beta Was this translation helpful? Give feedback.
-
Is this https://github.com/Laravel-Backpack/basset ? 👀 |
Beta Was this translation helpful? Give feedback.
Is this https://github.com/Laravel-Backpack/basset ? 👀