traceur-rails 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +20 -0
- data/Rakefile +12 -0
- data/lib/traceur-rails.rb +5 -0
- data/lib/traceur/config.rb +38 -0
- data/lib/traceur/sprockets.rb +4 -0
- data/lib/traceur/support/runner.js +21 -0
- data/lib/traceur/support/traceur/README.md +40 -0
- data/lib/traceur/support/traceur/bin/traceur-runtime.js +2039 -0
- data/lib/traceur/support/traceur/bin/traceur.js +23037 -0
- data/lib/traceur/support/traceur/package.json +60 -0
- data/lib/traceur/support/traceur/src/node/System.js +38 -0
- data/lib/traceur/support/traceur/src/node/api.js +124 -0
- data/lib/traceur/support/traceur/src/node/command.js +213 -0
- data/lib/traceur/support/traceur/src/node/compile-single-file.js +69 -0
- data/lib/traceur/support/traceur/src/node/compiler.js +113 -0
- data/lib/traceur/support/traceur/src/node/deferred.js +110 -0
- data/lib/traceur/support/traceur/src/node/file-util.js +73 -0
- data/lib/traceur/support/traceur/src/node/getopt.js +147 -0
- data/lib/traceur/support/traceur/src/node/inline-module.js +150 -0
- data/lib/traceur/support/traceur/src/node/interpreter.js +33 -0
- data/lib/traceur/support/traceur/src/node/nodeLoader.js +41 -0
- data/lib/traceur/support/traceur/src/node/require.js +85 -0
- data/lib/traceur/support/traceur/src/node/to-amd-compiler.js +33 -0
- data/lib/traceur/support/traceur/src/node/to-commonjs-compiler.js +33 -0
- data/lib/traceur/support/traceur/src/node/traceur.js +32 -0
- data/lib/traceur/support/traceur/traceur +3 -0
- data/lib/traceur/template.rb +61 -0
- data/lib/traceur/tilt.rb +4 -0
- data/lib/traceur/version.rb +3 -0
- data/test/template_test.rb +45 -0
- data/test/test_helper.rb +13 -0
- data/traceur-rails.gemspec +26 -0
- metadata +151 -0
@@ -0,0 +1,113 @@
|
|
1
|
+
// Copyright 2013 Traceur Authors.
|
2
|
+
//
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
// you may not use this file except in compliance with the License.
|
5
|
+
// You may obtain a copy of the License at
|
6
|
+
//
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
//
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
// See the License for the specific language governing permissions and
|
13
|
+
// limitations under the License.
|
14
|
+
|
15
|
+
'use strict';
|
16
|
+
|
17
|
+
var fs = require('fs');
|
18
|
+
var path = require('path');
|
19
|
+
|
20
|
+
var traceur = require('./traceur.js');
|
21
|
+
|
22
|
+
var inlineAndCompile = require('./inline-module.js').inlineAndCompile;
|
23
|
+
var util = require('./file-util.js');
|
24
|
+
var writeFile = util.writeFile;
|
25
|
+
var mkdirRecursive = util.mkdirRecursive;
|
26
|
+
var normalizePath = util.normalizePath;
|
27
|
+
|
28
|
+
var ErrorReporter = traceur.util.ErrorReporter;
|
29
|
+
var TreeWriter = traceur.outputgeneration.TreeWriter;
|
30
|
+
var SourceMapGenerator = traceur.outputgeneration.SourceMapGenerator;
|
31
|
+
|
32
|
+
function getSourceMapFileName(name) {
|
33
|
+
return name.replace(/\.js$/, '.map');
|
34
|
+
}
|
35
|
+
|
36
|
+
function writeTreeToFile(tree, filename, useSourceMaps, opt_sourceRoot) {
|
37
|
+
var options;
|
38
|
+
if (useSourceMaps) {
|
39
|
+
var sourceMapFilePath = getSourceMapFileName(filename);
|
40
|
+
var config = {
|
41
|
+
file: path.basename(filename),
|
42
|
+
sourceRoot: opt_sourceRoot
|
43
|
+
};
|
44
|
+
var sourceMapGenerator = new SourceMapGenerator(config);
|
45
|
+
options = {sourceMapGenerator: sourceMapGenerator};
|
46
|
+
}
|
47
|
+
|
48
|
+
var compiledCode = TreeWriter.write(tree, options);
|
49
|
+
if (useSourceMaps) {
|
50
|
+
compiledCode += '\n//# sourceMappingURL=' +
|
51
|
+
path.basename(sourceMapFilePath) + '\n';
|
52
|
+
}
|
53
|
+
writeFile(filename, compiledCode);
|
54
|
+
if (useSourceMaps)
|
55
|
+
writeFile(sourceMapFilePath, options.sourceMap);
|
56
|
+
}
|
57
|
+
|
58
|
+
function compileToSingleFile(outputFile, includes, useSourceMaps) {
|
59
|
+
var reporter = new ErrorReporter();
|
60
|
+
var resolvedOutputFile = path.resolve(outputFile);
|
61
|
+
var outputDir = path.dirname(resolvedOutputFile);
|
62
|
+
|
63
|
+
// Resolve includes before changing directory.
|
64
|
+
var resolvedIncludes = includes.map(function(include) {
|
65
|
+
return path.resolve(include);
|
66
|
+
});
|
67
|
+
|
68
|
+
mkdirRecursive(outputDir);
|
69
|
+
process.chdir(outputDir);
|
70
|
+
|
71
|
+
// Make includes relative to output dir so that sourcemap paths are correct.
|
72
|
+
resolvedIncludes = resolvedIncludes.map(function(include) {
|
73
|
+
return normalizePath(path.relative(outputDir, include));
|
74
|
+
});
|
75
|
+
|
76
|
+
inlineAndCompile(resolvedIncludes, traceur.options, reporter, function(tree) {
|
77
|
+
writeTreeToFile(tree, resolvedOutputFile, useSourceMaps);
|
78
|
+
process.exit(0);
|
79
|
+
}, function(err) {
|
80
|
+
process.exit(1);
|
81
|
+
});
|
82
|
+
}
|
83
|
+
|
84
|
+
function compileToDirectory(outputFile, includes, useSourceMaps) {
|
85
|
+
var reporter = new ErrorReporter();
|
86
|
+
var outputDir = path.resolve(outputFile);
|
87
|
+
|
88
|
+
var current = 0;
|
89
|
+
|
90
|
+
function next() {
|
91
|
+
if (current === includes.length)
|
92
|
+
process.exit(0);
|
93
|
+
|
94
|
+
inlineAndCompile(includes.slice(current, current + 1), traceur.options,
|
95
|
+
reporter,
|
96
|
+
function(tree) {
|
97
|
+
var outputFile = path.join(outputDir, includes[current]);
|
98
|
+
var sourceRoot = path.relative(path.dirname(outputFile), '.');
|
99
|
+
writeTreeToFile(tree, outputFile, useSourceMaps, sourceRoot);
|
100
|
+
current++;
|
101
|
+
next();
|
102
|
+
},
|
103
|
+
function(err) {
|
104
|
+
process.exit(1);
|
105
|
+
});
|
106
|
+
}
|
107
|
+
|
108
|
+
next();
|
109
|
+
}
|
110
|
+
|
111
|
+
exports.compileToSingleFile = compileToSingleFile;
|
112
|
+
exports.compileToDirectory = compileToDirectory;
|
113
|
+
exports.writeTreeToFile = writeTreeToFile;
|
@@ -0,0 +1,110 @@
|
|
1
|
+
// Copyright 2013 Traceur Authors.
|
2
|
+
//
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
// you may not use this file except in compliance with the License.
|
5
|
+
// You may obtain a copy of the License at
|
6
|
+
//
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
//
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
// See the License for the specific language governing permissions and
|
13
|
+
// limitations under the License.
|
14
|
+
|
15
|
+
'use strict';
|
16
|
+
|
17
|
+
/**
|
18
|
+
* Wrap a single async function to make the callback optional and hook it to
|
19
|
+
* trigger reject/resolve of the Promise, which it returns (this ignores the
|
20
|
+
* async function's return value). This enables the use of await with the
|
21
|
+
* wrapped function.
|
22
|
+
*
|
23
|
+
* @param {Function} fn Function to wrap.
|
24
|
+
* @param {boolean} firstArg True if the async callback is the first argument.
|
25
|
+
* @return {Function}
|
26
|
+
*/
|
27
|
+
function wrapFunction(fn, firstArg) {
|
28
|
+
return function() {
|
29
|
+
var resolve, reject;
|
30
|
+
var promise = new Promise(function(res, rej) {
|
31
|
+
resolve = res;
|
32
|
+
reject = rej;
|
33
|
+
});
|
34
|
+
|
35
|
+
var args = [].slice.call(arguments);
|
36
|
+
var originalCallback = args[firstArg ? 0 : args.length - 1];
|
37
|
+
|
38
|
+
function callback(err, value) {
|
39
|
+
if (originalCallback)
|
40
|
+
originalCallback.apply(this, arguments);
|
41
|
+
|
42
|
+
if (err)
|
43
|
+
reject(err);
|
44
|
+
else
|
45
|
+
resolve(value);
|
46
|
+
}
|
47
|
+
|
48
|
+
if (typeof originalCallback !== 'function') {
|
49
|
+
// Callback wasn't provided to the async function, add the custom one.
|
50
|
+
originalCallback = null;
|
51
|
+
if (firstArg)
|
52
|
+
args.unshift(callback);
|
53
|
+
else
|
54
|
+
args.push(callback);
|
55
|
+
} else {
|
56
|
+
// Callback was provided to the async function, replace it.
|
57
|
+
args[firstArg ? 0 : args.length - 1] = callback;
|
58
|
+
}
|
59
|
+
|
60
|
+
fn.apply(this, args);
|
61
|
+
|
62
|
+
return promise;
|
63
|
+
};
|
64
|
+
}
|
65
|
+
|
66
|
+
/**
|
67
|
+
* Wrap async functions in a module to enable the use of await.
|
68
|
+
* If no function name array is provided, every function with a fnSync
|
69
|
+
* variant will be wrapped.
|
70
|
+
*
|
71
|
+
* @param {string|Object} module The exports of the module or a string that
|
72
|
+
* will be passed to require to get the module.
|
73
|
+
* @param {Array.<string>} functions Function names to wrap.
|
74
|
+
* @return {object} The module.
|
75
|
+
*/
|
76
|
+
function wrapModule(module, functions) {
|
77
|
+
if (typeof module === 'string')
|
78
|
+
module = require(module);
|
79
|
+
|
80
|
+
if (!functions) {
|
81
|
+
for (var k in module) {
|
82
|
+
// HACK: wrap all functions with a fnSync variant.
|
83
|
+
if (typeof module[k] === 'function' &&
|
84
|
+
typeof module[k + 'Sync'] === 'function')
|
85
|
+
module[k] = wrapFunction(module[k]);
|
86
|
+
}
|
87
|
+
} else {
|
88
|
+
for (var i = 0, k; i < functions.length; i++) {
|
89
|
+
var k = functions[i];
|
90
|
+
module[k] = wrapFunction(module[k]);
|
91
|
+
}
|
92
|
+
}
|
93
|
+
|
94
|
+
return module;
|
95
|
+
}
|
96
|
+
|
97
|
+
/**
|
98
|
+
* Wrap async functions in Node.js to enable the use of await.
|
99
|
+
*
|
100
|
+
* @return {void}
|
101
|
+
*/
|
102
|
+
function wrap() {
|
103
|
+
// TODO: find and wrap everything that needs to be wrapped.
|
104
|
+
wrapModule('fs');
|
105
|
+
process.nextTick = wrapFunction(process.nextTick, true);
|
106
|
+
// FIXME: this would ignore the return value, making it impossible to cancel
|
107
|
+
// the timeout without implementing a cancel method and using it everywhere.
|
108
|
+
//global.setTimeout = wrapFunction(setTimeout, true);
|
109
|
+
}
|
110
|
+
exports.wrap = wrap;
|
@@ -0,0 +1,73 @@
|
|
1
|
+
// Copyright 2013 Traceur Authors.
|
2
|
+
//
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
// you may not use this file except in compliance with the License.
|
5
|
+
// You may obtain a copy of the License at
|
6
|
+
//
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
//
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
// See the License for the specific language governing permissions and
|
13
|
+
// limitations under the License.
|
14
|
+
|
15
|
+
var fs = require('fs');
|
16
|
+
var path = require('path');
|
17
|
+
|
18
|
+
function existsSync(p) {
|
19
|
+
return fs.existsSync ? fs.existsSync(p) : path.existsSync(p);
|
20
|
+
}
|
21
|
+
|
22
|
+
/**
|
23
|
+
* Recursively makes all directoires, similar to mkdir -p
|
24
|
+
* @param {string} dir
|
25
|
+
*/
|
26
|
+
function mkdirRecursive(dir) {
|
27
|
+
var parts = path.normalize(dir).split(path.sep);
|
28
|
+
dir = '';
|
29
|
+
for (var i = 0; i < parts.length; i++) {
|
30
|
+
dir += parts[i] + path.sep;
|
31
|
+
if (!existsSync(dir)) {
|
32
|
+
fs.mkdirSync(dir, 0x1FD); // 0775 permissions
|
33
|
+
}
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
/**
|
38
|
+
* Removes the common prefix of basedir and filedir from filedir
|
39
|
+
* @param {string} basedir
|
40
|
+
* @param {string} filedir
|
41
|
+
*/
|
42
|
+
function removeCommonPrefix(basedir, filedir) {
|
43
|
+
var baseparts = basedir.split(path.sep);
|
44
|
+
var fileparts = filedir.split(path.sep);
|
45
|
+
|
46
|
+
var i = 0;
|
47
|
+
while (i < fileparts.length && fileparts[i] === baseparts[i]) {
|
48
|
+
i++;
|
49
|
+
}
|
50
|
+
return fileparts.slice(i).join(path.sep);
|
51
|
+
}
|
52
|
+
|
53
|
+
function writeFile(filename, contents) {
|
54
|
+
// Compute the output path
|
55
|
+
var outputdir = fs.realpathSync(process.cwd());
|
56
|
+
mkdirRecursive(path.dirname(filename));
|
57
|
+
var filedir = fs.realpathSync(path.dirname(filename));
|
58
|
+
filedir = removeCommonPrefix(outputdir, filedir);
|
59
|
+
outputdir = path.join(outputdir, filedir);
|
60
|
+
|
61
|
+
mkdirRecursive(outputdir);
|
62
|
+
var outputfile = path.join(outputdir, path.basename(filename));
|
63
|
+
fs.writeFileSync(outputfile, contents, 'utf8');
|
64
|
+
}
|
65
|
+
|
66
|
+
function normalizePath(s) {
|
67
|
+
return path.sep == '\\' ? s.replace(/\\/g, '/') : s;
|
68
|
+
}
|
69
|
+
|
70
|
+
exports.mkdirRecursive = mkdirRecursive;
|
71
|
+
exports.normalizePath = normalizePath;
|
72
|
+
exports.removeCommonPrefix = removeCommonPrefix;
|
73
|
+
exports.writeFile = writeFile;
|
@@ -0,0 +1,147 @@
|
|
1
|
+
// Copyright 2013 Traceur Authors.
|
2
|
+
//
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
// you may not use this file except in compliance with the License.
|
5
|
+
// You may obtain a copy of the License at
|
6
|
+
//
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
//
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
// See the License for the specific language governing permissions and
|
13
|
+
// limitations under the License.
|
14
|
+
|
15
|
+
var format = require('util').format;
|
16
|
+
|
17
|
+
function addAbbrev(o) {
|
18
|
+
var ks = [''].concat(Object.keys(o).sort()), k, kprev = '';
|
19
|
+
for (var i = ks.length - 1; i > 0; i--) {
|
20
|
+
var ka = k = ks[i], pre = 0;
|
21
|
+
|
22
|
+
// find length of common prefix, clamp to min of 1.
|
23
|
+
while (kprev[pre] === k[pre]) {
|
24
|
+
pre++;
|
25
|
+
}
|
26
|
+
pre = pre || 1;
|
27
|
+
|
28
|
+
// add all unique prefixes for k.
|
29
|
+
while (!o[ka = ka.slice(0, -1)] && ka.length > pre && ka > ks[i - 1]) {
|
30
|
+
o[ka] = o[k];
|
31
|
+
}
|
32
|
+
kprev = k;
|
33
|
+
}
|
34
|
+
}
|
35
|
+
|
36
|
+
function Getopt(opts) {
|
37
|
+
this.opt = null;
|
38
|
+
this.optarg = null;
|
39
|
+
this.optopt = null;
|
40
|
+
this.optdata = null;
|
41
|
+
|
42
|
+
this.optind = 2;
|
43
|
+
this.nextchar = 0;
|
44
|
+
|
45
|
+
this.opts_ = Object.create(null);
|
46
|
+
for (var i = 0; i < opts.length; i++) {
|
47
|
+
var opt = opts[i], data = null, m;
|
48
|
+
if (Array.isArray(opt)) {
|
49
|
+
data = opt[1] || null;
|
50
|
+
opt = opt[0];
|
51
|
+
}
|
52
|
+
if (!(m = opt.match(/^([\w\-]+)(:{0,2})$/))) {
|
53
|
+
throw new Error('invalid option initializer: ' + opt);
|
54
|
+
}
|
55
|
+
this.opts_[m[1]] = {name: m[1], arg: m[2], data: data};
|
56
|
+
}
|
57
|
+
addAbbrev(this.opts_);
|
58
|
+
}
|
59
|
+
|
60
|
+
Getopt.prototype = {
|
61
|
+
getopt: function(argv) {
|
62
|
+
var m, arg, optInf;
|
63
|
+
this.opt = this.optarg = this.optopt = this.optdata = null;
|
64
|
+
if (this.optind >= argv.length) {
|
65
|
+
return false;
|
66
|
+
}
|
67
|
+
arg = argv[this.optind];
|
68
|
+
if (!this.nextchar && /^-[^\-]/.test(arg)) {
|
69
|
+
this.nextchar = 1;
|
70
|
+
}
|
71
|
+
if (this.nextchar) {
|
72
|
+
// short opt
|
73
|
+
this.opt = arg[this.nextchar] || null;
|
74
|
+
this.optarg = arg.slice(++this.nextchar) || null;
|
75
|
+
} else if (m = arg.match(/^--([^=]+)(?:=(.*))?$|^--(.+)$/)) {
|
76
|
+
// long opt
|
77
|
+
this.opt = m[1] || m[3];
|
78
|
+
this.optarg = m[2] === undefined ? null : m[2];
|
79
|
+
} else {
|
80
|
+
// free arg
|
81
|
+
this.optind++;
|
82
|
+
this.opt = '=';
|
83
|
+
this.optarg = arg;
|
84
|
+
return true;
|
85
|
+
}
|
86
|
+
|
87
|
+
if (optInf = this.opts_[this.opt]) {
|
88
|
+
this.opt = optInf.name;
|
89
|
+
this.optdata = optInf.data;
|
90
|
+
switch (optInf.arg) {
|
91
|
+
case '':
|
92
|
+
// no arg
|
93
|
+
if (!this.nextchar && this.optarg) {
|
94
|
+
// unexpected arg
|
95
|
+
this.optopt = this.opt;
|
96
|
+
this.opt = '!';
|
97
|
+
break;
|
98
|
+
}
|
99
|
+
this.optarg = null;
|
100
|
+
break;
|
101
|
+
case ':':
|
102
|
+
// required arg
|
103
|
+
if (this.optarg === null) {
|
104
|
+
if (++this.optind >= argv.length) {
|
105
|
+
// missing arg
|
106
|
+
this.optopt = this.opt;
|
107
|
+
this.opt = ':';
|
108
|
+
break;
|
109
|
+
}
|
110
|
+
this.optarg = argv[this.optind];
|
111
|
+
}
|
112
|
+
// fall through
|
113
|
+
case '::':
|
114
|
+
// optional arg
|
115
|
+
this.nextchar = 0;
|
116
|
+
break;
|
117
|
+
}
|
118
|
+
} else {
|
119
|
+
// unknown opt
|
120
|
+
this.optopt = this.opt;
|
121
|
+
this.opt = '?';
|
122
|
+
}
|
123
|
+
|
124
|
+
this.optind += !(this.nextchar %= arg.length);
|
125
|
+
|
126
|
+
return true;
|
127
|
+
},
|
128
|
+
message: function() {
|
129
|
+
switch (this.opt) {
|
130
|
+
case ':':
|
131
|
+
return format('missing argument for \'%s\'.', this.optopt);
|
132
|
+
case '?':
|
133
|
+
return format('unknown option \'%s\'.', this.optopt);
|
134
|
+
case '!':
|
135
|
+
return format('\'%s\' does not take an argument.', this.optopt);
|
136
|
+
case '=':
|
137
|
+
return format('optarg \'%s\'.', this.optarg);
|
138
|
+
default:
|
139
|
+
if (this.optarg === null)
|
140
|
+
return format('opt \'%s\'.', this.opt);
|
141
|
+
else
|
142
|
+
return format('opt \'%s\', optarg \'%s\'.', this.opt, this.optarg);
|
143
|
+
}
|
144
|
+
}
|
145
|
+
}
|
146
|
+
|
147
|
+
exports.Getopt = Getopt;
|
@@ -0,0 +1,150 @@
|
|
1
|
+
// Copyright 2012 Traceur Authors.
|
2
|
+
//
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
4
|
+
// you may not use this file except in compliance with the License.
|
5
|
+
// You may obtain a copy of the License at
|
6
|
+
//
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
8
|
+
//
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
12
|
+
// See the License for the specific language governing permissions and
|
13
|
+
// limitations under the License.
|
14
|
+
|
15
|
+
'use strict';
|
16
|
+
|
17
|
+
var fs = require('fs');
|
18
|
+
var path = require('path');
|
19
|
+
var nodeLoader = require('./nodeLoader.js');
|
20
|
+
var normalizePath = require('./file-util.js').normalizePath;
|
21
|
+
|
22
|
+
var TraceurLoader = traceur.runtime.TraceurLoader;
|
23
|
+
var LoaderHooks = traceur.runtime.LoaderHooks;
|
24
|
+
var Script = traceur.syntax.trees.Script;
|
25
|
+
var SourceFile = traceur.syntax.SourceFile
|
26
|
+
var moduleStore = traceur.runtime.ModuleStore;
|
27
|
+
|
28
|
+
/**
|
29
|
+
* @param {ErrorReporter} reporter
|
30
|
+
* @param {Array.<ParseTree>} elements
|
31
|
+
* @param {string|undefined} depTarget A valid depTarget means dependency
|
32
|
+
* printing was requested.
|
33
|
+
*/
|
34
|
+
function InlineLoaderHooks(reporter, url, elements, depTarget) {
|
35
|
+
LoaderHooks.call(this, reporter, url,
|
36
|
+
nodeLoader, // Load modules using node fs.
|
37
|
+
moduleStore); // Look up modules in our static module store
|
38
|
+
this.dirname = url;
|
39
|
+
this.elements = elements;
|
40
|
+
this.depTarget = depTarget && normalizePath(path.relative('.', depTarget));
|
41
|
+
this.codeUnitList = [];
|
42
|
+
}
|
43
|
+
|
44
|
+
InlineLoaderHooks.prototype = {
|
45
|
+
__proto__: LoaderHooks.prototype,
|
46
|
+
|
47
|
+
evaluateCodeUnit: function(codeUnit) {
|
48
|
+
if (this.depTarget) {
|
49
|
+
console.log('%s: %s', this.depTarget,
|
50
|
+
normalizePath(path.relative(path.join(__dirname, '../..'),
|
51
|
+
codeUnit.url)));
|
52
|
+
}
|
53
|
+
// Don't eval. Instead append the trees to the output.
|
54
|
+
var tree = codeUnit.metadata.transformedTree;
|
55
|
+
this.elements.push.apply(this.elements, tree.scriptItemList);
|
56
|
+
},
|
57
|
+
|
58
|
+
};
|
59
|
+
|
60
|
+
function allLoaded(url, reporter, elements) {
|
61
|
+
return new Script(null, elements);
|
62
|
+
}
|
63
|
+
|
64
|
+
/**
|
65
|
+
* Compiles the files in "filenames" along with any associated modules, into a
|
66
|
+
* single js file, in proper module dependency order.
|
67
|
+
*
|
68
|
+
* @param {Array.<string>} filenames The list of files to compile and concat.
|
69
|
+
* @param {Object} options A container for misc options. 'depTarget' is the
|
70
|
+
* only currently available option, which results in the dependencies for
|
71
|
+
* 'filenames' being printed to stdout, with 'depTarget' as the target.
|
72
|
+
* @param {ErrorReporter} reporter
|
73
|
+
* @param {Function} callback Callback used to return the result. A null result
|
74
|
+
* indicates that inlineAndCompile has returned successfully from a
|
75
|
+
* non-compile request.
|
76
|
+
* @param {Function} errback Callback used to return errors.
|
77
|
+
*/
|
78
|
+
function inlineAndCompile(filenames, options, reporter, callback, errback) {
|
79
|
+
|
80
|
+
var depTarget = options && options.depTarget;
|
81
|
+
var referrerName = options && options.referrer;
|
82
|
+
|
83
|
+
var basePath;
|
84
|
+
if (referrerName) {
|
85
|
+
// The compile occurs two directories down from current directory,
|
86
|
+
// in src/node. Thus the names will appear as eg ../src/x.
|
87
|
+
// We want something like referrerName/src/x. So we need to give
|
88
|
+
// the normalize() the 'package' or root name with src/node append
|
89
|
+
// to represent the referrer from here.
|
90
|
+
referrerName = referrerName && referrerName + 'src/node';
|
91
|
+
// The basePath will replace options.referrer in our final filename.
|
92
|
+
// Since we are in src/node, we need to back up two directories.
|
93
|
+
basePath = path.join(__dirname, '../../');
|
94
|
+
} else {
|
95
|
+
basePath = path.resolve('./') + '/';
|
96
|
+
}
|
97
|
+
basePath = basePath.replace(/\\/g, '/');
|
98
|
+
|
99
|
+
var scriptsCount = options.scripts.length;
|
100
|
+
|
101
|
+
var loadCount = 0;
|
102
|
+
var elements = [];
|
103
|
+
var hooks = new InlineLoaderHooks(reporter, basePath, elements, depTarget);
|
104
|
+
var loader = new TraceurLoader(hooks);
|
105
|
+
|
106
|
+
function appendEvaluateModule(name, referrerName) {
|
107
|
+
var normalizedName =
|
108
|
+
traceur.ModuleStore.normalize(name, referrerName);
|
109
|
+
// Create tree for System.get('normalizedName');
|
110
|
+
var tree =
|
111
|
+
traceur.codegeneration.module.createModuleEvaluationStatement(normalizedName);
|
112
|
+
elements.push(tree);
|
113
|
+
}
|
114
|
+
|
115
|
+
function loadNext() {
|
116
|
+
var loadAsScript = scriptsCount && (loadCount < scriptsCount);
|
117
|
+
var doEvaluateModule = false;
|
118
|
+
var loadFunction = loader.import;
|
119
|
+
var name = filenames[loadCount];
|
120
|
+
if (loadAsScript) {
|
121
|
+
loadFunction = loader.loadAsScript;
|
122
|
+
} else {
|
123
|
+
name = name.replace(/\.js$/,'');
|
124
|
+
if (options.modules !== 'inline' && options.modules !== 'instantiate')
|
125
|
+
doEvaluateModule = true;
|
126
|
+
}
|
127
|
+
var loadOptions = {referrerName: referrerName};
|
128
|
+
var codeUnit = loadFunction.call(loader, name, loadOptions).then(
|
129
|
+
function() {
|
130
|
+
if (doEvaluateModule)
|
131
|
+
appendEvaluateModule(name, referrerName);
|
132
|
+
loadCount++;
|
133
|
+
if (loadCount < filenames.length) {
|
134
|
+
loadNext();
|
135
|
+
} else if (depTarget) {
|
136
|
+
callback(null);
|
137
|
+
} else {
|
138
|
+
var tree = allLoaded(basePath, reporter, elements);
|
139
|
+
callback(tree);
|
140
|
+
}
|
141
|
+
}, function(err) {
|
142
|
+
errback(err);
|
143
|
+
}).catch(function(ex) {
|
144
|
+
console.error('Internal error ' + (ex.stack || ex));
|
145
|
+
});
|
146
|
+
}
|
147
|
+
|
148
|
+
loadNext();
|
149
|
+
}
|
150
|
+
exports.inlineAndCompile = inlineAndCompile;
|