@@ -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+
1941module . 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 ( / ^ \$ _ _ a n o n / ) ) {
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 ( / ^ P r o m i s e \. ( r e s o l v e | r e j e c t | a l l ) $ / ) ) {
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 ( / ^ P r o m i s e \. ( r e s o l v e | r e j e c t | a l l ) $ / ) ) {
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+ }
0 commit comments