From ae49085481177f030a31a7398d73a579f505b7be Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Thu, 11 Feb 2016 21:54:29 -0600 Subject: [PATCH] fix(angular_1_router): Renamed require statements after TypeScript files are transpiled The require function was causing failures when bundled using Browserify and SystemJS Closes #7049 --- modules/angular1_router/build.js | 12 +++++++++--- modules/angular1_router/src/module_template.js | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/modules/angular1_router/build.js b/modules/angular1_router/build.js index 99bd7561ef..8cc3f86b53 100644 --- a/modules/angular1_router/build.js +++ b/modules/angular1_router/build.js @@ -51,9 +51,10 @@ function main(modulesDirectory) { */ var IMPORT_RE = new RegExp("import \\{?([\\w\\n_, ]+)\\}? from '(.+)';?", 'g'); var INJECT_RE = new RegExp("@Inject\\(ROUTER_PRIMARY_COMPONENT\\)", 'g'); -var IMJECTABLE_RE = new RegExp("@Injectable\\(\\)", 'g'); +var INJECTABLE_RE = new RegExp("@Injectable\\(\\)", 'g'); +var REQUIRE_RE = new RegExp("require\\('(.*?)'\\);", 'g'); function transform(contents) { - contents = contents.replace(INJECT_RE, '').replace(IMJECTABLE_RE, ''); + contents = contents.replace(INJECT_RE, '').replace(INJECTABLE_RE, ''); contents = contents.replace(IMPORT_RE, function (match, imports, includePath) { //TODO: remove special-case if (isFacadeModule(includePath) || includePath === './router_outlet') { @@ -61,10 +62,15 @@ function transform(contents) { } return match; }); - return ts.transpile(contents, { + contents = ts.transpile(contents, { target: ts.ScriptTarget.ES5, module: ts.ModuleKind.CommonJS }); + + // Rename require functions from transpiled imports + contents = contents.replace(REQUIRE_RE, 'routerRequire(\'$1\');'); + + return contents; } function isFacadeModule(modulePath) { diff --git a/modules/angular1_router/src/module_template.js b/modules/angular1_router/src/module_template.js index 072f545c6d..9757a0aec5 100644 --- a/modules/angular1_router/src/module_template.js +++ b/modules/angular1_router/src/module_template.js @@ -17,7 +17,7 @@ function routerFactory($q, $location, $$directiveIntrospector, $browser, $rootSc OpaqueToken: function () {}, Inject: function () {} }; - var require = function () {return exports;}; + var routerRequire = function () {return exports;}; // When this file is processed, the line below is replaced with // the contents of the compiled TypeScript classes.