How to bundle different js and css files per controller action/view? - asp.net-core

I have site-wide js file (site.js) that goes out with every request.
I have 2 actions on the controller: Action1 and Action2. Each of these actions has a view. Each view references a view-specific js (action1.js and action2.js).
I would like to bundle site.js with action1.js when Action1 method is executing. And when Action2 method, I want to bundle site.js with action2.js.
The bundling should be done at build time.
Is this possible with .Net Core 1.x?

It's possible, but there's a lot of menial work involved, because you'll need to manually describe each view's bundle.
Everything you need to know can be found in the official documentation, but here's the gist of it:
Action1.cshtml
I assume that for debugging purposes, you want to include both files on your dev box, while you only want the bundle in production. In your view, you add the following tags:
<environment names="Development">
<script src="site.js"></script>
<script src="action1.js"></script>
</environment>
<environment names="Staging,Production">
<script src="view1.js" asp-append-version="true"></script>
</environment>
When in development, your two files will be included as is, while in production, the bundled file view1.js will be included.
asp-append-version is part of the cache busting mechanism: it will append the file's version to each request to that file (details here).
Create your bundled view1.js
There's a number of various possibilities to create the bundle, but they all revolve around the bundleconfig.json file. The simplest solution uses he BuildBundlerMinifier NuGet package, and simply requires you to add it to your project.
bundleconfig.json would look like this:
[
{
"outputFileName": "wwwroot/js/view1.js",
"inputFiles": [
"wwwroot/js/site.js",
"wwwroot/js/action1.js"
]
},
]
Repeat for each view
This is where things get boring, because you'll need to repeat this for each view.

Related

MAUI app doesn't pick up compiled CSS from post build action

I'm currently playing around with a MAUI Blazor app and set up Tailwind CSS instead of the default Bootstrap. This is done through Node.js for which I've set up an NPM script to build the CSS:
"scripts": {
"buildcss": "tailwindcss -o wwwroot/css/custom-tailwind.min.css --minify"
}
In the index.html I load this CSS file: <link rel="stylesheet" href="css/custom-tailwind.min.css" />.
Now, since Tailwind only includes the styles that my app actually uses (to minimize the file size), I want to rebuild this CSS file everytime I build my app. This is currectly done with a post build action:
<Target Name="BuildCSS" AfterTargets="PostBuildEvent">
<Exec Command="npm run buildcss" />
</Target>
The file is generated as expected, but when running the app, it doesn't seem to recognize the file and therefore no styling is loaded. Simply restarting the application after is enough for it to work, which means if the file already exists and is simply overwritten it works, but it annoys me.
Am I using the wrong build action or should this be done in a different way?
As a bonus question, is there a build action that will also be executed during Hot Reload?
I'm currently just testing it for a Windows app (net6.0-windows10.0.19041.0)

How to rename bundled static files(ProjectName.style.css and blazor.server.js) in The Blazor App

I wanna hide that i use The Blazor.
so, I should modify name of bundled css and js files.
How to do this?
According to this article, CSS isolation occurs at build time. During this process, Blazor rewrites CSS selectors to match markup rendered by the component. These rewritten CSS styles are bundled and produced as a static asset at {PROJECT NAME}.styles.css, where the placeholder {PROJECT NAME} is the referenced package or product name.
That means we could only disable the bundle not modify it during develop environment.
But after publish, it will generate the file like this:
You could modify the {PROJECT NAME}.styles.css to {other}.styles.css and modify the index.html css name as below:
<link href="{other}.styles.css" rel="stylesheet" />

How to access npm-installed package in hexo app

I am creating a web app using Hexo. I want to use a package called slick-carousel in one of my pages. This package also contains jQuery by the way. So I successfully installed (and "--save"ed) the package via npm. The package shows up in my node_modules folders and on my package.json file.
I expected that after doing this, I should have access to both jQuery and slick functions in my markdown files, but I don't. When I render the generated page on my browser, I am told that 'jQuery is undefined.' What step am I missing here so that I can actually use my installed packages?
Here is the script tag I added to my markdown file that I am trying to make work:
<script>
jQuery(document).ready(function(){
jQuery('.carousel').slick({
dots: true,
infinite: true,
speed: 300,
slidesToShow: 1,
centerMode: true,
variableWidth: true
});
});
</script>
I am still trying to fully grasp the relationship between installed packages and the rest of my application, so forgive me if this question doesn't even make sense. Any insight or explanation you can give me would be much appreciated!
Just because the scripts are in node_modules, doesn't mean they are automatically added to your projects frontend.
There are different ways to achieve what you need:
Manually moving the assets
Instead of trying to fiddle around with package.json and module requirements, the probably easiest way to get what you want is
moving the distribution files of jquery and slick-carousel out
of the node_modules folder into a folder where Hexo can work with
them better (after a quick read-up it should be source) then you
just link your JS file in your HTML layout and everything should work fine
Automatically moving the assets
With using some kind of task toolkit (like Gulp or Grunt) you could write tasks that automatically move the assets out of the node_modules folder inside a folder that is accessible by Hexo, a Gulp task could look something like this:
gulp.task('jquery', function () {
return gulp.src('./node_modules/jquery/dist/jquery.js')
.pipe(gulp.dest('./source/js'))
})
Using require (if supported)
I never used Hexo before, so I have no idea of it's internals, but sometimes it might be possible to just use require in the Javascript files to load modules that were downloaded, so you could use
window.jQuery = window.$ = require('jquery')
for example, to directly load the script as module.
You might need to test this yourself, but these are probably the three most common ways to handle assets in Node.js projects.

Bootstrap JavaScript works on localhost but not on webhost

I am using Twitter Bootstrap Installed Using Buget to an ASP.NET MVC Application. I am however surprised to find that all my bootstrap JavaScript which used to work on localhost and in fact still do have all stopped working when uploaded to my web hosting provider.
I am completely bumped on this and I cant seem to figure out what is wrong.
Some of your scripts, which depend on jQuery, are being included before jQuery had been loaded.
bootstrap-dropdown.js is included before jQuery
your custom javascript on line 75 is included before jQuery
I'd suggest moving
<script src="/Scripts/bootstrap.js" type="text/javascript"></script>
<script type="text/javascript" src="http://twitter.github.com/bootstrap/assets/js/bootstrap-dropdown.js"></script>
and your custom javascript from line 75, to the bottom of your page, after the jQuery bundle is included. Or alternatively move your jQuery bundle into your <head>. It's your preference as to which - see this question: Should Jquery code go in header or footer? regarding that particular issue...
You say it's working on your local box and not on the remote server - if that's really the case then my best guess is that this is to do with the fact that bundling/minification works differently depending on whether you are in debug or release mode.
UPDATE
Basically, in a debug build, no bundling/minification is performed on your scripts and css. Each <link> and <script> that you reference in your bundles will be included as a separate file, with no transformations applied to them. In release builds, files in a bundle are concatenated together into one file, and transformations such as minification are applied to them. As per your comment, in order to get the debug behaviour in both your local and published sites, I see 3 options:
1. In your release build, set the compilation debug flag to true in your web.config.
i.e.
<system.web>
<compilation debug="true" />
<!-- Lines removed for clarity. -->
</system.web>
I wouldn't particularly recommend this, as your release build shouldn't generally be flagged as being debug. But this is a useful setting to quickly toggle on your release build to check if your javascript/css works correctly when it is off.
2. In code, turn off bundling optimizations in your bundle table.
public static void RegisterBundles(BundleCollection bundles)
{
// other code...
BundleTable.EnableOptimizations = false;
}
3. Don't do it.
The difference in behaviour is by design, and makes sense - on your local box doing development, you can see all the individual script/css files and in unminified form, which makes debugging far easier. When published, the files are squashed down into one single, compressed file, which will reduce load time for your site.
I'd personally recommend option 3, and just make sure your styles/scripts are being included in the correct order.
In your page you have
<link href="/Content/bootstrapcss" rel="stylesheet"/>
... there's a blip with the file type, maybe this should be
<link href="/Content/bootstrap.css" rel="stylesheet"/> ?
And there's a path problem with your Modernizr file, check that the path is correct and the file exists:
http://bipscore.com/Scripts/modernizr-1.7.min.js
There may be other issues, but wouldn't hurt to get these two corrected first.
Good luck!

How to play dojo with sprockets in rails edge?

Rails 3.1 edge use sprockets to handle .js and .scss files.
Sprockets use comments to deal with dependencies.
I put the three folders (dojo dijit dojox) in vendor/assets/javascripts.
Then add a line in app/assets/javascripts/application.js
//= require dojo/dojo
console.log(dojo);
Now dojo has been merged into application.js.
But dojo has dependency system itself. When I require more dojo modules. It cannot find the right path.
dojo.require("dojox.grid.DataGrid");
// Error in webkit console: Error: Could not load 'dojox.grid.DataGrid'; last tried '../dojox/grid/DataGrid.js'
// Error in rails server log: Started GET "/undefined../dojox/grid/DataGrid.js" for 127.0.0.1 at Sat Apr 16 01:26:05 +0800 2011
These are two different dependency systems. How can I put them together?
dojo.js doesn't take too naturally to being renamed. This is because part of Dojo's initialization process involves searching through the DOM for the script tag responsible for loading itself. It does this for two reasons:
Determine its baseUrl if not already specified
Pick up any djConfig (or data-dojo-config in 1.6+) properties specified in the script tag itself
When it searches for this script tag, it looks for one with src set to dojo.js or dojo.xd.js; it's not finding it in your case, thus the failures.
It should be possible to work around both of these issues, by specifying djConfig (or dojoConfig in 1.6+) fully programmatically in another script tag before the one that loads Dojo, and by specifying baseUrl manually in these config properties.
For example, if your application.js were in vendor/assets/javascripts as referred to in your original post, you could try doing this:
<script type="text/javascript">
//if you're using 1.5 or earlier, use djConfig instead of dojoConfig
var dojoConfig = {
//note that baseUrl points to the folder containing dojo.js, therefore the dojo folder
baseUrl: 'vendor/assets/javascripts/dojo/'
};
</script>
<script type="text/javascript" src="vendor/assets/javascripts/application.js"></script>
For more information on dojo config settings: http://dojotoolkit.org/reference-guide/djConfig.html (in fact, that page actually has a note under baseUrl specifically about renamed dojo.js.)
Try adding the dojo files into a dojo/scripts directory under /vendor/assets/javascripts. In the dojo directory, create a dojo.js file in the dojo directory and put //= provide "scripts" in that file.
Then in your application.js file put //= require <dojo>. Check out the Sprockets site http://getsprockets.org/ for info but that should work.
The docs aren't out for Rails 3.1 and the asset pipeline yet so a lot of this stuff is kind of trying to figure out on your own.
Umm... why are you placing your javascripts in vendor and app rather than public/javascripts? you have include anything in public/javascripts with =javascript_include_tag('path_to_javascript') where path_to_javascript would be "dojo/DataGrid" for public/javascripts/dojo/DataGrid.js
http://guides.rubyonrails.org/getting_started.html
I know it's maybe a bit late but I could help someone else.
I finally made it work and posted the explanations here.