sindresorhus / globby

User-friendly glob matching
MIT License
2.49k stars 126 forks source link

Unknown file extension ".ts" #244

Closed FreePhoenix888 closed 1 year ago

FreePhoenix888 commented 1 year ago
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /home/freephoenix888/Programming/dev/gulpfile.ts
    at Loader.defaultGetFormat [as _getFormat] (internal/modules/esm/get_format.js:65:15)
    at Loader.getFormat (internal/modules/esm/loader.js:101:42)
    at Loader.getModuleJob (internal/modules/esm/loader.js:230:31)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at Loader.import (internal/modules/esm/loader.js:164:17) {
  code: 'ERR_UNKNOWN_FILE_EXTENSION'
}

Gulp task:

gulp.task('abc', async () => {
  const currentWorkingDirectory = process.cwd();
  const submodulesDirectory = pathUtils.join(currentWorkingDirectory, "packages");
  const submoduleNames = fsExtra.readdirSync(submodulesDirectory);
  const submodulePaths = submoduleNames.map(name => pathUtils.join(submodulesDirectory, name));
  // const filePaths = await globby(`./**/*.{ts,tsx}`, {
  //   ignore: ["./**/node_modules/**", "./**/*.d.ts"],
  // });
  const filePaths = await globby(`./**/*.{ts,tsx}`, {
    ignore: ["./**/node_modules/**", "./**/*.d.ts"],
  });
  const promises: Promise<string|void>[] = []
  for (const filePath of filePaths) {
    promises.push(
      fsExtra.readFile(filePath, {encoding: 'utf-8'}).then((result) => {
        console.log({result})
      })
    )
  }

  await Promise.all(promises);
})

If I comment the lines where globby is used error is missing

AlexAegis commented 1 year ago

This isn't related to globby.

This is about trying to load an un-transpiled TS file as an ES Module. I encountered the same thing in both vite and vitest configs when I tried to import ts files from a different compilation scope. At least in my case, my vite conf was able to use ts imports from the same package, but not when I imported it from another package from the monorepo. (In my case it was a pnpm workspace, but I think a regular npm repo with ts-paths would do the same thing)