From c411b2c3d3035607b1e6cf18c5b496c6d4e0eb08 Mon Sep 17 00:00:00 2001 From: Jack Moore Date: Wed, 15 Apr 2015 13:33:39 -0400 Subject: [PATCH] updated build script to use babel --- build.js | 52 +++++----- example/index.html | 2 +- package.json | 1 + src/autosize.js | 233 ++++++++++++++++++++++----------------------- 4 files changed, 144 insertions(+), 144 deletions(-) diff --git a/build.js b/build.js index 23f20e1..077c0ef 100644 --- a/build.js +++ b/build.js @@ -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 = { @@ -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, @@ -47,7 +31,7 @@ function lint(full) { eqeqeq: true, eqnull: true, noarg: true, - predef: ['define', 'module'] + predef: ['define', 'module', 'exports'] }); if (jshint.errors.length) { @@ -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); + } }); \ No newline at end of file diff --git a/example/index.html b/example/index.html index c69cda3..0a26d9d 100644 --- a/example/index.html +++ b/example/index.html @@ -22,7 +22,7 @@

max-height 300px

no max-height

- + diff --git a/package.json b/package.json index 1452854..40cb2c0 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ }, "dependencies": {}, "devDependencies": { + "babel": "^5.1.9", "jshint": "^2.5.6", "uglify-js": "^2.4.15", "watch": "^0.14.0" diff --git a/src/autosize.js b/src/autosize.js index 70c81bf..af3af72 100644 --- a/src/autosize.js +++ b/src/autosize.js @@ -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;