-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(env-browser): using new api and providing server for statics
- Loading branch information
Showing
3 changed files
with
46 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |