jaydenseric / extract-files

A function to recursively extract files and their object paths within a value, replacing them with null in a deep clone without mutating the original value. FileList instances are treated as File instance arrays. Files are typically File and Blob instances.
https://npm.im/extract-files
MIT License
56 stars 23 forks source link

RN: Unable to resolve index file #31

Closed giautm closed 2 years ago

giautm commented 2 years ago

Hello @jaydenseric, long time no talk. hehe.

I have an issue with extract-files today when I used it in the react-native when the metro bundling source code. It raises the error related to the main field being missing in the package.json

Because the main field is missing so it using the default value as extract-files/index but this file is not exists. Can you update it with a value?

[01:34:09] Error: While trying to resolve module `extract-files` from file `/home/runner/work/xxxx/packages/gql/src/Upload/index.ts`, the package `/home/runner/work/xxxx/packages/gql/node_modules/extract-files/package.json` was successfully found. However, this package itself specifies a `main` module field that could not be resolved (`/home/runner/work/xxxx/packages/gql/node_modules/extract-files/index`. Indeed, none of these files exist:

  * /home/runner/work/xxxx/packages/gql/node_modules/extract-files/index(.native|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx|.ios.js|.native.js|.js|.ios.jsx|.native.jsx|.jsx|.ios.json|.native.json|.json|.ios.svg|.native.svg|.svg)
  * /home/runner/work/xxxx/packages/gql/node_modules/extract-files/index/index(.native|.ios.ts|.native.ts|.ts|.ios.tsx|.native.tsx|.tsx|.ios.js|.native.js|.js|.ios.jsx|.native.jsx|.jsx|.ios.json|.native.json|.json|.ios.svg|.native.svg|.svg)
{
  "name": "extract-files",
  "version": "12.0.0",
  "description": "A function to recursively extract files and their object paths within a value, replacing them with null in a deep clone without mutating the original value. FileList instances are treated as File instance arrays. Files are typically File and Blob instances.",
  "license": "MIT",
  "author": {
    "name": "Jayden Seric",
    "email": "me@jaydenseric.com",
    "url": "https://jaydenseric.com"
  },
  "repository": "github:jaydenseric/extract-files",
  "homepage": "https://github.com/jaydenseric/extract-files#readme",
  "bugs": "https://github.com/jaydenseric/extract-files/issues",
  "funding": "https://github.com/sponsors/jaydenseric",
  "keywords": [
    "extract",
    "file",
    "files",
    "File",
    "FileList",
    "Blob",
    "esm",
    "mjs"
  ],
  "files": [
    "extractFiles.mjs",
    "isExtractableFile.mjs"
  ],
  "sideEffects": false,
  "exports": {
    "./extractFiles.mjs": "./extractFiles.mjs",
    "./isExtractableFile.mjs": "./isExtractableFile.mjs",
    "./package.json": "./package.json"
  },
  "engines": {
    "node": "^12.22.0 || ^14.17.0 || >= 16.0.0"
  },
  "browserslist": "Node 12.22 - 13 and Node < 13, Node 14.17 - 15 and Node < 15, Node >= 16, > 0.5%, not OperaMini all, not IE > 0, not dead",
  "dependencies": {
    "is-plain-obj": "^4.0.0"
  },
  "devDependencies": {
    "@types/node": "^17.0.8",
    "coverage-node": "^5.0.1",
    "esbuild": "^0.14.11",
    "eslint": "^8.6.0",
    "eslint-config-env": "^23.0.2",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-compat": "^4.0.0",
    "eslint-plugin-import": "^2.25.4",
    "eslint-plugin-jsdoc": "^37.6.1",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-prettier": "^4.0.0",
    "gzip-size": "^7.0.0",
    "prettier": "^2.5.1",
    "revertable-globals": "^3.0.0",
    "test-director": "^8.0.1",
    "typescript": "^4.6.0-dev.20220110"
  },
  "scripts": {
    "eslint": "eslint .",
    "prettier": "prettier -c .",
    "types": "tsc -p jsconfig.json",
    "tests": "coverage-node test.mjs",
    "test": "npm run eslint && npm run prettier && npm run types && npm run tests",
    "prepublishOnly": "npm test"
  }
}
giautm commented 2 years ago

I found the commit related, https://github.com/jaydenseric/extract-files/commit/f34e4b01bfa5a90360f84562fca0165dcfa395c2 and https://github.com/jaydenseric/extract-files/commit/23fa4941f720f3280d3206fbd04a5b47ef4b8a6e

giautm commented 2 years ago

I fixed by update the import statement:

- import extractFiles from 'extract-files'
+ import extractFiles from 'extract-files/extractFiles.mjs'

And declare ts module for it:

declare module 'extract-files/extractFiles.mjs' {
  export default function extractFiles<T>(
    values: any,
    isExtractableFile?: (value: T) => boolean,
    path: string = '',
  )
}

also, require the custom File:

export class ReactNativeFile {
  readonly uri: string
  readonly name: string
  readonly type: string
  constructor({ uri, name, type }) {
    this.uri = uri
    this.name = name
    this.type = type
  }
}

const { clone, files } = extractFiles<ReactNativeFile>(
  {
    query: req.operation.text,
    variables: req.variables,
  },
  (file) => file instanceof ReactNativeFile,
)
jaydenseric commented 2 years ago

Glad you figured it out :)

And declare ts module for it

This should not be necessary, because the module has built in TypeScript types via JSDoc:

https://github.com/jaydenseric/extract-files/blob/5232ae99c5b11ae545255dec86c02ff5ab079762/extractFiles.mjs#L8-L61

giautm commented 2 years ago

The TS compile in my project still complain about missing module. T_T

jaydenseric commented 2 years ago

Maybe you need to update to a modern version of TypeScript that is capable of resolving .mjs modules correctly. See TS config compilerOptions.module value nodenext:

https://www.typescriptlang.org/tsconfig#node12nodenext-nightly-builds

giautm commented 2 years ago

Maybe you need to update to a modern version of TypeScript that is capable of resolving .mjs modules correctly. See TS config compilerOptions.module value nodenext:

Thank you, let me try it. Currently, my app still cannot resolve the *.mjs module.

{
  "compilerOptions": {
    "target": "esnext",
    "module": "ESNext"
  }
}
giautm commented 2 years ago

I'm unable to use nodenext for RN project. So, I think I have to downgrade to version 11. T_T

jaydenseric commented 2 years ago

Ok, so I just learned something about TypeScript config after running into a similar weird problem in a project about certain module types being missing, even though it didn't make sense because the module was definitely there and the types can't be missing because they are inside the module as TypeScript JSDoc comments.

The fix was to set compilerOptions.maxNodeModuleJsDepth to something higher than the 0 default, i.e. 10. Otherwise TypeScript bails from type checking dependencies of dependencies, and instead of giving a relevant error it just spews an error beginning with TS7016: Could not find a declaration file for module.

Here is the jsconfig.json I'm using in projects now:

{
  "compilerOptions": {
    "maxNodeModuleJsDepth": 10,
    "module": "nodenext",
    "noEmit": true,
    "strict": true
  },
  "typeAcquisition": {
    "enable": false
  }
}