Skip to content

Commit 2083b55

Browse files
committed
allow string names in exports
1 parent 5845e21 commit 2083b55

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

hook.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,17 @@ function createHook (meta) {
124124
import { register } from '${iitmURL}'
125125
import * as namespace from ${JSON.stringify(url)}
126126
const set = {}
127-
${exportNames.map((n) => `
128-
let $${n} = namespace.${n}
129-
export { $${n} as ${n} }
130-
set.${n} = (v) => {
131-
$${n} = v
127+
${exportNames.map((n) => {
128+
const variableName = btoa(n).replace(/=/g, "");
129+
return `
130+
let $${variableName} = namespace["${n}"]
131+
export { $${variableName} as "${n}" }
132+
set["${n}"] = (v) => {
133+
$${variableName} = v
132134
return true
133135
}
134-
`).join('\n')}
136+
`
137+
}).join('\n')}
135138
register(${JSON.stringify(realUrl)}, namespace, set, ${JSON.stringify(specifiers.get(realUrl))})
136139
`
137140
}

test/fixtures/string-export.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const foo = 41
2+
3+
export { foo as "non-valid-identifier" }

test/hook/string-export.mjs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2.0 License.
2+
//
3+
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
4+
5+
import Hook from '../../index.js'
6+
import * as stringExportMjs from '../fixtures/string-export.mjs'
7+
import { strictEqual } from 'assert'
8+
9+
Hook((exports, name) => {
10+
if (name.match(/\/string-export\.mjs/)) {
11+
exports['non-valid-identifier'] += 1
12+
}
13+
})
14+
15+
strictEqual(stringExportMjs['non-valid-identifier'], 42)

0 commit comments

Comments
 (0)