├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012, James Long 2 | All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions are 6 | met: 7 | 8 | Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | Redistributions in binary form must reproduce the above copyright 12 | notice, this list of conditions and the following disclaimer in 13 | the documentation and/or other materials provided with the 14 | distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 | HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # gulp-sweetjs 3 | 4 | This lets you integrate [sweet.js](http://sweetjs.org/) with gulp. 5 | 6 | Example (also uses [gulp-sourcemaps](https://github.com/floridoo/gulp-sourcemaps)): 7 | 8 | ```js 9 | gulp.task("build", function() { 10 | gulp.src("src/**/*.js") 11 | .pipe(sourcemaps.init()) 12 | .pipe(sweetjs({ 13 | modules: ['es6-macros'], 14 | readtables: ['reader-module'] 15 | })) 16 | .pipe(sourcemaps.write('.')) 17 | .pipe(gulp.dest('build')); 18 | }) 19 | ``` 20 | 21 | This project is licensed under the BSD License. See the LICENSE file for more information. -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var fs = require('fs'); 2 | var sweet = require('sweet.js'); 3 | var gutil = require('gulp-util'); 4 | var applySourceMap = require('vinyl-sourcemaps-apply'); 5 | var es = require('event-stream'); 6 | var merge = require('merge'); 7 | 8 | module.exports = function(opts) { 9 | var moduleCache = {}; 10 | 11 | opts = merge({ 12 | modules: [], 13 | readtables: [], 14 | readableNames: false 15 | }, opts); 16 | 17 | opts.modules = opts.modules.map(function(mod) { 18 | if(moduleCache[mod]) { 19 | return moduleCache[mod]; 20 | } 21 | moduleCache[mod] = sweet.loadNodeModule(process.cwd(), mod); 22 | return moduleCache[mod]; 23 | }); 24 | 25 | opts.readtables.forEach(function(mod) { 26 | sweet.setReadtable(mod); 27 | }); 28 | 29 | return es.through(function(file) { 30 | if(file.isNull()) { 31 | return this.emit('data', file); 32 | } 33 | if(file.isStream()) { 34 | return this.emit( 35 | 'error', 36 | new Error('gulp-sweetjs: Streaming not supported') 37 | ); 38 | } 39 | 40 | var sjsOpts = merge({ 41 | sourceMap: !!file.sourceMap, 42 | filename: file.path, 43 | }, opts); 44 | 45 | var dest = gutil.replaceExtension(file.path, '.js'); 46 | try { 47 | var res = sweet.compile(file.contents.toString('utf8'), sjsOpts); 48 | } 49 | catch(err) { 50 | return this.emit('error', err); 51 | } 52 | 53 | var originalContents = String(file.contents); 54 | file.contents = new Buffer(res.code); 55 | 56 | if(res.sourceMap) { 57 | var sm = JSON.parse(res.sourceMap); 58 | sm.sources = [file.relative]; 59 | sm.sourcesContent = [originalContents]; 60 | sm.file = file.relative; 61 | applySourceMap(file, sm); 62 | } 63 | 64 | file.path = dest; 65 | this.emit('data', file); 66 | }); 67 | }; 68 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "gulp-sweetjs", 3 | "version": "0.6.1", 4 | "description": "gulp loader for sweetjs", 5 | "main": "index.js", 6 | "author": "James Long", 7 | "repository": { 8 | "type": "git", 9 | "url": "https://github.com/jlongster/gulp-sweetjs.git" 10 | }, 11 | "keywords": ["gulpplugin"], 12 | "dependencies": { 13 | "event-stream": "^3.1.5", 14 | "gulp-util": "^2.2.19", 15 | "merge": "^1.1.3", 16 | "sweet.js": "^0.7.1", 17 | "vinyl-sourcemaps-apply": "^0.1.3" 18 | } 19 | } 20 | --------------------------------------------------------------------------------