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

Add support for eager via the { eager: true } argument to import.meta.glob #43

Merged
Merged
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
19 changes: 17 additions & 2 deletions packages/babel-plugin-transform-vite-meta-glob/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
```js
const modules = import.meta.glob('./path/to/files/**/*')

const eagerModules = import.meta.globEager('./path/to/files/**/*')
// eager
const eagerModules = import.meta.glob('./path/to/files/**/*', { eager: true })

// deprecated eager
const deprecatedEagerModules = import.meta.globEager('./path/to/files/**/*')
```

**Out**
Expand All @@ -44,7 +48,18 @@ const modules = {
'./path/to/files/file3.js': () => import(('./path/to/files/file3.js')
}

const eagerModules = {
// eager
import * as __glob__0_0 from './path/to/files/file1.js'
import * as __glob__0_1 from './path/to/files/file2.js'
import * as __glob__0_2 from './path/to/files/file3.js'
const eagerModules = {
'./path/to/files/file1.js': __glob__0_1,
'./path/to/files/file2.js': __glob__0_2,
'./path/to/files/file3.js': __glob__0_3
}

// deprecated eager
const deprecatedEagerModules = {
'./path/to/files/file1.js': require('./path/to/files/file1.js'),
'./path/to/files/file2.js': require('./path/to/files/file2.js'),
'./path/to/files/file3.js': require('./path/to/files/file3.js')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`vite-meta-glob glob all files eagerly, with { eager: true }: glob all files eagerly, with { eager: true } 1`] = `

const modules = import.meta.glob("./fixtures/**/*", { eager: true })

↓ ↓ ↓ ↓ ↓ ↓

import * as __glob__0_0 from './fixtures/file1.ts'
import * as __glob__0_1 from './fixtures/file2.ts'
import * as __glob__0_2 from './fixtures/file3.ts'
const modules = {
'./fixtures/file1.ts': __glob__0_0,
'./fixtures/file2.ts': __glob__0_1,
'./fixtures/file3.ts': __glob__0_2
}


`;

exports[`vite-meta-glob glob all files eagerly: glob all files eagerly 1`] = `

const modules = import.meta.globEager("./fixtures/**/*")
Expand All @@ -13,6 +31,21 @@ const modules = {
}


`;

exports[`vite-meta-glob glob all files normally, with { eager: false }: glob all files normally, with { eager: false } 1`] = `

const modules = import.meta.glob("./fixtures/**/*", { eager: false })

↓ ↓ ↓ ↓ ↓ ↓

const modules = {
'./fixtures/file1.ts': () => import('./fixtures/file1.ts'),
'./fixtures/file2.ts': () => import('./fixtures/file2.ts'),
'./fixtures/file3.ts': () => import('./fixtures/file3.ts')
}


`;

exports[`vite-meta-glob glob all files: glob all files 1`] = `
Expand All @@ -28,6 +61,17 @@ const modules = {
}


`;

exports[`vite-meta-glob glob no files eagerly, with { eager: true }: glob no files eagerly, with { eager: true } 1`] = `

const modules = import.meta.glob("./fixtures/**/not-found", { eager: true })

↓ ↓ ↓ ↓ ↓ ↓

const modules = {}


`;

exports[`vite-meta-glob glob no files eagerly: glob no files eagerly 1`] = `
Expand All @@ -39,6 +83,17 @@ const modules = import.meta.globEager("./fixtures/**/not-found")
const modules = {}


`;

exports[`vite-meta-glob glob no files normally, with { eager: false }: glob no files normally, with { eager: false } 1`] = `

const modules = import.meta.glob("./fixtures/**/not-found", { eager: false })

↓ ↓ ↓ ↓ ↓ ↓

const modules = {}


`;

exports[`vite-meta-glob glob no files: glob no files 1`] = `
Expand All @@ -50,6 +105,22 @@ const modules = import.meta.glob("./fixtures/**/not-found")
const modules = {}


`;

exports[`vite-meta-glob glob some files eagerly, with { eager: true }: glob some files eagerly, with { eager: true } 1`] = `

const modules = import.meta.glob("./fixtures/**/*{1,3}*", { eager: true })

↓ ↓ ↓ ↓ ↓ ↓

import * as __glob__0_0 from './fixtures/file1.ts'
import * as __glob__0_1 from './fixtures/file3.ts'
const modules = {
'./fixtures/file1.ts': __glob__0_0,
'./fixtures/file3.ts': __glob__0_1
}


`;

exports[`vite-meta-glob glob some files eagerly: glob some files eagerly 1`] = `
Expand All @@ -64,6 +135,20 @@ const modules = {
}


`;

exports[`vite-meta-glob glob some files normally, with { eager: false }: glob some files normally, with { eager: false } 1`] = `

const modules = import.meta.glob("./fixtures/**/*{1,3}*", { eager: false })

↓ ↓ ↓ ↓ ↓ ↓

const modules = {
'./fixtures/file1.ts': () => import('./fixtures/file1.ts'),
'./fixtures/file3.ts': () => import('./fixtures/file3.ts')
}
jazlalli marked this conversation as resolved.
Show resolved Hide resolved


`;

exports[`vite-meta-glob glob some files: glob some files 1`] = `
Expand All @@ -78,6 +163,56 @@ const modules = {
}


`;

exports[`vite-meta-glob glob with non-boolean eager option: glob with non-boolean eager option 1`] = `

const modules = import.meta.glob("./fixtures/**/*", { eager: 11 })

↓ ↓ ↓ ↓ ↓ ↓

const modules = import.meta.glob('./fixtures/**/*', {
eager: 11
})


`;

exports[`vite-meta-glob glob with non-object options: glob with non-object options 1`] = `

const modules = import.meta.glob("./fixtures/**/*", true)

↓ ↓ ↓ ↓ ↓ ↓

const modules = import.meta.glob('./fixtures/**/*', true)


`;

exports[`vite-meta-glob no filename, with { eager: false }: no filename, with { eager: false } 1`] = `

import.meta.glob("./fixtures/**/*", { eager: false })

↓ ↓ ↓ ↓ ↓ ↓

import.meta.glob('./fixtures/**/*', {
eager: false
})


`;

exports[`vite-meta-glob no filename, with { eager: true }: no filename, with { eager: true } 1`] = `

import.meta.glob("./fixtures/**/*", { eager: true })

↓ ↓ ↓ ↓ ↓ ↓

import.meta.glob('./fixtures/**/*', {
eager: true
})


`;

exports[`vite-meta-glob no filename: no filename 1`] = `
Expand All @@ -89,6 +224,32 @@ import.meta.glob("./fixtures/**/*")
import.meta.glob('./fixtures/**/*')


`;

exports[`vite-meta-glob not a string arg, with { eager: false }: not a string arg, with { eager: false } 1`] = `

const modules = import.meta.glob(12, { eager: false })

↓ ↓ ↓ ↓ ↓ ↓

const modules = import.meta.glob(12, {
eager: false
})


`;

exports[`vite-meta-glob not a string arg, with { eager: true }: not a string arg, with { eager: true } 1`] = `

const modules = import.meta.glob(12, { eager: true })

↓ ↓ ↓ ↓ ↓ ↓

const modules = import.meta.glob(12, {
eager: true
})


`;

exports[`vite-meta-glob not a string arg: not a string arg 1`] = `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,36 @@ pluginTester({
'not import.meta.globEager': withFileName('globEager("./fixtures/**/*")'),
'not a string arg': withFileName('globEager(1)'),
'too many args': withFileName('globEager("./fixtures/**/*1*", "./fixtures/**/*2*")'),
'no filename': 'import.meta.glob("./fixtures/**/*")'
'no filename': 'import.meta.glob("./fixtures/**/*")',
// ImportGlobOptions test cases
'glob all files eagerly, with { eager: true }': withFileName(
'const modules = import.meta.glob("./fixtures/**/*", { eager: true })'
),
'glob some files eagerly, with { eager: true }': withFileName(
'const modules = import.meta.glob("./fixtures/**/*{1,3}*", { eager: true })'
),
'glob no files eagerly, with { eager: true }': withFileName(
'const modules = import.meta.glob("./fixtures/**/not-found", { eager: true })'
),
'glob all files normally, with { eager: false }': withFileName(
'const modules = import.meta.glob("./fixtures/**/*", { eager: false })'
),
'glob some files normally, with { eager: false }': withFileName(
'const modules = import.meta.glob("./fixtures/**/*{1,3}*", { eager: false })'
),
'glob no files normally, with { eager: false }': withFileName(
'const modules = import.meta.glob("./fixtures/**/not-found", { eager: false })'
),
'glob with non-object options': 'const modules = import.meta.glob("./fixtures/**/*", true)',
'glob with non-boolean eager option':
'const modules = import.meta.glob("./fixtures/**/*", { eager: 11 })',
'not a string arg, with { eager: true }': withFileName(
'const modules = import.meta.glob(12, { eager: true })'
),
'not a string arg, with { eager: false }': withFileName(
'const modules = import.meta.glob(12, { eager: false })'
),
'no filename, with { eager: true }': 'import.meta.glob("./fixtures/**/*", { eager: true })',
'no filename, with { eager: false }': 'import.meta.glob("./fixtures/**/*", { eager: false })'
}
})
71 changes: 71 additions & 0 deletions packages/babel-plugin-transform-vite-meta-glob/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,77 @@ export default function viteMetaGlobBabelPlugin({

path.replaceWith(replacement)
}
},
VariableDeclaration(path, state) {
const id = path.node.declarations[0].id
const call = path.node.declarations[0].init
const callee = t.isCallExpression(call) && t.isMemberExpression(call.callee) && call.callee
const identifier = t.isIdentifier(id) && t.identifier(id.name)

if (!identifier || !callee) {
return
}

const args = call.arguments
const sourceFile = state.file.opts.filename
const propertyName = t.isIdentifier(callee.property) && callee.property.name
const eagerOption =
t.isObjectExpression(args[1]) &&
args[1].properties.filter(
(p) => t.isObjectProperty(p) && t.isIdentifier(p.key) && p.key.name === 'eager'
)

if (!sourceFile || !propertyName || !eagerOption || eagerOption.length === 0) {
return
}

if (
isGlobKey(propertyName) &&
t.isMetaProperty(callee.object) &&
args.length === 2 &&
t.isStringLiteral(args[0]) &&
t.isObjectProperty(eagerOption[0]) &&
t.isBooleanLiteral(eagerOption[0].value)
) {
const cwd = nodePath.dirname(sourceFile)
const globPaths = glob.sync(args[0].value, { cwd })

if (eagerOption[0].value.value) {
const identifiers = globPaths.map((_, idx) => t.identifier(`__glob__0_${idx}`))

const imports = globPaths.map((globPath, idx) => {
const specifier = t.importNamespaceSpecifier(identifiers[idx])
const modulePath = t.stringLiteral(globPath)
return t.importDeclaration([specifier], modulePath)
})

const variable = t.variableDeclaration('const', [
t.variableDeclarator(
identifier,
t.objectExpression(
globPaths.map((globPath, idx) =>
t.objectProperty(t.stringLiteral(globPath), identifiers[idx])
)
)
)
])

path.replaceWithMultiple([...imports, variable])
} else {
const replacement = t.variableDeclaration('const', [
t.variableDeclarator(
identifier,
t.objectExpression(
globPaths.map((globPath) =>
t.objectProperty(t.stringLiteral(globPath), asts[propertyName](globPath))
)
)
)
])

path.replaceWith(replacement)
}
}
Copy link
Author

Choose a reason for hiding this comment

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

This can def be tidied up, but will wait until we're sure that this is producing the right/desired result before doing so

}
}
}
Expand Down
Loading