UNPKG

gulp-riot

Version:

gulp plugin for riot

61 lines (43 loc) 2.1 kB
'use strict'; var _pluginError = require('plugin-error'); var _pluginError2 = _interopRequireDefault(_pluginError); var _riot = require('riot'); var riot = _interopRequireWildcard(_riot); var _through = require('through2'); var _through2 = _interopRequireDefault(_through); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var PLUGIN_NAME = 'gulp-riot'; module.exports = function () { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; if (options.parsers) { Object.keys(options.parsers).forEach(function (x) { Object.keys(options.parsers[x]).forEach(function (y) { riot.parsers[x][y] = options.parsers[x][y]; }); }); delete options.parsers; } return _through2.default.obj(function (file, encoding, callback) { if (file.isNull()) { return callback(null, file); } if (file.isStream()) { this.emit('error', new _pluginError2.default(PLUGIN_NAME, 'Streaming not supported')); return callback(); } var compiledCode = void 0; try { compiledCode = riot.compile(file.contents.toString(), options, file.path); } catch (err) { this.emit('error', new _pluginError2.default(PLUGIN_NAME, `${file.path}: Compiler Error: ${err}`)); } if (options.modular) { compiledCode = `(function(tagger) {\n if (typeof define === 'function' && define.amd) {\n define(['riot'], function(riot) { tagger(riot); });\n } else if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {\n tagger(require('riot'));\n } else {\n tagger(window.riot);\n }\n})(function(riot) {\n${compiledCode}\n\n});`; } file.contents = new Buffer(compiledCode); file.extname = '.js'; this.push(file); callback(); }); };