Skip to content

Commit c885884

Browse files
snowyuTomerAberbach
authored andcommitted
feat: add meta object for import attributes if any
1 parent 690b147 commit c885884

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

src/index.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ export const parseImportsSync = (code, { resolveFrom } = {}) => {
2323
for (let {
2424
d: dynamicImportStartIndex,
2525
ss: statementStartIndex,
26+
se: statementEndIndex,
2627
s: moduleSpecifierStartIndex,
2728
e: moduleSpecifierEndIndexExclusive,
29+
a: metaStartIndex,
2830
} of imports) {
2931
const isImportMeta = dynamicImportStartIndex === -2
3032
if (isImportMeta) {
@@ -69,7 +71,24 @@ export const parseImportsSync = (code, { resolveFrom } = {}) => {
6971
importClause = parseImportClause(importClauseString)
7072
}
7173

72-
yield {
74+
let meta
75+
if (metaStartIndex > -1) {
76+
let metaString = code.slice(metaStartIndex, statementEndIndex).trim()
77+
if (isDynamicImport) {
78+
metaString = metaString.slice(0, -1)
79+
meta = runExpression(metaString)
80+
} else {
81+
const metaName = code
82+
.slice(moduleSpecifierEndIndexExclusive, metaStartIndex)
83+
.trim()
84+
if (metaName) {
85+
metaString = `{${metaName}:${metaString}}`
86+
}
87+
meta = runExpression(metaString)
88+
}
89+
}
90+
91+
const result = {
7392
startIndex: statementStartIndex,
7493
// Include the closing parenthesis for dynamic import
7594
endIndex: isDynamicImport
@@ -79,7 +98,17 @@ export const parseImportsSync = (code, { resolveFrom } = {}) => {
7998
moduleSpecifier,
8099
importClause,
81100
}
101+
102+
if (meta) {
103+
result.meta = meta
104+
}
105+
106+
yield result
82107
}
83108
},
84109
}
85110
}
111+
112+
function runExpression(code) {
113+
return new Function(`return ${code}`)()
114+
}

src/index.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@ import type { Import } from './index.js'
88

99
const currentDirectoryPath = dirname(fileURLToPath(import.meta.url))
1010

11+
test(`parseImports with meta`, async () => {
12+
let code = `import a from 'b'with {at: '12'}`
13+
const imports: any[] = []
14+
for (const $import of await parseImports(code)) {
15+
imports.push($import)
16+
}
17+
expect(imports).toHaveLength(1)
18+
expect(imports[0].meta).toStrictEqual({ with: { at: `12` } })
19+
20+
code = `import a from 'b' with {at: '12'}`
21+
imports.length = 0
22+
for (const $import of await parseImports(code)) {
23+
imports.push($import)
24+
}
25+
expect(imports).toHaveLength(1)
26+
expect(imports[0].meta).toStrictEqual({ with: { at: `12` } })
27+
28+
code = `await import("w", {with: {at: "1"}})`
29+
imports.length = 0
30+
for (const $import of await parseImports(code)) {
31+
imports.push($import)
32+
}
33+
expect(imports).toHaveLength(1)
34+
expect(imports[0].meta).toStrictEqual({ with: { at: `1` } })
35+
})
36+
1137
test.each([
1238
{
1339
path: `no-resolve.js`,

0 commit comments

Comments
 (0)