简体   繁体   中英

How to import with concatenation using Parcel?

I have a directory full of JSON files. I am trying to write a function to load one of them by name at runtime. When I am using Webpack, I can just do something like this:

const data = await import(`~/resources/levels-progression/${chapterName}.json`);

However, I am currently using Parcel, and doing that just results in an error:

TypeError: error resolving module specifier '~/resources/levels-progression/functions.json'

It seems that Parcel ignores my import when I do it this way. How can I fix this?

At the time of writing, there is no way to do this with code-splitting, asynchronous goodness in Parcel ( GitHub Issue ). However, we can get (kind of) close by using a glob import :

const allData = require("../resources/levels-progression/*.json");

allData will be a JS object with a key for each file that was present in the directory.

Note that this doesn't seem to work with tilde imports.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM