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

eager: true emits require (doesn't exist in browsers) instead of import. #57

Open
NullVoxPopuli opened this issue Aug 2, 2024 · 1 comment · May be fixed by #58
Open

eager: true emits require (doesn't exist in browsers) instead of import. #57

NullVoxPopuli opened this issue Aug 2, 2024 · 1 comment · May be fixed by #58

Comments

@NullVoxPopuli
Copy link

NullVoxPopuli commented Aug 2, 2024

"babel-plugin-transform-vite-meta-glob": "^1.1.2",

Relevant code or config:

const definedScenarios = import.meta.glob('./scenarios/*', {
  eager: true,
});

output:

const __glob__0_0 = require("./scenarios/baseline-handlebars-list.gjs");
const __glob__0_1 = require("./scenarios/baseline-inner-html.gjs");
const __glob__0_2 = require("./scenarios/ember-get.gjs");
const definedScenarios = {
  "./scenarios/baseline-handlebars-list.gjs": __glob__0_0,
  "./scenarios/baseline-inner-html.gjs": __glob__0_1,
  "./scenarios/ember-get.gjs": __glob__0_2
};

I would expect this:

import * as __glob__0_0 from "./scenarios/baseline-handlebars-list.gjs";
import * as __glob__0_1 from "./scenarios/baseline-inner-html.gjs";
import * as __glob__0_2 from "./scenarios/ember-get.gjs";
const definedScenarios = {
  "./scenarios/baseline-handlebars-list.gjs": __glob__0_0,
  "./scenarios/baseline-inner-html.gjs": __glob__0_1,
  "./scenarios/ember-get.gjs": __glob__0_2
};
@NullVoxPopuli NullVoxPopuli changed the title eager: true emits require (doesn't exist in brwsers) instead of import. eager: true emits require (doesn't exist in browsers) instead of import. Aug 2, 2024
@ArnaudWeyts
Copy link

I was running into the same issue for this. I actually think the import even needs to end up being something like:

import  __glob__0_0 from "./scenarios/baseline-handlebars-list.gjs";

Because otherwise you're grabbing a reference to the entire module. I'm using this for importing assets and in that case I just need the default export.

This pnpm patch seems to do the trick for me:

diff --git a/lib/index.js b/lib/index.js
index e928575b3063555fe9a6fb097c25335af952c11d..180fff32ec8ca6421e12831ba93ec5799c4e5379 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -64,7 +64,10 @@ function viteMetaGlobBabelPlugin({
             const identifiers = globPaths.map((_, idx) => t.identifier(`__glob__0_${idx}`));
             const imports = globPaths.map((globPath, idx) => {
               const modulePath = t.stringLiteral(globPath);
-              return t.variableDeclaration('const', [t.variableDeclarator(identifiers[idx], t.callExpression(t.identifier('require'), [modulePath]))]);
+              return t.importDeclaration(
+                [t.importDefaultSpecifier(identifiers[idx])],
+                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]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants