refactor($router): removed some redundant code
This commit is contained in:
parent
cd2050b6e0
commit
431ac33c26
|
@ -25,28 +25,21 @@ var DIRECTIVES = fs.readFileSync(__dirname + '/src/ng_outlet.js', 'utf8');
|
||||||
var moduleTemplate = fs.readFileSync(__dirname + '/src/module_template.js', 'utf8');
|
var moduleTemplate = fs.readFileSync(__dirname + '/src/module_template.js', 'utf8');
|
||||||
|
|
||||||
function main() {
|
function main() {
|
||||||
var ES6_SHIM = fs.readFileSync(__dirname + '/../../node_modules/es6-shim/es6-shim.js', 'utf8');
|
|
||||||
var dir = __dirname + '/../angular2/src/router/';
|
var dir = __dirname + '/../angular2/src/router/';
|
||||||
|
var sharedCode = files.reduce(function (prev, file) {
|
||||||
var sharedCode = '';
|
return prev + transform(fs.readFileSync(dir + file, 'utf8'));
|
||||||
files.forEach(function (file) {
|
}, '');
|
||||||
var moduleName = 'router/' + file.replace(/\.ts$/, '');
|
|
||||||
|
|
||||||
sharedCode += transform(moduleName, fs.readFileSync(dir + file, 'utf8'));
|
|
||||||
});
|
|
||||||
|
|
||||||
var out = moduleTemplate.replace('//{{FACADES}}', FACADES).replace('//{{SHARED_CODE}}', sharedCode);
|
var out = moduleTemplate.replace('//{{FACADES}}', FACADES).replace('//{{SHARED_CODE}}', sharedCode);
|
||||||
|
|
||||||
return PRELUDE + DIRECTIVES + out + POSTLUDE;
|
return PRELUDE + DIRECTIVES + out + POSTLUDE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Given a directory name and a file's TypeScript content, return an object with the ES5 code,
|
* Given a directory name and a file's TypeScript content, return an object with the ES5 code,
|
||||||
* sourcemap, and exported variable identifier name for the content.
|
* sourcemap, and exported variable identifier name for the content.
|
||||||
*/
|
*/
|
||||||
var IMPORT_RE = new RegExp("import \\{?([\\w\\n_, ]+)\\}? from '(.+)';?", 'g');
|
var IMPORT_RE = new RegExp("import \\{?([\\w\\n_, ]+)\\}? from '(.+)';?", 'g');
|
||||||
function transform(dir, contents) {
|
function transform(contents) {
|
||||||
contents = contents.replace(IMPORT_RE, function (match, imports, includePath) {
|
contents = contents.replace(IMPORT_RE, function (match, imports, includePath) {
|
||||||
//TODO: remove special-case
|
//TODO: remove special-case
|
||||||
if (isFacadeModule(includePath) || includePath === './router_outlet') {
|
if (isFacadeModule(includePath) || includePath === './router_outlet') {
|
||||||
|
@ -56,21 +49,10 @@ function transform(dir, contents) {
|
||||||
});
|
});
|
||||||
return ts.transpile(contents, {
|
return ts.transpile(contents, {
|
||||||
target: ts.ScriptTarget.ES5,
|
target: ts.ScriptTarget.ES5,
|
||||||
module: ts.ModuleKind.CommonJS,
|
module: ts.ModuleKind.CommonJS
|
||||||
sourceRoot: dir
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function angularFactory(name, deps, body) {
|
|
||||||
return ".factory('" + name + "', [" +
|
|
||||||
deps.map(function (service) {
|
|
||||||
return "'" + service + "', ";
|
|
||||||
}).join('') +
|
|
||||||
"function (" + deps.join(', ') + ") {\n" + body + "\n}])";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
function isFacadeModule(modulePath) {
|
function isFacadeModule(modulePath) {
|
||||||
return modulePath.indexOf('facade') > -1 ||
|
return modulePath.indexOf('facade') > -1 ||
|
||||||
modulePath === 'angular2/src/core/reflection/reflection';
|
modulePath === 'angular2/src/core/reflection/reflection';
|
||||||
|
@ -81,5 +63,5 @@ module.exports = function () {
|
||||||
if (!fs.existsSync(dist)) {
|
if (!fs.existsSync(dist)) {
|
||||||
fs.mkdirSync(dist);
|
fs.mkdirSync(dist);
|
||||||
}
|
}
|
||||||
fs.writeFileSync(dist + '/angular_1_router.js', main(files));
|
fs.writeFileSync(dist + '/angular_1_router.js', main());
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
|
|
||||||
angular.module('ngComponentRouter').
|
angular.module('ngComponentRouter').
|
||||||
value('$route', null). // can be overloaded with ngRouteShim
|
value('$route', null). // can be overloaded with ngRouteShim
|
||||||
factory('$router', ['$q', '$location', '$$directiveIntrospector', '$browser', '$rootScope', '$injector', '$route', routerFactory]);
|
factory('$router', ['$q', '$location', '$$directiveIntrospector', '$browser', '$rootScope', '$injector', routerFactory]);
|
||||||
|
|
||||||
function routerFactory($q, $location, $$directiveIntrospector, $browser, $rootScope, $injector) {
|
function routerFactory($q, $location, $$directiveIntrospector, $browser, $rootScope, $injector) {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue