Skip to content

Proxy default properties as well as named #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ ${exportNames.map((n) => `
let $${n} = namespace.${n}
export { $${n} as ${n} }
set.${n} = (v) => {
if ('${n}' !== 'default' && namespace.default['${n}']) {
namespace.default['${n}'] = v
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you need to make sure that this only applies to CJS modules. This could easily be incorrect for ESM modules.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noted. I'm putting this PR on hold until we have resolution on my others. Even if this one were merged, I'd still have to proxy the returned proxy because I need a symbol to detect the proxy. So I want to write up a case for that and discuss it.

}
$${n} = v
return true
}
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/cjs-exports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

exports.add = function add(a, b) {
return a + b
}

exports.name = 'foo'
24 changes: 24 additions & 0 deletions test/hook/static-import-aliased.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// The purpose of this test is to prove that CJS modules:
// 1. Hooks apply to the added `default` export.
// 2. Hooks apply to the "named" exports.

import Hook from '../../index.js'
import { strictEqual } from 'assert'
import * as something from '../fixtures/cjs-exports.js'

Hook((exports, name) => {
if (/cjs-exports\.js$/.test(name) === false) return
const add = exports.add
exports.add = function wrappedAdd(a, b) {
return 'wrapped: ' + add(a, b)
}

const namedName = exports.name
exports.name = `${namedName}-hooked`
})

strictEqual(something.default.add(2, 2), 'wrapped: 4')
strictEqual(something.add(2, 2), 'wrapped: 4')

strictEqual(something.default.name, 'foo-hooked')
strictEqual(something.name, 'foo-hooked')