| 1 |
var pkg = require('./package.json'); |
| 2 |
var fs = require('fs'); |
| 3 |
var ugly = require('uglify-js'); |
| 4 |
var jshint = require('jshint').JSHINT; |
| 5 |
var babel = require('babel-core'); |
| 6 |
var gaze = require('gaze'); |
| 7 |
|
| 8 |
function lint(full) { |
| 9 |
jshint(full.toString(), { |
| 10 |
browser: true, |
| 11 |
undef: true, |
| 12 |
unused: true, |
| 13 |
immed: true, |
| 14 |
eqeqeq: true, |
| 15 |
eqnull: true, |
| 16 |
noarg: true, |
| 17 |
immed: false, |
| 18 |
predef: ['define', 'module', 'exports', 'Map'] |
| 19 |
}); |
| 20 |
|
| 21 |
if (jshint.errors.length) { |
| 22 |
jshint.errors.forEach(function (err) { |
| 23 |
console.log(err.line+':'+err.character+' '+err.reason); |
| 24 |
}); |
| 25 |
} else { |
| 26 |
console.log('linted') |
| 27 |
} |
| 28 |
|
| 29 |
return true; |
| 30 |
} |
| 31 |
|
| 32 |
function build(code) { |
| 33 |
var minified = ugly.minify(code).code; |
| 34 |
var header = [ |
| 35 |
`/*!`, |
| 36 |
` ${pkg.name} ${pkg.version}`, |
| 37 |
` license: ${pkg.license}`, |
| 38 |
` ${pkg.homepage}`, |
| 39 |
`*/`, |
| 40 |
`` |
| 41 |
].join('\n'); |
| 42 |
|
| 43 |
fs.writeFileSync('dist/'+pkg.name+'.js', header+code); |
| 44 |
fs.writeFileSync('dist/'+pkg.name+'.min.js', header+minified); |
| 45 |
console.log('dist built'); |
| 46 |
} |
| 47 |
|
| 48 |
function transform(filepath) { |
| 49 |
babel.transformFile(filepath, { |
| 50 |
plugins: [ |
| 51 |
"add-module-exports", ["transform-es2015-modules-umd", {"strict": true, "noInterop": true}] |
| 52 |
], |
| 53 |
presets: ["env"], |
| 54 |
}, function (err,res) { |
| 55 |
if (err) { |
| 56 |
return console.log(err); |
| 57 |
} else { |
| 58 |
lint(res.code); |
| 59 |
build(res.code); |
| 60 |
} |
| 61 |
}); |
| 62 |
} |
| 63 |
|
| 64 |
gaze('src/'+pkg.name+'.js', function(err, watcher){ |
| 65 |
// On file changed |
| 66 |
this.on('changed', function(filepath) { |
| 67 |
transform(filepath); |
| 68 |
}); |
| 69 |
|
| 70 |
console.log('watching'); |
| 71 |
}); |
| 72 |
|
| 73 |
transform('src/'+pkg.name+'.js'); |