Skip to content

Commit

Permalink
updated build script to use babel
Browse files Browse the repository at this point in the history
  • Loading branch information
jackmoore committed Apr 15, 2015
1 parent b871844 commit c411b2c
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 144 deletions.
52 changes: 26 additions & 26 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var pkg = require('./package.json');
var fs = require('fs');
var ugly = require("uglify-js");
var jshint = require("jshint").JSHINT;
var ugly = require('uglify-js');
var jshint = require('jshint').JSHINT;
var babel = require('babel');

function writeBower() {
var bower = {
Expand All @@ -21,23 +22,6 @@ function writeBower() {
return true;
}

function build(full) {
var mini = ugly.minify(full, {fromString: true}).code;
var header = [
"/*!",
" "+pkg.config.title+" "+pkg.version,
" license: MIT",
" "+pkg.homepage,
"*/",
""
].join("\n");

fs.writeFile('dest/'+pkg.config.fileName+'.js', header+full);
fs.writeFile('dest/'+pkg.config.fileName+'.min.js', header+mini);

return true;
}

function lint(full) {
jshint(full.toString(), {
browser: true,
Expand All @@ -47,7 +31,7 @@ function lint(full) {
eqeqeq: true,
eqnull: true,
noarg: true,
predef: ['define', 'module']
predef: ['define', 'module', 'exports']
});

if (jshint.errors.length) {
Expand All @@ -59,10 +43,26 @@ function lint(full) {
}
}

fs.readFile('src/'+pkg.config.fileName+'.js', 'utf8', function (err,data) {
if (err) {
return console.log(err);
} else {
lint(data) && build(data) && writeBower();
}
function build(code) {
var minified = ugly.minify(code, {fromString: true}).code;
var header = [
'/*!',
' '+pkg.config.title+' '+pkg.version,
' license: MIT',
' '+pkg.homepage,
'*/',
''
].join('\n');

fs.writeFile('dest/'+pkg.config.fileName+'.js', header+code);
fs.writeFile('dest/'+pkg.config.fileName+'.min.js', header+minified);
writeBower();
}

babel.transformFile('src/'+pkg.config.fileName+'.js', {modules: 'umd'}, function (err,res) {
if (err) {
return console.log(err);
} else {
lint(res.code) && build(res.code);
}
});
2 changes: 1 addition & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ <h3>max-height 300px</h3>
<h3>no max-height</h3>
<textarea>The coconut palm (also, cocoanut), Cocos nucifera, is a member of the family Arecaceae (palm family). It is the only accepted species in the genus Cocos.[2] The term coconut can refer to the entire coconut palm, the seed, or the fruit, which, botanically, is a drupe, not a nut. The spelling cocoanut is an archaic form of the word.[3] The term is derived from 16th-century Portuguese and Spanish coco, meaning "head" or "skull",[4] from the three small holes on the coconut shell that resemble human facial features.</textarea>
</body>
<script src='../src/autosize.js'></script>
<script src='../dest/autosize.js'></script>
<script>
autosize(document.querySelectorAll('textarea'));
</script>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"dependencies": {},
"devDependencies": {
"babel": "^5.1.9",
"jshint": "^2.5.6",
"uglify-js": "^2.4.15",
"watch": "^0.14.0"
Expand Down
233 changes: 116 additions & 117 deletions src/autosize.js
Original file line number Diff line number Diff line change
@@ -1,131 +1,130 @@
(function (root, factory) {
'use strict';

if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.autosize = factory();
}
}(this, function () {
function main(ta) {
if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || ta.hasAttribute('data-autosize-on')) { return; }

var maxHeight;
var heightOffset;

function init() {
var style = window.getComputedStyle(ta, null);

if (style.resize === 'vertical') {
ta.style.resize = 'none';
} else if (style.resize === 'both') {
ta.style.resize = 'horizontal';
}
function main(ta) {
if (!ta || !ta.nodeName || ta.nodeName !== 'TEXTAREA' || ta.hasAttribute('data-autosize-on')) { return; }

// Chrome/Safari-specific fix:
// When the textarea y-overflow is hidden, Chrome/Safari doesn't reflow the text to account for the space
// made available by removing the scrollbar. This workaround will cause the text to reflow.
var width = ta.style.width;
ta.style.width = '0px';
// Force reflow:
/* jshint ignore:start */
ta.offsetWidth;
/* jshint ignore:end */
ta.style.width = width;

maxHeight = style.maxHeight !== 'none' ? parseFloat(style.maxHeight) : false;

if (style.boxSizing === 'content-box') {
heightOffset = -(parseFloat(style.paddingTop)+parseFloat(style.paddingBottom));
} else {
heightOffset = parseFloat(style.borderTopWidth)+parseFloat(style.borderBottomWidth);
}
var maxHeight;
var heightOffset;

adjust();
function init() {
var style = window.getComputedStyle(ta, null);

if (style.resize === 'vertical') {
ta.style.resize = 'none';
} else if (style.resize === 'both') {
ta.style.resize = 'horizontal';
}

function adjust() {
var startHeight = ta.style.height;
var htmlTop = document.documentElement.scrollTop;
var bodyTop = document.body.scrollTop;

ta.style.height = 'auto';

var endHeight = ta.scrollHeight+heightOffset;

if (maxHeight !== false && maxHeight < endHeight) {
endHeight = maxHeight;
if (ta.style.overflowY !== 'scroll') {
ta.style.overflowY = 'scroll';
}
} else if (ta.style.overflowY !== 'hidden') {
ta.style.overflowY = 'hidden';
}
// Chrome/Safari-specific fix:
// When the textarea y-over is hidden, Chrome/Safari doesn't reflow the text to account for the space
// made available by removing the scrollbar. This workaround will cause the text to reflow.
var width = ta.style.width;
ta.style.width = '0px';
// Force reflow:
/* jshint ignore:start */
ta.offsetWidth;
/* jshint ignore:end */
ta.style.width = width;

maxHeight = style.maxHeight !== 'none' ? parseFloat(style.maxHeight) : false;

if (style.boxSizing === 'content-box') {
heightOffset = -(parseFloat(style.paddingTop)+parseFloat(style.paddingBottom));
} else {
heightOffset = parseFloat(style.borderTopWidth)+parseFloat(style.borderBottomWidth);
}

adjust();
}

ta.style.height = endHeight+'px';
function adjust() {
var startHeight = ta.style.height;
var htmlTop = document.documentElement.scrollTop;
var bodyTop = document.body.scrollTop;

ta.style.height = 'auto';

// prevents scroll-position jumping
document.documentElement.scrollTop = htmlTop;
document.body.scrollTop = bodyTop;
var endHeight = ta.scrollHeight+heightOffset;

if (startHeight !== ta.style.height) {
var evt = document.createEvent('Event');
evt.initEvent('autosize.resized', true, false);
ta.dispatchEvent(evt);
if (maxHeight !== false && maxHeight < endHeight) {
endHeight = maxHeight;
if (ta.style.overflowY !== 'scroll') {
ta.style.overflowY = 'scroll';
}
} else if (ta.style.overflowY !== 'hidden') {
ta.style.overflowY = 'hidden';
}

ta.addEventListener('autosize.destroy', function(style){
window.removeEventListener('resize', adjust);
ta.removeEventListener('input', adjust);
ta.removeEventListener('keyup', adjust);
ta.removeAttribute('data-autosize-on');
ta.removeEventListener('autosize.destroy');

Object.keys(style).forEach(function(key){
ta.style[key] = style[key];
});
}.bind(ta, {
height: ta.style.height,
overflowY: ta.style.overflowY,
resize: ta.style.resize
}));

// IE9 does not fire onpropertychange or oninput for deletions,
// so binding to onkeyup to catch most of those events.
// There is no way that I know of to detect something like 'cut' in IE9.
if ('onpropertychange' in ta && 'oninput' in ta) {
ta.addEventListener('keyup', adjust);
}
ta.style.height = endHeight+'px';

// prevents scroll-position jumping
document.documentElement.scrollTop = htmlTop;
document.body.scrollTop = bodyTop;

window.addEventListener('resize', adjust);
ta.addEventListener('input', adjust);
ta.addEventListener('autosize.update', adjust);
ta.setAttribute('data-autosize-on', true);
ta.style.overflowY = 'hidden';
init();
if (startHeight !== ta.style.height) {
var evt = document.createEvent('Event');
evt.initEvent('autosize.resized', true, false);
ta.dispatchEvent(evt);
}
}

// Do nothing in IE8 or lower
if (typeof window.getComputedStyle !== 'function') {
return function(elements) {
return elements;
};
} else {
return function(elements) {
if (elements && elements.length) {
Array.prototype.forEach.call(elements, main);
} else if (elements && elements.nodeName) {
main(elements);
}
return elements;
};
ta.addEventListener('autosize.destroy', function(style){
window.removeEventListener('resize', adjust);
ta.removeEventListener('input', adjust);
ta.removeEventListener('keyup', adjust);
ta.removeAttribute('data-autosize-on');
ta.removeEventListener('autosize.destroy');

Object.keys(style).forEach(function(key){
ta.style[key] = style[key];
});
}.bind(ta, {
height: ta.style.height,
overflowY: ta.style.overflowY,
resize: ta.style.resize
}));

// IE9 does not fire onpropertychange or oninput for deletions,
// so binding to onkeyup to catch most of those events.
// There is no way that I know of to detect something like 'cut' in IE9.
if ('onpropertychange' in ta && 'oninput' in ta) {
ta.addEventListener('keyup', adjust);
}
}));

window.addEventListener('resize', adjust);
ta.addEventListener('input', adjust);
ta.addEventListener('autosize.update', adjust);
ta.setAttribute('data-autosize-on', true);
ta.style.overflowY = 'hidden';
init();
}

var autosize;

// Do nothing in IE8 or lower
if (typeof window.getComputedStyle !== 'function') {
autosize = function(elements) {
return elements;
};
autosize.destroy = function(){};
autosize.update = function(){};
} else {
autosize = function(elements) {
if (elements && elements.length) {
Array.prototype.forEach.call(elements, main);
} else if (elements && elements.nodeName) {
main(elements);
}
return elements;
};
autosize.destroy = function(ta) {
var evt = document.createEvent('Event');
evt.initEvent('autosize.destroy', true, false);
ta.dispatchEvent(evt);
};
autosize.update = function(ta) {
var evt = document.createEvent('Event');
evt.initEvent('autosize.update', true, false);
ta.dispatchEvent(evt);
};
}

export default autosize;

0 comments on commit c411b2c

Please sign in to comment.