Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit 8ddff79

Browse files
committed
chore(build): allow multiple *.ats files
1 parent 2bb06ef commit 8ddff79

File tree

4 files changed

+133
-68
lines changed

4 files changed

+133
-68
lines changed

gulpfile.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ var Dgeni = require('dgeni');
33
var traceur = require('gulp-traceur');
44
var connect = require('gulp-connect');
55
var concat = require('gulp-concat');
6-
var merge = require('merge-stream');
76
var rename = require('gulp-rename');
87
var ngAnnotate = require('gulp-ng-annotate');
9-
8+
var gulpMerge = require('gulp-merge')
109
var modulate = require('./scripts/angular-modulate');
1110

1211
var CONFIG = require('./config');
@@ -31,12 +30,12 @@ gulp.task('transpile', function() {
3130
gulp.task('angularify', ['transpile'], function() {
3231
var directive = gulp.src('./src/*.es5.js');
3332

34-
var generated = gulp.src('./src/router.ats')
33+
var generated = gulp.src('./src/**.ats')
3534
.pipe(modulate({
3635
moduleName: 'ngNewRouter.generated'
3736
}))
3837

39-
return merge(directive, generated)
38+
return gulpMerge(directive, generated)
4039
.pipe(concat('router.es5.js'))
4140
.pipe(ngAnnotate())
4241
.pipe(gulp.dest(BUILD_DIR));

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"gulp": "^3.8.9",
3333
"gulp-concat": "^2.4.2",
3434
"gulp-connect": "^2.0.6",
35+
"gulp-merge": "^0.1.0",
3536
"gulp-ng-annotate": "^0.5.2",
3637
"gulp-rename": "^1.2.0",
3738
"gulp-traceur": "^0.13.0",
@@ -45,7 +46,6 @@
4546
"karma-traceur-preprocessor": "^0.4.0",
4647
"lodash": "^2.4.1",
4748
"marked": "^0.3.2",
48-
"merge-stream": "^0.1.6",
4949
"protractor": "^0.21.0",
5050
"q": "^1.1.2",
5151
"through2": "^0.4.1"

scripts/angular-modulate.js

Lines changed: 104 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,28 @@ var TRACEUR_OPTS = {
1616
"modules": "inline"
1717
};
1818

19+
/*
20+
* recursively inline ONLY these definitions
21+
*/
22+
var MODULE_LOCATIONS = {
23+
'route-recognizer': 'node_modules/route-recognizer/lib/route-recognizer'
24+
};
25+
26+
27+
var TRACEUR_CREATE_CLASS = new RegExp('\\$traceurRuntime\\.(createClass|superCall)', 'g');
28+
function detraceurify (contents) {
29+
return contents.replace(TRACEUR_CREATE_CLASS, '$1');
30+
}
31+
32+
var IMPORT_RE = new RegExp("import \\{?(\\w+)\\}? from '(.+)';?", 'g');
33+
var IMPORT_PARTS_RE = new RegExp("import \\{?(\\w+)\\}? from '(.+)';?");
34+
var EXPORT_RE = new RegExp("export (default )?");
35+
36+
function getParts(importStatement) {
37+
return importStatement.match(IMPORT_PARTS_RE)[1];
38+
}
39+
40+
1941
module.exports = function (opts) {
2042

2143
// creating a stream through which each file will pass
@@ -26,12 +48,7 @@ module.exports = function (opts) {
2648

2749
if (file.isBuffer()) {
2850
var contents = file.contents.toString();
29-
contents = processFile(file.path, contents);
30-
contents = traceur.compile(contents, TRACEUR_OPTS);
31-
contents = stripIife(contents);
32-
contents = dollarQify(contents);
33-
contents = detraceurify(contents);
34-
contents = angularModulate(contents, opts);
51+
contents = transform(file.cwd, contents);
3552
file.contents = new Buffer(contents.toString());
3653
}
3754

@@ -44,36 +61,33 @@ module.exports = function (opts) {
4461
return stream;
4562
};
4663

47-
var TRACEUR_CREATE_CLASS = new RegExp('\\$traceurRuntime\\.createClass', 'g');
48-
function detraceurify (contents) {
49-
return traceurRuntime + ';\n' + contents.replace(TRACEUR_CREATE_CLASS, 'createClass');
50-
}
64+
// console.log("angular.module('ngNewRouter.generated', []);");
65+
// console.log(traceurRuntime);
5166

52-
var moduleLocations = {
53-
'route-recognizer': '../node_modules/route-recognizer/lib/route-recognizer'
54-
};
5567

56-
var IIEF_RE = new RegExp([
57-
escape('var $'),
58-
'__anon_[0-9_]+',
59-
escape(' = (function() {'),
60-
].join(''));
61-
62-
function stripIife (contents) {
63-
contents = contents.replace(IIEF_RE, '');
64-
contents = contents.replace([
65-
' return {get Router() {',
66-
' return Router;',
67-
' }};',
68-
'})();'
69-
].join('\n'), '');
70-
71-
return contents;
68+
function transform (dir, contents) {
69+
var imports = [];
70+
71+
contents = contents.replace(IMPORT_RE, function (match, obj, includePath) {
72+
if (!MODULE_LOCATIONS[includePath]) {
73+
imports.push(match);
74+
return '';
75+
}
76+
var includeFile = path.join(dir, MODULE_LOCATIONS[includePath]);
77+
console.log(dir, includeFile);
78+
if (includeFile.substr(-3) !== '.js') {
79+
includeFile += '.js';
80+
}
81+
return 'var ' + obj + '=' + inlineModule(includeFile) + ';';
82+
});
83+
84+
var services = imports.map(getParts).map(serviceify);
85+
contents = traceur.compile(contents, TRACEUR_OPTS);
86+
return detraceurify(unwrapify(contents, services));
7287
}
7388

74-
var IMPORT_RE = new RegExp("import (.+) from '(.+)'", 'g');
75-
var EXPORT_RE = new RegExp("export default ");
76-
function processFile (filePath, contents) {
89+
90+
function inlineModule (filePath, contents) {
7791
contents = contents || fs.readFileSync(filePath, 'utf8');
7892

7993
var dir = path.dirname(filePath);
@@ -88,53 +102,77 @@ function processFile (filePath, contents) {
88102
}
89103

90104
return contents.replace(IMPORT_RE, function (match, obj, includePath) {
91-
var includeFile = path.join(dir, moduleLocations[includePath] || includePath);
105+
var includeFile = path.join(dir, MODULE_LOCATIONS[includePath] || includePath);
92106
if (includeFile.substr(-3) !== '.js') {
93107
includeFile += '.js';
94108
}
95-
return 'var ' + obj + '=' + processFile(includeFile) + ';';
109+
return 'var ' + obj + '=' + inlineModule(includeFile) + ';';
96110
});
97111
}
98112

99113

114+
function unwrapify (src, services) {
115+
return falafel(src, {tolerant: true}, function (node) {
116+
dollarQify(node) || angularReWrap(node, services);
117+
}).toString();
118+
}
100119

101-
102-
/*
103-
* convert AMD to Angular modules
104-
*/
105-
function angularModulate (src, opts) {
106-
return [
107-
'angular.module(\'' + opts.moduleName + '\', [])',
108-
'.factory(\'$router\', [\'$q\', function($q) {',
109-
src,
110-
'return new Router();',
111-
'}]);'
112-
].join('');
120+
function angularReWrap (node, services) {
121+
var decls, decl;
122+
if (node.type === 'VariableDeclaration' &&
123+
(decls = node.declarations) && decls.length === 1 &&
124+
(decl = decls[0]) && decl.id.name.match(/^\$__anon/)) {
125+
126+
var body = decl.init.callee.body.body;
127+
var exports = body[body.length - 1];
128+
body = body.slice(0, -1);
129+
node.update("\nangular.module('ngNewRouter')" +
130+
exports.argument.properties.map(function (prop) {
131+
return angularFactory(serviceify(prop.key.name),
132+
['$q'].concat(services),
133+
traceurRuntime + '\n' +
134+
body.map(function (body) {
135+
return body.source();
136+
}).join('\n') +
137+
'\nreturn new ' +
138+
prop.value.body.body[0].argument.name + '(' + services.join(', ') + ');');
139+
}).join('\n'));
140+
return true;
141+
}
142+
return false;
113143
}
114144

145+
function angularFactory(name, deps, body) {
146+
return ".factory('" + name + "', [" +
147+
deps.map(function (service) {
148+
return "'" + service + "', ";
149+
}).join('') +
150+
"function (" + deps.join(', ') + ") {\n" + body + "\n}]);";
151+
}
115152

116153
/*
117154
* Replace ES6 promises with calls to $q
118155
*
119156
* note that this may not be comprehensive
120157
*/
121-
function dollarQify (src) {
122-
return falafel(src, {tolerant: true}, function (node) {
123-
if (node.type === 'NewExpression' && node.callee.name === 'Promise') {
124-
node.update('$q(' + argsToSrc(node) + ')');
125-
} else if (node.type === 'CallExpression') {
126-
var callee = node.callee.source(),
127-
match,
128-
method;
129-
if (match = callee.match(/^Promise\.(resolve|reject|all)$/)) {
130-
var method = match[1];
131-
if (method === 'resolve') {
132-
method = 'when';
133-
}
134-
node.update('$q.' + method + '(' + argsToSrc(node) + ')');
158+
function dollarQify (node) {
159+
if (node.type === 'NewExpression' && node.callee.name === 'Promise') {
160+
node.update('$q(' + argsToSrc(node) + ')');
161+
} else if (node.type === 'CallExpression') {
162+
var callee = node.callee.source(),
163+
match,
164+
method;
165+
if (match = callee.match(/^Promise\.(resolve|reject|all)$/)) {
166+
var method = match[1];
167+
if (method === 'resolve') {
168+
method = 'when';
135169
}
170+
node.update('$q.' + method + '(' + argsToSrc(node) + ')');
136171
}
137-
}).toString();
172+
} else {
173+
return false;
174+
}
175+
return true;
138176
}
139177

140178
/*
@@ -145,3 +183,8 @@ function argsToSrc (node) {
145183
return node.source();
146184
}).join(', ');
147185
}
186+
187+
188+
function serviceify (name) {
189+
return '$$' + name[0].toLowerCase() + name.substr(1);
190+
}

scripts/lib/traceur-runtime-custom.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ var $defineProperty = Object.defineProperty,
66
$defineProperties = Object.defineProperties,
77
$create = Object.create,
88
$getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor,
9-
$getOwnPropertyNames = Object.getOwnPropertyNames;
9+
$getOwnPropertyNames = Object.getOwnPropertyNames,
10+
$getPrototypeOf = Object.getPrototypeOf;
1011

1112
function createClass(ctor, object, staticObject, superClass) {
1213
$defineProperty(object, 'constructor', {
@@ -57,4 +58,26 @@ function getDescriptors(object) {
5758
// descriptors[$traceurRuntime.toProperty(symbol)] = $getOwnPropertyDescriptor(object, $traceurRuntime.toProperty(symbol));
5859
// }
5960
return descriptors;
60-
}
61+
}
62+
function superDescriptor(homeObject, name) {
63+
var proto = $getPrototypeOf(homeObject);
64+
do {
65+
var result = $getOwnPropertyDescriptor(proto, name);
66+
if (result)
67+
return result;
68+
proto = $getPrototypeOf(proto);
69+
} while (proto);
70+
return undefined;
71+
}
72+
function superCall(self, homeObject, name, args) {
73+
return superGet(self, homeObject, name).apply(self, args);
74+
}
75+
function superGet(self, homeObject, name) {
76+
var descriptor = superDescriptor(homeObject, name);
77+
if (descriptor) {
78+
if (!descriptor.get)
79+
return descriptor.value;
80+
return descriptor.get.call(self);
81+
}
82+
return undefined;
83+
}

0 commit comments

Comments
 (0)