Skip to content

Commit

Permalink
allow string names in exports
Browse files Browse the repository at this point in the history
  • Loading branch information
jackjennings committed Nov 17, 2023
1 parent 5845e21 commit 2578386
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
15 changes: 9 additions & 6 deletions hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,17 @@ function createHook (meta) {
import { register } from '${iitmURL}'
import * as namespace from ${JSON.stringify(url)}
const set = {}
${exportNames.map((n) => `
let $${n} = namespace.${n}
export { $${n} as ${n} }
set.${n} = (v) => {
$${n} = v
${exportNames.map((n) => {
const variableName = 'v' + Buffer.from(n, 'utf-8').toString('hex');
return `
let $${variableName} = namespace["${n}"]
export { $${variableName} as "${n}" }
set["${n}"] = (v) => {
$${variableName} = v
return true
}
`).join('\n')}
`
}).join('\n')}
register(${JSON.stringify(realUrl)}, namespace, set, ${JSON.stringify(specifiers.get(realUrl))})
`
}
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/string-export.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const foo = 41

export { foo as "non-valid-identifier" }
15 changes: 15 additions & 0 deletions test/hook/string-export.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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 * as stringExportMjs from '../fixtures/string-export.mjs'
import { strictEqual } from 'assert'

Hook((exports, name) => {
if (name.match(/fixtures\/string-export\.mjs/)) {
exports['non-valid-identifier'] += 1
}
})

strictEqual(stringExportMjs['non-valid-identifier'], 42)

0 comments on commit 2578386

Please sign in to comment.