UNPKG

1.99 kBJavaScriptView Raw
1var through = require('through2');
2var gutil = require('gulp-util');
3var PluginError = gutil.PluginError;
4var os = require('os');
5
6var PLUGIN_NAME = 'gulp-amd-to-netsuite';
7var defineLineRegex = /^define\(\["require", "exports",? ?(.*)\], function \(require, exports,? ?(.*)\) \{$/
8var lastLineRegex = /^\}\);$/
9var ssv2UtilRegex = /node_modules\/@hitc\/ssv2-utilities\//g
10
11// plugin level function (dealing with files)
12function gulpCompile() {
13 var s = through.obj(function(file, enc, cb) {
14 if (file.isStream()) {
15 this.emit('error', new PluginError(PLUGIN_NAME, 'Streams are not supported!'));
16 return cb();
17 }
18
19 if (file.isBuffer()) {
20 var lines = file.contents.toString().split('\n');
21 var outLines = [];
22 for (var i=0; i<lines.length; i++) {
23 var line = lines[i];
24 line = line.replace(ssv2UtilRegex, "/.bundle/121445/HITCSSV2Utilities/");
25 var matches = line.match(defineLineRegex)
26 if (matches && matches.length > 1) {
27 line = "define(["+matches[1]+"], function ("+matches[2]+") {";
28 outLines.push(line);
29 outLines.push(" var __extends = (this && this.__extends) || function (d, b) {");
30 outLines.push(" for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];")
31 outLines.push(" function __() { this.constructor = d; }")
32 outLines.push(" d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());")
33 outLines.push(" };")
34 outLines.push(" var exports = {};");
35 } else if (line.match(lastLineRegex) && i >= lines.length-2) {
36 outLines.push(" return exports;");
37 outLines.push(line);
38 } else {
39 outLines.push(line);
40 }
41 }
42 file.contents = new Buffer(outLines.join('\n'));
43 s.push(file);
44 cb();
45 }
46 });
47
48 // returning the file stream
49 return s;
50};
51
52// exporting the plugin main function
53module.exports = gulpCompile;