Skip to content
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

feat: add meta object for import attributes if any #18

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
},
"prettier": "tomer/prettier-config",
"dependencies": {
"es-module-lexer": "^1.5.3",
"es-module-lexer": "^1.5.4",
"slashes": "^3.0.12"
},
"devDependencies": {
Expand Down
27 changes: 26 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ export const parseImportsSync = (code, { resolveFrom } = {}) => {
for (let {
d: dynamicImportStartIndex,
ss: statementStartIndex,
se: statementEndIndex,
s: moduleSpecifierStartIndex,
e: moduleSpecifierEndIndexExclusive,
a: metaStartIndex,
} of imports) {
const isImportMeta = dynamicImportStartIndex === -2
if (isImportMeta) {
Expand Down Expand Up @@ -69,7 +71,20 @@ export const parseImportsSync = (code, { resolveFrom } = {}) => {
importClause = parseImportClause(importClauseString)
}

yield {
let meta
if (metaStartIndex > -1) {
let metaString = code.slice(metaStartIndex, statementEndIndex).trim()
if (isDynamicImport) {
metaString = metaString.slice(0, -1)
meta = runExpression(metaString)
} else {
const metaName = code.slice(moduleSpecifierEndIndexExclusive, metaStartIndex).trim()
if (metaName) {metaString = '{'+ metaName + ':' + metaString + '}'}
meta = runExpression(metaString)
}
}

const result = {
startIndex: statementStartIndex,
// Include the closing parenthesis for dynamic import
endIndex: isDynamicImport
Expand All @@ -79,7 +94,17 @@ export const parseImportsSync = (code, { resolveFrom } = {}) => {
moduleSpecifier,
importClause,
}

if (meta) {
result.meta = meta
}

yield result
}
},
}
}

function runExpression(code) {
return new Function(`return ${code}`)()
}
20 changes: 20 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ test(`parseImportsSync throws before WASM load`, () => {
)
})

test(`parseImports with meta`, async () => {
let code = `import a from 'b'with {at: '12'}`
const imports: any[] = []
for (const $import of await parseImports(code)) {imports.push($import)}
expect(imports).toHaveLength(1)
expect(imports[0].meta).toStrictEqual({with: {at: '12'}})

code = `import a from 'b' with {at: '12'}`
imports.length = 0
for (const $import of await parseImports(code)) {imports.push($import)}
expect(imports).toHaveLength(1)
expect(imports[0].meta).toStrictEqual({with: {at: '12'}})

code = `await import("w", {with: {at: "1"}})`
imports.length = 0
for (const $import of await parseImports(code)) {imports.push($import)}
expect(imports).toHaveLength(1)
expect(imports[0].meta).toStrictEqual({with: {at: '1'}})
})

test.each([
{
path: `no-resolve.js`,
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"extends": "tomer/tsconfig",
"compilerOptions": {
"target": "es2015"
},
"include": ["**/*"]
}