Skip to content

Instantly share code, notes, and snippets.

@happyguohua
Created August 11, 2015 07:13
Show Gist options
  • Select an option

  • Save happyguohua/b9ca56735c03e15f4d8f to your computer and use it in GitHub Desktop.

Select an option

Save happyguohua/b9ca56735c03e15f4d8f to your computer and use it in GitHub Desktop.
less without gulp-less plugin
var buildLatest = process.env.EVC15_BUILD_LATEST;
var path = require('path'),
util = require('util'),
gulp = require('gulp'),
replace = require('gulp-replace'),
gulpUtil = require('gulp-util'),
requirejs = require('requirejs'),
less = require('less'),
fs = require('fs'),
Q = require('q'),
config = require('./bower.json'),
npmConfig = require('./package.json'),
version = buildLatest ? 'latest' : (npmConfig.version || '0.0.1'),
packageName = config.name,
//paths that need to be configured
basePath = 'dist',
dist = path.join(__dirname, basePath, version);
var requirejsCommanConfig = {
optimize: 'uglify2',
uglify2: {
mangle: false
},
generateSourceMaps: true,
preserveLicenseComments: false,
baseUrl: 'bower_components'
}
var appBundleConfig = {
//use _{$packageName}/main as the entry point to avoid name conlict, cuz
//r.js name the opimized anonymous module {$packageName}/name
name: util.format('_%s/main', packageName),
out: path.join(dist, 'main.js'),
wrap: {
end: util.format('define(["_%s/main"], function(m){return m;});', packageName)
},
excludeShallow: [
packageName
],
stubModules: [
'less',
'text',
'rv'
],
paths: {
"text": "requirejs-plugins/lib/text",
"image": "requirejs-plugins/src/image",
"json": "requirejs-plugins/src/json",
"propertyParser": "requirejs-plugins/src/propertyParser",
"ractive": "ractive/ractive",
"fm": "empty:",
"rv": "rv/rv",
"bowser": "bowser/bowser",
"eventemitter2": "eventemitter2/lib/eventemitter2",
"resemble": "resemblejs/resemble",
"evc-ui-app-e15rtctest": ".."
},
shim: {
'resemble': {
exports: 'resemble'
}
},
map: {
'*': {
'less': 'require-less/less'
}
},
packages: [
{
'name': 'jquery',
'location': 'jquery/dist',
'main': 'jquery'
}
]
};
var fmBundleConfig = {
name: "fm/fm.icelink",
out: path.join(dist, 'fmBundle.js'),
paths: {
"fm": "evc15-fe-fm"
},
map: {
'*': {
"fm/icelink": "fm/fm.icelink",
"fm/icelink/webrtc": "fm/fm.icelink.webrtc",
"fm/icelink/websync": "fm/fm.icelink.websync",
"fm/websync": "fm/fm.websync",
"fm/websync/chat": "fm/fm.websync.chat",
"fm/websync/subscribers": "fm/fm.websync.subscribers"
}
},
deps: [
"fm/fm.icelink.webrtc",
"fm/fm.icelink.websync",
"fm/fm.websync",
"fm/fm.websync.chat",
"fm/fm.websync.subscribers"
]
}
_extend(appBundleConfig, requirejsCommanConfig);
_extend(fmBundleConfig, requirejsCommanConfig);
appBundleConfig.paths['_' + packageName] = '..';
gulp.task('build:app.js', function (done) {
requirejs.optimize(appBundleConfig, function (output) {
var messages = output.split('\n');
messages.forEach(function (msg){
gulpUtil.log(msg);
});
done();
});
});
gulp.task('build:fm.js', function (done) {
requirejs.optimize(fmBundleConfig, function (output) {
var messages = output.split('\n');
messages.forEach(function (msg){
gulpUtil.log(msg);
});
done();
});
});
gulp.task('build:less', function (done) {
var concatCss = '';
[
'./bower_components/bootstrap/less/bootstrap.less',
'./main.css'
].reduce(function(first, second) {
return first.then(function() {
return _processLess(second).then(function(result) {
concatCss += result;
});
});
}, Q.resolve())
.then(function() {
fs.writeFileSync(dist + '/main.css', {flag: 'w'}, concatCss);
done();
});
});
gulp.task('build:static', function(){
return gulp.src([ './index.html' ])
.pipe(replace('{{EVC15_COMPONENT_PATH}}', util.format(
'"%s":"%s/%s"',
packageName,
packageName,
version
)))
.pipe(replace(/\{\{EVC15_COMPONENT_VERSION\}\}/g, version))
.pipe(gulp.dest(dist));
});
gulp.task('build', ['build:less', 'build:app.js', 'build:fm.js', 'build:static']);
gulp.task('default', ['build']);
gulp.task('dev', ['build:app.js']);
function _extend(target, source) {
Object.keys(source).forEach(function(key) {
target[key] = source[key];
});
}
function _processLess(filePath) {
return Q.promise(function(resolve) {
less.render(fs.readFileSync(filePath ,{encoding: 'utf-8'})
,{
paths: ['./bower_components/bootstrap/less/']
}
,function(e, output) {
resolve(output);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment