How to manage CSS Libraries in the front end? - css

With node and browserify it's easy to manage js dependencies by just buttoning it all up into a bundle.js file and including that in the parent html.
What I'm wondering is how do you guys manage libs like bootstrap?
They download just fine to node_modules but how do you get them into your html files or asset folder?
Do you just reference them straight from node_modules? Have a task copy them in? Etc?
Caveat, I was hoping to do this without the browserify-css plugin because I want to keep my css and js separate.

I personally have a Gulp / Grunt workflow setup in which I included my js/css modules and then it automatically do all the important task and at the end I have only one css and js file. Then I import them to HTML.
I suggest you to make task for gulp or grunt which will check for changes in the folder that you decide ( where download of libraries will be ). If there is a change - it will automatically copy the .css files into another working directory.
I hope I understand what you want correctly.

i have created a little python script that allows me to import files(.js, .css, .younameit) from a directory to the html.
before:
<html>
<head>
<!-- {SVN_AUTO_IMPORT type:.js; dir:../tsc_out; exclude:; template:<script src="%PATH"</script>;} -->
</head>
after:
<html>
<head>
<script src="../tsc_out/script1.js"></script>
<script src="../tsc_out/script2.js"></script>
<script src="../tsc_out/script3.js"></script>
<script src="../tsc_out/script4.js"></script>
<script src="../tsc_out/script5.js"></script>
</head>
https://github.com/seven-jerry/html-importer

Related

How to add static CSS to Dojo 7 application e.g. FontAwesome?

I have a fairly basic Dojo 7 (Dojo 2) app built with the dojo-cli tool. Now I'd like to add FontAwesome icons to it. I have a Pro subscription so I have a zip file with various folders of css and web font files that I want to include in my project, then link to from index.html. I believe the Dojo build process uses webpack, and my knowledge of it is extremely limited.
I can link to fontawesome CDN free version in src/index.html easily, which works fine:
<!link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.3/css/all.css" integrity="sha384-SZXxX4whJ79/gErwcOYf+zWLeJdY/qpuqC4cAa9rOGUstPomtqpuNWT9wdPEn2fk" crossorigin="anonymous">
The Dojo 2 tutorial at https://dojo.io/learn/building/static-assets says I can put static assets in the assets folder, so I extract the zipfile to assets/fontawesome-pro-5.15.3-web and try to link to it in src/index.html:
<link rel="stylesheet" href="assets/fontawesome-pro-5.15.3-web/css/all.min.css">
My intention is to use FontAwesome in the traditional way, not using CSS Modules.
<i class="fas fa-question-mark"></i>
The dojo build process emits a large amount of "copying file blah.css" as it copies assets content to output/dev/assets and I see the FontAwesome files in output/dev/assets/fontawesome-pro-5.15.3-web, however the build gives:
Html Webpack Plugin:
Error: Child compilation failed:
Module not found: Error: Can't resolve './assets/fontawesome-pro-5.15.3-web' i n '/home/username/go/projectname/src':
Error: Can't resolve './assets/fontawesome-pro-5.15.3-web' in '/home/username/go/projectname/src'
- compiler.js:79
[travesty]/[html-webpack-plugin]/lib/compiler.js:79:16
I notice that it's referencing relative to /home/username/go/projectname/src, and the original assets folder is one level above that in the source tree, so I also tried this relative path in src/index.html:
<link rel="stylesheet" href="../assets/fontawesome-pro-5.15.3-web/css/all.min.css">
But this fails differently:
Html Webpack Plugin:
Error: /home/username/go/projectname/src/index.html:97
module.exports = __webpack_public_path__ + "all.min.30RjDni8.css";
^
ReferenceError: __webpack_public_path__ is not defined
- index.html:97 Object../assets/fontawesome-pro-5.15.3-web/css/all.min.css
/home/username/go/projectname/src/index.html:97:18
I have very similar results creating an assets/simple.css file and linking to that with <link rel="stylesheet" href="assets/simple.css"> so this seems a generic problem with my understanding of webpack.
Should I be using the "static assets" approach with these FontAwesome files, and if so how do I fix this build situation, or should I be trying to use this third party CSS library as a CSS Module, and if so, how?
It may be to do with webpack's "publicPath" concept, see https://webpack.js.org/guides/public-path/ .. if so I'd presume I need to do something to my .dojorc to control the Dojo build's use of webpack.
Thanks to #agubler on the Dojo discord channel, the fix is apprently this simple:
<link rel="stylesheet" href="/assets/fontawesome-pro-5.15.3-web/css/all.min.css">
Builds and works fine. Feels dirty to use an absolute URL like that so if anyone has a more meaty, explanatory answer to give, I'd be happy to mark that as the Answer instead of this :)

How to use font-awesome in ejs file from node modules?

