Specify namespace with url in ESM

Aperta
#948 8 commenti 5 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
javascript, nodejs
Ambito
backend

Direzione di ricerca

Start by reviewing the proposed native ESM URL syntax and the package.json exports idea described in the issue. Determine whether namespace and optional color parameters belong in the library, then define the supported import forms and tests needed to show the behavior is complete.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

change-minor feature

When using native esm in Node, it's not possible anymore to use the pattern

const debug = require('debug')('namespace');

and we have to do

import createDebug from 'debug';
const debug = createDebug('namespace');

instead. In one of my projects, I've found myself to use an alternative approach though, where I do something like

import createDebug from 'debug';
const url = new URL(import.meta.url);
const [[namespace]] = url.searchParams;
export default createDebug(namespace);

so that it becomes possible to do stuff like

// In server
import debug from './debug.js?server';

// In worker
import debug from './debug.js?worker';

I was wondering whether it would be useful to have this functionality in the library itself. I'd be happy to clean my code up a bit and file a PR for this. An alternative approach would be to create my own npm module on top of the debug module if it's not desirable to have this in the library itself.

For the syntax, I was thinking about something like

import debug from 'debug/url?namespace'

where debug/url can become an exports in the package.json.

The url approach can be used to add more functionality as well, for example to force a specific color or something:

import createDebug from 'debug';
const url = new URL(import.meta.url);
const [namespace] = [...url.searchParams].find(([key, value]) => value === '');
const debug = createDebug(namespace);
const color = url.searchParams.get('color');
if (color !== null) {
  debug.color = color;
}
export default debug;

I'd love to hear some thoughts about this from the maintainers.

Lingua principale
JavaScript
Stelle
11.5k
Fork
992
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di debug-js/debug

Tutte le issue di debug-js/debug

Issue simili

Altre issue su JavaScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.