-
Notifications
You must be signed in to change notification settings - Fork 13
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
Cannot use import.meta.url in _.context.ts #1086
Comments
I've played with the repo and am trying some alternate ways to load the file. This gets closer:
But that doesn't seem to find the proper JSON file so I'm still doing something wrong. But something like this might be an alternative to using |
Ok, using an absolute path works. Probably a better way to help the compiled code in |
I've discovered that JSON.parse(readFileSync(require.resolve('../../test.json'), 'utf8')); |
I guess since all the code transpiles down to cjs this is why require is available. Glad this use case works. |
Nice solution, @jrunning!
Yep! Because there's no API to clear the module cache with ESM, and I wanted to be able to "hot reload" code, I had to compile everything down to CJS. It gets the job done but leaves us with some leaky abstractions like this one. Would it help if we add a utility function to Counterfact for loading JSON files? export class Context {
constructor({ readJson }) {
this.readJson = readJson;
}
getResponse() {
return this.readJson('../mocks/response.json');
}
} |
It seems like loading data from JSON could be a common use case, but the only data point I have is my own. |
I'm sure it's common enough, just gut-checking the API. |
I wanted to load the contents of a JSON file so I put some code like this in my
/api/routes/_.context.ts
file, as I'm accustomed to in ES modules:When I run Counterfact with the usual arguments I get the following error after "Starting REPL, type .help for more info":
The "unexpected reserved word" in question is the
import
inimport.meta.url
, which I have confirmed by removing all code except forconsole.log(import.meta)
.However, when I run the
_.context.ts
file directly (withtsx
and addingconsole.log(mockResponse)
at the end), it runs without an error:I think something is going wrong in Counterfact's module loader, but I'm not sure what.
In the meantime I'm using
JSON.parse(fs.readFileSync(...))
, which works fine, but is less than optimal, and there may be other uses ofimport.meta
that are blocked by this issue.Minimal reproduction
I've created a minimal reproduction of the issue here: jrunning/counterfact-repro.
The text was updated successfully, but these errors were encountered: