1 | var through = require('through2');
|
2 | var gutil = require('gulp-util');
|
3 | var PluginError = gutil.PluginError;
|
4 | var os = require('os');
|
5 |
|
6 | var PLUGIN_NAME = 'gulp-amd-to-netsuite';
|
7 | var defineLineRegex = /^define\(\["require", "exports",? ?(.*)\], function \(require, exports,? ?(.*)\) \{$/
|
8 | var lastLineRegex = /^\}\);$/
|
9 | var ssv2UtilRegex = /node_modules\/@hitc\/ssv2-utilities\//g
|
10 |
|
11 |
|
12 | function 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 |
|
49 | return s;
|
50 | };
|
51 |
|
52 |
|
53 | module.exports = gulpCompile;
|