Using Gulp useref concatenate in footer.php(templating) - javascript

I use Gulp as my taskrunner. In my gulpfile.js I use useref to concatenate my JS files. It works fine, when my scripts are in my index.php file. But if I try move them to ex footer.php for templating reasons, the useref won't concatenate the scripts to dist/master/footer.php
Gulp code:
gulp.task('useref', function() {
return gulp.src('app/**/*.php')
.pipe(useref())
.pipe(gulpIf('*.js', uglify()))
.pipe(gulpIf('*.css', cssnano()))
.pipe(gulp.dest('dist'));
});
footer.php file:
</main>
<!-- page content end-->
<!-- page footer -->
<footer class="site-footer">
test
</footer>
<!--build:js js/app.min.js -->
<script src="js/jquery.js"></script>
<script src="js/jquery-validate.js"></script>
<script src="js/skip-link.js"></script> <!-- script for keyboards users to tap easy to content -->
<script src="js/svg4all.js"></script>
<script src="js/picturefill.js"></script>
<script src="js/app.js"></script>
<!-- endbuild -->
</body>
</html>
I include the footer.php in my index.php file(at the end of the doc).
Is it possible to make gulp useref to concatenate JS files in a templating file as my footer.php?

Related

gulp useref not working with multiple html files

I have 5 HTML files in my project and every has a useref block in the bottom like this:
<!-- build:js static/js/main.js -->
<script src="static/js/plugins.js"></script>
<!-- endbuild -->
Common JS file in all 5 files are plugins.js . I need that JS file in every HTML file and its repeating in all my files.
My second HTML file has this block (again plugins.js is there) :
<!-- build:js static/js/main.js -->
<script src="static/js/jquery.barrating.min.js"></script>
<script src="static/js/plugins.js"></script>
<!-- endbuild -->
And my fifth HTML file has this block:
<!-- build:js static/js/main.js -->
<script src="static/js/jquery.matchHeight-min.js"></script>
<script src="static/js/jquery.barrating.min.js"></script>
<script src="static/js/plugins.js"></script>
<!-- endbuild -->
So in every file im using plugins.js and in some files i use some other libraries. But when i run task this three files are not concatenated into main.js. Only thing in main.js is content from plugins.js . Other libraries are not included into main.js.
Why is that so? Can i even use this plugin like that?
My gulp task:
gulp.task('compress:js:html', ['clear:dist'], function () {
return gulp.src('./*.html')
.pipe(useref())
.pipe(htmlmin({collapseWhitespace: true}))
.pipe(gulp.dest('./dist/'))
});
All your HTML files specify the same location for the concatenated file (static/js/main.js), but they all specify different source files that should be concatenated. Depending on the order in which your HTML files are processed you end up with a different static/js/main.js. In your case the one from your first example.
You have two options:
For each HTML file specify a different location for the concatenated file:
HTML File 1:
<!-- build:js static/js/main1.js -->
<script src="static/js/plugins.js"></script>
<!-- endbuild -->
HTML File 2:
<!-- build:js static/js/main2.js -->
<script src="static/js/jquery.barrating.min.js"></script>
<script src="static/js/plugins.js"></script>
<!-- endbuild -->
HTML File 3:
<!-- build:js static/js/main3.js -->
<script src="static/js/jquery.matchHeight-min.js"></script>
<script src="static/js/jquery.barrating.min.js"></script>
<script src="static/js/plugins.js"></script>
<!-- endbuild -->
This will reduce the number of requests a browser has to make for each page, but it doesn't leverage browser caching.
Refer to all JS files in each HTML file, whether you need the JS file for that specific page or not. That means you have the following in all three of your HTML files:
<!-- build:js static/js/main.js -->
<script src="static/js/jquery.matchHeight-min.js"></script>
<script src="static/js/jquery.barrating.min.js"></script>
<script src="static/js/plugins.js"></script>
<!-- endbuild -->
Since browsers will cache the resulting main.js file it will only have to be downloaded once.

Keeping all the JavaScript in the bottom of my page using Response.Filter in .NET MVC

I have loaded some external JavaScript and some inline JavaScript to View page in my MVC project. I have heard about Response filter to move these scripts to the end of the page after content loaded but I have not exact knowledge about this.
How can I achieve this in my whole project?
This article http://weblogs.asp.net/imranbaloch/bundling-and-minifying-inline-css-and-js describes a solution for your problem.
I hope it helps.
I would use build tasks (either using Grunt or Gulp) to combine and minify your scripts as static content and to rewrite your HTML to include CSS and JS resources where you want them in your page.
You could use a Gulp task such as https://www.npmjs.com/package/gulp-uglify to combine and minify and then https://github.com/jonkemp/gulp-useref
The whole task would look something like this:
gulp.task('html', function () {
var assets = useref.assets();
return gulp.src('app/*.html')
.pipe(assets)
.pipe(gulpif('*.js', uglify()))
.pipe(gulpif('*.css', minifyCss()))
.pipe(assets.restore())
.pipe(useref())
.pipe(gulp.dest('dist'));
});
In addition to minifying and combining assets, it allows you to define which parts of your HTML you want to target for the output:
<html>
<head>
<!-- build:css css/combined.css -->
<link href="css/one.css" rel="stylesheet">
<link href="css/two.css" rel="stylesheet">
<!-- endbuild -->
</head>
<body>
<!-- build:js scripts/combined.js -->
<script type="text/javascript" src="scripts/one.js"></script>
<script type="text/javascript" src="scripts/two.js"></script>
<!-- endbuild -->
</body>
</html>
And rewrite it into this:
<html>
<head>
<link rel="stylesheet" href="css/combined.css"/>
</head>
<body>
<script src="scripts/combined.js"></script>
</body>
</html>

including external js files in laravel 5.1.2

I'm working with laravel for the first time, i'm currently running the 5.1.2 version, i'm having issues with including external js files from the /public/js/ directory into the master view file using the asset() function. it works with css files though. i have this in my master file:
<!-- load angularJS lib -->
<script src="{{ asset('js/angular/angular.min.js') }}" type="text/javascript"></script>
<!-- load angularJS loader lib -->
<script src="{{asset('js/angular/angular-loader.min.js')}}" type="text/javascript"></script>
<script src="{{asset('js/metro.js')}}" type="text/javascript"></script>
<script src="{{asset('js/app.style.js')}}" type="text/javascript"></script>
<script src="{{asset('js/Jquery/jquery-2.1.4.js')}}" type="text/javascript"></script>
Page renders HTML source code as such:
<!-- load angularJS lib -->
<script src="http://localhost/Online-Phone/public/js/angular/angular.min.js" type="text/javascript"></script>
<!-- load angularJS loader lib -->
<script src="http://localhost/Online-Phone/public/js/angular/angular-loader.min.js" type="text/javascript"></script>
<script src="http://localhost/Online-Phone/public/js/metro.js" type="text/javascript"></script>
<script src="http://localhost/Online-Phone/public/js/app.style.js" type="text/javascript"></script>
<script src="http://localhost/Online-Phone/public/js/Jquery/jquery-2.1.4.js" type="text/javascript"></script>
Including files located in "public/" folder.
1) "URL::asset()" works only with ".blade.php" file.
<script type="text/javascript" src="{{ URL::asset('js/jquery-ui.js') }}"></script>
<link rel="stylesheet" href="{{ URL::asset('css/jquery-ui.css') }}">
2) Plain file
<script type="text/javascript" src="/js/jquery-3.3.1.min.js"></script>
Try this.
{{HTML::style('css/style.css')}}
{{ HTML::script('js/javascript.js') }}
Try this, It works
<script type="text/javascript" src="{{URL::asset('js/angular/angular.min.js')}}"></script>

Complette workflow for minifying and concatenating (angular) web apps with gulp

It feels like 99% of all tutorials about using gulp.js and the right workflow showing only the part where the js / css files are (for example) minified and concatenated.
But what I haven't found is what to do next to get the right index.html file with the new processed files correctly referenced inside without the need of manual editing the file.
Example index.html:
<head>
<link rel="stylesheet" href="css/bootstrap.min.css">
<link rel="stylesheet" href="css/app.css">
</head>
<body>
[...]
<script src="javascript/angular.min.js"></script>
<script src="javascript/angular-animate.min.js"></script>
<script src="javascript/ui-bootstrap-tpls-0.11.0.min.js"></script>
<script src="javascript/app/app.js"></script>
<script src="javascript/app/controllers/controller.js"></script>
<script src="javascript/app/services/factory.js"></script>
</body>
And the result should be something like this:
<head>
<link rel="stylesheet" href="css/combined.css">
</head>
<body>
[...]
<script src="javascript/combined.js"></script>
</body>
where inside the combined.* files are all css/js files concatenated but only these minified with gulp, that do not end with *.min.js or *.min.css.
Gulp-usemin and gulp-useref are not suitable for, because they concatenate all files inside a build block and after that can you minify the (combined) result (what would minify already minified files again - I do not want that).
How can this missing last step look like to make the workflow complete?
As it seams not to be any satisfying answer / solution to my question as stated above, I ended up using another approach that works as expected good on production side and have benefits during development.
I am using always the unminified versions of the vendor libs (as AngularJS, jQuery etc.) and the HTML looks then like this (with special comments for useref):
<head>
<!-- build:css css/styles.css -->
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/app.css">
<!-- endbuild -->
</head>
<body>
[...]
<!-- build:js js/scripts.js -->
<script src="javascript/angular.js"></script>
<script src="javascript/angular-animate.js"></script>
<script src="javascript/ui-bootstrap-tpls-0.11.0.js"></script>
<script src="javascript/app/app.js"></script>
<script src="javascript/app/controllers/controller.js"></script>
<script src="javascript/app/services/factory.js"></script>
<!-- endbuild -->
</body>
The gulpfile.js:
var gulp = require('gulp'),
useref = require('gulp-useref'),
gulpif = require('gulp-if'),
uglify = require('gulp-uglify'),
cleanCSS = require('gulp-clean-css'),
rev = require('gulp-rev'),
revReplace = require('gulp-rev-replace');
gulp.task('default', ['combineminify']);
gulp.task('combineminify', function () {
return gulp.src('index.html')
.pipe(useref())
.pipe(gulpif('*.js', uglify({
preserveComments: 'license'
})))
.pipe(gulpif('*.css', cleanCSS({
keepSpecialComments: '*'
})))
.pipe(gulpif('*.js', rev()))
.pipe(gulpif('*.css', rev()))
.pipe(revReplace())
.pipe(gulp.dest('/path/to/destination/folder/'));
});
This gulp script creates then a new index.html file in the destination folder:
<head>
<link rel="stylesheet" href="css/styles-1627b7eb0c.css">
</head>
<body>
[...]
<script src="js/scripts-595ed88b32.js"></script>
</body>
And the expected two uglified and minified *.css and *.js files (with a calculated string attached to the filename to avoid caching issues in some browsers) are created in the folders ./css and ./js.
Try gulp-html-replace, wrapping your scripts or css:
<!-- build:test-js -->
stuff to be replaced
<!-- endbuild -->
In your gulpfile:
.pipe(htmlreplace({
'test-js': 'myfile.min.js'
}))

angularjs controller module reference loads blank page

I'm trying to implement the angular-wizard module.
I have a angular app generated by yeoman. I used bower install angular-wizard to get the module and referenced it in my index.html right above the app.js script tag with the other modules, like angular-resource, angular-cookies etc.
The main view loads fine until I add the mgo-angular-wizard reference to the controller dependencies, when added the page loads blank with no errors in the console.
eg:
'use strict';
angular.module('merlinApp', ['mgo-angular-wizard']) // this is where everything breaks
.controller('MainCtrl', function ($scope) {
$scope.awesomeThings = [
'HTML5 Boilerplate',
'AngularJS',
'Karma'
];
});
My index.html has the following scripts included in this order:
<script src="bower_components/jquery/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
<!-- build:js scripts/modules.js -->
<script src="bower_components/angular-resource/angular-resource.js"></script>
<script src="bower_components/angular-cookies/angular-cookies.js"></script>
<script src="bower_components/angular-sanitize/angular-sanitize.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="bower_components/angular-wizard/dist/angular-wizard.js"></script>
<!-- endbuild -->
<!-- build:js({.tmp,app}) scripts/scripts.js -->
<script src="scripts/app.js"></script>
<script src="scripts/controllers/main.js"></script>
<!-- endbuild -->
Lodash (or Underscore) dependency ?
If still not working, try adding wizard directive in your html page.

Categories