Skip to content

Commit

Permalink
Fixes issues that were causing runtests-browser to fail
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Apr 7, 2016
1 parent 9b8436c commit 3507ed0
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Jakefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ compileFile(nodeServerOutFile, [nodeServerInFile], [builtLocalDirectory, tscFile

desc("Runs browserify on run.js to produce a file suitable for running tests in the browser");
task("browserify", ["tests", builtLocalDirectory, nodeServerOutFile], function() {
var cmd = 'browserify built/local/run.js -o built/local/bundle.js';
var cmd = 'browserify built/local/run.js -t ./scripts/browserify-optional -o built/local/bundle.js';
exec(cmd);
}, {async: true});

Expand Down
24 changes: 24 additions & 0 deletions scripts/browserify-optional.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// simple script to optionally elide source-map-support (or other optional modules) when running browserify.

var stream = require("stream"),
Transform = stream.Transform,
resolve = require("browser-resolve");

var requirePattern = /require\s*\(\s*['"](source-map-support)['"]\s*\)/;
module.exports = function (file) {
return new Transform({
transform: function (data, encoding, cb) {
var text = encoding === "buffer" ? data.toString("utf8") : data;
this.push(new Buffer(text.replace(requirePattern, function (originalText, moduleName) {
try {
resolve.sync(moduleName, { filename: file });
return originalText;
}
catch (e) {
return "(function () { throw new Error(\"module '" + moduleName + "' not found.\"); })()";
}
}), "utf8"));
cb();
}
});
};
5 changes: 3 additions & 2 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,8 @@ namespace ts {
fileExists: fileName => sys.fileExists(fileName),
readFile: fileName => sys.readFile(fileName),
trace: (s: string) => sys.write(s + newLine),
directoryExists: directoryName => sys.directoryExists(directoryName)
directoryExists: directoryName => sys.directoryExists(directoryName),
getEnvironmentVariable: sys.getEnvironmentVariable
};
}

Expand Down Expand Up @@ -995,7 +996,7 @@ namespace ts {
const start = new Date().getTime();

// TODO(rbuckton): remove USE_TRANSFORMS condition when we switch to transforms permanently.
if (/^(y(es)?|t(rue|ransforms?)?|1|\+)$/i.test(sys.getEnvironmentVariable("USE_TRANSFORMS"))) {
if (/^(y(es)?|t(rue|ransforms?)?|1|\+)$/i.test(getEnvironmentVariable("USE_TRANSFORMS", host))) {
options.experimentalTransforms = true;
}

Expand Down
5 changes: 2 additions & 3 deletions src/compiler/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ namespace ts {
readDirectory(path: string, extension?: string, exclude?: string[]): string[];
watchFile?(path: string, callback: FileWatcherCallback): FileWatcher;
watchDirectory?(path: string, callback: DirectoryWatcherCallback, recursive?: boolean): FileWatcher;
getEnvironmentVariable?(name: string): string;
};

export var sys: System = (function () {
Expand Down Expand Up @@ -632,9 +633,7 @@ namespace ts {
createDirectory: ChakraHost.createDirectory,
getExecutingFilePath: () => ChakraHost.executingFile,
getCurrentDirectory: () => ChakraHost.currentDirectory,
getEnvironmentVariable(name: string) {
return "";
},
getEnvironmentVariable: ChakraHost.getEnvironmentVariable || ((name: string) => ""),
readDirectory: ChakraHost.readDirectory,
exit: ChakraHost.quit,
};
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2805,6 +2805,7 @@ namespace ts {
* 'throw new Error("NotImplemented")'
*/
resolveModuleNames?(moduleNames: string[], containingFile: string): ResolvedModule[];
getEnvironmentVariable?(name: string): string;
}

/* @internal */
Expand Down
12 changes: 12 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,18 @@ namespace ts {
return `${ file.fileName }(${ loc.line + 1 },${ loc.character + 1 })`;
}

export function getEnvironmentVariable(name: string, host?: CompilerHost) {
if (host && host.getEnvironmentVariable) {
return host.getEnvironmentVariable(name);
}

if (sys && sys.getEnvironmentVariable) {
return sys.getEnvironmentVariable(name);
}

return "";
}

export function getStartPosOfNode(node: Node): number {
return node.pos;
}
Expand Down
2 changes: 1 addition & 1 deletion src/harness/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1681,7 +1681,7 @@ namespace Harness {
if (Error) (<any>Error).stackTraceLimit = 1;
}

if (ts.sys.tryEnableSourceMapsForHost && /^development$/i.test(ts.sys.getEnvironmentVariable("NODE_ENV"))) {
if (ts.sys && ts.sys.tryEnableSourceMapsForHost && /^development$/i.test(ts.sys.getEnvironmentVariable("NODE_ENV"))) {
ts.sys.tryEnableSourceMapsForHost();
}

Expand Down

0 comments on commit 3507ed0

Please sign in to comment.