Skip to content

Commit 2c2df9e

Browse files
authored
Fix runtests-browser in gulp, including RWC, remove into-stream (#17540)
1 parent b74ec1c commit 2c2df9e

File tree

4 files changed

+62
-46
lines changed

4 files changed

+62
-46
lines changed

Gulpfile.ts

Lines changed: 60 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import minimist = require("minimist");
2828
import browserify = require("browserify");
2929
import through2 = require("through2");
3030
import merge2 = require("merge2");
31-
import intoStream = require("into-stream");
3231
import * as os from "os";
3332
import fold = require("travis-fold");
3433
const gulp = helpMaker(originalGulp);
@@ -737,50 +736,75 @@ gulp.task(nodeServerOutFile, /*help*/ false, [servicesFile], () => {
737736

738737
import convertMap = require("convert-source-map");
739738
import sorcery = require("sorcery");
740-
declare module "convert-source-map" {
741-
export function fromSource(source: string, largeSource?: boolean): SourceMapConverter;
742-
}
739+
import Vinyl = require("vinyl");
743740

744-
gulp.task("browserify", "Runs browserify on run.js to produce a file suitable for running tests in the browser", [servicesFile, run], (done) => {
745-
const testProject = tsc.createProject("src/harness/tsconfig.json", getCompilerSettings({ outFile: "../../built/local/bundle.js" }, /*useBuiltCompiler*/ true));
746-
return testProject.src()
747-
.pipe(newer("built/local/bundle.js"))
741+
const bundlePath = path.resolve("built/local/bundle.js");
742+
743+
gulp.task("browserify", "Runs browserify on run.js to produce a file suitable for running tests in the browser", [servicesFile], (done) => {
744+
const testProject = tsc.createProject("src/harness/tsconfig.json", getCompilerSettings({ outFile: bundlePath, inlineSourceMap: true }, /*useBuiltCompiler*/ true));
745+
let originalMap: any;
746+
let prebundledContent: string;
747+
browserify(testProject.src()
748+
.pipe(newer(bundlePath))
748749
.pipe(sourcemaps.init())
749750
.pipe(testProject())
750751
.pipe(through2.obj((file, enc, next) => {
751-
const originalMap = file.sourceMap;
752-
const prebundledContent = file.contents.toString();
752+
if (originalMap) {
753+
throw new Error("Should only recieve one file!");
754+
}
755+
console.log(`Saving sourcemaps for ${file.path}`);
756+
originalMap = file.sourceMap;
757+
prebundledContent = file.contents.toString();
753758
// Make paths absolute to help sorcery deal with all the terrible paths being thrown around
754759
originalMap.sources = originalMap.sources.map(s => path.resolve(path.join("src/harness", s)));
755-
// intoStream (below) makes browserify think the input file is named this, so this is what it puts in the sourcemap
760+
// browserify names input files this when they are streamed in, so this is what it puts in the sourcemap
756761
originalMap.file = "built/local/_stream_0.js";
757762

758-
browserify(intoStream(file.contents), { debug: true })
759-
.bundle((err, res) => {
760-
// assumes file.contents is a Buffer
761-
const maps = JSON.parse(convertMap.fromSource(res.toString(), /*largeSource*/ true).toJSON());
762-
delete maps.sourceRoot;
763-
maps.sources = maps.sources.map(s => path.resolve(s === "_stream_0.js" ? "built/local/_stream_0.js" : s));
764-
// Strip browserify's inline comments away (could probably just let sorcery do this, but then we couldn't fix the paths)
765-
file.contents = new Buffer(convertMap.removeComments(res.toString()));
766-
const chain = sorcery.loadSync("built/local/bundle.js", {
767-
content: {
768-
"built/local/_stream_0.js": prebundledContent,
769-
"built/local/bundle.js": file.contents.toString()
770-
},
771-
sourcemaps: {
772-
"built/local/_stream_0.js": originalMap,
773-
"built/local/bundle.js": maps,
774-
"node_modules/source-map-support/source-map-support.js": undefined,
775-
}
776-
});
777-
const finalMap = chain.apply();
778-
file.sourceMap = finalMap;
779-
next(/*err*/ undefined, file);
780-
});
763+
next(/*err*/ undefined, file.contents);
781764
}))
782-
.pipe(sourcemaps.write(".", { includeContent: false }))
783-
.pipe(gulp.dest("src/harness"));
765+
.on("error", err => {
766+
return done(err);
767+
}), { debug: true, basedir: __dirname }) // Attach error handler to inner stream
768+
.bundle((err, contents) => {
769+
if (err) {
770+
if (err.message.match(/Cannot find module '.*_stream_0.js'/)) {
771+
return done(); // Browserify errors when we pass in no files when `newer` filters the input, we should count that as a success, though
772+
}
773+
return done(err);
774+
}
775+
const stringContent = contents.toString();
776+
const file = new Vinyl({ contents, path: bundlePath });
777+
console.log(`Fixing sourcemaps for ${file.path}`);
778+
// assumes contents is a Buffer, since that's what browserify yields
779+
const maps = convertMap.fromSource(stringContent, /*largeSource*/ true).toObject();
780+
delete maps.sourceRoot;
781+
maps.sources = maps.sources.map(s => path.resolve(s === "_stream_0.js" ? "built/local/_stream_0.js" : s));
782+
// Strip browserify's inline comments away (could probably just let sorcery do this, but then we couldn't fix the paths)
783+
file.contents = new Buffer(convertMap.removeComments(stringContent));
784+
const chain = sorcery.loadSync(bundlePath, {
785+
content: {
786+
"built/local/_stream_0.js": prebundledContent,
787+
[bundlePath]: stringContent
788+
},
789+
sourcemaps: {
790+
"built/local/_stream_0.js": originalMap,
791+
[bundlePath]: maps,
792+
"node_modules/source-map-support/source-map-support.js": undefined,
793+
}
794+
});
795+
const finalMap = chain.apply();
796+
file.sourceMap = finalMap;
797+
798+
const stream = through2.obj((file, enc, callback) => {
799+
return callback(/*err*/ undefined, file);
800+
});
801+
stream.pipe(sourcemaps.write(".", { includeContent: false }))
802+
.pipe(gulp.dest("."))
803+
.on("end", done)
804+
.on("error", done);
805+
stream.write(file);
806+
stream.end();
807+
});
784808
});
785809

786810

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@
6060
"gulp-newer": "latest",
6161
"gulp-sourcemaps": "latest",
6262
"gulp-typescript": "latest",
63-
"into-stream": "latest",
6463
"istanbul": "latest",
6564
"jake": "latest",
6665
"merge2": "latest",

scripts/types/ambient.d.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,5 @@ declare module "gulp-insert" {
1313
export function transform(cb: (contents: string, file: {path: string, relative: string}) => string): NodeJS.ReadWriteStream; // file is a vinyl file
1414
}
1515

16-
declare module "into-stream" {
17-
function IntoStream(content: string | Buffer | (string | Buffer)[]): NodeJS.ReadableStream;
18-
namespace IntoStream {
19-
export function obj(content: any): NodeJS.ReadableStream
20-
}
21-
export = IntoStream;
22-
}
23-
2416
declare module "sorcery";
2517
declare module "travis-fold";

src/harness/rwcRunner.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ namespace RWC {
5454
useCustomLibraryFile = undefined;
5555
});
5656

57-
it("can compile", () => {
57+
it("can compile", function(this: Mocha.ITestCallbackContext) {
58+
this.timeout(800000); // Allow long timeouts for RWC compilations
5859
let opts: ts.ParsedCommandLine;
5960

6061
const ioLog: IOLog = JSON.parse(Harness.IO.readFile(jsonPath));

0 commit comments

Comments
 (0)