Skip to content

Commit

Permalink
feat: enable transform in protractor (#220)
Browse files Browse the repository at this point in the history
* feat: enable transform in protractor

* chore: safe check option

* chore: fix lint
  • Loading branch information
stoffeastrom committed May 21, 2018
1 parent 05d33ee commit 33345d3
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/cdp/http-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const path = require('path');
const Koa = require('koa');
const serve = require('koa-static');
const favicon = require('koa-favicon');
const transform = require('./transform');
const transform = require('../transform');

module.exports = function createHttpServer(argv) {
const app = new Koa();
Expand Down
2 changes: 1 addition & 1 deletion src/cdp/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const NYC = require('nyc');
const Mediator = require('./mediator');
const connect = require('./connect');
const utils = require('../terminal-utils');
const { ensureFilePath, getExt, getPathWithExt } = require('./file-utils');
const { ensureFilePath, getExt, getPathWithExt } = require('../file-utils');

class Runner {
constructor(argv = { chrome: { chromeFlags: [] }, client: {} }) {
Expand Down
File renamed without changes.
7 changes: 6 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ const Koa = require('koa');
const serve = require('koa-static');
const favicon = require('koa-favicon');
const rewrite = require('koa-rewrite');
const transform = require('./transform');
const testExclude = require('test-exclude');

module.exports = function createServer(options) {
options = Object.assign({}, { port: 9000, root: ['./'], rewrite: {} }, options);
options = Object.assign({}, { port: 9000, root: ['./'], rewrite: {}, instrument: { exclude: '**' }, transform: { exclude: '**' } }, options); //eslint-disable-line
options.instrument.testExclude = testExclude({ include: options.instrument.include, exclude: options.instrument.exclude }); //eslint-disable-line
options.transform.testExclude = testExclude({ include: options.transform.include, exclude: options.transform.exclude }); //eslint-disable-line
const app = new Koa();
app.use(favicon(path.resolve(__dirname, '../aw.png')));
Object.keys(options.rewrite).forEach(key => app.use(rewrite(key, options.rewrite[key])));
app.use(transform(options));
app.use(...options.root.map(root => serve(path.resolve(process.cwd(), root))));
return app.listen(options.port);
};
11 changes: 8 additions & 3 deletions src/cdp/transform.js → src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ async function transformFile(filePath, argv) {
babelOpts = Object.assign({}, babelOpts, tsBabelOpts);
}
const { code, map } = babel.transform(content, babelOpts);

if (map) {
const key = getPathWithExt(filePath, 'js.map');
virtualSourceMap.set(key, map);
Expand All @@ -87,10 +88,14 @@ async function transformFile(filePath, argv) {
module.exports = function transform(argv) {
return async (ctx, next) => {
await next();
let { url } = ctx;
// We need to remove the leading slash else it will be excluded by default
const url = ctx.url.substring(1);
const shouldInstrument = argv.coverage && argv.instrument.testExclude.shouldInstrument(url);
const shouldTransform = argv.transform.testExclude.shouldInstrument(url);
if (ctx.url.length && ctx.url.startsWith('/')) {
url = ctx.url.substring(1);
}
const shouldInstrument = argv.coverage && argv.instrument && argv.instrument.testExclude.shouldInstrument(url); //eslint-disable-line
const shouldTransform = argv.transform && argv.transform.testExclude.shouldInstrument(url);

if (shouldInstrument || shouldTransform) {
const { response } = ctx;
response.body = await transformFile(url, argv);
Expand Down

0 comments on commit 33345d3

Please sign in to comment.