Skip to content

Commit

Permalink
fix: Prepare for ESM loader hooks moving to worker threads. (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
coreyfarrell committed May 31, 2020
1 parent 2f1be99 commit 8105a8f
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 22 deletions.
20 changes: 9 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ let nycConfig;
let testExclude;
let babelConfig;

export async function transformSource(source, context) {
if (loader.isLoading()) {
return {source};
export async function transformSource(source, context, defaultTransformSource) {
if (context.format !== 'module' || loader.isLoading()) {
return defaultTransformSource(source, context, defaultTransformSource);
}

if (!nycConfig) {
Expand All @@ -41,16 +41,14 @@ export async function transformSource(source, context) {
};
}

if (Buffer.isBuffer(source)) {
source = source.toString();
} else if (typeof source !== 'string') {
return {source};
}

const filename = fileURLToPath(context.url);
/* babel-plugin-istanbul does this but the early check optimizes */
if (!testExclude.shouldInstrument(filename)) {
return {source};
return defaultTransformSource(source, context, defaultTransformSource);
}

if (typeof source !== 'string') {
source = new TextDecoder().decode(source);
}

/* Can/should this handle inputSourceMap? */
Expand All @@ -71,5 +69,5 @@ export async function transformSource(source, context) {
]
});

return {source: code};
return defaultTransformSource(code, context, defaultTransformSource);
}
12 changes: 3 additions & 9 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {fileURLToPath} from 'url';

import {test} from 'libtap';
// eslint-disable-next-line import/no-unresolved
import * as loaderHook from '@istanbuljs/esm-loader-hook';

test('exports', async t => {
Expand All @@ -13,22 +12,17 @@ test('transform buffer', async t => {
const {source} = await loaderHook.transformSource(
Buffer.from('export default true;'),
{
format: 'module',
url: new URL('../fixtures/buffer.js', import.meta.url)
}
},
source => ({source})
);

t.match(source, /function\s+cov_/u);
t.match(source, /export\s+default\s+true/u);
t.match(source, /\/\/# sourceMappingURL=/u);
});

test('transform not string', async t => {
const notString = {};
const {source} = await loaderHook.transformSource(notString);

t.equal(source, notString);
});

test('transform hook', async t => {
await t.spawn(
process.execPath,
Expand Down
4 changes: 2 additions & 2 deletions test/no-source-maps.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {test} from 'libtap';
// eslint-disable-next-line import/no-unresolved
import {transformSource} from '@istanbuljs/esm-loader-hook';

test('check transform', async t => {
const context = {
format: 'module',
url: new URL('../fixtures/no-source-map.js', import.meta.url)
};
const {source} = await transformSource('export default true;', context);
const {source} = await transformSource('export default true;', context, source => ({source}));

t.match(source, /function\s+cov_/u);
t.match(source, /export\s+default\s+true/u);
Expand Down

0 comments on commit 8105a8f

Please sign in to comment.