Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

esm-utils

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

esm-utils - npm Package Compare versions

Comparing version 2.2.0 to 3.0.0

28

index.d.ts

@@ -36,13 +36,6 @@ // From https://github.com/sindresorhus/type-fest

export interface JsonUtils {
read: readJson
load: readJson
readSync: readJsonSync
loadSync: readJsonSync
}
/**
Import a file
Import a module
@param file string | URL
@param source string | URL

@@ -52,8 +45,8 @@ @example

import createEsmUtils from 'esm-utils'
const {importFile} = createEsmUtils(import.meta)
const {importModule} = createEsmUtils(import.meta)
const foo = await importFile('./foo.js')
const foo = await importModule('./foo.js')
```
*/
export type importFile = (file: string | URL) => Promise<unknown>
export type importModule = (file: string | URL) => Promise<unknown>

@@ -63,3 +56,3 @@ /**

@param importMeta import.meta
@param sourceModule - `import.meta`, `URL`, or path to the source module

@@ -73,7 +66,9 @@ @example

*/
export default function createEsmUtils(importMeta: ImportMeta): {
export default function createEsmUtils(
sourceModule: ImportMeta | URL | string,
): {
readonly filename: string
readonly dirname: string
readonly require: NodeRequire
readonly importFile: importFile
readonly importModule: importModule
readonly readJson: readJson

@@ -85,6 +80,5 @@ readonly readJsonSync: readJsonSync

readonly __dirname: string
readonly import: importFile
readonly import: importModule
readonly loadJson: readJson
readonly loadJsonSync: readJsonSync
readonly json: JsonUtils
}
import {pathToFileURL} from 'node:url'
import {isUrl} from 'url-or-path'
function createImport(base) {
return function (file) {
const url =
typeof file === 'string' && /^[a-z]:/i.test(file)
? pathToFileURL(file)
: new URL(file, base)
function getModuleSource(source, base) {
if (isUrl(source)) {
return source
}
return import(url)
if (typeof source === 'string' && /^[a-z]:/i.test(source)) {
return pathToFileURL(source)
}
if (source.startsWith('.')) {
return new URL(source, base)
}
return source
}
const createImport = (base) => (source) => import(getModuleSource(source, base))
export default createImport

@@ -0,6 +1,13 @@

import path from 'node:path'
import {pathToFileURL} from 'node:url'
import fs, {promises as fsAsync} from 'node:fs'
const toUrl = (file, base) =>
typeof file === 'string' && path.isAbsolute(file)
? pathToFileURL(file)
: new URL(file, base)
function createReadJson(base) {
return async function (file) {
const url = new URL(file, base)
const url = toUrl(file, base)
const buffer = await fsAsync.readFile(url)

@@ -13,3 +20,3 @@ return JSON.parse(buffer)

return function (file) {
const url = new URL(file, base)
const url = toUrl(file, base)
const buffer = fs.readFileSync(url)

@@ -16,0 +23,0 @@ return JSON.parse(buffer)

import {fileURLToPath} from 'node:url'
import path from 'node:path'
import {createRequire} from 'node:module'
import {toUrl} from 'url-or-path'
import {createReadJson, createReadJsonSync} from './json.js'

@@ -21,26 +22,26 @@ import createImport from './import.js'

const createObject = (properties) => Object.create(null, properties)
const getModuleUrl = (sourceModule) => sourceModule.url || toUrl(sourceModule)
function createEsmUtils({url: importMetaUrl}) {
function createEsmUtils(sourceModule) {
sourceModule = getModuleUrl(sourceModule)
const utils = createObject({
filename: toDescriptor(() => fileURLToPath(importMetaUrl)),
// Path
filename: toDescriptor(() => fileURLToPath(sourceModule)),
dirname: toDescriptor(() => path.dirname(utils.filename)),
require: toDescriptor(() => createRequire(importMetaUrl)),
importFile: toDescriptor(() => createImport(importMetaUrl)),
readJson: toDescriptor(() => createReadJson(importMetaUrl)),
readJsonSync: toDescriptor(() => createReadJsonSync(importMetaUrl)),
// Module
require: toDescriptor(() => createRequire(sourceModule)),
importModule: toDescriptor(() => createImport(sourceModule)),
// JSON
readJson: toDescriptor(() => createReadJson(sourceModule)),
readJsonSync: toDescriptor(() => createReadJsonSync(sourceModule)),
// Aliases
__filename: toDescriptor(() => utils.filename),
__dirname: toDescriptor(() => utils.dirname),
import: toDescriptor(() => utils.importFile),
import: toDescriptor(() => utils.importModule),
loadJson: toDescriptor(() => utils.readJson),
loadJsonSync: toDescriptor(() => utils.readJsonSync),
json: toDescriptor(() =>
createObject({
read: toDescriptor(() => utils.readJson),
load: toDescriptor(() => utils.readJson),
readSync: toDescriptor(() => utils.readJsonSync),
loadSync: toDescriptor(() => utils.readJsonSync),
}),
),
})

@@ -47,0 +48,0 @@

{
"name": "esm-utils",
"version": "2.2.0",
"version": "3.0.0",
"description": "Utilities you'll need when migrating to ESModule.",

@@ -64,6 +64,9 @@ "homepage": "https://github.com/fisker/esm-utils#readme",

},
"dependencies": {
"url-or-path": "2.1.0"
},
"devDependencies": {
"@commitlint/cli": "16.1.0",
"@fisker/commitlint-config": "2.0.2",
"@fisker/eslint-config": "10.0.12",
"@fisker/eslint-config": "10.0.13",
"@fisker/eslint-config-ava": "2.0.5",

@@ -70,0 +73,0 @@ "@fisker/husky-config": "4.1.2",

@@ -37,3 +37,3 @@ # esm-utils

readJsonSync,
importFile,
importModule,
} = createEsmUtils(import.meta)

@@ -44,22 +44,15 @@ ```

### `createEsmUtils(import.meta)`
### `createEsmUtils(import.meta | URL | 'string')`
Returns an `object` with the following properties:
- `require`
- `dirname` (alias `__dirname`)
- `filename` (alias `__filename`)
- `require`
- `importModule` (alias `import`)
- `readJson` (alias `loadJson`)
- `readJsonSync` (alias `loadJsonSync`)
- `importFile` (alias `import`)
- `json`
**Please read [this note](#you-dont-need-dirname-and-filename) before you use `dirname` and `filename`**
### `readJson(string | URL)`
Returns `Promise<jsonObject>`.
### `readJsonSync(string | URL)`
Sync version of `readJson`.

@@ -72,8 +65,12 @@

### `importFile(string | URL)`
### `importModule(string | URL)`
> Don't use this to import a NPM module
Same as `import()`, but accepts absolute path (on Windows, `import('C:\\foo.js')` error throws when pass a absolute path starts with a drive letter).
### `readJson(string | URL)`
Returns `Promise<jsonObject>`.
### `readJsonSync(string | URL)`
## Import json file

@@ -80,0 +77,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc