Glob import returns empty module in Parcel

I use Parcel for its speed but I have a little problem. The Glob resolver just ignores the folder content.

I need import all content from folder /documents which contains PDF & HTML files. So I tried:

import * Docs from "url:/documents/*.{html,pdf}"

But it does not work and in source map, there is a file /documents/*.js:

module.exports = {}

.parcelrc

{
    "extends": "@parcel/config-default",
    "resolvers": [
        "@parcel/resolver-glob", 
        "..."
    ]
}

Upvotes: 0

Views: 625

Answers (2)

The real problem why parcel returned {} was Scope Hoisting. I wanted to access dynamically:

import * as Docs from "url:./documents/*.{html,pdf}";

const embeds = Object.values(Docs).map(src => (
    <iframe src={src} className="document" />
));

So the solution is turn off scope hoisting via --no-scope-hoist

Upvotes: 0

Andrew Stegmaier
Andrew Stegmaier

Reputation: 3777

I think you need to change /documents to ./documents, and add an "as" keyword, so the full import is:

import * as Docs from "url:./documents/*.{html,pdf}"

Internally, @parcel/resolver-glob uses isGlob to test whether the string is a valid glob, and exits silently if it is not. It would probably be a better error experience if it gave you some sort of feedback about the error ¯_(ツ)_/¯

Here's an example project where glob imports are working correctly.

Upvotes: 1

Related Questions