Skip to content

Commit 774d961

Browse files
committed
Merge pull request #7946 from Microsoft/transforms-dontBreakRunTestsBrowser
[Transforms] Fixes issues that were causing runtests-browser to fail
2 parents 2413e5e + 018a0d1 commit 774d961

File tree

7 files changed

+55
-8
lines changed

7 files changed

+55
-8
lines changed

Jakefile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,7 @@ compileFile(nodeServerOutFile, [nodeServerInFile], [builtLocalDirectory, tscFile
909909

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

scripts/browserify-optional.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// simple script to optionally elide source-map-support (or other optional modules) when running browserify.
2+
3+
var stream = require("stream"),
4+
Transform = stream.Transform,
5+
resolve = require("browser-resolve");
6+
7+
var requirePattern = /require\s*\(\s*['"](source-map-support)['"]\s*\)/;
8+
module.exports = function (file) {
9+
return new Transform({
10+
transform: function (data, encoding, cb) {
11+
var text = encoding === "buffer" ? data.toString("utf8") : data;
12+
this.push(new Buffer(text.replace(requirePattern, function (originalText, moduleName) {
13+
try {
14+
resolve.sync(moduleName, { filename: file });
15+
return originalText;
16+
}
17+
catch (e) {
18+
return "(function () { throw new Error(\"module '" + moduleName + "' not found.\"); })()";
19+
}
20+
}), "utf8"));
21+
cb();
22+
}
23+
});
24+
};

src/compiler/program.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,8 @@ namespace ts {
644644
fileExists: fileName => sys.fileExists(fileName),
645645
readFile: fileName => sys.readFile(fileName),
646646
trace: (s: string) => sys.write(s + newLine),
647-
directoryExists: directoryName => sys.directoryExists(directoryName)
647+
directoryExists: directoryName => sys.directoryExists(directoryName),
648+
getEnvironmentVariable: sys.getEnvironmentVariable
648649
};
649650
}
650651

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

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

src/compiler/sys.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ namespace ts {
7474
readDirectory(path: string, extension?: string, exclude?: string[]): string[];
7575
watchFile?(path: string, callback: FileWatcherCallback): FileWatcher;
7676
watchDirectory?(path: string, callback: DirectoryWatcherCallback, recursive?: boolean): FileWatcher;
77+
getEnvironmentVariable?(name: string): string;
7778
};
7879

7980
export var sys: System = (function () {
@@ -632,9 +633,7 @@ namespace ts {
632633
createDirectory: ChakraHost.createDirectory,
633634
getExecutingFilePath: () => ChakraHost.executingFile,
634635
getCurrentDirectory: () => ChakraHost.currentDirectory,
635-
getEnvironmentVariable(name: string) {
636-
return "";
637-
},
636+
getEnvironmentVariable: ChakraHost.getEnvironmentVariable || ((name: string) => ""),
638637
readDirectory: ChakraHost.readDirectory,
639638
exit: ChakraHost.quit,
640639
};

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2805,6 +2805,7 @@ namespace ts {
28052805
* 'throw new Error("NotImplemented")'
28062806
*/
28072807
resolveModuleNames?(moduleNames: string[], containingFile: string): ResolvedModule[];
2808+
getEnvironmentVariable?(name: string): string;
28082809
}
28092810

28102811
/* @internal */

src/compiler/utilities.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,18 @@ namespace ts {
169169
return `${ file.fileName }(${ loc.line + 1 },${ loc.character + 1 })`;
170170
}
171171

172+
export function getEnvironmentVariable(name: string, host?: CompilerHost) {
173+
if (host && host.getEnvironmentVariable) {
174+
return host.getEnvironmentVariable(name);
175+
}
176+
177+
if (sys && sys.getEnvironmentVariable) {
178+
return sys.getEnvironmentVariable(name);
179+
}
180+
181+
return "";
182+
}
183+
172184
export function getStartPosOfNode(node: Node): number {
173185
return node.pos;
174186
}

src/harness/harness.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,8 @@ namespace Harness {
480480
getExecutingFilePath(): string;
481481
exit(exitCode?: number): void;
482482
readDirectory(path: string, extension?: string, exclude?: string[]): string[];
483+
tryEnableSourceMapsForHost?(): void;
484+
getEnvironmentVariable?(name: string): string;
483485
}
484486
export var IO: IO;
485487

@@ -518,6 +520,7 @@ namespace Harness {
518520
export const fileExists: typeof IO.fileExists = fso.FileExists;
519521
export const log: typeof IO.log = global.WScript && global.WScript.StdOut.WriteLine;
520522
export const readDirectory: typeof IO.readDirectory = (path, extension, exclude) => ts.sys.readDirectory(path, extension, exclude);
523+
export const getEnvironmentVariable: typeof IO.getEnvironmentVariable = name => ts.sys.getEnvironmentVariable(name);
521524

522525
export function createDirectory(path: string) {
523526
if (directoryExists(path)) {
@@ -587,6 +590,13 @@ namespace Harness {
587590
export const log: typeof IO.log = s => console.log(s);
588591

589592
export const readDirectory: typeof IO.readDirectory = (path, extension, exclude) => ts.sys.readDirectory(path, extension, exclude);
593+
export const getEnvironmentVariable: typeof IO.getEnvironmentVariable = name => ts.sys.getEnvironmentVariable(name);
594+
595+
export function tryEnableSourceMapsForHost() {
596+
if (ts.sys.tryEnableSourceMapsForHost) {
597+
ts.sys.tryEnableSourceMapsForHost();
598+
}
599+
}
590600

591601
export function createDirectory(path: string) {
592602
if (!directoryExists(path)) {
@@ -1681,8 +1691,8 @@ namespace Harness {
16811691
if (Error) (<any>Error).stackTraceLimit = 1;
16821692
}
16831693

1684-
if (ts.sys.tryEnableSourceMapsForHost && /^development$/i.test(ts.sys.getEnvironmentVariable("NODE_ENV"))) {
1685-
ts.sys.tryEnableSourceMapsForHost();
1694+
if (Harness.IO.tryEnableSourceMapsForHost && /^development$/i.test(Harness.IO.getEnvironmentVariable("NODE_ENV"))) {
1695+
Harness.IO.tryEnableSourceMapsForHost();
16861696
}
16871697

16881698
// TODO: not sure why Utils.evalFile isn't working with this, eventually will concat it like old compiler instead of eval

0 commit comments

Comments
 (0)