0

I have a web project using gulp and browserfy . This is my gulpfile.js

     var gulp = require('gulp'),
    gutil = require('gulp-util'),
    browserify = require('gulp-browserify'),
    compass = require('gulp-compass'),
    connect = require('gulp-connect'),
    gulpif = require('gulp-if'),
    uglify = require('gulp-uglify'),
    minifyHTML = require('gulp-minify-html'),
    concat = require('gulp-concat');
    path = require('path');

var env,
    jsSources,
    sassSources,
    htmlSources,
    outputDir,
    sassStyle;

env = 'development';

if (env==='development') {
  outputDir = 'builds/development/';
  sassStyle = 'expanded';
} else {
  outputDir = 'builds/production/';
  sassStyle = 'compressed';
}

jsSources = [
 'components/scripts/jqloader.js',
 'components/scripts/supersized.core.min.js',
 'components/scripts/slidebars.js',
 'components/scripts/doubletaptogo.js',
  'components/scripts/script.js'
];
sassSources = ['components/sass/style.scss'];
htmlSources = [outputDir + '*.html'];

gulp.task('js', function() {
  gulp.src(jsSources)
    .pipe(concat('script.js'))
    .pipe(browserify())
    .on('error', gutil.log)
    .pipe(gulpif(env === 'production', uglify()))
    .pipe(gulp.dest(outputDir + 'js'))
    .pipe(connect.reload())
});

gulp.task('compass', function() {
  gulp.src(sassSources)
    .pipe(compass({
      sass: 'components/sass',
      css: outputDir + 'css',
      image: outputDir + 'images',
      style: sassStyle,
      require: ['susy', 'breakpoint']
    })
    .on('error', gutil.log))
//    .pipe(gulp.dest( outputDir + 'css'))
    .pipe(connect.reload())
});

gulp.task('watch', function() {
  gulp.watch(jsSources, ['js']);
  gulp.watch(['components/sass/*.scss', 'components/sass/*/*.scss'], ['compass']);
  gulp.watch('builds/development/*.html', ['html']);
});

gulp.task('connect', function() {
  connect.server({
    root: outputDir,
    livereload: true
  });
});

gulp.task('html', function() {
  gulp.src('builds/development/*.html')
    .pipe(gulpif(env === 'production', minifyHTML()))
    .pipe(gulpif(env === 'production', gulp.dest(outputDir)))
    .pipe(connect.reload())
});

// Copy images to production
gulp.task('move', function() {
  gulp.src('builds/development/images/**/*.*')
  .pipe(gulpif(env === 'production', gulp.dest(outputDir+'images')))
});

gulp.task('default', ['watch', 'html', 'js', 'compass', 'move', 'connect']);

My html is :

<!doctype html>
<html>
<head>


</head>
<body >
<div id="divExample"></div>
  <script src="js/script.js"></script>
<script>alert($("divExample").width());</script>



</body>
</html>

I got a error that $ is not define, but it is define in the script. I am not sure if i can use the jquery lib that is include on the script. If not what should i do for use the jquery lib. Do I have to add the jquery again or there is any other solution.

cimmanon
  • 67,211
  • 17
  • 165
  • 171

1 Answers1

0

When you do var $ = require('jquery') in browserify it won't be available outside of that file. Whenever you need jQuery in another file you'll need to do var $ = require('jquery') again (don't worry, it'll only include it one time). You shouldn't access anything compiled with browserify outside of that file. I recommend reading this, it's a bit lengthly but well worth it.

https://github.com/substack/browserify-handbook

If you want to be define globals in browserify you can check out this answer: Defining global variable for Browserify

Community
  • 1
  • 1
Bill Criswell
  • 32,161
  • 7
  • 75
  • 66