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

import.meta.glob undefined when imported #7811

Closed
7 tasks done
mkilpatrick opened this issue Apr 19, 2022 · 3 comments
Closed
7 tasks done

import.meta.glob undefined when imported #7811

mkilpatrick opened this issue Apr 19, 2022 · 3 comments

Comments

@mkilpatrick
Copy link

Describe the bug

I have an entry.js that's being imported in my index.html. My entry.js looks like this:

import { createElement } from "react";
import { hydrate } from 'foo/bob';

export const App = ({ page }) => {
  return createElement(page?.component, page?.props);
};

if (!import.meta.env.SSR) hydrate(App);

Where the hydrate function is coming from a foo/bob node_module package that does:

import ReactDOM from "react-dom";
import React from "react";
const hydrate = async (App) => {
  const templates = import.meta.glob("/src/templates/*.(jsx|tsx)");
  const routes = Object.keys(templates).map((path) => {
    return {
      name: path.split("/").pop()?.split(".")[0] || "index",
      path,
      getComponent: templates[path]
    };
  });
  const templateFilename = window._RSS_TEMPLATE_?.split(".")[0];
  const { default: component } = await routes.find((route) => route.name === templateFilename)?.getComponent() || {};
  ReactDOM.hydrate(/* @__PURE__ */ React.createElement(App, {
    page: {
      props: window._RSS_PROPS_,
      path: window.location.pathname,
      component
    }
  }), document.getElementById("root"));
};
export {
  hydrate
};

This results in:

bob.js:4 Uncaught (in promise) TypeError: import_meta.glob is not a function
    at hydrate (bob.js:4:33)
    at entry.js:8:27

If I move the hydrate code directly to entry.js then it works fine. I'm guessing that Vite doesn't know about the imported module so it doesn't do its static replacement of import.meta.x. Is there a way to register this import with Vite?

Reproduction

N/A

System Info

System:
    OS: macOS 12.2.1
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Memory: 99.48 MB / 16.00 GB
    Shell: 5.1.16 - /usr/local/bin/bash
  Binaries:
    Node: 17.7.1 - ~/.nvm/versions/node/v17.7.1/bin/node
    Yarn: 1.22.18 - /usr/local/bin/yarn
    npm: 8.5.2 - ~/.nvm/versions/node/v17.7.1/bin/npm
    Watchman: 2021.10.18.00 - /usr/local/bin/watchman
  Browsers:
    Chrome: 100.0.4896.88
    Firefox: 52.0.2
    Safari: 15.3

Used Package Manager

npm

Logs

No response

Validations

@github-actions
Copy link

Hello @mkilpatrick. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with need reproduction will be closed if they have no activity within 3 days.

@sapphi-red
Copy link
Member

sapphi-red commented Apr 20, 2022

I feel it could be fixed by adding the config below. But I cannot know the reason without any complete reproductions.

export default {
  optimizeDeps: {
    esbuildOptions: {
      target: 'es2020'
    }
  }
}

@bluwy
Copy link
Member

bluwy commented Apr 20, 2022

Duplicate of #5270

@bluwy bluwy marked this as a duplicate of #5270 Apr 20, 2022
@bluwy bluwy closed this as completed Apr 20, 2022
@github-actions github-actions bot locked and limited conversation to collaborators May 5, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants