How to concat js/css files and replace in html using gulp? - css

Here is a lot of questions and answers how to make assets concatination or replacement in template. Use grunt-concat to merge files, use gulp-html-replace to replace in templates. And i can`t understand how to connect them with each other.
So given template, friends.tpl with special set of css files:
!DOCTYPE html>
<html>
<head>
<!-- build:css friends.min.css -->
<link rel="stylesheet" href="css/component1/style.css">
...
<link rel="stylesheet" href="css/componentN/style.css">
<!-- /build -->
</head>
<body/>
The desired result
!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="css/friends.min.css">
</head>
<body/>
And created friends.min.css file with concatenated files above.
How to achieve this - concat based on template data

Use gulp-htmlbuild,it does what you want.
gulp.task('build', function () {
gulp.src(['./index.html'])
.pipe(htmlbuild({
// build js with preprocessor
js: htmlbuild.preprocess.js(function (block) {
// read paths from the [block] stream and build them
// ...
// then write the build result path to it
block.write('buildresult.js');
block.end();
})
}))
.pipe(gulp.dest('./build'));
});
gulp build will take index.html:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<!-- htmlbuild:js -->
<script src="js/script1.js"></script>
<script src="js/script2.js"></script>
<!-- endbuild -->
</body>
</html>
And turn it into:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script src="buildresult.js"></script>
</body>
</html>
https://www.npmjs.com/package/gulp-htmlbuild

Related

Why does my CSS not work when I pass two parameters to app.get?

I'm creating an express app and my CSS code stops working whenever i add an ":ID" parameter to the URL. I know it's a filepath issue because bootstrap still comes in fine, but on the page with the ID parameter it shows this: "Refused to apply style from 'https://XXXXXX.c9users.io/unapproved/main.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled."
main.css is in my /public folder, but it looks like it's looking in a folder titled "unapproved".
I've tried changing the routing order, i've tried changing my app.use(express.static(__dirname)) code.
here's my app.get:
app.get("/unapproved/:id/", function(req, res){
var invoiceID = mongoose.mongo.ObjectId(req.params.id);
InvoiceObj.findById(invoiceID,function(err,foundInvoice){
if(err){
console.log(err);
}else{
res.render("invoiceScreen",{invoice:foundInvoice});
}
});
here's my html:
<% include partials/header %>
<h1><image src="<%= invoice.imageUrl %>"</h1>
<% include partials/footer %>
here's my header:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Invoice system</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="main.css">
<link href="https://fonts.googleapis.com/css?family=Cabin" rel="stylesheet">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.7.2/css/all.css" integrity="sha384-fnmOCqbTlWIlj8LyTjo7mOUStjsKC4pOpQbqyi7RrhN7udi9RwhKkMHpvLbHG9Sr" crossorigin="anonymous">
<script src='https://code.jquery.com/jquery-2.1.4.js'></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
</head>
<body>
I think change to css path absolute path will solve your problem
<link rel="stylesheet" href="/main.css">
In this kind of situation, it's better to use virtual path for your static files because relative path doens't work very well; like:
Assuming folder structure:
app.js
public
| +-- main.css
app.use('/static', express.static(__dirname + "/public"))
And in you html, make path absolute
<link rel="stylesheet" href="/static/main.css">
instead of using ./ or ../ before your assest(images / css / js ) for example.
./css/style.css or css/bootstrap.css ../images/example.png
you can use / for example
/css/style.css or /css/bootraps.css /images/example.png
/ will tell your app to look from root dir.

Alchemy.js simple example not working

I'm trying to implement this example in order to become familiar with Alchemy.js. http://graphalchemist.github.io/Alchemy/#/examples how can I fix this to make it work?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Alchemy</title>
<link rel="stylesheet" href="bower_components/alchemy.min.css"/>
</head>
<body>
<div class="alchemy" id="alchemy"></div>
<script src="bower_components/alchemy.min.js"></script>
<script type="text/javascript">
alchemy.begin({dataSource: "charlize.json"});
</script>
</body>
</html>
Probably you are using the wrong library, try with this:
(Can you try including the vendor script first and then the alchmey script.)
<script src="https://cdnjs.cloudflare.com/ajax/libs/alchemyjs/0.4.2/scripts/vendor.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/alchemyjs/0.4.2/alchemy.min.js"></script>

View can't find layout page

My view can't find layout. But everything looks fine. My structure is shown below:
And my view is shown below:
#model xxx.Web.Model.Home.IndexModel
#{
ViewBag.Title = "Index";
Layout = "~/Views/_MainLayout.cshtml";
}
And my layout is shown below:
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="utf-8">
<title>Bootstrappage:Free Bootstrap Template (bootstrap 2.3.1 version)</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!--
<link rel="stylesheet" href="themes/style/bootstrap.min.css" />
<link rel="stylesheet" href="themes/style/bootstrap-responsive.min.css" />
-->
<link rel="stylesheet/less" type="text/css" href="../../themes/less/bootstrap.less">
<script src="../../themes/js/less/less.js" type="text/javascript"></script>
<link rel="shortcut icon" href="assets/ico/favicon.ico">
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="assets/ico/apple-touch-icon-144-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="assets/ico/apple-touch-icon-114-precomposed.png">
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="assets/ico/apple-touch-icon-72-precomposed.png">
<link rel="apple-touch-icon-precomposed" href="assets/ico/apple-touch-icon-57-precomposed.png">
<script type="text/javascript">
$(function () {
#RenderSection("DocumentReady", false)
});
</script>
</head>
<body data-offset="40">
#{Html.RenderAction("Header", "Partial");}
#{Html.RenderAction("Navbar", "Partial");}
#RenderBody()
#{Html.RenderAction("Footer", "Partial");}
<script src="themes/js/lib/jquery-1.9.1.min.js"></script>
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
Also my controller is shown below:
public ActionResult Index()
{
IndexModel model = new IndexModel();
HomeModelFactory modelFactory = new HomeModelFactory();
model.Files = modelFactory.CreateIndexModel();
return View(model);
}
And exception:
Why I am getting this exception? It was working well.
And the full contents of the View / Controller as well please?
Some possible scenarios without looking at the View / Controller are;
- Some sections are missing (e.g. "DocumentReady")
- The view is trying to resolve model properties which are not set (i.e null)
You might check your properties setting on your _MainLayout.cshtml file. I had this same problem (the Layout page could not be found in the search path) until I set the VisualStudio properties BuildAction to Content. I had copied the file and renamed it, and the property had been set to 'none'.
This is your problem: Layout = "~/Views/_MainLayout.cshtml";
The _Layout reference should be Layout = "~/Views/Shared/_MainLayout.cshtml";
Dont make these type of mistakes again!

Phpunit - Selenium2 - Wait for page to be completely loaded

I'm using phpunit with the extension of selenium 2 (Selenium2TestCase), for checking an interface of my website.
So my test page contains this :
testDisplay()
{
...
file_put_contents('/home/toto/www/screenshots/before.html', $this->source());
sleep(5);
file_put_contents(/home/toto/www/screenshots/after.html', $this->source());
$this->assertContains('toto42', $this->source());
...
}
Then when I execute :
$> phpunit testSelenium.php
I have 2 html files of my page, before the sleep(5) and after :
before.html :
<META charset="utf-8"/>
<META content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<TITLE>Test</TITLE>
<LINK href="/css/style.css" rel="stylesheet"/>
<LINK href="/css/bootstrap.css" rel="stylesheet"/>
<SCRIPT src="/js/jquery-1.7.1.min.js" type="text/javascript"/>
</HEAD></HTML>
after.html :
<META charset="utf-8"/>
<META content="text/html; charset=utf-8" http-equiv="Content-Type"/>
<TITLE>Test</TITLE>
<LINK href="/css/style.css" rel="stylesheet"/>
<LINK href="/css/bootstrap.css" rel="stylesheet"/>
<SCRIPT src="/js/jquery-1.7.1.min.js" type="text/javascript"/>
</HEAD>
<BODY>
...
toto42
...
</BODY>
I don't understand why I have to put a sleep(5) to entirely load my page, did someone have an idea ?
And an other question what is the equivalent of clickAtAndWait (of PHPUnit_Extensions_SeleniumTestCase) for the Selenium2TestCase ?
Thanks in advance
I found an answer.
I'm looking for an HTML element in my page with a timeout and if it does not appear I wait with the function implicitWait();
testDisplay()
{
...
//I use 25000 for the timeout but anything else works too it depends on your loading page
$this->timeouts()->implicitWait(25000);
$this->assertContains('toto42', $this->source());
...
}

The sequence between CSS and other DOM in Phonegap

below is part of my head section in HTML. i found that the content of my page always load before the jquery.css such that the layout is so plain and return to the desired layout after 0.5 second loading time. What should I modify the code in order to avoid the plain content? is it related to the deviceready? in before, href is to the hyperlink, after that, i change it to a local file. i surprised that even a local file, it use 0.5 second to load it, instead of loading it instantly.
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" charset="utf-8" src="cordova-2.3.0.js"></script>
<link rel="stylesheet" type="text/css" href="css/index.css">
<link rel="stylesheet" href="jquery.mobile-1.0a1.min.css" />
<script src="jquery-1.4.3.min.js"></script>
<script src="jquery.mobile-1.0a1.min.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
document.addEventListener("backbutton", onBackKeyDown, false);
document.addEventListener("menubutton", showConfirm, false);
}
</head>
Try moving the line that loads Cordova-2.3.0.js below the lines that load your CSS. So something like:
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="css/index.css">
<link rel="stylesheet" href="jquery.mobile-1.0a1.min.css" />
<script type="text/javascript" charset="utf-8" src="cordova-2.3.0.js"></script>
<script src="jquery-1.4.3.min.js"></script>
<script src="jquery.mobile-1.0a1.min.js"></script>
<script type="text/javascript" charset="utf-8">
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
document.addEventListener("backbutton", onBackKeyDown, false);
document.addEventListener("menubutton", showConfirm, false);
}
</head>
It works after i change jquery mobile and jquery to latest version............

Resources