jonkemp / gulp-useref

Parse build blocks in HTML files to replace references to non-optimized scripts or stylesheets.
MIT License
705 stars 93 forks source link

Unable to use useref for JavaScript #231

Closed mikaelplouhinecvetup closed 7 years ago

mikaelplouhinecvetup commented 7 years ago

Hello,

I have this in my php file :

<!-- build:js scripts/lib/ext-lib.min.js -->
<script type="text/javascript" src="scripts/lib/bowser-1.6.0.js"></script>
<script type="text/javascript" src="scripts/lib/jquery-3.1.1.min.js"></script>
<script type="text/javascript" src="scripts/lib/jquery-ui-1.12.1.min.js"></script>
<script type="text/javascript" src="scripts/lib/sortable.js"></script>
<!-- endbuild -->

When I execute the following gulpfile task,

// Update css, JS reference in index.php
gulp.task('updateReferenceInPHP', function(){
  return gulp.src(target + "/*.php")
        .pipe(debug())
        .pipe(useref())
        .pipe(debug())
        .pipe(gulp.dest(target));
});

I receive the following error :

[16:51:18] Starting 'updateReferenceInPHP'...
[16:51:18] gulp-debug: app\dist\index.php
[16:51:18] gulp-debug: app\dist\index.php
events.js:160
  throw er; // Unhandled 'error' event
  ^

Error: Error: File not found with singular glob: D:\Projets\vetup\myband-    player\source\app\dist\scripts\lib\bowser-1.6.0.js
at DestroyableTransform.<anonymous> (D:\Projets\vetup\myband-player\source\node_modules\gulp-useref\index.js:65:28)
at emitOne (events.js:101:20)
at DestroyableTransform.emit (events.js:188:7)
at emitOne (events.js:101:20)
at Through2.emit (events.js:188:7)
at OrderedStreams.<anonymous> (D:\Projets\vetup\myband-player\source\node_modules\gulp-useref\node_modules\glob-stream\index.js:140:20)
at emitOne (events.js:96:13)
at OrderedStreams.emit (events.js:188:7)
at emitOne (events.js:96:13)
at DestroyableTransform.emit (events.js:188:7)

I thought userref doesn't check the existence of files. Is there a problem in my task?

jonkemp commented 7 years ago

Are you saying D:\Projets\vetup\myband- player\source\app\dist\scripts\lib\bowser-1.6.0.js the file is not there?

mikaelplouhinecvetup commented 7 years ago

Yes that'r right.

Its the same for css.

In my PHP I have also :

<!-- build:css styles/main-min.css -->
    <link rel="stylesheet" type="text/css" href="styles/main.css?cache-desktop=<?php echo APP_VERSION; ?>">
    <!-- endbuild -->

For the css, I don't have the main.css file in my target folder. I thought JS would be work the same way as CSS.

For more informations, this is my gulpfile :


var gulp = require('gulp');
var csso = require('gulp-csso');
var rename = require('gulp-rename');
var del = require('del');
var debug = require('gulp-debug');
var stripDebug = require('gulp-strip-debug');
var useref = require('gulp-useref');
var count = require('gulp-count');
var bowerMain    = require('main-bower-files');
var concat       = require('gulp-concat');
var less         = require('gulp-less');
var sourcemaps   = require('gulp-sourcemaps');
var gulpFilter   = require('gulp-filter');
var uglify       = require('gulp-uglify');
var flatten      = require('gulp-flatten');
var vulcanize = require('gulp-vulcanize');

// Déclaration des constantes de paths
var source = './app/public';
var target = './app/dist';

// Déclaration des répertoires à copier tels quels
var foldersToBeCopied = [
  source + "/bower_components/**/*",
  source + "/config/**/*",
  source + "/elements/**/*",
  source + "/images/**/*",
  source + "/locales/**/*",
  source + "/scripts/**/*",
  source + "/sendgrid-php/**/*",
  source + "/styles/**/*",
  source + "/*",
  "!"+source+"/bower.json",
  "!"+source+"/styles/*.css", // On ne copie pas le css car on le minifie
  "!"+source+"/scripts/lib/*.js" // On ne copie pas le js des libs ext
];

// clean dist folder
gulp.task('cleanTargetFolder', function(){
  return del(target + "/*", {force:true});
});

// full copy
gulp.task('copyToTarget', ['cleanTargetFolder'], function(){
  return gulp.src(foldersToBeCopied, { base: source }).
    pipe(gulp.dest(target));
});

// Minify du css
gulp.task('minifyCss', function(){

    return gulp.src([source + '/styles/**/*.css', source + '!styles/**/*.min.css'])
      .pipe(csso())
      .pipe(rename({
        suffix: '.min'
      }))
      .pipe(gulp.dest(target + "/styles/"));

});

// Ext lib JS
gulp.task('extLibJS', function(){

    return gulp.src([source + '/scripts/lib/*.js'])
      .pipe(concat('ext-lib.js'))
      .pipe(rename({
        suffix: '.min'
      }))
      .pipe(gulp.dest(target + "/scripts/lib/"));

});

// Update css, JS reference in index.php
gulp.task('updateReferenceInPHP', function(){
  return gulp.src(target + "/*.php")
        .pipe(debug())
        .pipe(useref())
        .pipe(debug())
        .pipe(gulp.dest(target));
});
jonkemp commented 7 years ago

Yeah the files have to be there or you have to give it the path to the files.

mikaelplouhinecvetup commented 7 years ago

Ok. I'm going to try specifying not the target path but the source path.

Could you just explain me why It works for css files and not for JS files?

jonkemp commented 7 years ago

No. It shouldn't work for CSS files either.

mikaelplouhinecvetup commented 7 years ago

But id does ^^

Thanks for your help.