jest icon indicating copy to clipboard operation
jest copied to clipboard

Impossible to get dynamically imported files transpiled

Open Aram1d opened this issue 2 years ago • 0 comments

Hi,

I have a TS source code where I use fs.readdir to get a list of files I want to dynamically import. The following code is an example of how do I import the files.

import { readdir } from "fs/promises";
import { resolve } from "path";

async function* getFiles(dir: string): AsyncIterable<string> {
  const dirents = await readdir(dir, { withFileTypes: true });
  for (const dirent of dirents) {
    const res = resolve(dir, dirent.name);
    if (dirent.isDirectory()) {
      yield* getFiles(res);
    } else {
      yield res;
    }
  }
}

const schemaChunks = [];

for await (const f of getFiles(__dirname + "/schemas")) {
  const match = f.match(/schemas\/.*Schema.ts/);
  if (match)
    schemaChunks.push(
      await import("./" + match[0]).then(({ typeDefs, resolvers }) => ({
        typeDefs,
        resolvers,
      }))
    );
}

Is there any way to force swc to transpile these file, despite the fact swc cannot load get their names, because this information comes at runtime. Can we have a swc config key which can take a glob pattern parameter too force inclusion of some files?

Tanks a lot

Aram1d avatar Aug 22 '22 10:08 Aram1d