Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress等で<script>で読み込まれているjQueryとかが居る場合にbrowserifyで上手いことやる。
'use strict';
// ==================================
//
// Config.
//
// ==================================
var config = {
browserify: {
bundleOption: {
cache: {}, packageCache: {}, fullPaths: false, //for watchfy.
debug: true,
entries: './src/scripts/app.js',
extensions: ['js', 'jsx'],
},
dest: './dist/scripts/',
filename: 'app.js'
}
}
// ==================================
//
// Load modules.
//
// ==================================
var source = require('vinyl-source-stream');
var browserify = require('browserify');
var watchify = require('watchify');
var gulp = require('gulp');
var notify = require("gulp-notify");
var handleErrors = function() {
var args = Array.prototype.slice.call(arguments);
notify.onError({
title: "Compile Error",
message: "<%= error.message %>"
}).apply(this, args);
this.emit('end');
};
// ==================================
//
// Compile JavaScripts.
//
// ==================================
gulp.task('setWatch', function () {
global.isWatching = true;
});
gulp.task('browserify', function () {
var b = browserify(config.browserify.bundleOption)
.transform('babelify')
.transform("browserify-shim")
.transform("debowerify");
var bundle = function () {
b.bundle().on('error', handleErrors)
.pipe(source(config.browserify.filename))
.pipe(gulp.dest(config.browserify.dest));
};
if (global.isWatching) {
var bundler = watchify(b);
bundler.on('update', bundle);
}
bundle();
});
gulp.task('watchify', ['setWatch', 'browserify']);
{
"browserify-shim": {
"jquery": "global:jQuery",
"underscore": "global:_",
"backbone": "global:Backbone"
},
"devDependencies": {
"babelify": "^6.1.3",
"browserify": "^11.0.0",
"browserify-shim": "^3.8.10",
"debowerify": "^1.3.1",
"gulp": "^3.9.0",
"gulp-notify": "^2.2.0",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0",
"watchify": "^3.3.0"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Toro_Unit",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment