Skip to content

Commit

Permalink
accept non-inline source maps from transformers
Browse files Browse the repository at this point in the history
  • Loading branch information
jwbay committed Dec 20, 2016
1 parent 8320519 commit bd19866
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
28 changes: 20 additions & 8 deletions packages/jest-runtime/src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
'use strict';

import type {Config, Path} from 'types/Config';
import type {Transformer} from 'types/Transform';
import type {Transformer, TransformedSource} from 'types/Transform';

const createDirectory = require('jest-util').createDirectory;
const crypto = require('crypto');
Expand Down Expand Up @@ -235,7 +235,7 @@ const stripShebang = content => {
};

const instrumentFile = (
content: string,
transformedSource: TransformedSource,
filename: Path,
config: Config,
): string => {
Expand All @@ -244,7 +244,7 @@ const instrumentFile = (
const babel = require('babel-core');
const babelPluginIstanbul = require('babel-plugin-istanbul').default;

return babel.transform(content, {
return babel.transform(transformedSource.content, {
auxiliaryCommentBefore: ' istanbul ignore next ',
babelrc: false,
filename,
Expand All @@ -254,6 +254,7 @@ const instrumentFile = (
{
cwd: config.rootDir, // files outside `cwd` will not be instrumented
exclude: [],
inputSourceMap: transformedSource.sourceMap,
},
],
],
Expand All @@ -276,21 +277,32 @@ const transformSource = (
return result;
}

result = content;
let transformed: TransformedSource = {
content,
sourceMap: null,
};

if (transform && shouldTransform(filename, config)) {
result = transform.process(result, filename, config, {
const processed = transform.process(content, filename, config, {
instrument,
watch: config.watch,
});

if (typeof processed === 'string') {
transformed.content = processed;
} else {
transformed = processed;
}
}

// That means that the transform has a custom instrumentation
// logic and will handle it based on `config.collectCoverage` option
const transformWillInstrument = transform && transform.canInstrument;
const transformDidInstrument = transform && transform.canInstrument;

if (!transformWillInstrument && instrument) {
result = instrumentFile(result, filename, config);
if (!transformDidInstrument && instrument) {
result = instrumentFile(transformed, filename, config);
} else {
result = transformed.content;
}

writeCacheFile(cacheFilePath, result);
Expand Down
7 changes: 6 additions & 1 deletion types/Transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@

import type {Config, Path} from 'types/Config';

export type TransformedSource = {|
content: string,
sourceMap: ?string,
|};

export type SourceMap = {|
file: string,
mappings: string,
Expand Down Expand Up @@ -41,5 +46,5 @@ export type Transformer = {|
sourcePath: Path,
config: Config,
options?: TransformOptions,
) => string,
) => string | TransformedSource,
|};

0 comments on commit bd19866

Please sign in to comment.