0.0.18 • Published 5 months ago

@itrocks/class-file v0.0.18

Weekly downloads
-
License
LGPL-3.0-or-later
Repository
github
Last release
5 months ago

npm version npm downloads GitHub issues discord

class-file

Retrieve the absolute file path of an imported class:

  • Automatic for your CommonJS-compiled projects,
  • Using a @File decorator in ESM-compiled projects.

Installation

npm install @itrocks/class-file

Activation

This library associates each imported class with the absolute file path where it is defined. Its behavior differs depending on the module compilation mode:

CommonJS: Recommended for most use cases.\ Add this line to the very start of your main file:

import '@itrocks/class-file/automation'

or:

require('@itrocks/class-file/automation')

This enables automatic file path resolution for all subsequently imported classes.

ESModule: Requires manual setup.\ Due to the limitation of ESM, you must explicitly decorate classes with the @File(fileURLToPath(import.meta.url)) decorator to enable file path resolution. Without this decorator, fileOf() will return undefined.

Example

Given a file my-class.js, with CommonJS transpilation:

export class MyClass {}

Or on transpiling to an ESModule:

import { fileURLToPath } from 'node:url'

@File(fileURLToPath(import.meta.url))
export class MyClass {}

Retrieving the file path:

import { fileOf }  from '@itrocks/class-file'
import { MyClass } from './my-class.js'

console.log(fileOf(MyClass))       // Returns the absolute file path
console.log(fileOf(new MyClass))   // Also returns the absolute file path
0.0.18

5 months ago

0.0.17

7 months ago

0.0.16

7 months ago

0.0.15

7 months ago

0.0.14

9 months ago

0.0.13

9 months ago

0.0.12

9 months ago

0.0.11

9 months ago

0.0.10

9 months ago

0.0.9

10 months ago

0.0.8

10 months ago

0.0.7

10 months ago

0.0.6

10 months ago

0.0.5

10 months ago

0.0.4

10 months ago

0.0.3

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago