Last active
August 12, 2021 20:26
-
-
Save jlandure/b8ff6fedd7ff34aff76a to your computer and use it in GitHub Desktop.
Gulp config with browserify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* eslint-env node */ | |
| "use strict"; | |
| var del = require("del"); | |
| var eslint = require("gulp-eslint"); | |
| var gulp = require("gulp"); | |
| var gutil = require("gulp-util"); | |
| var mkdirp = require("mkdirp"); | |
| var nodemon = require("gulp-nodemon"); | |
| var zip = require("gulp-zip"); | |
| var packageVersion = require("./package.json"); | |
| var request = require("request"); | |
| var fs = require("fs"); | |
| var logDir = "./target/logs"; | |
| gulp.task("lint", function() { | |
| gulp.src(["gulpfile.js", "app.js", "lib/**/*.js"]) | |
| .pipe(eslint()) | |
| .pipe(eslint.format()); | |
| }); | |
| gulp.task("create", function() { | |
| mkdirp(logDir, function(err) { | |
| if (err) { | |
| console.error(err); | |
| } | |
| }); | |
| }); | |
| gulp.task("clean", function() { | |
| return del.sync(["./target", "dist"]); | |
| }); | |
| gulp.task("copyConf", ["clean"], function() { | |
| return gulp.src("conf/**/*.json") | |
| .pipe(gulp.dest("./target/conf")); | |
| }); | |
| gulp.task("copyLib", ["clean"], function() { | |
| return gulp.src("lib/**") | |
| .pipe(gulp.dest("./target/lib")); | |
| }); | |
| gulp.task("copyAppJs", ["clean"], function() { | |
| return gulp.src("app.js") | |
| .pipe(gulp.dest("./target")); | |
| }); | |
| gulp.task("copyPackageJson", ["clean"], function() { | |
| return gulp.src("package.json") | |
| .pipe(gulp.dest("./target")); | |
| }); | |
| gulp.task("prepareProduction", ["copyConf", "copyLib", "copyAppJs", "copyPackageJson"], function() { | |
| return true; | |
| }); | |
| gulp.task("production", function() { | |
| var fileName = packageVersion.name + "-" + packageVersion.version + ".zip"; | |
| return gulp.src("./target/**") | |
| .pipe(zip(fileName)) | |
| .pipe(gulp.dest("dist")); | |
| }); | |
| gulp.task("publish", ["production"], function() { | |
| var fileName = packageVersion.name + "-" + packageVersion.version + ".zip"; | |
| var filePath = "./dist/" + fileName; | |
| fs.createReadStream(filePath).pipe( | |
| request.post( | |
| { | |
| url: "http://nexus:8081/nexus/content/repositories/artifact-snapshots/" + packageVersion.name + "/" + fileName, | |
| auth: { | |
| username: "", | |
| password: "" | |
| } | |
| }) | |
| .on("error", function(err) { | |
| throw new Error("Error while uploading %s", err); | |
| }) | |
| .on("response", function(response) { | |
| gutil.log("Upload DONE ! " + response.statusCode); | |
| }) | |
| ); | |
| }); | |
| gulp.task("demon", function () { | |
| nodemon({ | |
| script: "app.js", | |
| watch: ["app.js", "lib", "conf"], | |
| ext: "js json", | |
| env: { | |
| "ENVIRONMENT": "local", | |
| "LOG_DIR": logDir | |
| }, | |
| ignore: ["./node_modules/**"] | |
| }) | |
| .on("start", ["lint", "create"]) | |
| .on("change", ["lint"]) | |
| .on("restart", function () { | |
| console.log("restarted!"); | |
| }); | |
| }); | |
| //Default Task | |
| gulp.task("default", ["demon"]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* eslint-env node */ | |
| "use strict"; | |
| var browserify = require("browserify"); | |
| var browserSync = require("browser-sync"); | |
| var del = require("del"); | |
| var eslint = require("gulp-eslint"); | |
| var gulp = require("gulp"); | |
| var gutil = require("gulp-util"); | |
| var proxy = require("proxy-middleware"); | |
| var source = require("vinyl-source-stream"); | |
| var buffer = require("vinyl-buffer"); | |
| var concat = require("gulp-concat"); | |
| var htmlmin = require("gulp-minify-html"); | |
| var uglify = require("gulp-uglify"); | |
| var url = require("url"); | |
| var watchify = require("watchify"); | |
| var ngHtml2Js = require("gulp-ng-html2js"); | |
| var cdnizer = require("gulp-cdnizer"); | |
| var inject = require("gulp-inject"); | |
| var zip = require("gulp-zip"); | |
| var packageVersion = require("./package.json"); | |
| var vpPortailCdn = require("vp-portail-cdn"); | |
| var request = require("request"); | |
| var fs = require("fs"); | |
| var path = require("path"); | |
| var less = require("gulp-less");//-sourcemap | |
| var sourcemaps = require("gulp-sourcemaps"); | |
| var flatten = require("gulp-flatten"); | |
| var autoprefixer = require("gulp-autoprefixer"); | |
| var usemin = require("gulp-usemin"); | |
| var cssmin = require("gulp-minify-css"); | |
| var rev = require("gulp-rev"); | |
| var fixWindowsSourceMaps = require("gulp-fix-windows-source-maps"); | |
| function vpCdnify() { | |
| return cdnizer({ | |
| //specify the file to use based on vp-portail-cdn project | |
| files: [ | |
| { | |
| file: "jquery/dist/jquery.js", | |
| cdn: "" | |
| }, | |
| { | |
| file: "angular/angular.js", | |
| cdn: "" | |
| }, | |
| { | |
| file: "bootstrap/dist/js/bootstrap.js", | |
| cdn: "" | |
| }, | |
| { | |
| file: "moment/min/moment-with-locales.js", | |
| cdn: "" | |
| }, | |
| { | |
| file: "lodash/dist/lodash.js", | |
| cdn: "" | |
| }, | |
| { | |
| file: "angular-resource/angular-resource.js", | |
| cdn: "" | |
| }, | |
| { | |
| file: "angular-sanitize/angular-sanitize.js", | |
| cdn: "" | |
| }, | |
| { | |
| file: "angular-ui-router/release/angular-ui-router.js", | |
| cdn: "" | |
| }, | |
| { | |
| file: "angular-strap/dist/angular-strap.js", | |
| cdn: "//cdnjs.cloudflare.com/ajax/libs/angular-strap/2.1.2/angular-strap.min.js" | |
| }, | |
| { | |
| file: "angular-strap/dist/angular-strap.tpl.js", | |
| cdn: "//cdnjs.cloudflare.com/ajax/libs/angular-strap/2.1.2/angular-strap.tpl.min.js" | |
| }, | |
| { | |
| file: "bootstrap/dist/css/bootstrap.css", | |
| cdn: "//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" | |
| }, | |
| { | |
| file: "font-awesome/css/font-awesome.css", | |
| cdn: "//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" | |
| } | |
| ] | |
| }); | |
| } | |
| function getVersion(name) { | |
| var npmVersion = packageVersion.dependencies[name]; | |
| if (npmVersion.indexOf("^") !== 0 || npmVersion.indexOf("~") !== 0) { | |
| return npmVersion.substring(1); | |
| } | |
| return npmVersion; | |
| } | |
| gulp.task("lint", function() { | |
| gulp.src(["gulpfile.js", "app/**/*.js"]) | |
| .pipe(eslint()) | |
| .pipe(eslint.format()); | |
| }); | |
| gulp.task("styles", function () { | |
| return gulp.src(["app/**/*.less", "!app/common/variables.less"]) | |
| .pipe(sourcemaps.init({loadMaps: true})) | |
| .pipe(less({ | |
| paths: [path.join(__dirname, "app")], | |
| rootpath: "../" | |
| })) | |
| .pipe(autoprefixer({ | |
| browsers: ["last 2 versions"], | |
| cascade: false | |
| })) | |
| .pipe(flatten()) | |
| .pipe(sourcemaps.write("./")) | |
| .pipe(gulp.dest("./target")) | |
| .on("error", function(error) { | |
| //If you want details of the error in the console | |
| console.log(error.toString()); | |
| }); | |
| }); | |
| gulp.task("browserify", ["copy"], function() { | |
| var bundler = browserify(".", | |
| { | |
| //the next three options are required by watchify | |
| cache: {}, | |
| packageCache: {}, | |
| fullPaths: true, | |
| debug: true, | |
| transform: ["browserify-shim", "browserify-ngannotate"] | |
| }); | |
| if (global.enableWatchify) { | |
| bundler = watchify(bundler); | |
| bundler.on("update", bundle); | |
| } | |
| function bundle() { | |
| return bundler.bundle() | |
| .on("error", gutil.log.bind(gutil, "Browserify Error")) | |
| .pipe(source("scripts.js")) | |
| .pipe(buffer()) | |
| .pipe(fixWindowsSourceMaps()) | |
| .pipe(gulp.dest("./target/")); | |
| } | |
| return bundle(); | |
| }); | |
| gulp.task("html2js", ["clean"], function() { | |
| return gulp.src(["app/**/*.html", "!app/index.html"]) | |
| .pipe(htmlmin({ | |
| empty: true, | |
| spare: true, | |
| quotes: true | |
| })) | |
| .pipe(ngHtml2Js({ | |
| moduleName: "[MODULE_NAME]" | |
| })) | |
| .pipe(concat("templates.js")) | |
| .pipe(gulp.dest("./target")); | |
| }); | |
| gulp.task("browserifyProduction", ["html2js"], function() { | |
| return browserify(".", | |
| { | |
| //the next three options are required by watchify | |
| cache: {}, | |
| packageCache: {}, | |
| fullPaths: false, | |
| transform: ["browserify-shim", "browserify-ngannotate"] | |
| }) | |
| .bundle() | |
| .pipe(source("scripts.js")) | |
| .pipe(buffer()) //convert from streaming to buffered vinyl file object | |
| .pipe(gulp.dest("./target")); | |
| }); | |
| gulp.task("prepareProduction", ["browserifyProduction", "styles"], function() { | |
| return gulp.src("./app/index.html") | |
| .pipe(inject(gulp.src(["templates.js"], {cwd: "target", relative: false, addRootSlash: false, read: false}))) //add templates.js in index.html | |
| .pipe(usemin({ | |
| css: [ | |
| cssmin(), | |
| "concat", | |
| rev() | |
| ], | |
| html: [ | |
| htmlmin({ | |
| empty: true | |
| }) | |
| ], | |
| js: [ | |
| uglify(), | |
| rev() | |
| ] | |
| })) | |
| .pipe(vpCdnify()) //cdnify | |
| .pipe(gulp.dest("./target")); | |
| }); | |
| gulp.task("production", ["prepareProduction"], function() { | |
| del.sync(["target/common.css", "target/login.css", "target/scripts.js", "target/templates.js", "target/**/*.map"]); | |
| return gulp.src("./target/**") | |
| .pipe(zip(packageVersion.name + "-" + packageVersion.version + ".zip")) | |
| .pipe(gulp.dest("dist")); | |
| }); | |
| gulp.task("publish", ["production"], function() { | |
| var fileName = packageVersion.name + "-" + packageVersion.version + ".zip"; | |
| var filePath = "./dist/" + fileName; | |
| fs.createReadStream(filePath).pipe( | |
| request.post( | |
| { | |
| url: "http://nexus:8081/nexus/content/repositories/artifact-snapshots/" + packageVersion.name + "/" + fileName, | |
| auth: { | |
| username: "nexus", | |
| password: "nexus" | |
| } | |
| }) | |
| .on("error", function(err) { | |
| throw new Error("Error while uploading %s", err); | |
| }) | |
| .on("response", function(response) { | |
| gutil.log("Upload DONE ! " + response.statusCode); | |
| }) | |
| ); | |
| }); | |
| gulp.task("copy", function() { | |
| del.sync("target/**/*.html"); | |
| return gulp.src("app/**/*.html") | |
| .pipe(gulp.dest("target/")); | |
| }); | |
| gulp.task("clean", function() { | |
| //use del.sync to disable async problem with the others tasks | |
| return del.sync(["target", "dist"]); | |
| }); | |
| gulp.task("build", ["lint", "styles", "browserify"]); | |
| gulp.task("serve", ["build"], function() { | |
| var proxyOptions = url.parse("http://localhost:8000/api/"); | |
| proxyOptions.route = "/api"; | |
| browserSync({ | |
| server: { | |
| baseDir: ["target/", "node_modules/"], | |
| middleware: proxy(proxyOptions) | |
| }, | |
| port: 8080, | |
| files: [ | |
| "target/**" | |
| ], | |
| open: false, | |
| notify: false | |
| }); | |
| }); | |
| gulp.task("enableWatchify", function() { | |
| global.enableWatchify = true; | |
| }); | |
| gulp.task("watch", ["enableWatchify", "serve"], function() { | |
| gulp.watch(["gulpfile.js", "app/**/*.js"], ["lint"]); | |
| gulp.watch("app/**/*.html", ["copy"]); | |
| gulp.watch("app/**/*.less", ["styles"]); | |
| }); | |
| gulp.task("default", ["clean", "watch"]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment