Compile less file to css in expressjs 4.x - node.js

I want to compile the less file in public folder of expressjs application.
Dependencies which I am using are
"devDependencies": {
"ejs": "^2.3.1",
"express": "^4.10.6",
"mysql": "^2.5.4"
},
"dependencies": {
"less": "^2.5.1",
"less-middleware": "^2.0.1"
}
Content in styles.less file
header {
background-image: url('../img/bg.png');
height: 380px;
input#searchBox {
width: 100%;
height: 70px;
}
}
Server file looks like below
var express = require('express'); // call express
var app = express(); // define our app using express
app.use(require('less-middleware')('public'));
// Public folder
app.use(express.static('public'));
The less file is not modified while running it on server.
Remote Address:127.0.0.1:3000
Request URL:http://localhost:3000/css/styles.less
Request Method:GET
Status Code:304 Not Modified
What is the error here.
Directory structure:
Edit:
header {
background-image: url('../img/bg.png');
height: 380px;
#cloud-tag span {
color: #fff;
font-weight: 800;
letter-spacing: 2px;
}
#cloud-tag_word_0 {
font-size: 100px !important;
}
}
I added this css in less file and it is being sent to browser in same format without being compiled.

You must request the css file, not the less file, in your case http://localhost:3000/css/styles.css not http://localhost:3000/css/styles.less.
The middleware will compile the less file to the one you requested.

Looks like the way you initilize the middleware to express might be the issue . Use "__dirname + '/public'" to denote the folder.

Related

hot-proxy does not handle `$app-css-relative-asset-path` properly

When I use the bin/watch-storefront.sh my custom font does not load properly.
My fonts are defined like the default font in a own file like
/* /MyTheme/src/Ressources/app/storefront/src/scss/vendor/_outfit-fontface.scss */
...
#font-face {
font-family: 'Outfit';
src: url('#{$app-css-relative-asset-path}/font/Outfit/Outfit-SemiBold.woff2') format('woff2'),
url('#{$app-css-relative-asset-path}/font/Outfit/Outfit-SemiBold.woff') format('woff');
font-weight: 600;
font-style: normal;
font-display: swap;
}
...
When I run bin/console theme:compile && bin/console assets:install The fonts get loaded just fine on the regular website.
The URL of the font files is like https://my-site.de/theme/f9cf8c2bbd24d1ca2941b5120cde3278/assets/font/Outfit/Outfit-Regular.woff2
But when I run the hot-proxy via bin/watch-storefront the compiled CSS font path is like http://my-site.de:9998/bundles/storefront/assets/font/Outfit/Outfit-Bold.woff2
I looked into the directories and my fonts are not located in storefront/assets but in mythemeplugin/assets
The wrong path seems to be exported to /var/theme-entry.scss where it already sets the $app-css-relative-asset-path to
$app-css-relative-asset-path: '/bundles/storefront/assets'; $sw-asset-public-url: '';
I tried to replace the variable $app-css-relative-asset-path with a hard-coded ../assets/ in my *-fontface.scss but this will cause a compile error in the hot-proxy.
What can I do to make the fonts load properly in the hot-proxy and the normal site?
See this issue on GitHub with a possible workaround.
Adding to the description, you'll have to change the type of your style property in theme.json to an object:
{
// ..
"style": {
"app/storefront/src/scss/overrides.scss": [],
"#Storefront": [],
"app/storefront/src/scss/base.scss": {
"resolve": {
"my-theme-env": "app/storefront/env/default"
}
}
},
// ...
}

Replacing Font Awesome in heading with local SVG

I'm using this snippet for adding a Font Awesome icon in front of H1 headings:
h1:before {
content: "\f192 ";
font-family: "FontAwesome";
color: blueviolet;
}
How to adapt it for using a locally served (on site's server) SVG icon instead?
(that is uploaded in WP Media Library and using SVG Support plugin)
If your icon needs to remain a separate file, you can set it as a background image.
h1:before {
display: inline-block;
background-image: url(resources/icon.svg)
}
Otherwise you can embed your icon as a data url.
h1:before {
display: inline-block;
background-image: url(data:image/svg+xml,...etc...)
}
Update
Working example:
h1:before {
content: " ";
display: inline-block;
width: 0.7em;
height: 0.7em;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 10'%3E%3Ccircle cx='5' cy='5' r='5'/%3E%3C/svg%3E");
background-size: contain;
background-repeat: no-repeat;
}
<h1>This is a title</h1>

Is there a way to make an SPFx custom webpart to occupy the whole real estate?

I have been using SharePoint Online (SPO 2016) and SPFx for a while now but up until recently there is something happening with the Full-width web parts, they appear squeezed to the center of the page. An example is shown:
On the code, I have added the option to support full bleed to true. Everything configured properly but upon inspection on the browser dev tools, I found out that a certain class with the name f_b_50a7110f had a property of margin: auto, now to fix the issue I added on my code the same class and gave it a width of 100%. .f_b_50a7110f {width: 100%;}.
This solves the issue temporarily but the problem is that the name of the class changes from time to time. For instance, at the time of asking this, it is called p_i_50a7110f thus the solution does not stick. Has anyone encountered the same issue? And what was the solution? Any help will be appreciated. Thanks. Here is my package.json file.
{
"name": "homepage-global",
"version": "0.0.1",
"private": true,
"main": "lib/index.js",
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"build": "gulp bundle",
"clean": "gulp clean",
"test": "gulp test"
},
"dependencies": {
"#material-ui/core": "^4.11.3",
"#material-ui/icons": "^4.11.2",
"#microsoft/sp-core-library": "1.11.0",
"#microsoft/sp-lodash-subset": "1.11.0",
"#microsoft/sp-office-ui-fabric-core": "1.11.0",
"#microsoft/sp-property-pane": "1.11.0",
"#microsoft/sp-webpart-base": "1.11.0",
"#pnp/sp": "^2.1.1",
"#pnp/spfx-controls-react": "2.4.0",
"materialize-css": "^1.0.0-rc.2",
"office-ui-fabric-react": "6.214.0",
"react": "16.8.5",
"react-dom": "16.8.5",
"react-materialize": "^3.9.7"
},
"devDependencies": {
"#microsoft/rush-stack-compiler-3.3": "0.3.5",
"#microsoft/sp-build-web": "1.11.0",
"#microsoft/sp-module-interfaces": "1.11.0",
"#microsoft/sp-tslint-rules": "1.11.0",
"#microsoft/sp-webpart-workbench": "1.11.0",
"#types/chai": "3.4.34",
"#types/es6-promise": "0.0.33",
"#types/mocha": "2.2.38",
"#types/react": "16.8.8",
"#types/react-dom": "16.8.3",
"#types/webpack-env": "1.13.1",
"ajv": "~5.2.2",
"css-loader": "^5.0.1",
"gulp": "~3.9.1"
}
}
By default, SharePoint Framework client-side web parts can't be placed in full-width column layouts. To allow users to add your web part to full-width columns, in the web part manifest (the *.manifest.json file next to the web part *.ts file) set the supportsFullBleed property to true.
{
//...
"requiresCustomScript": false,
"supportsFullBleed": true,
"preconfiguredEntries": [{
//...
}]
}
https://learn.microsoft.com/en-us/sharepoint/dev/spfx/web-parts/basics/use-web-parts-full-width-column
You could also use an extension:
import { override } from '#microsoft/decorators';
import { BaseApplicationCustomizer } from '#microsoft/sp-application-base';
export interface ISiteFullWidthApplicationCustomizerProperties {
message: string;
}
require("./css/custom.module.scss");
export default class SiteFullWidthApplicationCustomizer extends BaseApplicationCustomizer<ISiteFullWidthApplicationCustomizerProperties> {
#override
public onInit(): Promise<void> {
if (typeof (Event) === 'function') {
window.dispatchEvent(new Event('resize'));
}
else {
var resizeEvent = window.document.createEvent('UIEvents');
resizeEvent.initUIEvent('resize', true, false, window, 0);
window.dispatchEvent(resizeEvent);
}
return Promise.resolve();
}
}
custom.module.scss:
:global {
.sp-pageLayout-sideNav [class^='deferredLeftNav'] {
max-width: 0px;
}
.sp-pageLayout-sideNav .SPCanvas-canvas {
max-width: 100%;
}
.sp-pageLayout-sideNav .CanvasZoneContainer:first-child .CanvasZone.row {
max-width: 100%;
}
.sp-pageLayout-sideNav .CanvasZoneContainer:first-child .CanvasZone.row,
.sp-pageLayout-sideNav .CanvasZoneContainer:first-child .CanvasZone.row .CanvasSection,
.sp-pageLayout-sideNav .CanvasZoneContainer:first-child .CanvasZone.row .CanvasSection .ControlZone,
{
margin: 0px;
padding: 0px;
}
.sp-pageLayout-sideNav .CanvasZone.row.CanvasZone--alignment.CanvasZone--read.CanvasZone--noMargin {
margin: auto;
}
.sp-pageLayout-sideNav [class^='spInnerCommentsWrapper_'] {
width: 100%;
margin-left: auto;
margin-right: auto;
}
}
Just thought I would mention I created a chrome extension that removed max-width restrictions and makes the workbench 1920px width, with scaling down working as well.
https://chrome.google.com/webstore/detail/spfx-workbench-manager/gnchlhbjhljidedhighhkgbfchmejlcp

Gulp ' Unregognised input in file'

Lately my gulp task started to give me errors. It doesnt want to compile my .scss files anymore. Very strange.
The big chunk of my gulp task seems to be running fine, it's the styles task that throws me an error.
This is my CMD when I try to run the gulp styles command
As you can see it doesnt recognise input from the dropdowns.scss file as valid.
//
// Dropdown menus
// --------------------------------------------------
// Dropdown arrow/caret
.caret {
display: inline-block;
width: 0;
height: 0;
margin-left: 2px;
vertical-align: middle;
border-top: $caret-width-base dashed;
border-top: $caret-width-base solid \9; // IE8
border-right: $caret-width-base solid transparent;
border-left: $caret-width-base solid transparent;
}
There seems to be nothing wrong with this file, however on line 13 it is the first time it calls a variable. When I remove this file it will find other scss variables that it wont recognise in other files. In short: all scss variables are unrecognisable.
// ### Styles
// `gulp styles` - Compiles, combines, and optimizes Bower CSS and project CSS.
// By default this task will only log a warning if a precompiler error is
// raised. If the `--production` flag is set: this task will fail outright.
gulp.task('styles', ['wiredep'], function() {
var merged = merge();
manifest.forEachDependency('css', function(dep) {
var cssTasksInstance = cssTasks(dep.name);
if (!enabled.failStyleTask) {
cssTasksInstance.on('error', function(err) {
console.error(err.message);
this.emit('end');
});
}
merged.add(gulp.src(dep.globs, {base: 'styles'})
.pipe(cssTasksInstance));
});
return merged
.pipe(writeToManifest('styles'));
});
I'm thinking Windows 10 might have done a upgrade to the system or perhaps it had something to do that I tried to install critical css throught NPM. However, I doubt this can destroy a perfectly fine task.
So far I tried reinstalling node.js bower and gulp itself but it keeps giving me this error. Also I've tried to configure a fresh project (which worked everytime over the previous months) but even with a fresh project it wont compile. I'm thinking some dependency might be outdated.
Ugh, I've been working on this for hours and I'm running out of ideas of what might be the cause of this.
Thanks in advance!
Update2--
Bower.json
{
"name": "starter-template",
"homepage": "http://www.thomwensink.nl",
"authors": [
"Thom Wensink <info#thomwensink.nl>"
],
"license": "MIT",
"private": true,
"dependencies": {
"bootstrap-sass": "^3.3.6",
"slick-carousel": "^1.5.9",
"font-awesome": "fontawesome#^4.5.0",
"imgLiquid": "^0.9.944",
"modernizr": "2.8.2"
},
"overrides": {
"bootstrap-sass": {
"main": [
"./assets/stylesheets/bootstrap/_variables.scss",
"./assets/stylesheets/bootstrap/_mixins.scss",
"./assets/stylesheets/bootstrap/_normalize.scss",
"./assets/stylesheets/bootstrap/_print.scss",
"./assets/stylesheets/bootstrap/_scaffolding.scss",
"./assets/stylesheets/bootstrap/_type.scss",
"./assets/stylesheets/bootstrap/_code.scss",
"./assets/stylesheets/bootstrap/_grid.scss",
"./assets/stylesheets/bootstrap/_tables.scss",
"./assets/stylesheets/bootstrap/_forms.scss",
"./assets/stylesheets/bootstrap/_component-animations.scss",
"./assets/stylesheets/bootstrap/_navs.scss",
"./assets/stylesheets/bootstrap/_navbar.scss",
"./assets/stylesheets/bootstrap/_utilities.scss",
"./assets/stylesheets/bootstrap/_responsive-utilities.scss",
"./assets/javascripts/bootstrap/transition.js",
"./assets/javascripts/bootstrap/collapse.js",
"./assets/javascripts/bootstrap/scrollspy.js",
"./assets/javascripts/bootstrap/affix.js"
]
},
"font-awesome": {
"main": [
"./scss/font-awesome.scss",
"./fonts/*"
]
},
"modernizr": {
"main": "./modernizr.js"
}
}
}
Regarding the error message of reinstalling bootstrap, there is none. It goes as expected. However, the problem persists.
Update--
Turns out the problem is with bootstrap's version 3.3.5 version.
Could you please change the version of your bootstrap in the bower.json?
From:
"bootstrap": "~3.3.1",
To:
"bootstrap": "3.3.1",
and the run bower install again, please.
Are you using some kind of gulp-sass plugin ? Below code is is require for compiling sass to css from gulp. You may need to install gulp-sass by npm install --save-dev gulp-sass
var sass = require('gulp-sass');
gulp.task('sass', function () {
return gulp.src('client/sass/*.scss')
.pipe(sass().on('error', sass.logError) )
.pipe(gulp.dest('css'))
});
For other people that are having this issue: I managed to solve it following the steps from this website.

Integrating Font Squirrel generated font in Twitter Bootstrap via LESS

I am attempting to use a font generated from Font Squirrel as the base font for Twitter Bootstrap (compiled from the LESS files). I am using Express.js with Node.js, and have included the font files within the font directory, i.e.
myapp
|_ public
|_ stylesheets
|_ font
I have "installed" Font Awesome by changing the variables in bootstrap.less and have it working, so I know the directory structure is correct. With the custom font files in the font directory, where do I go next? Do I need to make a my-custom-font.less file that contains the #font-face declarations, or do I need to declare this within one of the bootstrap LESS files? I am aware that the base font is declared in variables.less via the #baseFontFamily attribute, but as I described I am not really sure how to declare this to be my custom font family. Thanks in advance.
EDIT
Below is the snippet of code I am attempting to use:
#ChatypePath: "font";
#font-face {
font-family: 'chatypeb2.1regular';
src: url('#{ChatypePathPath}/chatypeb2.1regular-webfont.eot?v=3.0.1');
src: url('#{ChatypePathPath}/chatypeb2.1regular-webfont.eot?#iefix&v=3.0.1') format('embedded-opentype'),
url('#{ChatypePathPath}/chatypeb2.1regular-webfont.woff?v=3.0.1') format('woff'),
url('#{ChatypePathPath}/chatypeb2.1regular-webfont.ttf?v=3.0.1') format('truetype');
}
NOTE: There is something erroneous here.
UPDATE:
Correct declaration:
#chatypeFontFamily: "ChatypeB2.1ThinThin", "Courier New", monospace;
#font-face {
font-family: 'ChatypeB2.1ThinThin';
src: url('font/chatypeb2.1thin-webfont.eot');
src: url('font/chatypeb2.1thin-webfont.eot?#iefix') format('embedded-opentype'),
url('font/chatypeb2.1thin-webfont.woff') format('woff'),
url('font/chatypeb2.1thin-webfont.ttf') format('truetype');
font-weight: normal;
font-style: normal;
}
I would try something like this in the variables.less:
#customFontFamily: "Custom", "Courier New", monospace;
/* here you should use whatever #font-face squirrel generated in the stylesheet.css */
#font-face {
font-family: 'Custom';
font-style: normal;
font-weight: 400;
src: local('Custom'), local('Custom-Regular'), url(path.to.font.file.woff) format('woff');
}
you can also put the font-face into a separate file and then using #import, but I don't think it's necessary. And then you can call the #cusomFontFamily and use it as #baseFontFamily, or wherever you want.

Resources