babel-plugin-shopware-vite-meta-glob
Advanced tools
Comparing version 0.0.1 to 0.0.2
@@ -75,2 +75,4 @@ "use strict"; | ||
const eagerOption = t.isObjectExpression(args[1]) && args[1].properties.filter(p => t.isObjectProperty(p) && t.isIdentifier(p.key) && p.key.name === 'eager'); | ||
const importOption = t.isObjectExpression(args[1]) && args[1].properties.filter(p => t.isObjectProperty(p) && t.isIdentifier(p.key) && p.key.name === 'import'); | ||
const useImportOption = !!importOption && importOption.length > 0 && t.isObjectProperty(importOption[0]) && t.isStringLiteral(importOption[0].value); | ||
if (!eagerOption || eagerOption.length === 0 || !t.isObjectProperty(eagerOption[0]) || !t.isBooleanLiteral(eagerOption[0].value)) { | ||
@@ -90,3 +92,8 @@ console.warn('Did not transform because eager option is not set'); | ||
const imports = globPaths.map((globPath, idx) => { | ||
const modulePath = t.stringLiteral(globPath); | ||
let modulePath = t.stringLiteral(globPath); | ||
if (useImportOption) { | ||
return t.variableDeclaration('const', [t.variableDeclarator(identifiers[idx], t.memberExpression(t.callExpression(t.identifier('require'), [modulePath]), | ||
// @ts-ignore - We know it's a string literal | ||
t.identifier(importOption[0].value.value)))]); | ||
} | ||
return t.variableDeclaration('const', [t.variableDeclarator(identifiers[idx], t.callExpression(t.identifier('require'), [modulePath]))]); | ||
@@ -93,0 +100,0 @@ }); |
{ | ||
"name": "babel-plugin-shopware-vite-meta-glob", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"homepage": "https://github.com/shopware/babel-plugin-shopware-vite-meta-glob", | ||
@@ -5,0 +5,0 @@ "bugs": { |
@@ -24,2 +24,3 @@ # Babel Plugin Shopware Vite Meta Glob | ||
- Supports `{ eager: true }` option for direct module imports | ||
- Supports `{ import: 'default' }` **only in combination with eager: true** | ||
- Handles file path normalization | ||
@@ -34,4 +35,2 @@ - Works with both Windows and Unix-style file paths | ||
eager false will not be transformed | ||
- **Doesn't transform** `import.meta.glob('./dir1/*.js', { import: 'foo' })` | ||
import option will be ignored | ||
- **Doesn't transform** `import.meta.glob('./dir1/*.js', { query: 'foo' })` | ||
@@ -38,0 +37,0 @@ query option will be ignored |
@@ -23,2 +23,5 @@ import pluginTester from 'babel-plugin-tester' | ||
), | ||
'should transform glob with eager and import option': withFileName( | ||
'const modules = import.meta.glob("./fixtures/**/*", { eager: true, import: "default" })', | ||
), | ||
@@ -36,3 +39,6 @@ // Negative tests | ||
), | ||
'should not transform glob with import option without eager': withFileName( | ||
'const modules = import.meta.glob("./fixtures/**/*", { import: "default" })', | ||
), | ||
}, | ||
}) |
@@ -104,2 +104,12 @@ import nodePath from 'path' | ||
) | ||
const importOption = | ||
t.isObjectExpression(args[1]) && | ||
args[1].properties.filter( | ||
p => | ||
t.isObjectProperty(p) && | ||
t.isIdentifier(p.key) && | ||
p.key.name === 'import', | ||
) | ||
const useImportOption = !!importOption && importOption.length > 0 && t.isObjectProperty(importOption[0]) && t.isStringLiteral(importOption[0].value); | ||
@@ -128,3 +138,17 @@ if ( | ||
const imports = globPaths.map((globPath, idx) => { | ||
const modulePath = t.stringLiteral(globPath) | ||
let modulePath = t.stringLiteral(globPath) | ||
if (useImportOption) { | ||
return t.variableDeclaration('const', [ | ||
t.variableDeclarator( | ||
identifiers[idx], | ||
t.memberExpression( | ||
t.callExpression(t.identifier('require'), [modulePath]), | ||
// @ts-ignore - We know it's a string literal | ||
t.identifier(importOption[0].value.value), | ||
), | ||
), | ||
]) | ||
} | ||
return t.variableDeclaration('const', [ | ||
@@ -131,0 +155,0 @@ t.variableDeclarator( |
Sorry, the diff of this file is not supported yet
21368
315
107