diff --git a/packages/babel-plugin-transform-vite-meta-glob/README.md b/packages/babel-plugin-transform-vite-meta-glob/README.md index 407525c..83d576b 100644 --- a/packages/babel-plugin-transform-vite-meta-glob/README.md +++ b/packages/babel-plugin-transform-vite-meta-glob/README.md @@ -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** @@ -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') diff --git a/packages/babel-plugin-transform-vite-meta-glob/src/__tests__/__snapshots__/index.ts.snap b/packages/babel-plugin-transform-vite-meta-glob/src/__tests__/__snapshots__/index.ts.snap index 8542638..bc5dbae 100644 --- a/packages/babel-plugin-transform-vite-meta-glob/src/__tests__/__snapshots__/index.ts.snap +++ b/packages/babel-plugin-transform-vite-meta-glob/src/__tests__/__snapshots__/index.ts.snap @@ -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/**/*") @@ -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`] = ` @@ -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`] = ` @@ -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`] = ` @@ -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`] = ` @@ -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') +} + + `; exports[`vite-meta-glob glob some files: glob some files 1`] = ` @@ -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`] = ` @@ -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`] = ` diff --git a/packages/babel-plugin-transform-vite-meta-glob/src/__tests__/index.ts b/packages/babel-plugin-transform-vite-meta-glob/src/__tests__/index.ts index 129cef1..ff1e2ec 100644 --- a/packages/babel-plugin-transform-vite-meta-glob/src/__tests__/index.ts +++ b/packages/babel-plugin-transform-vite-meta-glob/src/__tests__/index.ts @@ -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 })' } }) diff --git a/packages/babel-plugin-transform-vite-meta-glob/src/index.ts b/packages/babel-plugin-transform-vite-meta-glob/src/index.ts index c623e19..9f31a1d 100644 --- a/packages/babel-plugin-transform-vite-meta-glob/src/index.ts +++ b/packages/babel-plugin-transform-vite-meta-glob/src/index.ts @@ -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) + } + } } } }