Skip to content

Commit

Permalink
feat(env-browser): using new api and providing server for statics
Browse files Browse the repository at this point in the history
  • Loading branch information
Igmat committed Mar 7, 2018
1 parent f37f42f commit 36b9103
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
2 changes: 2 additions & 0 deletions packages/baset-env-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@
"devDependencies": {
"@types/jsdom": "^11.0.4",
"@types/node": "^9.3.0",
"@types/express": "^4.11.1",
"baset": "^0.9.0",
"doctoc": "^1.3.0",
"tslint": "^5.9.1",
"typescript": "next"
},
"dependencies": {
"baset-core": "^0.9.0",
"express": "^4.16.2",
"canvas-prebuilt": "^1.6.5-prerelease.1",
"jsdom": "^11.6.2"
}
Expand Down
45 changes: 33 additions & 12 deletions packages/baset-env-browser/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,34 @@
import { AbstractEnvironmet } from 'baset-core';
import { ConstructorOptions, JSDOM } from 'jsdom';

// FIXME: seems like jsdom.d.ts is incorrect and `pretendToBeVisual` is missing
// tslint:disable-next-line:no-object-literal-type-assertion
const dom = new JSDOM('', {
resources: 'usable',
pretendToBeVisual: true,
} as ConstructorOptions);
Object.setPrototypeOf(global, dom.window);

export default class BrowserEnv extends AbstractEnvironmet {
import { AbstractEnvironment, utils } from 'baset-core';
import express from 'express';
import path from 'path';
import { Server } from 'http';

export interface IBrowserEnvOptions {
staticFolder?: string;
serverPort?: number;
}

export default class BrowserEnv extends AbstractEnvironment {
private serveUrl = 'about:blank';
private server?: Server;
constructor(public options: IBrowserEnvOptions) {
super(options);
if (options && (options.staticFolder || options.serverPort)) {
const port = options.serverPort || 1337;
const folder = options.staticFolder || process.cwd();
this.serveUrl = `http://localhost:${port}/`;

const app = express();
app.use('/', express.static(folder));
setTimeout(() => this.server = app.listen(port));
}
}
getContextImport(sandbox: utils.IDictionary<any>) {
sandbox.basetBrowserEnv__StaticUrl = this.serveUrl;

return path.resolve(__dirname, 'runInContext').split('\\').join('/');
}
dispose() {
if (this.server) this.server.close();
}
}
11 changes: 11 additions & 0 deletions packages/baset-env-browser/src/runInContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ConstructorOptions, JSDOM } from 'jsdom';
declare const basetSandbox: { basetBrowserEnv__StaticUrl: string };

// FIXME: seems like jsdom.d.ts is incorrect and `pretendToBeVisual` is missing
// tslint:disable-next-line:no-object-literal-type-assertion
const dom = new JSDOM('', {
url: basetSandbox.basetBrowserEnv__StaticUrl,
resources: 'usable',
pretendToBeVisual: true,
} as ConstructorOptions);
Object.setPrototypeOf(global, dom.window);

0 comments on commit 36b9103

Please sign in to comment.