Skip to content

Commit

Permalink
filtered export
Browse files Browse the repository at this point in the history
  • Loading branch information
ida613 committed Apr 5, 2024
1 parent 2e930f9 commit a21fa6e
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
6 changes: 6 additions & 0 deletions filtered-hook.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { createHook } from './hook.js'
import path from 'path'

const { load, resolve, getFormat, getSource } = createHook(import.meta, new Set([path.join(import.meta.url, '../test/fixtures/export-types/default-expression-array.mjs').replace(':/', ':///')]))

export { load, resolve, getFormat, getSource }
9 changes: 7 additions & 2 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ function addIitm (url) {
}

// moduleList is an optional Set specifiying which modules need IITM patching
// if moduleList is not set, IITM patches everything
// moduleList contains full file paths
// if moduleList is empty, IITM patches everything
function createHook (meta, moduleList = new Set()) {
async function resolve (specifier, context, parentResolve) {
const { parentURL = '' } = context
Expand All @@ -243,6 +244,11 @@ function createHook (meta, moduleList = new Set()) {
return { url: url.url, format: 'commonjs' }
}

// early return if
if (moduleList.size && !moduleList.has(url.url) && url.url.startsWith('file:///')) {
return { url: url.url, format: url.format }
}

if (isIitm(parentURL, meta) || hasIitm(parentURL)) {
return url
}
Expand All @@ -265,7 +271,6 @@ function createHook (meta, moduleList = new Set()) {

const iitmURL = new URL('lib/register.js', meta.url).toString()
async function getSource (url, context, parentGetSource) {
if (moduleList.size && !moduleList.has(url)) return parentGetSource(url, context, parentGetSource)
if (hasIitm(url)) {
const realUrl = deleteIitm(url)
const { imports, namespaces, setters: mapSetters } = await processModule({
Expand Down
4 changes: 3 additions & 1 deletion test/generic-loader.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

import * as tsLoader from './typescript/iitm-ts-node-loader.mjs'
import * as regularLoader from '../hook.mjs'
import * as filteredLoader from '../filtered-hook.mjs'
import path from 'path'

const filename = process.env.IITM_TEST_FILE

export const { load, resolve, getFormat, getSource } =
filename.includes('disabled')
? {}
: (path.extname(filename).slice(-2) === 'ts' ? tsLoader : regularLoader)
: (path.extname(filename).slice(-2) === 'ts' ? tsLoader :

Check failure on line 15 in test/generic-loader.mjs

View workflow job for this annotation

GitHub Actions / lint

Expected newline between test and consequent of ternary expression

Check failure on line 15 in test/generic-loader.mjs

View workflow job for this annotation

GitHub Actions / lint

':' should be placed at the beginning of the line

Check failure on line 15 in test/generic-loader.mjs

View workflow job for this annotation

GitHub Actions / lint

Expected newline between test and consequent of ternary expression

Check failure on line 15 in test/generic-loader.mjs

View workflow job for this annotation

GitHub Actions / lint

':' should be placed at the beginning of the line
filename.endsWith('filtered-export.mjs') ? filteredLoader: regularLoader)

Check failure on line 16 in test/generic-loader.mjs

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 8 spaces but found 4

Check failure on line 16 in test/generic-loader.mjs

View workflow job for this annotation

GitHub Actions / lint

Operator ':' must be spaced

Check failure on line 16 in test/generic-loader.mjs

View workflow job for this annotation

GitHub Actions / lint

Expected indentation of 8 spaces but found 4

Check failure on line 16 in test/generic-loader.mjs

View workflow job for this annotation

GitHub Actions / lint

Operator ':' must be spaced
19 changes: 19 additions & 0 deletions test/hook/filtered-export.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

import Hook from '../../index.js'
import a from '../fixtures/export-types/default-expression-array.mjs'
import n from '../fixtures/export-types/default-expression-num.mjs'
import { strictEqual } from 'assert'

Hook((exports, name) => {
if (name.match(/default-expression-array\.m?js/)) {
exports.default[0] += 1
} else if (name.match(/default-expression-num\.m?js/)) {
exports.default += 1
}
})

strictEqual(a[0], 2)
strictEqual(n, 1)

0 comments on commit a21fa6e

Please sign in to comment.