Skip to content

Dynamically importing dependency modules #1903

@privatenumber

Description

@privatenumber

Is your feature request related to a problem? Please describe.
I'd like to dynamically import language files in highlight.js:

import.meta.glob(`highlight.js/lib/languages/*.js`)

However, I get the following error:

[plugin:vite:import-analysis] Invalid glob import syntax: pattern must start with "." or "/" (relative to project root)

This works, but the results aren't very nice:

import.meta.glob(`../../node_modules/highlight.js/lib/languages/*`);

Screen Shot 2021-02-05 at 10 53 45 PM

Describe the solution you'd like

For the following API to work:

import.meta.glob(`highlight.js/lib/languages/*.js`)

Describe alternatives you've considered
Manually hard-coding dynamic imports for each language.

Additional context
Would also be nice if Vite could display a warning if the dynamic import glob has 0 matches. Since it's kinda failing silently, I didn't realize I had the wrong path for a while.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions