You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.0 KiB

3 months ago
  1. # is-file-esm
  2. > Determines whether a Node file is a Module (`import`) or a Script (`require`)
  3. ## Algorithm
  4. Determining the module system of a file comes from three inputs, the `type` field
  5. of the closest `package.json` to that file, the file extension (`.js`, `.mjs` or `.cjs`)
  6. and the lesser know `--input-type` command-line flag. The latter only applies to
  7. dyamic input (via STDIN, `--eval` or `--print` flags) so is not considered with this library.
  8. So to determine whether a file is an esm file (e.g. native EcmaScript modules) or not,
  9. we use the following procedure:
  10. ```
  11. read package.json for "type" field,
  12. if type is "module"
  13. if answer.mjs -> module
  14. if answer.js -> module
  15. if answer.cjs -> script
  16. if type is "commonjs"
  17. if answer.mjs -> module
  18. if answer.js -> script
  19. if answer.cjs -> script
  20. if type is not set
  21. if answer.mjs -> module
  22. if answer.js -> script
  23. if answer.cjs -> script
  24. ```
  25. ## API
  26. The `is-file-esm` module provides synchronous, awaitable (promise-based) and callback based APIs.
  27. In each case the Result object has the following shape:
  28. ```js
  29. {
  30. esm: Boolean, // true if file is a Module, false if it is a Script
  31. type: String, // the determined package.json type, may be undefined, 'module', or 'commonjs'
  32. extType: String, // the file extension type, may be 'c', 'm' or 'j'
  33. path: String, // the input path
  34. pkgPath: String // the path to the package.json from which the type was determined
  35. }
  36. ```
  37. ### Awaitable (promise-based)
  38. #### `await isFileEsm(path) => Result`
  39. ```js
  40. import isFileEsm from 'is-file-esm'
  41. const { esm, path } = await isFileEsm('/path/to/file.js')
  42. if (esm) console.log(`File ${path} is a Module`)
  43. else console.log(`File ${path} is a Script`)
  44. ```
  45. ### Callback-style
  46. #### `isFileEsm(path, cb(err, Result))`
  47. ```js
  48. const isFileEsm = require('is-file-esm')
  49. isFileEsm('/path/to/file.js', (err, result) => {
  50. if (err) {
  51. console.error(err)
  52. return
  53. }
  54. if (result.esm) console.log(`File ${result.path} is a Module`)
  55. else console.log(`File ${result.path} is a Script`)
  56. })
  57. ```
  58. ### Synchronous
  59. #### `isFileEsm.sync(path) => Result`
  60. ```js
  61. import isFileEsm from 'is-file-esm'
  62. const { esm, path } = isFileEsm.sync('/path/to/file.js')
  63. if (esm) console.log(`File ${path} is a Module`)
  64. else console.log(`File ${path} is a Script`)
  65. ```
  66. ### Test
  67. ```sh
  68. npm test
  69. ```
  70. ```
  71. test/index.js ..................................... 213/213
  72. total ............................................. 213/213
  73. 213 passing (927.584ms)
  74. ok
  75. ----------|----------|----------|----------|----------|-------------------|
  76. File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
  77. ----------|----------|----------|----------|----------|-------------------|
  78. All files | 100 | 100 | 100 | 100 | |
  79. index.js | 100 | 100 | 100 | 100 | |
  80. ----------|----------|----------|----------|----------|-------------------|
  81. ```
  82. ### License
  83. MIT