Skip to content

Commit

Permalink
fix: Work with Node.js >=16.12.0
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Node.js 16.12.0 is required

Fixes #6
  • Loading branch information
Corey Farrell committed Jul 15, 2022
1 parent 8a7f2a3 commit 4ea4529
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
17 changes: 11 additions & 6 deletions index.js
Expand Up @@ -15,9 +15,9 @@ let nycConfig;
let testExclude;
let babelConfig;

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

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

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

const fromNext = await nextLoad(url, context);
let {source} = fromNext;
if (typeof source !== 'string') {
source = new TextDecoder().decode(source);
}
Expand All @@ -56,7 +58,7 @@ export async function transformSource(source, context, defaultTransformSource) {
babelrc: false,
configFile: false,
filename,
/* Revisit this if transformSource adds support for returning sourceMap object */
/* Revisit this if the load hook adds support for returning sourceMap object */
sourceMaps: babelConfig.produceSourceMap ? 'inline' : false,
compact: babelConfig.compact,
comments: babelConfig.preserveComments,
Expand All @@ -69,5 +71,8 @@ export async function transformSource(source, context, defaultTransformSource) {
]
});

return defaultTransformSource(code, context, defaultTransformSource);
return {
format: fromNext.format,
source: code
};
}
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -14,7 +14,7 @@
"release": "standard-version"
},
"engines": {
"node": ">=13.7.0"
"node": ">=16.12.0"
},
"license": "ISC",
"repository": {
Expand Down
15 changes: 8 additions & 7 deletions test/index.js
Expand Up @@ -4,18 +4,19 @@ import {test} from 'libtap';
import * as loaderHook from '@istanbuljs/esm-loader-hook';

test('exports', async t => {
t.same(Object.keys(loaderHook), ['transformSource']);
t.match(loaderHook, {transformSource: Function});
t.same(Object.keys(loaderHook), ['load']);
t.match(loaderHook, {load: Function});
});

test('transform buffer', async t => {
const {source} = await loaderHook.transformSource(
Buffer.from('export default true;'),
const {source} = await loaderHook.load(
new URL('../fixtures/buffer.js', import.meta.url),
{
format: 'module',
url: new URL('../fixtures/buffer.js', import.meta.url)
format: 'module'
},
source => ({source})
() => ({
source: Buffer.from('export default true;')
})
);

t.match(source, /function\s+cov_/u);
Expand Down
11 changes: 5 additions & 6 deletions test/no-source-maps.js
@@ -1,12 +1,11 @@
import {test} from 'libtap';
import {transformSource} from '@istanbuljs/esm-loader-hook';
import {load} 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, source => ({source}));
const {source} = await load(
new URL('../fixtures/no-source-map.js', import.meta.url),
{format: 'module'},
() => ({source: 'export default true;'}));

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

0 comments on commit 4ea4529

Please sign in to comment.