I have installed font-awesome 4.7 in my nodejs application
I have to get it in my views.ejs file.
i tried importing using tag...
<link rel="stylesheet" href="../node_modules/font-awesome/css/font-awesome.min.css" >
and from css file like this...
#import url('../node_modules/font-awesome/css/font-awesome.min.css');
but they doesn't help....
It is failing to load when looking at the browser's network tab...
Please help.. Thanks in advance!
I have an ejs app in production that uses font awesome and I just use the CDN. But also Atheesh Thirumalairajan post is a valid way. Just make sure you are serving from a public folder.
<script src="https://kit.fontawesome.com/c03ec31dc2.js" crossorigin="anonymous"></script>
You need not use node_modules instead, the easy way of doing it is to download Font Awesome from https://fontawesome.com/v4.7.0/get-started/
Then, extract the files your public folder (You need to define the public directory in express)
Then, In the <head></head> section of your HTML, add:
<link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">
You can also checkout their official examples.
The same thing applies to Font Awesome 5 But, you need to download from https://fontawesome.com/how-to-use/on-the-web/setup/hosting-font-awesome-yourself

CSS cannot be loaded from `file:` URLs unless they end in a `.css` file extension

i'm trying for the first time to use sass and not css but when i try to load the stylesheet the browser tell me that it can only load .css as stylesheet.
Here is the html
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="./index_style.scss">
</head>
<body>
</body>
</html>
And the error
[Deprecation] CSS cannot be loaded from `file:` URLs unless they end in a `.css` file extension.
i'm using chrome
SASS is a preprocessor, the browser doesn't understand .scss, but only .css. SASS converts .scss files to .css.
There may be some projects that allow loading dynamically a .scss file "on the fly" from a HTTP server, but the message indicates that you are loading the page directly from your local file system.
In that case you need to invoke the preprocessor and point the href on the link to the generated file. It can be done directly with a command line command, with the instructions you can find here (https://sass-lang.com/guide) or if you are using a task runner like grunt, gulp, etc. there are task for Sass in those.

Adding CSS to webpack build

I have a fairly simple webpack project - built using the Webpack guide. See https://github.com/timburgess/webpack-postcss-tailwind
There is a style.css in the src directory but there is no .css being added to the /dist directory on build.
Reading further, any css should be added as an inline style and that's not occuring.
Resolved with https://github.com/webpack-contrib/mini-css-extract-plugin per Richards answer.
Webpack will bundle the css files referenced via import statements in your source javascript files into the output javascript file (bundle.js). You'll see the classes being applied to the webapp at runtime via inline <style> tags applied dynamically to the html.
Many developers do not think this behaviour appropriate and will use a special plugin to get webpack to produce seperate bundled .css files that you then reference in your html using the traditional (and caching friendly) <link rel="stylesheet" type="text/css" href="bundle.css"> tag. See:
https://github.com/webpack-contrib/mini-css-extract-plugin

Which .less files I should import to start working with Twitter Bootstrap?

I create a custom.less file. Which files I should import (#import) to start working with Bootstrap?
How to get started with Bootstrap and LESS?? I can't find any tutorials...
You'll need just a few things:
Bootstrap's .css and .js files
The less.js file
jquery.js
Linking them to your site:
To help optimize your website, take advantage of a few content delivery networks (CDNs) that are already out there. The following lines will get you bootstrap and jquery.
<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/css/bootstrap-combined.min.css" rel="stylesheet">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>
<script src="//netdna.bootstrapcdn.com/twitter-bootstrap/2.1.1/js/bootstrap.min.js"></script>
Since there isn't a Less CDN, you will need to download the less.js file here, and then serve it up through your Static Media along with your custom .less file:
<script type="text/javascript" src="{{ STATIC_URL }}js/less-1.3.0.min.js"></script>
<link rel="stylesheet/less" type="text/css" href="{{ STATIC_URL }}css/custom.less">
Here is my suggestion, before you kill yourself trying to get LESS to compile with Bootstrap, just set that folder aside for a bit and create a test project to get LESS working. Start like this:
Download and install node
Create a folder called something like "less-build" just to get this working
In windows, shift + right click (not sure what mac is) and select "open command window here". Type npm install less -g and hit enter to install LESS "globally" on your computer
Now type "npm install less" to install LESS "locally". If all is well you should see a folder called "node_modules" inside the less-build folder. If you click into node_modules you will see a folder for less.
Next, in the less-build folder create a file called styles.less. Inside that file add the following code:
#backgroundColor: #900;
.block {
background: #backgroundColor;
}
Save the file, close it.
Now, again in the command prompt type the following:
lessc --compile styles.less > styles.css
If all is well you should see the file styles.css. Open it and confirm that there is valid css in the file.
If that works, open the Bootstrap folder and find the less folder, you'll find a file called bootstrap.less. Use the process I described on bootstrap.less. This should give you a head start.
You can download Bootstrap LESS files from GitHub.
Each files are often named like its corresponding component, check the Bootstrap component's list.
So, you can import files which are necessary for your need.
Hope that helps
Clone or download the content of the Twitter Bootstrap repository: https://github.com/twitter/bootstrap/
Then import less/bootstrap.less, and optionally less/responsive.less. With that, all mixins and variables defined by Twitter Bootstrap are available in your own LESS file.

Resources